mcp-expose 1.0.0
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/CHANGELOG.md +39 -0
- package/LICENSE +21 -0
- package/README.md +708 -0
- package/dist/adonisjs/index.cjs +708 -0
- package/dist/adonisjs/index.cjs.map +1 -0
- package/dist/adonisjs/index.d.cts +68 -0
- package/dist/adonisjs/index.d.ts +68 -0
- package/dist/adonisjs/index.js +684 -0
- package/dist/adonisjs/index.js.map +1 -0
- package/dist/express/index.cjs +789 -0
- package/dist/express/index.cjs.map +1 -0
- package/dist/express/index.d.cts +61 -0
- package/dist/express/index.d.ts +61 -0
- package/dist/express/index.js +763 -0
- package/dist/express/index.js.map +1 -0
- package/dist/fastify/index.cjs +660 -0
- package/dist/fastify/index.cjs.map +1 -0
- package/dist/fastify/index.d.cts +42 -0
- package/dist/fastify/index.d.ts +42 -0
- package/dist/fastify/index.js +635 -0
- package/dist/fastify/index.js.map +1 -0
- package/dist/hono/index.cjs +656 -0
- package/dist/hono/index.cjs.map +1 -0
- package/dist/hono/index.d.cts +44 -0
- package/dist/hono/index.d.ts +44 -0
- package/dist/hono/index.js +630 -0
- package/dist/hono/index.js.map +1 -0
- package/dist/index.cjs +720 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +81 -0
- package/dist/index.d.ts +81 -0
- package/dist/index.js +686 -0
- package/dist/index.js.map +1 -0
- package/dist/koa/index.cjs +739 -0
- package/dist/koa/index.cjs.map +1 -0
- package/dist/koa/index.d.cts +68 -0
- package/dist/koa/index.d.ts +68 -0
- package/dist/koa/index.js +709 -0
- package/dist/koa/index.js.map +1 -0
- package/dist/nestjs/index.cjs +1117 -0
- package/dist/nestjs/index.cjs.map +1 -0
- package/dist/nestjs/index.d.cts +104 -0
- package/dist/nestjs/index.d.ts +104 -0
- package/dist/nestjs/index.js +1079 -0
- package/dist/nestjs/index.js.map +1 -0
- package/dist/server-fzkb-wJo.d.cts +194 -0
- package/dist/server-fzkb-wJo.d.ts +194 -0
- package/package.json +225 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,720 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
var src_exports = {};
|
|
23
|
+
__export(src_exports, {
|
|
24
|
+
DEFAULT_FORWARD_HEADERS: () => DEFAULT_FORWARD_HEADERS,
|
|
25
|
+
LATEST_PROTOCOL_VERSION: () => LATEST_PROTOCOL_VERSION,
|
|
26
|
+
McpServer: () => McpServer,
|
|
27
|
+
SUPPORTED_PROTOCOL_VERSIONS: () => SUPPORTED_PROTOCOL_VERSIONS,
|
|
28
|
+
createFetchDispatcher: () => createFetchDispatcher,
|
|
29
|
+
createRouteTool: () => createRouteTool,
|
|
30
|
+
defineTool: () => defineTool,
|
|
31
|
+
toJsonSchema: () => toJsonSchema,
|
|
32
|
+
toToolResult: () => toToolResult,
|
|
33
|
+
toolsFromOpenApi: () => toolsFromOpenApi
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(src_exports);
|
|
36
|
+
|
|
37
|
+
// src/core/server.ts
|
|
38
|
+
var SUPPORTED_PROTOCOL_VERSIONS = [
|
|
39
|
+
"2025-11-25",
|
|
40
|
+
"2025-06-18",
|
|
41
|
+
"2025-03-26",
|
|
42
|
+
"2024-11-05"
|
|
43
|
+
];
|
|
44
|
+
var LATEST_PROTOCOL_VERSION = SUPPORTED_PROTOCOL_VERSIONS[0];
|
|
45
|
+
var PARSE_ERROR = -32700;
|
|
46
|
+
var INVALID_REQUEST = -32600;
|
|
47
|
+
var METHOD_NOT_FOUND = -32601;
|
|
48
|
+
var INVALID_PARAMS = -32602;
|
|
49
|
+
var ok = /* @__PURE__ */ __name((id, result) => ({
|
|
50
|
+
jsonrpc: "2.0",
|
|
51
|
+
id,
|
|
52
|
+
result
|
|
53
|
+
}), "ok");
|
|
54
|
+
var fail = /* @__PURE__ */ __name((id, code, message) => ({
|
|
55
|
+
jsonrpc: "2.0",
|
|
56
|
+
id,
|
|
57
|
+
error: {
|
|
58
|
+
code,
|
|
59
|
+
message
|
|
60
|
+
}
|
|
61
|
+
}), "fail");
|
|
62
|
+
var McpServer = class {
|
|
63
|
+
static {
|
|
64
|
+
__name(this, "McpServer");
|
|
65
|
+
}
|
|
66
|
+
options;
|
|
67
|
+
tools = /* @__PURE__ */ new Map();
|
|
68
|
+
loaders = [];
|
|
69
|
+
constructor(options) {
|
|
70
|
+
this.options = options;
|
|
71
|
+
}
|
|
72
|
+
/** Register a tool. Throws on duplicate names. */
|
|
73
|
+
addTool(tool) {
|
|
74
|
+
if (this.tools.has(tool.name)) {
|
|
75
|
+
throw new Error(`mcp-expose: duplicate tool name "${tool.name}". Set an explicit \`name\`.`);
|
|
76
|
+
}
|
|
77
|
+
this.tools.set(tool.name, tool);
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
/** Register a function that adds tools lazily, right before the first MCP request is served. */
|
|
81
|
+
onLoad(loader) {
|
|
82
|
+
this.loaders.push(loader);
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
async listTools() {
|
|
86
|
+
await this.load();
|
|
87
|
+
return [
|
|
88
|
+
...this.tools.values()
|
|
89
|
+
];
|
|
90
|
+
}
|
|
91
|
+
async load() {
|
|
92
|
+
if (!this.loaders.length) return;
|
|
93
|
+
const loaders = this.loaders;
|
|
94
|
+
this.loaders = [];
|
|
95
|
+
for (const l of loaders) await l();
|
|
96
|
+
}
|
|
97
|
+
/** Handle one JSON-RPC message (or a batch). Returns null when no response is due. */
|
|
98
|
+
async handleMessage(message, ctx) {
|
|
99
|
+
if (Array.isArray(message)) {
|
|
100
|
+
const results = (await Promise.all(message.map((m) => this.handleOne(m, ctx)))).filter((r) => r !== null);
|
|
101
|
+
return results.length ? results : null;
|
|
102
|
+
}
|
|
103
|
+
return this.handleOne(message, ctx);
|
|
104
|
+
}
|
|
105
|
+
async handleOne(message, ctx) {
|
|
106
|
+
const msg = message;
|
|
107
|
+
if (!msg || typeof msg !== "object" || msg.jsonrpc !== "2.0") {
|
|
108
|
+
return fail(null, INVALID_REQUEST, "Invalid JSON-RPC message");
|
|
109
|
+
}
|
|
110
|
+
if (typeof msg.method !== "string" || msg.id === void 0) return null;
|
|
111
|
+
const id = msg.id;
|
|
112
|
+
const params = msg.params ?? {};
|
|
113
|
+
await this.load();
|
|
114
|
+
switch (msg.method) {
|
|
115
|
+
case "initialize": {
|
|
116
|
+
const requested = params.protocolVersion;
|
|
117
|
+
const protocolVersion = requested && SUPPORTED_PROTOCOL_VERSIONS.includes(requested) ? requested : LATEST_PROTOCOL_VERSION;
|
|
118
|
+
return ok(id, {
|
|
119
|
+
protocolVersion,
|
|
120
|
+
capabilities: {
|
|
121
|
+
tools: {
|
|
122
|
+
listChanged: false
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
serverInfo: {
|
|
126
|
+
name: this.options.name,
|
|
127
|
+
version: this.options.version ?? "1.0.0"
|
|
128
|
+
},
|
|
129
|
+
...this.options.instructions ? {
|
|
130
|
+
instructions: this.options.instructions
|
|
131
|
+
} : {}
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
case "ping":
|
|
135
|
+
return ok(id, {});
|
|
136
|
+
case "tools/list":
|
|
137
|
+
return ok(id, {
|
|
138
|
+
tools: [
|
|
139
|
+
...this.tools.values()
|
|
140
|
+
].map((t) => ({
|
|
141
|
+
name: t.name,
|
|
142
|
+
...t.title ? {
|
|
143
|
+
title: t.title
|
|
144
|
+
} : {},
|
|
145
|
+
description: t.description,
|
|
146
|
+
inputSchema: t.inputSchema,
|
|
147
|
+
...t.annotations ? {
|
|
148
|
+
annotations: t.annotations
|
|
149
|
+
} : {}
|
|
150
|
+
}))
|
|
151
|
+
});
|
|
152
|
+
case "tools/call": {
|
|
153
|
+
const tool = this.tools.get(params.name);
|
|
154
|
+
if (!tool) return fail(id, INVALID_PARAMS, `Unknown tool: ${String(params.name)}`);
|
|
155
|
+
return ok(id, await this.callTool(tool, params.arguments ?? {}, ctx));
|
|
156
|
+
}
|
|
157
|
+
case "resources/list":
|
|
158
|
+
return ok(id, {
|
|
159
|
+
resources: []
|
|
160
|
+
});
|
|
161
|
+
case "prompts/list":
|
|
162
|
+
return ok(id, {
|
|
163
|
+
prompts: []
|
|
164
|
+
});
|
|
165
|
+
default:
|
|
166
|
+
return fail(id, METHOD_NOT_FOUND, `Method not found: ${msg.method}`);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
async callTool(tool, args, ctx) {
|
|
170
|
+
try {
|
|
171
|
+
if (tool.validate) {
|
|
172
|
+
const v = await tool.validate(args);
|
|
173
|
+
if (!v.ok) return {
|
|
174
|
+
isError: true,
|
|
175
|
+
content: [
|
|
176
|
+
{
|
|
177
|
+
type: "text",
|
|
178
|
+
text: `Invalid arguments: ${v.message}`
|
|
179
|
+
}
|
|
180
|
+
]
|
|
181
|
+
};
|
|
182
|
+
args = v.value;
|
|
183
|
+
}
|
|
184
|
+
return await tool.handler(args, ctx);
|
|
185
|
+
} catch (err) {
|
|
186
|
+
return {
|
|
187
|
+
isError: true,
|
|
188
|
+
content: [
|
|
189
|
+
{
|
|
190
|
+
type: "text",
|
|
191
|
+
text: `Tool error: ${err.message}`
|
|
192
|
+
}
|
|
193
|
+
]
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
/** Check the Origin header (DNS rebinding protection). */
|
|
198
|
+
isOriginAllowed(origin) {
|
|
199
|
+
if (!origin) return true;
|
|
200
|
+
const allowed = this.options.allowedOrigins;
|
|
201
|
+
if (allowed === "*") return true;
|
|
202
|
+
return (allowed ?? []).includes(origin);
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Framework-neutral Streamable HTTP handler. Adapters convert their request
|
|
206
|
+
* into McpHttpRequest, call this, and write the McpHttpResponse back.
|
|
207
|
+
*/
|
|
208
|
+
async handleHttp(req, ctx) {
|
|
209
|
+
const json = {
|
|
210
|
+
"content-type": "application/json"
|
|
211
|
+
};
|
|
212
|
+
if (!this.isOriginAllowed(req.headers.origin)) {
|
|
213
|
+
return {
|
|
214
|
+
status: 403,
|
|
215
|
+
headers: json,
|
|
216
|
+
body: JSON.stringify(fail(null, INVALID_REQUEST, "Origin not allowed"))
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
if (req.method.toUpperCase() !== "POST") {
|
|
220
|
+
return {
|
|
221
|
+
status: 405,
|
|
222
|
+
headers: {
|
|
223
|
+
allow: "POST"
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
let body = req.body;
|
|
228
|
+
if (typeof body === "string" || body instanceof Uint8Array) {
|
|
229
|
+
try {
|
|
230
|
+
body = JSON.parse(typeof body === "string" ? body : new TextDecoder().decode(body));
|
|
231
|
+
} catch {
|
|
232
|
+
return {
|
|
233
|
+
status: 400,
|
|
234
|
+
headers: json,
|
|
235
|
+
body: JSON.stringify(fail(null, PARSE_ERROR, "Parse error"))
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
const response = await this.handleMessage(body, ctx);
|
|
240
|
+
if (response === null) return {
|
|
241
|
+
status: 202,
|
|
242
|
+
headers: {}
|
|
243
|
+
};
|
|
244
|
+
return {
|
|
245
|
+
status: 200,
|
|
246
|
+
headers: json,
|
|
247
|
+
body: JSON.stringify(response)
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
// src/core/schema.ts
|
|
253
|
+
function isStandard(s) {
|
|
254
|
+
return typeof s === "object" && s !== null && "~standard" in s;
|
|
255
|
+
}
|
|
256
|
+
__name(isStandard, "isStandard");
|
|
257
|
+
function toJsonSchema(schema) {
|
|
258
|
+
if (schema === void 0 || schema === null) return void 0;
|
|
259
|
+
let out;
|
|
260
|
+
if (isStandard(schema) && typeof schema["~standard"].jsonSchema?.input === "function") {
|
|
261
|
+
out = schema["~standard"].jsonSchema.input({
|
|
262
|
+
target: "draft-2020-12"
|
|
263
|
+
});
|
|
264
|
+
} else if (typeof schema.toJSONSchema === "function") {
|
|
265
|
+
out = schema.toJSONSchema();
|
|
266
|
+
} else if (isStandard(schema)) {
|
|
267
|
+
throw new Error("mcp-expose: this schema library does not support JSON Schema export. Pass a plain JSON Schema instead.");
|
|
268
|
+
} else {
|
|
269
|
+
out = schema;
|
|
270
|
+
}
|
|
271
|
+
const { $schema: _ignored, ...rest } = out;
|
|
272
|
+
return rest;
|
|
273
|
+
}
|
|
274
|
+
__name(toJsonSchema, "toJsonSchema");
|
|
275
|
+
function toValidator(schema) {
|
|
276
|
+
if (!isStandard(schema) || typeof schema["~standard"].validate !== "function") return void 0;
|
|
277
|
+
const validate = schema["~standard"].validate;
|
|
278
|
+
return async (value) => {
|
|
279
|
+
const result = await validate(value);
|
|
280
|
+
if (!result.issues) return {
|
|
281
|
+
ok: true,
|
|
282
|
+
value: result.value
|
|
283
|
+
};
|
|
284
|
+
const message = result.issues.map((i) => {
|
|
285
|
+
const path = (i.path ?? []).map((p) => typeof p === "object" ? String(p.key) : String(p)).join(".");
|
|
286
|
+
return path ? `${path}: ${i.message}` : i.message;
|
|
287
|
+
}).join("; ");
|
|
288
|
+
return {
|
|
289
|
+
ok: false,
|
|
290
|
+
message
|
|
291
|
+
};
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
__name(toValidator, "toValidator");
|
|
295
|
+
function checkRequired(schema, args) {
|
|
296
|
+
const missing = (schema.required ?? []).filter((k) => args[k] === void 0);
|
|
297
|
+
return missing.length ? `Missing required argument(s): ${missing.join(", ")}` : void 0;
|
|
298
|
+
}
|
|
299
|
+
__name(checkRequired, "checkRequired");
|
|
300
|
+
|
|
301
|
+
// src/core/route-tool.ts
|
|
302
|
+
var DEFAULT_FORWARD_HEADERS = [
|
|
303
|
+
"authorization",
|
|
304
|
+
"cookie",
|
|
305
|
+
"x-api-key",
|
|
306
|
+
"accept-language"
|
|
307
|
+
];
|
|
308
|
+
var BODY_METHODS = /* @__PURE__ */ new Set([
|
|
309
|
+
"POST",
|
|
310
|
+
"PUT",
|
|
311
|
+
"PATCH"
|
|
312
|
+
]);
|
|
313
|
+
var PARAM_RE = /:([A-Za-z0-9_]+)\??|\{([A-Za-z0-9_]+)\}/g;
|
|
314
|
+
function pathParams(path) {
|
|
315
|
+
return [
|
|
316
|
+
...path.matchAll(PARAM_RE)
|
|
317
|
+
].map((m) => m[1] ?? m[2]);
|
|
318
|
+
}
|
|
319
|
+
__name(pathParams, "pathParams");
|
|
320
|
+
function encodeSegment(name, value) {
|
|
321
|
+
const raw = String(value);
|
|
322
|
+
if (raw === "" || raw === "." || raw === "..") {
|
|
323
|
+
throw new Error(`Invalid value for path parameter "${name}": ${JSON.stringify(raw)}`);
|
|
324
|
+
}
|
|
325
|
+
return encodeURIComponent(raw);
|
|
326
|
+
}
|
|
327
|
+
__name(encodeSegment, "encodeSegment");
|
|
328
|
+
function interpolatePath(path, values) {
|
|
329
|
+
return path.replace(/\/:([A-Za-z0-9_]+)\?/g, (_, k) => values[k] === void 0 ? "" : `/${encodeSegment(k, values[k])}`).replace(PARAM_RE, (_, a, b) => {
|
|
330
|
+
const k = a ?? b;
|
|
331
|
+
return encodeSegment(k, values[k]);
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
__name(interpolatePath, "interpolatePath");
|
|
335
|
+
function toolNameFromRoute(method, path) {
|
|
336
|
+
const segments = path.split("/").filter(Boolean).map((s) => {
|
|
337
|
+
const m = /^(?::([A-Za-z0-9_]+)\??|\{([A-Za-z0-9_]+)\})$/.exec(s);
|
|
338
|
+
return m ? `by_${m[1] ?? m[2]}` : s;
|
|
339
|
+
});
|
|
340
|
+
return sanitizeToolName([
|
|
341
|
+
method.toLowerCase(),
|
|
342
|
+
...segments
|
|
343
|
+
].join("_"));
|
|
344
|
+
}
|
|
345
|
+
__name(toolNameFromRoute, "toolNameFromRoute");
|
|
346
|
+
function sanitizeToolName(name) {
|
|
347
|
+
return name.replace(/[^A-Za-z0-9_-]+/g, "_").replace(/_+/g, "_").replace(/^_|_$/g, "").slice(0, 64);
|
|
348
|
+
}
|
|
349
|
+
__name(sanitizeToolName, "sanitizeToolName");
|
|
350
|
+
function defaultAnnotations(method) {
|
|
351
|
+
switch (method) {
|
|
352
|
+
case "GET":
|
|
353
|
+
case "HEAD":
|
|
354
|
+
return {
|
|
355
|
+
readOnlyHint: true,
|
|
356
|
+
openWorldHint: false
|
|
357
|
+
};
|
|
358
|
+
case "DELETE":
|
|
359
|
+
return {
|
|
360
|
+
readOnlyHint: false,
|
|
361
|
+
destructiveHint: true,
|
|
362
|
+
idempotentHint: true,
|
|
363
|
+
openWorldHint: false
|
|
364
|
+
};
|
|
365
|
+
case "PUT":
|
|
366
|
+
return {
|
|
367
|
+
readOnlyHint: false,
|
|
368
|
+
destructiveHint: false,
|
|
369
|
+
idempotentHint: true,
|
|
370
|
+
openWorldHint: false
|
|
371
|
+
};
|
|
372
|
+
default:
|
|
373
|
+
return {
|
|
374
|
+
readOnlyHint: false,
|
|
375
|
+
destructiveHint: false,
|
|
376
|
+
openWorldHint: false
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
__name(defaultAnnotations, "defaultAnnotations");
|
|
381
|
+
function buildInputSchema(route, opts) {
|
|
382
|
+
const method = route.method.toUpperCase();
|
|
383
|
+
const params = pathParams(route.path);
|
|
384
|
+
const mapping = {
|
|
385
|
+
params,
|
|
386
|
+
query: /* @__PURE__ */ new Set(),
|
|
387
|
+
body: /* @__PURE__ */ new Set(),
|
|
388
|
+
wholeBody: false,
|
|
389
|
+
bodyMethod: BODY_METHODS.has(method)
|
|
390
|
+
};
|
|
391
|
+
if (opts.input) {
|
|
392
|
+
const schema2 = toJsonSchema(opts.input);
|
|
393
|
+
return {
|
|
394
|
+
schema: {
|
|
395
|
+
type: "object",
|
|
396
|
+
...schema2
|
|
397
|
+
},
|
|
398
|
+
mapping
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
const properties = {};
|
|
402
|
+
const required = /* @__PURE__ */ new Set();
|
|
403
|
+
const paramSchema = toJsonSchema(opts.params);
|
|
404
|
+
for (const p of params) {
|
|
405
|
+
properties[p] = paramSchema?.properties?.[p] ?? {
|
|
406
|
+
type: "string",
|
|
407
|
+
description: `Path parameter "${p}"`
|
|
408
|
+
};
|
|
409
|
+
if (!route.path.includes(`:${p}?`)) required.add(p);
|
|
410
|
+
}
|
|
411
|
+
const querySchema = toJsonSchema(opts.query);
|
|
412
|
+
for (const [k, v] of Object.entries(querySchema?.properties ?? {})) {
|
|
413
|
+
properties[k] = v;
|
|
414
|
+
mapping.query.add(k);
|
|
415
|
+
}
|
|
416
|
+
for (const k of querySchema?.required ?? []) required.add(k);
|
|
417
|
+
const bodySchema = toJsonSchema(opts.body);
|
|
418
|
+
if (bodySchema) {
|
|
419
|
+
if (bodySchema.properties && (bodySchema.type === void 0 || bodySchema.type === "object")) {
|
|
420
|
+
for (const [k, v] of Object.entries(bodySchema.properties)) {
|
|
421
|
+
properties[k] = v;
|
|
422
|
+
mapping.body.add(k);
|
|
423
|
+
}
|
|
424
|
+
for (const k of bodySchema.required ?? []) required.add(k);
|
|
425
|
+
} else {
|
|
426
|
+
properties.body = bodySchema;
|
|
427
|
+
required.add("body");
|
|
428
|
+
mapping.wholeBody = true;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
const schema = {
|
|
432
|
+
type: "object",
|
|
433
|
+
properties
|
|
434
|
+
};
|
|
435
|
+
if (required.size) schema.required = [
|
|
436
|
+
...required
|
|
437
|
+
];
|
|
438
|
+
return {
|
|
439
|
+
schema,
|
|
440
|
+
mapping
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
__name(buildInputSchema, "buildInputSchema");
|
|
444
|
+
function splitArgs(args, mapping) {
|
|
445
|
+
const pathValues = {};
|
|
446
|
+
const query = {};
|
|
447
|
+
let body;
|
|
448
|
+
for (const [k, v] of Object.entries(args)) {
|
|
449
|
+
if (v === void 0) continue;
|
|
450
|
+
if (mapping.params.includes(k)) {
|
|
451
|
+
pathValues[k] = v;
|
|
452
|
+
} else if (mapping.wholeBody && k === "body") {
|
|
453
|
+
body = v;
|
|
454
|
+
} else if (mapping.query.has(k) || !mapping.body.has(k) && !mapping.bodyMethod) {
|
|
455
|
+
query[k] = Array.isArray(v) ? v.map(String) : typeof v === "object" && v !== null ? JSON.stringify(v) : String(v);
|
|
456
|
+
} else {
|
|
457
|
+
body = {
|
|
458
|
+
...body ?? {},
|
|
459
|
+
[k]: v
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
if (body === void 0 && mapping.bodyMethod) body = {};
|
|
464
|
+
return {
|
|
465
|
+
pathValues,
|
|
466
|
+
query,
|
|
467
|
+
body
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
__name(splitArgs, "splitArgs");
|
|
471
|
+
function responseToResult(res, maxChars) {
|
|
472
|
+
let text = res.body;
|
|
473
|
+
if (text.length > maxChars) text = `${text.slice(0, maxChars)}
|
|
474
|
+
\u2026[truncated ${text.length - maxChars} chars]`;
|
|
475
|
+
const isError = res.status >= 400;
|
|
476
|
+
const result = {
|
|
477
|
+
content: [
|
|
478
|
+
{
|
|
479
|
+
type: "text",
|
|
480
|
+
text: isError ? `HTTP ${res.status}
|
|
481
|
+
${text}` : text || `HTTP ${res.status}`
|
|
482
|
+
}
|
|
483
|
+
]
|
|
484
|
+
};
|
|
485
|
+
if (isError) {
|
|
486
|
+
result.isError = true;
|
|
487
|
+
return result;
|
|
488
|
+
}
|
|
489
|
+
if ((res.headers["content-type"] ?? "").includes("json") && text === res.body) {
|
|
490
|
+
try {
|
|
491
|
+
const parsed = JSON.parse(res.body);
|
|
492
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) result.structuredContent = parsed;
|
|
493
|
+
} catch {
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
return result;
|
|
497
|
+
}
|
|
498
|
+
__name(responseToResult, "responseToResult");
|
|
499
|
+
function pickForwardHeaders(ctx, names) {
|
|
500
|
+
const out = {};
|
|
501
|
+
for (const name of names) {
|
|
502
|
+
const v = ctx.headers[name.toLowerCase()];
|
|
503
|
+
if (v !== void 0) out[name.toLowerCase()] = v;
|
|
504
|
+
}
|
|
505
|
+
return out;
|
|
506
|
+
}
|
|
507
|
+
__name(pickForwardHeaders, "pickForwardHeaders");
|
|
508
|
+
function createRouteTool(route, opts, dispatch, serverOpts = {}) {
|
|
509
|
+
const method = route.method.toUpperCase();
|
|
510
|
+
const name = sanitizeToolName(opts.name ?? toolNameFromRoute(method, route.path));
|
|
511
|
+
const { schema, mapping } = buildInputSchema(route, opts);
|
|
512
|
+
const forward = serverOpts.forwardHeaders ?? DEFAULT_FORWARD_HEADERS;
|
|
513
|
+
const maxChars = serverOpts.maxResponseChars ?? 1e5;
|
|
514
|
+
return {
|
|
515
|
+
name,
|
|
516
|
+
title: opts.title,
|
|
517
|
+
description: opts.description ?? `${method} ${route.path}`,
|
|
518
|
+
inputSchema: schema,
|
|
519
|
+
annotations: {
|
|
520
|
+
...defaultAnnotations(method),
|
|
521
|
+
...opts.annotations
|
|
522
|
+
},
|
|
523
|
+
validate: toValidator(opts.input),
|
|
524
|
+
async handler(args, ctx) {
|
|
525
|
+
const missing = opts.input ? void 0 : checkRequired(schema, args);
|
|
526
|
+
if (missing) return {
|
|
527
|
+
isError: true,
|
|
528
|
+
content: [
|
|
529
|
+
{
|
|
530
|
+
type: "text",
|
|
531
|
+
text: missing
|
|
532
|
+
}
|
|
533
|
+
]
|
|
534
|
+
};
|
|
535
|
+
const { pathValues, query, body } = splitArgs(args, mapping);
|
|
536
|
+
const headers = {
|
|
537
|
+
accept: "application/json",
|
|
538
|
+
...pickForwardHeaders(ctx, forward),
|
|
539
|
+
...opts.headers,
|
|
540
|
+
"x-mcp-tool": name
|
|
541
|
+
};
|
|
542
|
+
if (ctx.clientIp) headers["x-forwarded-for"] = ctx.clientIp;
|
|
543
|
+
if (body !== void 0) headers["content-type"] = "application/json";
|
|
544
|
+
let path;
|
|
545
|
+
try {
|
|
546
|
+
path = interpolatePath(route.path, pathValues);
|
|
547
|
+
} catch (err) {
|
|
548
|
+
return {
|
|
549
|
+
isError: true,
|
|
550
|
+
content: [
|
|
551
|
+
{
|
|
552
|
+
type: "text",
|
|
553
|
+
text: err.message
|
|
554
|
+
}
|
|
555
|
+
]
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
const res = await dispatch({
|
|
559
|
+
method,
|
|
560
|
+
path,
|
|
561
|
+
query,
|
|
562
|
+
body,
|
|
563
|
+
headers
|
|
564
|
+
}, ctx);
|
|
565
|
+
return responseToResult(res, maxChars);
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
}
|
|
569
|
+
__name(createRouteTool, "createRouteTool");
|
|
570
|
+
|
|
571
|
+
// src/core/dispatch.ts
|
|
572
|
+
function buildUrl(base, req) {
|
|
573
|
+
const url = new URL(base.replace(/\/+$/, "") + req.path);
|
|
574
|
+
for (const [k, v] of Object.entries(req.query)) {
|
|
575
|
+
for (const item of Array.isArray(v) ? v : [
|
|
576
|
+
v
|
|
577
|
+
]) url.searchParams.append(k, item);
|
|
578
|
+
}
|
|
579
|
+
return url.toString();
|
|
580
|
+
}
|
|
581
|
+
__name(buildUrl, "buildUrl");
|
|
582
|
+
function createFetchDispatcher(opts) {
|
|
583
|
+
const doFetch = opts.fetch ?? fetch;
|
|
584
|
+
return async (req, ctx) => {
|
|
585
|
+
const base = typeof opts.baseUrl === "function" ? opts.baseUrl(ctx) : opts.baseUrl;
|
|
586
|
+
const res = await doFetch(buildUrl(base, req), {
|
|
587
|
+
method: req.method,
|
|
588
|
+
headers: req.headers,
|
|
589
|
+
body: req.body === void 0 || req.method === "GET" || req.method === "HEAD" ? void 0 : JSON.stringify(req.body),
|
|
590
|
+
signal: AbortSignal.timeout(opts.timeoutMs ?? 3e4)
|
|
591
|
+
});
|
|
592
|
+
const headers = {};
|
|
593
|
+
res.headers.forEach((v, k) => headers[k] = v);
|
|
594
|
+
return {
|
|
595
|
+
status: res.status,
|
|
596
|
+
headers,
|
|
597
|
+
body: await res.text()
|
|
598
|
+
};
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
__name(createFetchDispatcher, "createFetchDispatcher");
|
|
602
|
+
|
|
603
|
+
// src/core/define.ts
|
|
604
|
+
function defineTool(opts) {
|
|
605
|
+
return {
|
|
606
|
+
name: opts.name,
|
|
607
|
+
title: opts.title,
|
|
608
|
+
description: opts.description,
|
|
609
|
+
inputSchema: {
|
|
610
|
+
type: "object",
|
|
611
|
+
...toJsonSchema(opts.input) ?? {
|
|
612
|
+
properties: {}
|
|
613
|
+
}
|
|
614
|
+
},
|
|
615
|
+
annotations: opts.annotations,
|
|
616
|
+
validate: toValidator(opts.input),
|
|
617
|
+
async handler(args, ctx) {
|
|
618
|
+
return toToolResult(await opts.handler(args, ctx));
|
|
619
|
+
}
|
|
620
|
+
};
|
|
621
|
+
}
|
|
622
|
+
__name(defineTool, "defineTool");
|
|
623
|
+
function toToolResult(value) {
|
|
624
|
+
if (value && typeof value === "object" && Array.isArray(value.content)) return value;
|
|
625
|
+
if (typeof value === "string") return {
|
|
626
|
+
content: [
|
|
627
|
+
{
|
|
628
|
+
type: "text",
|
|
629
|
+
text: value
|
|
630
|
+
}
|
|
631
|
+
]
|
|
632
|
+
};
|
|
633
|
+
const text = JSON.stringify(value ?? null);
|
|
634
|
+
const result = {
|
|
635
|
+
content: [
|
|
636
|
+
{
|
|
637
|
+
type: "text",
|
|
638
|
+
text
|
|
639
|
+
}
|
|
640
|
+
]
|
|
641
|
+
};
|
|
642
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
643
|
+
result.structuredContent = value;
|
|
644
|
+
}
|
|
645
|
+
return result;
|
|
646
|
+
}
|
|
647
|
+
__name(toToolResult, "toToolResult");
|
|
648
|
+
function toolsFromOpenApi(doc, dispatch, opts = {}) {
|
|
649
|
+
const include = opts.include ?? (({ operation }) => !!operation["x-mcp"]);
|
|
650
|
+
const methods = [
|
|
651
|
+
"get",
|
|
652
|
+
"post",
|
|
653
|
+
"put",
|
|
654
|
+
"patch",
|
|
655
|
+
"delete"
|
|
656
|
+
];
|
|
657
|
+
const tools = [];
|
|
658
|
+
for (const [path, item] of Object.entries(doc.paths ?? {})) {
|
|
659
|
+
for (const method of methods) {
|
|
660
|
+
const operation = item[method];
|
|
661
|
+
if (!operation || !include({
|
|
662
|
+
method,
|
|
663
|
+
path,
|
|
664
|
+
operation
|
|
665
|
+
})) continue;
|
|
666
|
+
const extra = typeof operation["x-mcp"] === "object" ? operation["x-mcp"] : {};
|
|
667
|
+
const query = {
|
|
668
|
+
type: "object",
|
|
669
|
+
properties: {},
|
|
670
|
+
required: []
|
|
671
|
+
};
|
|
672
|
+
const params = {
|
|
673
|
+
type: "object",
|
|
674
|
+
properties: {}
|
|
675
|
+
};
|
|
676
|
+
for (const p of operation.parameters ?? []) {
|
|
677
|
+
const s = {
|
|
678
|
+
...p.schema ?? {
|
|
679
|
+
type: "string"
|
|
680
|
+
},
|
|
681
|
+
...p.description ? {
|
|
682
|
+
description: p.description
|
|
683
|
+
} : {}
|
|
684
|
+
};
|
|
685
|
+
if (p.in === "query") {
|
|
686
|
+
query.properties[p.name] = s;
|
|
687
|
+
if (p.required) query.required.push(p.name);
|
|
688
|
+
} else if (p.in === "path") params.properties[p.name] = s;
|
|
689
|
+
}
|
|
690
|
+
const body = operation.requestBody?.content?.["application/json"]?.schema;
|
|
691
|
+
tools.push(createRouteTool({
|
|
692
|
+
method,
|
|
693
|
+
path
|
|
694
|
+
}, {
|
|
695
|
+
name: operation.operationId,
|
|
696
|
+
description: operation.description ?? operation.summary,
|
|
697
|
+
params,
|
|
698
|
+
query,
|
|
699
|
+
body,
|
|
700
|
+
...extra
|
|
701
|
+
}, dispatch, opts));
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
return tools;
|
|
705
|
+
}
|
|
706
|
+
__name(toolsFromOpenApi, "toolsFromOpenApi");
|
|
707
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
708
|
+
0 && (module.exports = {
|
|
709
|
+
DEFAULT_FORWARD_HEADERS,
|
|
710
|
+
LATEST_PROTOCOL_VERSION,
|
|
711
|
+
McpServer,
|
|
712
|
+
SUPPORTED_PROTOCOL_VERSIONS,
|
|
713
|
+
createFetchDispatcher,
|
|
714
|
+
createRouteTool,
|
|
715
|
+
defineTool,
|
|
716
|
+
toJsonSchema,
|
|
717
|
+
toToolResult,
|
|
718
|
+
toolsFromOpenApi
|
|
719
|
+
});
|
|
720
|
+
//# sourceMappingURL=index.cjs.map
|