vite-plugin-swagger-mcp 0.0.10 → 0.0.11

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 CHANGED
@@ -217,36 +217,47 @@ function vitePluginSwaggerMcp({
217
217
  );
218
218
  const SSE_PATH = "/_mcp/sse/swagger";
219
219
  const MESSAGES_PATH = "/_mcp/sse/swagger/messages";
220
- server.middlewares.use(
221
- async (req, res, next) => {
222
- var _a2;
223
- const url = (_a2 = req.url) == null ? void 0 : _a2.split("?")[0];
224
- if (url === SSE_PATH || url === MESSAGES_PATH) {
225
- if (req.method === "GET") {
226
- if (req.headers["accept"] !== "text/event-stream") {
227
- res.statusCode = 405;
228
- res.end("Method Not Allowed - Expected Accept: text/event-stream");
229
- return;
230
- }
231
- res.setHeader("Content-Type", "text/event-stream");
232
- res.setHeader("Cache-Control", "no-cache");
233
- res.setHeader("Connection", "keep-alive");
234
- res.setHeader("X-Accel-Buffering", "no");
235
- }
236
- try {
237
- await transport.handleRequest(req, res);
238
- } catch (err) {
239
- console.error("MCP Transport Error:", err);
240
- if (!res.headersSent) {
241
- res.statusCode = 500;
242
- res.end("Internal MCP Error");
243
- }
244
- }
220
+ server.middlewares.use("/mcp", async (req, res) => {
221
+ var _a2;
222
+ if (req.method === "POST") {
223
+ let body = "";
224
+ for await (const chunk of req)
225
+ body += chunk;
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));
245
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
+ );
246
243
  }
247
- next();
248
244
  }
249
- );
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
+ });
250
261
  } catch (error) {
251
262
  console.log("MCP server error", error);
252
263
  }
package/dist/esm/index.js CHANGED
@@ -10,6 +10,8 @@ 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); }
13
15
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
14
16
  import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
15
17
  import { randomUUID } from "node:crypto";
@@ -376,62 +378,116 @@ export default function vitePluginSwaggerMcp(_ref) {
376
378
  // 2. 定义 SSE 和消息路径
377
379
  SSE_PATH = "/_mcp/sse/swagger";
378
380
  MESSAGES_PATH = "/_mcp/sse/swagger/messages"; // 规范建议分开或明确指定
379
- server.middlewares.use( /*#__PURE__*/function () {
380
- var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(req, res, next) {
381
- var _req$url;
382
- var url;
381
+ server.middlewares.use('/mcp', /*#__PURE__*/function () {
382
+ 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;
383
384
  return _regeneratorRuntime().wrap(function _callee8$(_context8) {
384
385
  while (1) switch (_context8.prev = _context8.next) {
385
386
  case 0:
386
- url = (_req$url = req.url) === null || _req$url === void 0 ? void 0 : _req$url.split("?")[0]; // 处理 SSE 建立连接 (GET) 和 消息接收 (POST)
387
- if (!(url === SSE_PATH || url === MESSAGES_PATH)) {
388
- _context8.next = 21;
387
+ if (!(req.method === 'POST')) {
388
+ _context8.next = 44;
389
389
  break;
390
390
  }
391
- if (!(req.method === "GET")) {
392
- _context8.next = 11;
393
- break;
394
- }
395
- if (!(req.headers["accept"] !== "text/event-stream")) {
396
- _context8.next = 7;
391
+ body = '';
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;
397
402
  break;
398
403
  }
399
- res.statusCode = 405;
400
- res.end("Method Not Allowed - Expected Accept: text/event-stream");
401
- return _context8.abrupt("return");
402
- case 7:
403
- // 对应文档 2.2.3: 设置正确的 Content-Type
404
- res.setHeader("Content-Type", "text/event-stream");
405
- res.setHeader("Cache-Control", "no-cache");
406
- res.setHeader("Connection", "keep-alive");
407
- // 禁用某些代理的缓冲
408
- res.setHeader("X-Accel-Buffering", "no");
404
+ chunk = _step.value;
405
+ body += chunk;
409
406
  case 11:
410
- _context8.prev = 11;
411
- _context8.next = 14;
412
- return transport.handleRequest(req, res);
407
+ _iteratorAbruptCompletion = false;
408
+ _context8.next = 6;
409
+ break;
413
410
  case 14:
414
411
  _context8.next = 20;
415
412
  break;
416
413
  case 16:
417
414
  _context8.prev = 16;
418
- _context8.t0 = _context8["catch"](11);
419
- console.error("MCP Transport Error:", _context8.t0);
420
- if (!res.headersSent) {
421
- res.statusCode = 500;
422
- res.end("Internal MCP Error");
423
- }
415
+ _context8.t0 = _context8["catch"](4);
416
+ _didIteratorError = true;
417
+ _iteratorError = _context8.t0;
424
418
  case 20:
419
+ _context8.prev = 20;
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;
470
+ }
471
+ res.writeHead(405);
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');
425
480
  return _context8.abrupt("return");
426
- case 21:
427
- next();
428
- case 22:
481
+ case 51:
482
+ res.statusCode = 405;
483
+ res.end();
484
+ case 53:
429
485
  case "end":
430
486
  return _context8.stop();
431
487
  }
432
- }, _callee8, null, [[11, 16]]);
488
+ }, _callee8, null, [[4, 16, 20, 30], [21,, 25, 29], [31, 40]]);
433
489
  }));
434
- return function (_x6, _x7, _x8) {
490
+ return function (_x6, _x7) {
435
491
  return _ref6.apply(this, arguments);
436
492
  };
437
493
  }());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-swagger-mcp",
3
- "version": "0.0.10",
3
+ "version": "0.0.11",
4
4
  "description": "vite plugin for swagger mcp",
5
5
  "homepage": "https://github.com/mmdctjj/vite-plugin-swagger-mcp",
6
6
  "repository": {