vite-plugin-swagger-mcp 0.0.1 → 0.0.3

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.
@@ -1,5 +1,48 @@
1
1
  import type { Plugin } from "vite";
2
- export default function vitePluginMcp({ swaggerUrl, token, }: {
2
+ import { SchemaObject } from "./type";
3
+ export declare class SwaggerMcpServer {
4
+ private swaggerUrl;
5
+ private token?;
6
+ private swaggerDoc?;
7
+ constructor(swaggerUrl: string, token?: string);
8
+ private loadSwagger;
9
+ /** 1. 获取模块列表 */
10
+ getModules(): Promise<{
11
+ name: string;
12
+ description: string;
13
+ }[]>;
14
+ /**
15
+ * 递归解析引用类型,获取最底层类型定义
16
+ * @param doc Swagger文档对象
17
+ * @param ref 引用路径,如 '#/definitions/统一响应体«PageWrapper«VideoCreateResponse»»'
18
+ * @param visited 已访问的引用路径,用于防止循环引用
19
+ * @returns 最底层的类型定义
20
+ */
21
+ private resolveRef;
22
+ /** 2. 获取某模块下的接口 */
23
+ getModuleApis(module: string): Promise<any[]>;
24
+ /** 3. 获取某个接口的类型 */
25
+ getApiTypes(path: string, method: string): Promise<{
26
+ definitions: SchemaObject | undefined;
27
+ tags?: string[] | undefined;
28
+ summary?: string | undefined;
29
+ description?: string | undefined;
30
+ externalDocs?: import("./type").ExternalDocsObject | undefined;
31
+ operationId?: string | undefined;
32
+ consumes?: string[] | undefined;
33
+ produces?: string[] | undefined;
34
+ parameters?: import("./type").ParameterObject[] | undefined;
35
+ responses: {
36
+ [statusCode: string]: import("./type").ResponseObject;
37
+ };
38
+ schemes?: ("http" | "https" | "ws" | "wss")[] | undefined;
39
+ deprecated?: boolean | undefined;
40
+ security?: import("./type").SecurityRequirementObject[] | undefined;
41
+ path: string;
42
+ method: string;
43
+ }>;
44
+ }
45
+ export default function vitePluginSwaggerMcp({ swaggerUrl, token, }: {
3
46
  swaggerUrl: string;
4
47
  token?: string;
5
48
  }): Plugin;
package/dist/cjs/index.js CHANGED
@@ -19,16 +19,124 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  // src/index.ts
20
20
  var src_exports = {};
21
21
  __export(src_exports, {
22
- default: () => vitePluginMcp
22
+ SwaggerMcpServer: () => SwaggerMcpServer,
23
+ default: () => vitePluginSwaggerMcp
23
24
  });
24
25
  module.exports = __toCommonJS(src_exports);
25
26
  var import_mcp = require("@modelcontextprotocol/sdk/server/mcp.js");
26
27
  var import_zod = require("zod");
27
28
  var import_streamableHttp = require("@modelcontextprotocol/sdk/server/streamableHttp.js");
28
29
  var import_node_crypto = require("node:crypto");
29
- var import_swagger = require("./swagger");
30
+ var SwaggerMcpServer = class {
31
+ constructor(swaggerUrl, token) {
32
+ this.swaggerUrl = swaggerUrl;
33
+ this.token = token;
34
+ }
35
+ async loadSwagger() {
36
+ const res = await fetch(this.swaggerUrl, {
37
+ headers: this.token ? { Authorization: `${this.token}` } : {}
38
+ });
39
+ const data = await res.json();
40
+ this.swaggerDoc = data;
41
+ return this.swaggerDoc;
42
+ }
43
+ /** 1. 获取模块列表 */
44
+ async getModules() {
45
+ const doc = await this.loadSwagger();
46
+ return (doc.tags ?? []).map((t) => ({
47
+ name: t.name,
48
+ description: t.description || ""
49
+ }));
50
+ }
51
+ /**
52
+ * 递归解析引用类型,获取最底层类型定义
53
+ * @param doc Swagger文档对象
54
+ * @param ref 引用路径,如 '#/definitions/统一响应体«PageWrapper«VideoCreateResponse»»'
55
+ * @param visited 已访问的引用路径,用于防止循环引用
56
+ * @returns 最底层的类型定义
57
+ */
58
+ resolveRef(doc, ref, visited = /* @__PURE__ */ new Set()) {
59
+ var _a;
60
+ if (!ref)
61
+ return void 0;
62
+ if (visited.has(ref))
63
+ return void 0;
64
+ visited.add(ref);
65
+ const definition = (_a = doc.definitions) == null ? void 0 : _a[ref];
66
+ if (!definition)
67
+ return void 0;
68
+ if (definition.originalRef) {
69
+ return this.resolveRef(doc, definition.originalRef, visited);
70
+ }
71
+ if (definition.properties) {
72
+ Object.keys(definition.properties).forEach((key) => {
73
+ var _a2, _b, _c;
74
+ const prop = (_a2 = definition.properties) == null ? void 0 : _a2[key];
75
+ if (prop == null ? void 0 : prop.originalRef) {
76
+ const resolvedProp = this.resolveRef(
77
+ doc,
78
+ prop.originalRef,
79
+ new Set(visited)
80
+ );
81
+ if (resolvedProp && definition.properties) {
82
+ definition.properties[key] = resolvedProp;
83
+ }
84
+ }
85
+ if ((_b = prop == null ? void 0 : prop.items) == null ? void 0 : _b.originalRef) {
86
+ const resolvedProp = this.resolveRef(
87
+ doc,
88
+ (_c = prop == null ? void 0 : prop.items) == null ? void 0 : _c.originalRef,
89
+ new Set(visited)
90
+ );
91
+ if (resolvedProp && definition.properties) {
92
+ definition.properties[key] = resolvedProp;
93
+ }
94
+ }
95
+ });
96
+ }
97
+ return definition;
98
+ }
99
+ /** 2. 获取某模块下的接口 */
100
+ async getModuleApis(module2) {
101
+ const doc = await this.loadSwagger();
102
+ const apis = [];
103
+ Object.keys(doc.paths).forEach((path) => {
104
+ Object.keys(doc.paths[path]).map((method) => {
105
+ var _a, _b, _c, _d, _e;
106
+ const op = (_b = (_a = doc.paths[path][method]) == null ? void 0 : _a.tags) == null ? void 0 : _b.includes(module2);
107
+ if (op) {
108
+ const originalRef = (_e = (_d = (_c = doc.paths[path][method].responses) == null ? void 0 : _c["200"]) == null ? void 0 : _d.schema) == null ? void 0 : _e.originalRef;
109
+ const resolvedDefinition = this.resolveRef(doc, originalRef);
110
+ apis.push({
111
+ path,
112
+ method,
113
+ ...doc.paths[path][method],
114
+ definitions: resolvedDefinition
115
+ });
116
+ }
117
+ });
118
+ });
119
+ return apis;
120
+ }
121
+ /** 3. 获取某个接口的类型 */
122
+ async getApiTypes(path, method) {
123
+ var _a, _b, _c, _d;
124
+ const doc = await this.loadSwagger();
125
+ const op = (_a = doc.paths[path]) == null ? void 0 : _a[method.toLowerCase()];
126
+ if (!op)
127
+ throw new Error("接口不存在");
128
+ const originalRef = (_d = (_c = (_b = doc.paths[path][method].responses) == null ? void 0 : _b["200"]) == null ? void 0 : _c.schema) == null ? void 0 : _d.originalRef;
129
+ const resolvedDefinition = this.resolveRef(doc, originalRef);
130
+ return {
131
+ path,
132
+ method,
133
+ ...doc.paths[path][method],
134
+ definitions: resolvedDefinition
135
+ };
136
+ }
137
+ };
30
138
  var transports = {};
31
- function vitePluginMcp({
139
+ function vitePluginSwaggerMcp({
32
140
  swaggerUrl,
33
141
  token
34
142
  }) {
@@ -48,22 +156,11 @@ function vitePluginMcp({
48
156
  delete transports[transport.sessionId];
49
157
  }
50
158
  };
51
- const swaggerServer = new import_swagger.SwaggerMcpServer(swaggerUrl, token);
159
+ const swaggerServer = new SwaggerMcpServer(swaggerUrl, token);
52
160
  const mcpServer = new import_mcp.McpServer({
53
161
  name: "swagger-mcp-server",
54
162
  version: "0.1.0"
55
163
  });
56
- mcpServer.tool("updateSwaggerDoc", "获取最新接口文档", async () => {
57
- const res = await swaggerServer.getModules();
58
- return {
59
- content: [
60
- {
61
- type: "text",
62
- text: JSON.stringify(res)
63
- }
64
- ]
65
- };
66
- });
67
164
  mcpServer.tool("getModules", "获取模块列表", async () => {
68
165
  const res = await swaggerServer.getModules();
69
166
  return {
@@ -134,3 +231,7 @@ function vitePluginMcp({
134
231
  }
135
232
  };
136
233
  }
234
+ // Annotate the CommonJS export names for ESM import in node:
235
+ 0 && (module.exports = {
236
+ SwaggerMcpServer
237
+ });
@@ -1,5 +1,48 @@
1
1
  import type { Plugin } from "vite";
2
- export default function vitePluginMcp({ swaggerUrl, token, }: {
2
+ import { SchemaObject } from "./type";
3
+ export declare class SwaggerMcpServer {
4
+ private swaggerUrl;
5
+ private token?;
6
+ private swaggerDoc?;
7
+ constructor(swaggerUrl: string, token?: string);
8
+ private loadSwagger;
9
+ /** 1. 获取模块列表 */
10
+ getModules(): Promise<{
11
+ name: string;
12
+ description: string;
13
+ }[]>;
14
+ /**
15
+ * 递归解析引用类型,获取最底层类型定义
16
+ * @param doc Swagger文档对象
17
+ * @param ref 引用路径,如 '#/definitions/统一响应体«PageWrapper«VideoCreateResponse»»'
18
+ * @param visited 已访问的引用路径,用于防止循环引用
19
+ * @returns 最底层的类型定义
20
+ */
21
+ private resolveRef;
22
+ /** 2. 获取某模块下的接口 */
23
+ getModuleApis(module: string): Promise<any[]>;
24
+ /** 3. 获取某个接口的类型 */
25
+ getApiTypes(path: string, method: string): Promise<{
26
+ definitions: SchemaObject | undefined;
27
+ tags?: string[] | undefined;
28
+ summary?: string | undefined;
29
+ description?: string | undefined;
30
+ externalDocs?: import("./type").ExternalDocsObject | undefined;
31
+ operationId?: string | undefined;
32
+ consumes?: string[] | undefined;
33
+ produces?: string[] | undefined;
34
+ parameters?: import("./type").ParameterObject[] | undefined;
35
+ responses: {
36
+ [statusCode: string]: import("./type").ResponseObject;
37
+ };
38
+ schemes?: ("http" | "https" | "ws" | "wss")[] | undefined;
39
+ deprecated?: boolean | undefined;
40
+ security?: import("./type").SecurityRequirementObject[] | undefined;
41
+ path: string;
42
+ method: string;
43
+ }>;
44
+ }
45
+ export default function vitePluginSwaggerMcp({ swaggerUrl, token, }: {
3
46
  swaggerUrl: string;
4
47
  token?: string;
5
48
  }): Plugin;
package/dist/esm/index.js CHANGED
@@ -1,28 +1,247 @@
1
1
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
2
4
  function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
3
5
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
4
6
  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
7
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
9
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
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
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
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); }
5
13
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
6
14
  import { z } from "zod";
7
15
  import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
8
16
  import { randomUUID } from "node:crypto";
9
- import { SwaggerMcpServer } from "./swagger";
17
+ export var SwaggerMcpServer = /*#__PURE__*/function () {
18
+ function SwaggerMcpServer(swaggerUrl, token) {
19
+ _classCallCheck(this, SwaggerMcpServer);
20
+ _defineProperty(this, "swaggerUrl", void 0);
21
+ _defineProperty(this, "token", void 0);
22
+ _defineProperty(this, "swaggerDoc", void 0);
23
+ this.swaggerUrl = swaggerUrl;
24
+ this.token = token;
25
+ }
26
+ _createClass(SwaggerMcpServer, [{
27
+ key: "loadSwagger",
28
+ value: function () {
29
+ var _loadSwagger = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
30
+ var res, data;
31
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
32
+ while (1) switch (_context.prev = _context.next) {
33
+ case 0:
34
+ _context.next = 2;
35
+ return fetch(this.swaggerUrl, {
36
+ headers: this.token ? {
37
+ Authorization: "".concat(this.token)
38
+ } : {}
39
+ });
40
+ case 2:
41
+ res = _context.sent;
42
+ _context.next = 5;
43
+ return res.json();
44
+ case 5:
45
+ data = _context.sent;
46
+ this.swaggerDoc = data;
47
+ return _context.abrupt("return", this.swaggerDoc);
48
+ case 8:
49
+ case "end":
50
+ return _context.stop();
51
+ }
52
+ }, _callee, this);
53
+ }));
54
+ function loadSwagger() {
55
+ return _loadSwagger.apply(this, arguments);
56
+ }
57
+ return loadSwagger;
58
+ }() /** 1. 获取模块列表 */
59
+ }, {
60
+ key: "getModules",
61
+ value: (function () {
62
+ var _getModules = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
63
+ var _doc$tags;
64
+ var doc;
65
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
66
+ while (1) switch (_context2.prev = _context2.next) {
67
+ case 0:
68
+ _context2.next = 2;
69
+ return this.loadSwagger();
70
+ case 2:
71
+ doc = _context2.sent;
72
+ return _context2.abrupt("return", ((_doc$tags = doc.tags) !== null && _doc$tags !== void 0 ? _doc$tags : []).map(function (t) {
73
+ return {
74
+ name: t.name,
75
+ description: t.description || ""
76
+ };
77
+ }));
78
+ case 4:
79
+ case "end":
80
+ return _context2.stop();
81
+ }
82
+ }, _callee2, this);
83
+ }));
84
+ function getModules() {
85
+ return _getModules.apply(this, arguments);
86
+ }
87
+ return getModules;
88
+ }()
89
+ /**
90
+ * 递归解析引用类型,获取最底层类型定义
91
+ * @param doc Swagger文档对象
92
+ * @param ref 引用路径,如 '#/definitions/统一响应体«PageWrapper«VideoCreateResponse»»'
93
+ * @param visited 已访问的引用路径,用于防止循环引用
94
+ * @returns 最底层的类型定义
95
+ */
96
+ )
97
+ }, {
98
+ key: "resolveRef",
99
+ value: function resolveRef(doc, ref) {
100
+ var _doc$definitions,
101
+ _this = this;
102
+ var visited = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : new Set();
103
+ if (!ref) return undefined;
104
+
105
+ // 提取定义名称,处理泛型语法如 "统一响应体«PageWrapper«VideoCreateResponse»»"
106
+
107
+ // 防止循环引用
108
+ if (visited.has(ref)) return undefined;
109
+ visited.add(ref);
110
+
111
+ // 获取定义
112
+ var definition = (_doc$definitions = doc.definitions) === null || _doc$definitions === void 0 ? void 0 : _doc$definitions[ref];
113
+ if (!definition) return undefined;
114
+
115
+ // 如果定义是引用类型,继续解析
116
+ if (definition.originalRef) {
117
+ return this.resolveRef(doc, definition.originalRef, visited);
118
+ }
119
+
120
+ // 处理泛型类型,如包含items的情况
121
+ if (definition.properties) {
122
+ Object.keys(definition.properties).forEach(function (key) {
123
+ var _definition$propertie, _prop$items;
124
+ var prop = (_definition$propertie = definition.properties) === null || _definition$propertie === void 0 ? void 0 : _definition$propertie[key];
125
+ if (prop !== null && prop !== void 0 && prop.originalRef) {
126
+ var resolvedProp = _this.resolveRef(doc, prop.originalRef, new Set(visited));
127
+ if (resolvedProp && definition.properties) {
128
+ definition.properties[key] = resolvedProp;
129
+ }
130
+ }
131
+ if (prop !== null && prop !== void 0 && (_prop$items = prop.items) !== null && _prop$items !== void 0 && _prop$items.originalRef) {
132
+ var _prop$items2;
133
+ var _resolvedProp = _this.resolveRef(doc, prop === null || prop === void 0 || (_prop$items2 = prop.items) === null || _prop$items2 === void 0 ? void 0 : _prop$items2.originalRef, new Set(visited));
134
+ if (_resolvedProp && definition.properties) {
135
+ definition.properties[key] = _resolvedProp;
136
+ }
137
+ }
138
+ });
139
+ }
140
+ return definition;
141
+ }
142
+
143
+ /** 2. 获取某模块下的接口 */
144
+ }, {
145
+ key: "getModuleApis",
146
+ value: (function () {
147
+ var _getModuleApis = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(module) {
148
+ var _this2 = this;
149
+ var doc, apis;
150
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
151
+ while (1) switch (_context3.prev = _context3.next) {
152
+ case 0:
153
+ _context3.next = 2;
154
+ return this.loadSwagger();
155
+ case 2:
156
+ doc = _context3.sent;
157
+ apis = [];
158
+ Object.keys(doc.paths).forEach(function (path) {
159
+ Object.keys(doc.paths[path]).map(function (method) {
160
+ var _doc$paths$path$metho;
161
+ var op = (_doc$paths$path$metho = doc.paths[path][method]) === null || _doc$paths$path$metho === void 0 || (_doc$paths$path$metho = _doc$paths$path$metho.tags) === null || _doc$paths$path$metho === void 0 ? void 0 : _doc$paths$path$metho.includes(module);
162
+ if (op) {
163
+ var _doc$paths$path$metho2;
164
+ var originalRef = (_doc$paths$path$metho2 = doc.paths[path][method].responses) === null || _doc$paths$path$metho2 === void 0 || (_doc$paths$path$metho2 = _doc$paths$path$metho2["200"]) === null || _doc$paths$path$metho2 === void 0 || (_doc$paths$path$metho2 = _doc$paths$path$metho2.schema) === null || _doc$paths$path$metho2 === void 0 ? void 0 : _doc$paths$path$metho2.originalRef;
165
+ var resolvedDefinition = _this2.resolveRef(doc, originalRef);
166
+ apis.push(_objectSpread(_objectSpread({
167
+ path: path,
168
+ method: method
169
+ }, doc.paths[path][method]), {}, {
170
+ definitions: resolvedDefinition
171
+ }));
172
+ }
173
+ });
174
+ });
175
+ return _context3.abrupt("return", apis);
176
+ case 6:
177
+ case "end":
178
+ return _context3.stop();
179
+ }
180
+ }, _callee3, this);
181
+ }));
182
+ function getModuleApis(_x) {
183
+ return _getModuleApis.apply(this, arguments);
184
+ }
185
+ return getModuleApis;
186
+ }() /** 3. 获取某个接口的类型 */)
187
+ }, {
188
+ key: "getApiTypes",
189
+ value: (function () {
190
+ var _getApiTypes = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(path, method) {
191
+ var _doc$paths$path, _doc$paths$path$metho3;
192
+ var doc, op, originalRef, resolvedDefinition;
193
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
194
+ while (1) switch (_context4.prev = _context4.next) {
195
+ case 0:
196
+ _context4.next = 2;
197
+ return this.loadSwagger();
198
+ case 2:
199
+ doc = _context4.sent;
200
+ op = (_doc$paths$path = doc.paths[path]) === null || _doc$paths$path === void 0 ? void 0 : _doc$paths$path[method.toLowerCase()];
201
+ if (op) {
202
+ _context4.next = 6;
203
+ break;
204
+ }
205
+ throw new Error("接口不存在");
206
+ case 6:
207
+ originalRef = (_doc$paths$path$metho3 = doc.paths[path][method].responses) === null || _doc$paths$path$metho3 === void 0 || (_doc$paths$path$metho3 = _doc$paths$path$metho3["200"]) === null || _doc$paths$path$metho3 === void 0 || (_doc$paths$path$metho3 = _doc$paths$path$metho3.schema) === null || _doc$paths$path$metho3 === void 0 ? void 0 : _doc$paths$path$metho3.originalRef;
208
+ resolvedDefinition = this.resolveRef(doc, originalRef);
209
+ return _context4.abrupt("return", _objectSpread(_objectSpread({
210
+ path: path,
211
+ method: method
212
+ }, doc.paths[path][method]), {}, {
213
+ definitions: resolvedDefinition
214
+ }));
215
+ case 9:
216
+ case "end":
217
+ return _context4.stop();
218
+ }
219
+ }, _callee4, this);
220
+ }));
221
+ function getApiTypes(_x2, _x3) {
222
+ return _getApiTypes.apply(this, arguments);
223
+ }
224
+ return getApiTypes;
225
+ }())
226
+ }]);
227
+ return SwaggerMcpServer;
228
+ }();
10
229
 
