vite-plugin-swagger-mcp 0.0.11 → 0.0.13
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 +8 -40
- package/dist/esm/index.js +21 -111
- package/package.json +1 -1
package/dist/cjs/index.js
CHANGED
|
@@ -215,48 +215,16 @@ function vitePluginSwaggerMcp({
|
|
|
215
215
|
"MCP server connected:",
|
|
216
216
|
`http://localhost:${(_b = (_a = server.config) == null ? void 0 : _a.server) == null ? void 0 : _b.port}/_mcp/sse/swagger`
|
|
217
217
|
);
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
const json = JSON.parse(body);
|
|
227
|
-
try {
|
|
228
|
-
const result = await transport.handleRequest(req, res, json);
|
|
229
|
-
res.writeHead(200, {
|
|
230
|
-
"Content-Type": "application/json"
|
|
231
|
-
});
|
|
232
|
-
res.end(JSON.stringify(result));
|
|
233
|
-
return;
|
|
234
|
-
} catch (err) {
|
|
235
|
-
res.writeHead(500);
|
|
236
|
-
res.end(
|
|
237
|
-
JSON.stringify({
|
|
238
|
-
jsonrpc: "2.0",
|
|
239
|
-
id: (json == null ? void 0 : json.id) ?? null,
|
|
240
|
-
error: { message: err == null ? void 0 : err.message }
|
|
241
|
-
})
|
|
242
|
-
);
|
|
218
|
+
server.middlewares.use("/_mcp/sse/swagger", async (req, res) => {
|
|
219
|
+
try {
|
|
220
|
+
await transport.handleRequest(req, res);
|
|
221
|
+
} catch (err) {
|
|
222
|
+
console.error("[MCP]", err);
|
|
223
|
+
if (!res.headersSent) {
|
|
224
|
+
res.statusCode = 500;
|
|
225
|
+
res.end();
|
|
243
226
|
}
|
|
244
227
|
}
|
|
245
|
-
if (req.method === "GET") {
|
|
246
|
-
if (!((_a2 = req.headers.accept) == null ? void 0 : _a2.includes("text/event-stream"))) {
|
|
247
|
-
res.writeHead(405);
|
|
248
|
-
return res.end();
|
|
249
|
-
}
|
|
250
|
-
res.writeHead(200, {
|
|
251
|
-
"Content-Type": "text/event-stream",
|
|
252
|
-
"Cache-Control": "no-cache",
|
|
253
|
-
Connection: "keep-alive"
|
|
254
|
-
});
|
|
255
|
-
res.write("\n");
|
|
256
|
-
return;
|
|
257
|
-
}
|
|
258
|
-
res.statusCode = 405;
|
|
259
|
-
res.end();
|
|
260
228
|
});
|
|
261
229
|
} catch (error) {
|
|
262
230
|
console.log("MCP server error", error);
|
package/dist/esm/index.js
CHANGED
|
@@ -10,8 +10,6 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
|
|
|
10
10
|
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
11
11
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
12
12
|
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
13
|
-
function _asyncIterator(r) { var n, t, o, e = 2; for ("undefined" != typeof Symbol && (t = Symbol.asyncIterator, o = Symbol.iterator); e--;) { if (t && null != (n = r[t])) return n.call(r); if (o && null != (n = r[o])) return new AsyncFromSyncIterator(n.call(r)); t = "@@asyncIterator", o = "@@iterator"; } throw new TypeError("Object is not async iterable"); }
|
|
14
|
-
function AsyncFromSyncIterator(r) { function AsyncFromSyncIteratorContinuation(r) { if (Object(r) !== r) return Promise.reject(new TypeError(r + " is not an object.")); var n = r.done; return Promise.resolve(r.value).then(function (r) { return { value: r, done: n }; }); } return AsyncFromSyncIterator = function AsyncFromSyncIterator(r) { this.s = r, this.n = r.next; }, AsyncFromSyncIterator.prototype = { s: null, n: null, next: function next() { return AsyncFromSyncIteratorContinuation(this.n.apply(this.s, arguments)); }, return: function _return(r) { var n = this.s.return; return void 0 === n ? Promise.resolve({ value: r, done: !0 }) : AsyncFromSyncIteratorContinuation(n.apply(this.s, arguments)); }, throw: function _throw(r) { var n = this.s.return; return void 0 === n ? Promise.reject(r) : AsyncFromSyncIteratorContinuation(n.apply(this.s, arguments)); } }, new AsyncFromSyncIterator(r); }
|
|
15
13
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
16
14
|
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
17
15
|
import { randomUUID } from "node:crypto";
|
|
@@ -238,7 +236,7 @@ export default function vitePluginSwaggerMcp(_ref) {
|
|
|
238
236
|
enforce: "pre",
|
|
239
237
|
configureServer: function configureServer(server) {
|
|
240
238
|
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9() {
|
|
241
|
-
var _server$config, transport, swaggerServer, mcpServer
|
|
239
|
+
var _server$config, transport, swaggerServer, mcpServer;
|
|
242
240
|
return _regeneratorRuntime().wrap(function _callee9$(_context9) {
|
|
243
241
|
while (1) switch (_context9.prev = _context9.next) {
|
|
244
242
|
case 0:
|
|
@@ -374,134 +372,46 @@ export default function vitePluginSwaggerMcp(_ref) {
|
|
|
374
372
|
return mcpServer.connect(transport);
|
|
375
373
|
case 10:
|
|
376
374
|
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"));
|
|
377
|
-
|
|
378
|
-
// 2. 定义 SSE 和消息路径
|
|
379
|
-
SSE_PATH = "/_mcp/sse/swagger";
|
|
380
|
-
MESSAGES_PATH = "/_mcp/sse/swagger/messages"; // 规范建议分开或明确指定
|
|
381
|
-
server.middlewares.use('/mcp', /*#__PURE__*/function () {
|
|
375
|
+
server.middlewares.use('/_mcp/sse/swagger', /*#__PURE__*/function () {
|
|
382
376
|
var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(req, res) {
|
|
383
|
-
var body, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, chunk, json, result, _json$id, _req$headers$accept;
|
|
384
377
|
return _regeneratorRuntime().wrap(function _callee8$(_context8) {
|
|
385
378
|
while (1) switch (_context8.prev = _context8.next) {
|
|
386
379
|
case 0:
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
_iteratorAbruptCompletion = false;
|
|
393
|
-
_didIteratorError = false;
|
|
394
|
-
_context8.prev = 4;
|
|
395
|
-
_iterator = _asyncIterator(req);
|
|
396
|
-
case 6:
|
|
397
|
-
_context8.next = 8;
|
|
398
|
-
return _iterator.next();
|
|
399
|
-
case 8:
|
|
400
|
-
if (!(_iteratorAbruptCompletion = !(_step = _context8.sent).done)) {
|
|
401
|
-
_context8.next = 14;
|
|
402
|
-
break;
|
|
403
|
-
}
|
|
404
|
-
chunk = _step.value;
|
|
405
|
-
body += chunk;
|
|
406
|
-
case 11:
|
|
407
|
-
_iteratorAbruptCompletion = false;
|
|
408
|
-
_context8.next = 6;
|
|
409
|
-
break;
|
|
410
|
-
case 14:
|
|
411
|
-
_context8.next = 20;
|
|
380
|
+
_context8.prev = 0;
|
|
381
|
+
_context8.next = 3;
|
|
382
|
+
return transport.handleRequest(req, res);
|
|
383
|
+
case 3:
|
|
384
|
+
_context8.next = 9;
|
|
412
385
|
break;
|
|
413
|
-
case
|
|
414
|
-
_context8.prev =
|
|
415
|
-
_context8.t0 = _context8["catch"](
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
_context8.prev = 21;
|
|
421
|
-
if (!(_iteratorAbruptCompletion && _iterator.return != null)) {
|
|
422
|
-
_context8.next = 25;
|
|
423
|
-
break;
|
|
424
|
-
}
|
|
425
|
-
_context8.next = 25;
|
|
426
|
-
return _iterator.return();
|
|
427
|
-
case 25:
|
|
428
|
-
_context8.prev = 25;
|
|
429
|
-
if (!_didIteratorError) {
|
|
430
|
-
_context8.next = 28;
|
|
431
|
-
break;
|
|
432
|
-
}
|
|
433
|
-
throw _iteratorError;
|
|
434
|
-
case 28:
|
|
435
|
-
return _context8.finish(25);
|
|
436
|
-
case 29:
|
|
437
|
-
return _context8.finish(20);
|
|
438
|
-
case 30:
|
|
439
|
-
json = JSON.parse(body);
|
|
440
|
-
_context8.prev = 31;
|
|
441
|
-
_context8.next = 34;
|
|
442
|
-
return transport.handleRequest(req, res, json);
|
|
443
|
-
case 34:
|
|
444
|
-
result = _context8.sent;
|
|
445
|
-
// 规范:POST 一定返回 response
|
|
446
|
-
res.writeHead(200, {
|
|
447
|
-
'Content-Type': 'application/json'
|
|
448
|
-
});
|
|
449
|
-
res.end(JSON.stringify(result));
|
|
450
|
-
return _context8.abrupt("return");
|
|
451
|
-
case 40:
|
|
452
|
-
_context8.prev = 40;
|
|
453
|
-
_context8.t1 = _context8["catch"](31);
|
|
454
|
-
res.writeHead(500);
|
|
455
|
-
res.end(JSON.stringify({
|
|
456
|
-
jsonrpc: '2.0',
|
|
457
|
-
id: (_json$id = json === null || json === void 0 ? void 0 : json.id) !== null && _json$id !== void 0 ? _json$id : null,
|
|
458
|
-
error: {
|
|
459
|
-
message: _context8.t1 === null || _context8.t1 === void 0 ? void 0 : _context8.t1.message
|
|
460
|
-
}
|
|
461
|
-
}));
|
|
462
|
-
case 44:
|
|
463
|
-
if (!(req.method === 'GET')) {
|
|
464
|
-
_context8.next = 51;
|
|
465
|
-
break;
|
|
466
|
-
}
|
|
467
|
-
if ((_req$headers$accept = req.headers.accept) !== null && _req$headers$accept !== void 0 && _req$headers$accept.includes('text/event-stream')) {
|
|
468
|
-
_context8.next = 48;
|
|
469
|
-
break;
|
|
386
|
+
case 5:
|
|
387
|
+
_context8.prev = 5;
|
|
388
|
+
_context8.t0 = _context8["catch"](0);
|
|
389
|
+
console.error('[MCP]', _context8.t0);
|
|
390
|
+
if (!res.headersSent) {
|
|
391
|
+
res.statusCode = 500;
|
|
392
|
+
res.end();
|
|
470
393
|
}
|
|
471
|
-
|
|
472
|
-
return _context8.abrupt("return", res.end());
|
|
473
|
-
case 48:
|
|
474
|
-
res.writeHead(200, {
|
|
475
|
-
'Content-Type': 'text/event-stream',
|
|
476
|
-
'Cache-Control': 'no-cache',
|
|
477
|
-
Connection: 'keep-alive'
|
|
478
|
-
});
|
|
479
|
-
res.write('\n');
|
|
480
|
-
return _context8.abrupt("return");
|
|
481
|
-
case 51:
|
|
482
|
-
res.statusCode = 405;
|
|
483
|
-
res.end();
|
|
484
|
-
case 53:
|
|
394
|
+
case 9:
|
|
485
395
|
case "end":
|
|
486
396
|
return _context8.stop();
|
|
487
397
|
}
|
|
488
|
-
}, _callee8, null, [[
|
|
398
|
+
}, _callee8, null, [[0, 5]]);
|
|
489
399
|
}));
|
|
490
400
|
return function (_x6, _x7) {
|
|
491
401
|
return _ref6.apply(this, arguments);
|
|
492
402
|
};
|
|
493
403
|
}());
|
|
494
|
-
_context9.next =
|
|
404
|
+
_context9.next = 17;
|
|
495
405
|
break;
|
|
496
|
-
case
|
|
497
|
-
_context9.prev =
|
|
406
|
+
case 14:
|
|
407
|
+
_context9.prev = 14;
|
|
498
408
|
_context9.t0 = _context9["catch"](0);
|
|
499
409
|
console.log("MCP server error", _context9.t0);
|
|
500
|
-
case
|
|
410
|
+
case 17:
|
|
501
411
|
case "end":
|
|
502
412
|
return _context9.stop();
|
|
503
413
|
}
|
|
504
|
-
}, _callee9, null, [[0,
|
|
414
|
+
}, _callee9, null, [[0, 14]]);
|
|
505
415
|
}))();
|
|
506
416
|
}
|
|
507
417
|
};
|