vite-plugin-swagger-mcp 0.0.15 → 0.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +1 -1
- package/dist/esm/index.js +255 -253
- package/package.json +1 -1
package/dist/cjs/index.js
CHANGED
|
@@ -210,6 +210,7 @@ function vitePluginSwaggerMcp({
|
|
|
210
210
|
]
|
|
211
211
|
})
|
|
212
212
|
);
|
|
213
|
+
await mcpServer.connect(transport);
|
|
213
214
|
console.log(
|
|
214
215
|
"MCP server connected:",
|
|
215
216
|
`http://localhost:${(_b = (_a = server.config) == null ? void 0 : _a.server) == null ? void 0 : _b.port}/_mcp/sse/swagger`
|
|
@@ -231,7 +232,6 @@ function vitePluginSwaggerMcp({
|
|
|
231
232
|
return;
|
|
232
233
|
}
|
|
233
234
|
if (!sessionId) {
|
|
234
|
-
await mcpServer.connect(transport);
|
|
235
235
|
await transport.handleRequest(req, res, json);
|
|
236
236
|
const sessionId2 = transport.sessionId;
|
|
237
237
|
if (sessionId2) {
|
package/dist/esm/index.js
CHANGED
|
@@ -242,275 +242,277 @@ export default function vitePluginSwaggerMcp(_ref) {
|
|
|
242
242
|
return _regeneratorRuntime().wrap(function _callee9$(_context9) {
|
|
243
243
|
while (1) switch (_context9.prev = _context9.next) {
|
|
244
244
|
case 0:
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
245
|
+
_context9.prev = 0;
|
|
246
|
+
transport = new StreamableHTTPServerTransport({
|
|
247
|
+
sessionIdGenerator: function sessionIdGenerator() {
|
|
248
|
+
return randomUUID();
|
|
249
|
+
},
|
|
250
|
+
onsessioninitialized: function onsessioninitialized(sessionId) {
|
|
251
|
+
// Store the transport by session ID
|
|
252
|
+
transports[sessionId] = transport;
|
|
253
|
+
}
|
|
254
|
+
}); // Clean up transport when closed
|
|
255
|
+
transport.onclose = function () {
|
|
256
|
+
if (transport.sessionId) {
|
|
257
|
+
delete transports[transport.sessionId];
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
swaggerServer = new SwaggerMcpServer(swaggerUrl, token); // 实例化 MCP Server
|
|
261
|
+
mcpServer = new McpServer({
|
|
262
|
+
name: "swagger-mcp-server",
|
|
263
|
+
version: "0.1.0"
|
|
264
|
+
}); // 注册工具
|
|
265
|
+
/***
|
|
266
|
+
* 获取模块列表
|
|
267
|
+
*/
|
|
268
|
+
mcpServer.tool("getModules", "获取模块列表", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
|
|
269
|
+
var res;
|
|
270
|
+
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
271
|
+
while (1) switch (_context5.prev = _context5.next) {
|
|
272
|
+
case 0:
|
|
273
|
+
_context5.next = 2;
|
|
274
|
+
return swaggerServer.getModules();
|
|
275
|
+
case 2:
|
|
276
|
+
res = _context5.sent;
|
|
277
|
+
return _context5.abrupt("return", {
|
|
278
|
+
content: [{
|
|
279
|
+
type: "text",
|
|
280
|
+
text: JSON.stringify(res)
|
|
281
|
+
}]
|
|
282
|
+
});
|
|
283
|
+
case 4:
|
|
284
|
+
case "end":
|
|
285
|
+
return _context5.stop();
|
|
253
286
|
}
|
|
254
|
-
});
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
*/
|
|
268
|
-
mcpServer.tool("getModules", "获取模块列表", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
|
|
269
|
-
var res;
|
|
270
|
-
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
271
|
-
while (1) switch (_context5.prev = _context5.next) {
|
|
287
|
+
}, _callee5);
|
|
288
|
+
})));
|
|
289
|
+
|
|
290
|
+
/***
|
|
291
|
+
* 获取特定模块下的所有接口及返回值类型
|
|
292
|
+
*/
|
|
293
|
+
mcpServer.tool("getModuleApis", "获取特定模块下的所有接口及返回值类型", {
|
|
294
|
+
module: z.string().describe("模块名称")
|
|
295
|
+
}, /*#__PURE__*/function () {
|
|
296
|
+
var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(_ref3) {
|
|
297
|
+
var module, res;
|
|
298
|
+
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
299
|
+
while (1) switch (_context6.prev = _context6.next) {
|
|
272
300
|
case 0:
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
301
|
+
module = _ref3.module;
|
|
302
|
+
if (module) {
|
|
303
|
+
_context6.next = 3;
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
306
|
+
return _context6.abrupt("return", {
|
|
307
|
+
content: [{
|
|
308
|
+
type: "text",
|
|
309
|
+
text: JSON.stringify({
|
|
310
|
+
error: "模块名称不能为空"
|
|
311
|
+
})
|
|
312
|
+
}]
|
|
313
|
+
});
|
|
314
|
+
case 3:
|
|
315
|
+
_context6.next = 5;
|
|
316
|
+
return swaggerServer.getModuleApis(module);
|
|
317
|
+
case 5:
|
|
318
|
+
res = _context6.sent;
|
|
319
|
+
return _context6.abrupt("return", {
|
|
278
320
|
content: [{
|
|
279
321
|
type: "text",
|
|
280
322
|
text: JSON.stringify(res)
|
|
281
323
|
}]
|
|
282
324
|
});
|
|
283
|
-
case
|
|
325
|
+
case 7:
|
|
284
326
|
case "end":
|
|
285
|
-
return
|
|
327
|
+
return _context6.stop();
|
|
286
328
|
}
|
|
287
|
-
},
|
|
288
|
-
}))
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
mcpServer.tool("getModuleApis", "获取特定模块下的所有接口及返回值类型", {
|
|
294
|
-
module: z.string().describe("模块名称")
|
|
295
|
-
}, /*#__PURE__*/function () {
|
|
296
|
-
var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(_ref3) {
|
|
297
|
-
var module, res;
|
|
298
|
-
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
299
|
-
while (1) switch (_context6.prev = _context6.next) {
|
|
300
|
-
case 0:
|
|
301
|
-
module = _ref3.module;
|
|
302
|
-
if (module) {
|
|
303
|
-
_context6.next = 3;
|
|
304
|
-
break;
|
|
305
|
-
}
|
|
306
|
-
return _context6.abrupt("return", {
|
|
307
|
-
content: [{
|
|
308
|
-
type: "text",
|
|
309
|
-
text: JSON.stringify({
|
|
310
|
-
error: "模块名称不能为空"
|
|
311
|
-
})
|
|
312
|
-
}]
|
|
313
|
-
});
|
|
314
|
-
case 3:
|
|
315
|
-
_context6.next = 5;
|
|
316
|
-
return swaggerServer.getModuleApis(module);
|
|
317
|
-
case 5:
|
|
318
|
-
res = _context6.sent;
|
|
319
|
-
return _context6.abrupt("return", {
|
|
320
|
-
content: [{
|
|
321
|
-
type: "text",
|
|
322
|
-
text: JSON.stringify(res)
|
|
323
|
-
}]
|
|
324
|
-
});
|
|
325
|
-
case 7:
|
|
326
|
-
case "end":
|
|
327
|
-
return _context6.stop();
|
|
328
|
-
}
|
|
329
|
-
}, _callee6);
|
|
330
|
-
}));
|
|
331
|
-
return function (_x4) {
|
|
332
|
-
return _ref4.apply(this, arguments);
|
|
333
|
-
};
|
|
334
|
-
}());
|
|
335
|
-
|
|
336
|
-
/***
|
|
337
|
-
* 获取特定接口的参数及返回值类型
|
|
338
|
-
*/
|
|
339
|
-
mcpServer.tool("getApiTypes", "获取特定接口的参数及返回值类型", {
|
|
340
|
-
path: z.string(),
|
|
341
|
-
method: z.string()
|
|
342
|
-
}, /*#__PURE__*/function () {
|
|
343
|
-
var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(args) {
|
|
344
|
-
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
|
|
345
|
-
while (1) switch (_context7.prev = _context7.next) {
|
|
346
|
-
case 0:
|
|
347
|
-
_context7.t0 = JSON;
|
|
348
|
-
_context7.next = 3;
|
|
349
|
-
return swaggerServer.getApiTypes(args.path, args.method);
|
|
350
|
-
case 3:
|
|
351
|
-
_context7.t1 = _context7.sent;
|
|
352
|
-
_context7.t2 = _context7.t0.stringify.call(_context7.t0, _context7.t1);
|
|
353
|
-
_context7.t3 = {
|
|
354
|
-
type: "text",
|
|
355
|
-
text: _context7.t2
|
|
356
|
-
};
|
|
357
|
-
_context7.t4 = [_context7.t3];
|
|
358
|
-
return _context7.abrupt("return", {
|
|
359
|
-
content: _context7.t4
|
|
360
|
-
});
|
|
361
|
-
case 8:
|
|
362
|
-
case "end":
|
|
363
|
-
return _context7.stop();
|
|
364
|
-
}
|
|
365
|
-
}, _callee7);
|
|
366
|
-
}));
|
|
367
|
-
return function (_x5) {
|
|
368
|
-
return _ref5.apply(this, arguments);
|
|
369
|
-
};
|
|
370
|
-
}());
|
|
329
|
+
}, _callee6);
|
|
330
|
+
}));
|
|
331
|
+
return function (_x4) {
|
|
332
|
+
return _ref4.apply(this, arguments);
|
|
333
|
+
};
|
|
334
|
+
}());
|
|
371
335
|
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
336
|
+
/***
|
|
337
|
+
* 获取特定接口的参数及返回值类型
|
|
338
|
+
*/
|
|
339
|
+
mcpServer.tool("getApiTypes", "获取特定接口的参数及返回值类型", {
|
|
340
|
+
path: z.string(),
|
|
341
|
+
method: z.string()
|
|
342
|
+
}, /*#__PURE__*/function () {
|
|
343
|
+
var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(args) {
|
|
344
|
+
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
|
|
345
|
+
while (1) switch (_context7.prev = _context7.next) {
|
|
346
|
+
case 0:
|
|
347
|
+
_context7.t0 = JSON;
|
|
348
|
+
_context7.next = 3;
|
|
349
|
+
return swaggerServer.getApiTypes(args.path, args.method);
|
|
350
|
+
case 3:
|
|
351
|
+
_context7.t1 = _context7.sent;
|
|
352
|
+
_context7.t2 = _context7.t0.stringify.call(_context7.t0, _context7.t1);
|
|
353
|
+
_context7.t3 = {
|
|
354
|
+
type: "text",
|
|
355
|
+
text: _context7.t2
|
|
356
|
+
};
|
|
357
|
+
_context7.t4 = [_context7.t3];
|
|
358
|
+
return _context7.abrupt("return", {
|
|
359
|
+
content: _context7.t4
|
|
360
|
+
});
|
|
361
|
+
case 8:
|
|
362
|
+
case "end":
|
|
363
|
+
return _context7.stop();
|
|
364
|
+
}
|
|
365
|
+
}, _callee7);
|
|
366
|
+
}));
|
|
367
|
+
return function (_x5) {
|
|
368
|
+
return _ref5.apply(this, arguments);
|
|
369
|
+
};
|
|
370
|
+
}());
|
|
371
|
+
_context9.next = 10;
|
|
372
|
+
return mcpServer.connect(transport);
|
|
373
|
+
case 10:
|
|
374
|
+
// Connect to the MCP mcpServer
|
|
375
|
+
console.log("MCP server connected:", "http://localhost:".concat((_server$config = server.config) === null || _server$config === void 0 || (_server$config = _server$config.server) === null || _server$config === void 0 ? void 0 : _server$config.port, "/_mcp/sse/swagger"));
|
|
376
|
+
server.middlewares.use('/_mcp/sse/swagger', /*#__PURE__*/function () {
|
|
377
|
+
var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(req, res) {
|
|
378
|
+
var _sessionId, body, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, chunk, json, _sessionId2, _req$headers$accept;
|
|
379
|
+
return _regeneratorRuntime().wrap(function _callee8$(_context8) {
|
|
380
|
+
while (1) switch (_context8.prev = _context8.next) {
|
|
381
|
+
case 0:
|
|
382
|
+
if (!(req.method === 'POST')) {
|
|
383
|
+
_context8.next = 57;
|
|
403
384
|
break;
|
|
404
|
-
|
|
405
|
-
|
|
385
|
+
}
|
|
386
|
+
_sessionId = req.headers["mcp-session-id"];
|
|
387
|
+
body = '';
|
|
388
|
+
_iteratorAbruptCompletion = false;
|
|
389
|
+
_didIteratorError = false;
|
|
390
|
+
_context8.prev = 5;
|
|
391
|
+
_iterator = _asyncIterator(req);
|
|
392
|
+
case 7:
|
|
393
|
+
_context8.next = 9;
|
|
394
|
+
return _iterator.next();
|
|
395
|
+
case 9:
|
|
396
|
+
if (!(_iteratorAbruptCompletion = !(_step = _context8.sent).done)) {
|
|
397
|
+
_context8.next = 15;
|
|
406
398
|
break;
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
399
|
+
}
|
|
400
|
+
chunk = _step.value;
|
|
401
|
+
body += chunk;
|
|
402
|
+
case 12:
|
|
403
|
+
_iteratorAbruptCompletion = false;
|
|
404
|
+
_context8.next = 7;
|
|
405
|
+
break;
|
|
406
|
+
case 15:
|
|
407
|
+
_context8.next = 21;
|
|
408
|
+
break;
|
|
409
|
+
case 17:
|
|
410
|
+
_context8.prev = 17;
|
|
411
|
+
_context8.t0 = _context8["catch"](5);
|
|
412
|
+
_didIteratorError = true;
|
|
413
|
+
_iteratorError = _context8.t0;
|
|
414
|
+
case 21:
|
|
415
|
+
_context8.prev = 21;
|
|
416
|
+
_context8.prev = 22;
|
|
417
|
+
if (!(_iteratorAbruptCompletion && _iterator.return != null)) {
|
|
419
418
|
_context8.next = 26;
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
_context8.next =
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
419
|
+
break;
|
|
420
|
+
}
|
|
421
|
+
_context8.next = 26;
|
|
422
|
+
return _iterator.return();
|
|
423
|
+
case 26:
|
|
424
|
+
_context8.prev = 26;
|
|
425
|
+
if (!_didIteratorError) {
|
|
426
|
+
_context8.next = 29;
|
|
427
|
+
break;
|
|
428
|
+
}
|
|
429
|
+
throw _iteratorError;
|
|
430
|
+
case 29:
|
|
431
|
+
return _context8.finish(26);
|
|
432
|
+
case 30:
|
|
433
|
+
return _context8.finish(21);
|
|
434
|
+
case 31:
|
|
435
|
+
json = JSON.parse(body);
|
|
436
|
+
console.log("post request received");
|
|
437
|
+
console.log("body: ", json);
|
|
438
|
+
_context8.prev = 34;
|
|
439
|
+
if (!(_sessionId && transports[_sessionId])) {
|
|
440
|
+
_context8.next = 40;
|
|
441
|
+
break;
|
|
442
|
+
}
|
|
443
|
+
transport = transports[_sessionId];
|
|
444
|
+
_context8.next = 39;
|
|
445
|
+
return transport.handleRequest(req, res, json);
|
|
446
|
+
case 39:
|
|
447
|
+
return _context8.abrupt("return");
|
|
448
|
+
case 40:
|
|
449
|
+
if (_sessionId) {
|
|
450
|
+
_context8.next = 46;
|
|
451
|
+
break;
|
|
452
|
+
}
|
|
453
|
+
_context8.next = 43;
|
|
454
|
+
return transport.handleRequest(req, res, json);
|
|
455
|
+
case 43:
|
|
456
|
+
// session ID will only be available (if in not Stateless-Mode)
|
|
457
|
+
// after handling the first request
|
|
458
|
+
_sessionId2 = transport.sessionId;
|
|
459
|
+
if (_sessionId2) {
|
|
460
|
+
transports[_sessionId2] = transport;
|
|
461
|
+
}
|
|
462
|
+
return _context8.abrupt("return");
|
|
463
|
+
case 46:
|
|
464
|
+
res.statusCode = 400;
|
|
465
|
+
res.end("Bad Request: invalid session ID or method.");
|
|
466
|
+
return _context8.abrupt("return");
|
|
467
|
+
case 51:
|
|
468
|
+
_context8.prev = 51;
|
|
469
|
+
_context8.t1 = _context8["catch"](34);
|
|
470
|
+
console.error('Error handling MCP request:', _context8.t1);
|
|
471
|
+
res.statusCode = 500;
|
|
472
|
+
res.end("Internal server error.");
|
|
473
|
+
return _context8.abrupt("return");
|
|
474
|
+
case 57:
|
|
475
|
+
if (!(req.method === 'GET')) {
|
|
476
|
+
_context8.next = 64;
|
|
477
|
+
break;
|
|
478
|
+
}
|
|
479
|
+
if ((_req$headers$accept = req.headers.accept) !== null && _req$headers$accept !== void 0 && _req$headers$accept.includes('text/event-stream')) {
|
|
480
|
+
_context8.next = 61;
|
|
481
|
+
break;
|
|
482
|
+
}
|
|
483
|
+
res.writeHead(405);
|
|
484
|
+
return _context8.abrupt("return", res.end());
|
|
485
|
+
case 61:
|
|
486
|
+
res.writeHead(200, {
|
|
487
|
+
'Content-Type': 'text/event-stream',
|
|
488
|
+
'Cache-Control': 'no-cache',
|
|
489
|
+
Connection: 'keep-alive'
|
|
490
|
+
});
|
|
491
|
+
transport.onmessage = function (message) {
|
|
492
|
+
res.write(message);
|
|
493
|
+
};
|
|
494
|
+
return _context8.abrupt("return");
|
|
495
|
+
case 64:
|
|
496
|
+
case "end":
|
|
497
|
+
return _context8.stop();
|
|
498
|
+
}
|
|
499
|
+
}, _callee8, null, [[5, 17, 21, 31], [22,, 26, 30], [34, 51]]);
|
|
500
|
+
}));
|
|
501
|
+
return function (_x6, _x7) {
|
|
502
|
+
return _ref6.apply(this, arguments);
|
|
503
|
+
};
|
|
504
|
+
}());
|
|
505
|
+
_context9.next = 17;
|
|
506
|
+
break;
|
|
507
|
+
case 14:
|
|
508
|
+
_context9.prev = 14;
|
|
509
|
+
_context9.t0 = _context9["catch"](0);
|
|
510
|
+
console.log("MCP server error", _context9.t0);
|
|
511
|
+
case 17:
|
|
510
512
|
case "end":
|
|
511
513
|
return _context9.stop();
|
|
512
514
|
}
|
|
513
|
-
}, _callee9);
|
|
515
|
+
}, _callee9, null, [[0, 14]]);
|
|
514
516
|
}))();
|
|
515
517
|
}
|
|
516
518
|
};
|