11
230
  // Map to store transports by session ID
12
231
  var transports = {};
13
- export default function vitePluginMcp(_ref) {
232
+ export default function vitePluginSwaggerMcp(_ref) {
14
233
  var swaggerUrl = _ref.swaggerUrl,
15
234
  token = _ref.token;
16
235
  return {
17
236
  name: "vite-plugin-swagger-mcp",
18
237
  enforce: "pre",
19
238
  configureServer: function configureServer(server) {
20
- return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
239
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9() {
21
240
  var transport, swaggerServer, mcpServer;
22
- return _regeneratorRuntime().wrap(function _callee6$(_context6) {
23
- while (1) switch (_context6.prev = _context6.next) {
241
+ return _regeneratorRuntime().wrap(function _callee9$(_context9) {
242
+ while (1) switch (_context9.prev = _context9.next) {
24
243
  case 0:
25
- _context6.prev = 0;
244
+ _context9.prev = 0;
26
245
  transport = new StreamableHTTPServerTransport({
27
246
  sessionIdGenerator: function sessionIdGenerator() {
28
247
  return randomUUID();
@@ -42,44 +261,19 @@ export default function vitePluginMcp(_ref) {
42
261
  name: "swagger-mcp-server",
43
262
  version: "0.1.0"
44
263
  }); // 注册工具
45
- /***
46
- * 获取最新接口文档
47
- */
48
- mcpServer.tool("updateSwaggerDoc", "获取最新接口文档", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
49
- var res;
50
- return _regeneratorRuntime().wrap(function _callee$(_context) {
51
- while (1) switch (_context.prev = _context.next) {
52
- case 0:
53
- _context.next = 2;
54
- return swaggerServer.getModules();
55
- case 2:
56
- res = _context.sent;
57
- return _context.abrupt("return", {
58
- content: [{
59
- type: "text",
60
- text: JSON.stringify(res)
61
- }]
62
- });
63
- case 4:
64
- case "end":
65
- return _context.stop();
66
- }
67
- }, _callee);
68
- })));
69
-
70
264
  /***
71
265
  * 获取模块列表
72
266
  */
73
- mcpServer.tool("getModules", "获取模块列表", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
267
+ mcpServer.tool("getModules", "获取模块列表", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
74
268
  var res;
75
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
76
- while (1) switch (_context2.prev = _context2.next) {
269
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
270
+ while (1) switch (_context5.prev = _context5.next) {
77
271
  case 0:
78
- _context2.next = 2;
272
+ _context5.next = 2;
79
273
  return swaggerServer.getModules();
80
274
  case 2:
81
- res = _context2.sent;
82
- return _context2.abrupt("return", {
275
+ res = _context5.sent;
276
+ return _context5.abrupt("return", {
83
277
  content: [{
84
278
  type: "text",
85
279
  text: JSON.stringify(res)
@@ -87,9 +281,9 @@ export default function vitePluginMcp(_ref) {
87
281
  });
88
282
  case 4:
89
283
  case "end":
90
- return _context2.stop();
284
+ return _context5.stop();
91
285
  }
92
- }, _callee2);
286
+ }, _callee5);
93
287
  })));
94
288
 
95
289
  /***
@@ -98,17 +292,17 @@ export default function vitePluginMcp(_ref) {
98
292
  mcpServer.tool("getModuleApis", "获取特定模块下的所有接口及返回值类型", {
99
293
  module: z.string().describe("模块名称")
100
294
  }, /*#__PURE__*/function () {
101
- var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(_ref4) {
295
+ var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(_ref3) {
102
296
  var module, res;
103
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
104
- while (1) switch (_context3.prev = _context3.next) {
297
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
298
+ while (1) switch (_context6.prev = _context6.next) {
105
299
  case 0:
106
- module = _ref4.module;
300
+ module = _ref3.module;
107
301
  if (module) {
108
- _context3.next = 3;
302
+ _context6.next = 3;
109
303
  break;
110
304
  }
111
- return _context3.abrupt("return", {
305
+ return _context6.abrupt("return", {
112
306
  content: [{
113
307
  type: "text",
114
308
  text: JSON.stringify({
@@ -117,11 +311,11 @@ export default function vitePluginMcp(_ref) {
117
311
  }]
118
312
  });
119
313
  case 3:
120
- _context3.next = 5;
314
+ _context6.next = 5;
121
315
  return swaggerServer.getModuleApis(module);
122
316
  case 5:
123
- res = _context3.sent;
124
- return _context3.abrupt("return", {
317
+ res = _context6.sent;
318
+ return _context6.abrupt("return", {
125
319
  content: [{
126
320
  type: "text",
127
321
  text: JSON.stringify(res)
@@ -129,12 +323,12 @@ export default function vitePluginMcp(_ref) {
129
323
  });
130
324
  case 7:
131
325
  case "end":
132
- return _context3.stop();
326
+ return _context6.stop();
133
327
  }
134
- }, _callee3);
328
+ }, _callee6);
135
329
  }));
136
- return function (_x) {
137
- return _ref5.apply(this, arguments);
330
+ return function (_x4) {
331
+ return _ref4.apply(this, arguments);
138
332
  };
139
333
  }());
140
334
 
@@ -145,48 +339,48 @@ export default function vitePluginMcp(_ref) {
145
339
  path: z.string(),
146
340
  method: z.string()
147
341
  }, /*#__PURE__*/function () {
148
- var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(args) {
149
- return _regeneratorRuntime().wrap(function _callee4$(_context4) {
150
- while (1) switch (_context4.prev = _context4.next) {
342
+ var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(args) {
343
+ return _regeneratorRuntime().wrap(function _callee7$(_context7) {
344
+ while (1) switch (_context7.prev = _context7.next) {
151
345
  case 0:
152
- _context4.t0 = JSON;
153
- _context4.next = 3;
346
+ _context7.t0 = JSON;
347
+ _context7.next = 3;
154
348
  return swaggerServer.getApiTypes(args.path, args.method);
155
349
  case 3:
156
- _context4.t1 = _context4.sent;
157
- _context4.t2 = _context4.t0.stringify.call(_context4.t0, _context4.t1);
158
- _context4.t3 = {
350
+ _context7.t1 = _context7.sent;
351
+ _context7.t2 = _context7.t0.stringify.call(_context7.t0, _context7.t1);
352
+ _context7.t3 = {
159
353
  type: "text",
160
- text: _context4.t2
354
+ text: _context7.t2
161
355
  };
162
- _context4.t4 = [_context4.t3];
163
- return _context4.abrupt("return", {
164
- content: _context4.t4
356
+ _context7.t4 = [_context7.t3];
357
+ return _context7.abrupt("return", {
358
+ content: _context7.t4
165
359
  });
166
360
  case 8:
167
361
  case "end":
168
- return _context4.stop();
362
+ return _context7.stop();
169
363
  }
170
- }, _callee4);
364
+ }, _callee7);
171
365
  }));
172
- return function (_x2) {
173
- return _ref6.apply(this, arguments);
366
+ return function (_x5) {
367
+ return _ref5.apply(this, arguments);
174
368
  };
175
369
  }());
176
370
 
177
371
  // Connect to the MCP mcpServer
178
- _context6.next = 11;
372
+ _context9.next = 10;
179
373
  return mcpServer.connect(transport);
180
- case 11:
374
+ case 10:
181
375
  console.log("MCP server connected");
182
376
  server.middlewares.use( /*#__PURE__*/function () {
183
- var _ref7 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(req, res, next) {
377
+ var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(req, res, next) {
184
378
  var _req$url;
185
- return _regeneratorRuntime().wrap(function _callee5$(_context5) {
186
- while (1) switch (_context5.prev = _context5.next) {
379
+ return _regeneratorRuntime().wrap(function _callee8$(_context8) {
380
+ while (1) switch (_context8.prev = _context8.next) {
187
381
  case 0:
188
382
  if (!(req.method === "POST" && (_req$url = req.url) !== null && _req$url !== void 0 && _req$url.startsWith("/_mcp/sse/swagger"))) {
189
- _context5.next = 6;
383
+ _context8.next = 6;
190
384
  break;
191
385
  }
192
386
  if (!req.headers["mcp-session-id"] && transport.sessionId) {
@@ -194,34 +388,34 @@ export default function vitePluginMcp(_ref) {
194
388
  req.headers["mcp-session-id"] = transport.sessionId;
195
389
  }
196
390
  // Handle the request
197
- _context5.next = 4;
391
+ _context8.next = 4;
198
392
  return transport.handleRequest(req, res);
199
393
  case 4:
200
- _context5.next = 7;
394
+ _context8.next = 7;
201
395
  break;
202
396
  case 6:
203
397
  next();
204
398
  case 7:
205
399
  case "end":
206
- return _context5.stop();
400
+ return _context8.stop();
207
401
  }
208
- }, _callee5);
402
+ }, _callee8);
209
403
  }));
210
- return function (_x3, _x4, _x5) {
211
- return _ref7.apply(this, arguments);
404
+ return function (_x6, _x7, _x8) {
405
+ return _ref6.apply(this, arguments);
212
406
  };
213
407
  }());
214
- _context6.next = 18;
408
+ _context9.next = 17;
215
409
  break;
216
- case 15:
217
- _context6.prev = 15;
218
- _context6.t0 = _context6["catch"](0);
219
- console.log("MCP server error", _context6.t0);
220
- case 18:
410
+ case 14:
411
+ _context9.prev = 14;
412
+ _context9.t0 = _context9["catch"](0);
413
+ console.log("MCP server error", _context9.t0);
414
+ case 17:
221
415
  case "end":
222
- return _context6.stop();
416
+ return _context9.stop();
223
417
  }
224
- }, _callee6, null, [[0, 15]]);
418
+ }, _callee9, null, [[0, 14]]);
225
419
  }))();
226
420
  }
227
421
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-swagger-mcp",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "vite plugin for swagger mcp",
5
5
  "homepage": "https://github.com/mmctjj/vite-plugin-swagger-mcp",
6
6
  "repository": {
@@ -13,7 +13,7 @@
13
13
  "exports": {
14
14
  ".": {
15
15
  "import": "./dist/esm/index.js",
16
- "require": "./dist/esm/index.js",
16
+ "require": "./dist/cjs/index.js",
17
17
  "types": "./dist/esm/index.d.ts"
18
18
  }
19
19
  },
@@ -1,43 +0,0 @@
1
- import { SchemaObject } from "./type";
2
- export declare class SwaggerMcpServer {
3
- private swaggerUrl;
4
- private token?;
5
- private swaggerDoc?;
6
- constructor(swaggerUrl: string, token?: string);
7
- private loadSwagger;
8
- /** 1. 获取模块列表 */
9
- getModules(): Promise<{
10
- name: string;
11
- description: string;
12
- }[]>;
13
- /**
14
- * 递归解析引用类型,获取最底层类型定义
15
- * @param doc Swagger文档对象
16
- * @param ref 引用路径,如 '#/definitions/统一响应体«PageWrapper«VideoCreateResponse»»'
17
- * @param visited 已访问的引用路径,用于防止循环引用
18
- * @returns 最底层的类型定义
19
- */
20
- private resolveRef;
21
- /** 2. 获取某模块下的接口 */
22
- getModuleApis(module: string): Promise<any[]>;
23
- /** 3. 获取某个接口的类型 */
24
- getApiTypes(path: string, method: string): Promise<{
25
- definitions: SchemaObject | undefined;
26
- tags?: string[] | undefined;
27
- summary?: string | undefined;
28
- description?: string | undefined;
29
- externalDocs?: import("./type").ExternalDocsObject | undefined;
30
- operationId?: string | undefined;
31
- consumes?: string[] | undefined;
32
- produces?: string[] | undefined;
33
- parameters?: import("./type").ParameterObject[] | undefined;
34
- responses: {
35
- [statusCode: string]: import("./type").ResponseObject;
36
- };
37
- schemes?: ("http" | "https" | "ws" | "wss")[] | undefined;
38
- deprecated?: boolean | undefined;
39
- security?: import("./type").SecurityRequirementObject[] | undefined;
40
- path: string;
41
- method: string;
42
- }>;
43
- }
@@ -1,136 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __export = (target, all) => {
6
- for (var name in all)
7
- __defProp(target, name, { get: all[name], enumerable: true });
8
- };
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") {
11
- for (let key of __getOwnPropNames(from))
12
- if (!__hasOwnProp.call(to, key) && key !== except)
13
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
- }
15
- return to;
16
- };
17
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
-
19
- // src/swagger.ts
20
- var swagger_exports = {};
21
- __export(swagger_exports, {
22
- SwaggerMcpServer: () => SwaggerMcpServer
23
- });
24
- module.exports = __toCommonJS(swagger_exports);
25
- var SwaggerMcpServer = class {
26
- constructor(swaggerUrl, token) {
27
- this.swaggerUrl = swaggerUrl;
28
- this.token = token;
29
- }
30
- async loadSwagger() {
31
- const res = await fetch(this.swaggerUrl, {
32
- headers: this.token ? { Authorization: `${this.token}` } : {}
33
- });
34
- const data = await res.json();
35
- this.swaggerDoc = data;
36
- return this.swaggerDoc;
37
- }
38
- /** 1. 获取模块列表 */
39
- async getModules() {
40
- const doc = await this.loadSwagger();
41
- return (doc.tags ?? []).map((t) => ({
42
- name: t.name,
43
- description: t.description || ""
44
- }));
45
- }
46
- /**
47
- * 递归解析引用类型,获取最底层类型定义
48
- * @param doc Swagger文档对象
49
- * @param ref 引用路径,如 '#/definitions/统一响应体«PageWrapper«VideoCreateResponse»»'
50
- * @param visited 已访问的引用路径,用于防止循环引用
51
- * @returns 最底层的类型定义
52
- */
53
- resolveRef(doc, ref, visited = /* @__PURE__ */ new Set()) {
54
- var _a;
55
- if (!ref)
56
- return void 0;
57
- if (visited.has(ref))
58
- return void 0;
59
- visited.add(ref);
60
- const definition = (_a = doc.definitions) == null ? void 0 : _a[ref];
61
- if (!definition)
62
- return void 0;
63
- if (definition.originalRef) {
64
- return this.resolveRef(doc, definition.originalRef, visited);
65
- }
66
- if (definition.properties) {
67
- Object.keys(definition.properties).forEach((key) => {
68
- var _a2, _b, _c;
69
- const prop = (_a2 = definition.properties) == null ? void 0 : _a2[key];
70
- if (prop == null ? void 0 : prop.originalRef) {
71
- const resolvedProp = this.resolveRef(
72
- doc,
73
- prop.originalRef,
74
- new Set(visited)
75
- );
76
- if (resolvedProp && definition.properties) {
77
- definition.properties[key] = resolvedProp;
78
- }
79
- }
80
- if ((_b = prop == null ? void 0 : prop.items) == null ? void 0 : _b.originalRef) {
81
- const resolvedProp = this.resolveRef(
82
- doc,
83
- (_c = prop == null ? void 0 : prop.items) == null ? void 0 : _c.originalRef,
84
- new Set(visited)
85
- );
86
- if (resolvedProp && definition.properties) {
87
- definition.properties[key] = resolvedProp;
88
- }
89
- }
90
- });
91
- }
92
- return definition;
93
- }
94
- /** 2. 获取某模块下的接口 */
95
- async getModuleApis(module2) {
96
- const doc = await this.loadSwagger();
97
- const apis = [];
98
- Object.keys(doc.paths).forEach((path) => {
99
- Object.keys(doc.paths[path]).map((method) => {
100
- var _a, _b, _c, _d, _e;
101
- const op = (_b = (_a = doc.paths[path][method]) == null ? void 0 : _a.tags) == null ? void 0 : _b.includes(module2);
102
- if (op) {
103
- const originalRef = (_e = (_d = (_c = doc.paths[path][method].responses) == null ? void 0 : _c["200"]) == null ? void 0 : _d.schema) == null ? void 0 : _e.originalRef;
104
- const resolvedDefinition = this.resolveRef(doc, originalRef);
105
- apis.push({
106
- path,
107
- method,
108
- ...doc.paths[path][method],
109
- definitions: resolvedDefinition
110
- });
111
- }
112
- });
113
- });
114
- return apis;
115
- }
116
- /** 3. 获取某个接口的类型 */
117
- async getApiTypes(path, method) {
118
- var _a, _b, _c, _d;
119
- const doc = await this.loadSwagger();
120
- const op = (_a = doc.paths[path]) == null ? void 0 : _a[method.toLowerCase()];
121
- if (!op)
122
- throw new Error("接口不存在");
123
- const originalRef = (_d = (_c = (_b = doc.paths[path][method].responses) == null ? void 0 : _b["200"]) == null ? void 0 : _c.schema) == null ? void 0 : _d.originalRef;
124
- const resolvedDefinition = this.resolveRef(doc, originalRef);
125
- return {
126
- path,
127
- method,
128
- ...doc.paths[path][method],
129
- definitions: resolvedDefinition
130
- };
131
- }
132
- };
133
- // Annotate the CommonJS export names for ESM import in node:
134
- 0 && (module.exports = {
135
- SwaggerMcpServer
136
- });
@@ -1,43 +0,0 @@
1
- import { SchemaObject } from "./type";
2
- export declare class SwaggerMcpServer {
3
- private swaggerUrl;
4
- private token?;
5
- private swaggerDoc?;
6
- constructor(swaggerUrl: string, token?: string);
7
- private loadSwagger;
8
- /** 1. 获取模块列表 */
9
- getModules(): Promise<{
10
- name: string;
11
- description: string;
12
- }[]>;
13
- /**
14
- * 递归解析引用类型,获取最底层类型定义
15
- * @param doc Swagger文档对象
16
- * @param ref 引用路径,如 '#/definitions/统一响应体«PageWrapper«VideoCreateResponse»»'
17
- * @param visited 已访问的引用路径,用于防止循环引用
18
- * @returns 最底层的类型定义
19
- */
20
- private resolveRef;
21
- /** 2. 获取某模块下的接口 */
22
- getModuleApis(module: string): Promise<any[]>;
23
- /** 3. 获取某个接口的类型 */
24
- getApiTypes(path: string, method: string): Promise<{
25
- definitions: SchemaObject | undefined;
26
- tags?: string[] | undefined;
27
- summary?: string | undefined;
28
- description?: string | undefined;
29
- externalDocs?: import("./type").ExternalDocsObject | undefined;
30
- operationId?: string | undefined;
31
- consumes?: string[] | undefined;
32
- produces?: string[] | undefined;
33
- parameters?: import("./type").ParameterObject[] | undefined;
34
- responses: {
35
- [statusCode: string]: import("./type").ResponseObject;
36
- };
37
- schemes?: ("http" | "https" | "ws" | "wss")[] | undefined;
38
- deprecated?: boolean | undefined;
39
- security?: import("./type").SecurityRequirementObject[] | undefined;
40
- path: string;
41
- method: string;
42
- }>;
43
- }
@@ -1,224 +0,0 @@
1
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
- function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
5
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
6
- function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
7
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
9
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
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
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
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
- export var SwaggerMcpServer = /*#__PURE__*/function () {
14
- function SwaggerMcpServer(swaggerUrl, token) {
15
- _classCallCheck(this, SwaggerMcpServer);
16
- _defineProperty(this, "swaggerUrl", void 0);
17
- _defineProperty(this, "token", void 0);
18
- _defineProperty(this, "swaggerDoc", void 0);
19
- this.swaggerUrl = swaggerUrl;
20
- this.token = token;
21
- }
22
- _createClass(SwaggerMcpServer, [{
23
- key: "loadSwagger",
24
- value: function () {
25
- var _loadSwagger = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
26
- var res, data;
27
- return _regeneratorRuntime().wrap(function _callee$(_context) {
28
- while (1) switch (_context.prev = _context.next) {
29
- case 0:
30
- _context.next = 2;
31
- return fetch(this.swaggerUrl, {
32
- headers: this.token ? {
33
- Authorization: "".concat(this.token)
34
- } : {}
35
- });
36
- case 2:
37
- res = _context.sent;
38
- _context.next = 5;
39
- return res.json();
40
- case 5:
41
- data = _context.sent;
42
- this.swaggerDoc = data;
43
- return _context.abrupt("return", this.swaggerDoc);
44
- case 8:
45
- case "end":
46
- return _context.stop();
47
- }
48
- }, _callee, this);
49
- }));
50
- function loadSwagger() {
51
- return _loadSwagger.apply(this, arguments);
52
- }
53
- return loadSwagger;
54
- }() /** 1. 获取模块列表 */
55
- }, {
56
- key: "getModules",
57
- value: (function () {
58
- var _getModules = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
59
- var _doc$tags;
60
- var doc;
61
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
62
- while (1) switch (_context2.prev = _context2.next) {
63
- case 0:
64
- _context2.next = 2;
65
- return this.loadSwagger();
66
- case 2:
67
- doc = _context2.sent;
68
- return _context2.abrupt("return", ((_doc$tags = doc.tags) !== null && _doc$tags !== void 0 ? _doc$tags : []).map(function (t) {
69
- return {
70
- name: t.name,
71
- description: t.description || ""
72
- };
73
- }));
74
- case 4:
75
- case "end":
76
- return _context2.stop();
77
- }
78
- }, _callee2, this);
79
- }));
80
- function getModules() {
81
- return _getModules.apply(this, arguments);
82
- }
83
- return getModules;
84
- }()
85
- /**
86
- * 递归解析引用类型,获取最底层类型定义
87
- * @param doc Swagger文档对象
88
- * @param ref 引用路径,如 '#/definitions/统一响应体«PageWrapper«VideoCreateResponse»»'
89
- * @param visited 已访问的引用路径,用于防止循环引用
90
- * @returns 最底层的类型定义
91
- */
92
- )
93
- }, {
94
- key: "resolveRef",
95
- value: function resolveRef(doc, ref) {
96
- var _doc$definitions,
97
- _this = this;
98
- var visited = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : new Set();
99
- if (!ref) return undefined;
100
-
101
- // 提取定义名称,处理泛型语法如 "统一响应体«PageWrapper«VideoCreateResponse»»"
102
-
103
- // 防止循环引用
104
- if (visited.has(ref)) return undefined;
105
- visited.add(ref);
106
-
107
- // 获取定义
108
- var definition = (_doc$definitions = doc.definitions) === null || _doc$definitions === void 0 ? void 0 : _doc$definitions[ref];
109
- if (!definition) return undefined;
110
-
111
- // 如果定义是引用类型,继续解析
112
- if (definition.originalRef) {
113
- return this.resolveRef(doc, definition.originalRef, visited);
114
- }
115
-
116
- // 处理泛型类型,如包含items的情况
117
- if (definition.properties) {
118
- Object.keys(definition.properties).forEach(function (key) {
119
- var _definition$propertie, _prop$items;
120
- var prop = (_definition$propertie = definition.properties) === null || _definition$propertie === void 0 ? void 0 : _definition$propertie[key];
121
- if (prop !== null && prop !== void 0 && prop.originalRef) {
122
- var resolvedProp = _this.resolveRef(doc, prop.originalRef, new Set(visited));
123
- if (resolvedProp && definition.properties) {
124
- definition.properties[key] = resolvedProp;
125
- }
126
- }
127
- if (prop !== null && prop !== void 0 && (_prop$items = prop.items) !== null && _prop$items !== void 0 && _prop$items.originalRef) {
128
- var _prop$items2;
129
- var _resolvedProp = _this.resolveRef(doc, prop === null || prop === void 0 || (_prop$items2 = prop.items) === null || _prop$items2 === void 0 ? void 0 : _prop$items2.originalRef, new Set(visited));
130
- if (_resolvedProp && definition.properties) {
131
- definition.properties[key] = _resolvedProp;
132
- }
133
- }
134
- });
135
- }
136
- return definition;
137
- }
138
-
139
- /** 2. 获取某模块下的接口 */
140
- }, {
141
- key: "getModuleApis",
142
- value: (function () {
143
- var _getModuleApis = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(module) {
144
- var _this2 = this;
145
- var doc, apis;
146
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
147
- while (1) switch (_context3.prev = _context3.next) {
148
- case 0:
149
- _context3.next = 2;
150
- return this.loadSwagger();
151
- case 2:
152
- doc = _context3.sent;
153
- apis = [];
154
- Object.keys(doc.paths).forEach(function (path) {
155
- Object.keys(doc.paths[path]).map(function (method) {
156
- var _doc$paths$path$metho;
157
- var op = (_doc$paths$path$metho = doc.paths[path][method]) === null || _doc$paths$path$metho === void 0 || (_doc$paths$path$metho = _doc$paths$path$metho.tags) === null || _doc$paths$path$metho === void 0 ? void 0 : _doc$paths$path$metho.includes(module);
158
- if (op) {
159
- var _doc$paths$path$metho2;
160
- var originalRef = (_doc$paths$path$metho2 = doc.paths[path][method].responses) === null || _doc$paths$path$metho2 === void 0 || (_doc$paths$path$metho2 = _doc$paths$path$metho2["200"]) === null || _doc$paths$path$metho2 === void 0 || (_doc$paths$path$metho2 = _doc$paths$path$metho2.schema) === null || _doc$paths$path$metho2 === void 0 ? void 0 : _doc$paths$path$metho2.originalRef;
161
- var resolvedDefinition = _this2.resolveRef(doc, originalRef);
162
- apis.push(_objectSpread(_objectSpread({
163
- path: path,
164
- method: method
165
- }, doc.paths[path][method]), {}, {
166
- definitions: resolvedDefinition
167
- }));
168
- }
169
- });
170
- });
171
- return _context3.abrupt("return", apis);
172
- case 6:
173
- case "end":
174
- return _context3.stop();
175
- }
176
- }, _callee3, this);
177
- }));
178
- function getModuleApis(_x) {
179
- return _getModuleApis.apply(this, arguments);
180
- }
181
- return getModuleApis;
182
- }() /** 3. 获取某个接口的类型 */)
183
- }, {
184
- key: "getApiTypes",
185
- value: (function () {
186
- var _getApiTypes = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(path, method) {
187
- var _doc$paths$path, _doc$paths$path$metho3;
188
- var doc, op, originalRef, resolvedDefinition;
189
- return _regeneratorRuntime().wrap(function _callee4$(_context4) {
190
- while (1) switch (_context4.prev = _context4.next) {
191
- case 0:
192
- _context4.next = 2;
193
- return this.loadSwagger();
194
- case 2:
195
- doc = _context4.sent;
196
- op = (_doc$paths$path = doc.paths[path]) === null || _doc$paths$path === void 0 ? void 0 : _doc$paths$path[method.toLowerCase()];
197
- if (op) {
198
- _context4.next = 6;
199
- break;
200
- }
201
- throw new Error("接口不存在");
202
- case 6:
203
- originalRef = (_doc$paths$path$metho3 = doc.paths[path][method].responses) === null || _doc$paths$path$metho3 === void 0 || (_doc$paths$path$metho3 = _doc$paths$path$metho3["200"]) === null || _doc$paths$path$metho3 === void 0 || (_doc$paths$path$metho3 = _doc$paths$path$metho3.schema) === null || _doc$paths$path$metho3 === void 0 ? void 0 : _doc$paths$path$metho3.originalRef;
204
- resolvedDefinition = this.resolveRef(doc, originalRef);
205
- return _context4.abrupt("return", _objectSpread(_objectSpread({
206
- path: path,
207
- method: method
208
- }, doc.paths[path][method]), {}, {
209
- definitions: resolvedDefinition
210
- }));
211
- case 9:
212
- case "end":
213
- return _context4.stop();
214
- }
215
- }, _callee4, this);
216
- }));
217
- function getApiTypes(_x2, _x3) {
218
- return _getApiTypes.apply(this, arguments);
219
- }
220
- return getApiTypes;
221
- }())
222
- }]);
223
- return SwaggerMcpServer;
224
- }();