mcp-expose 1.0.0 → 1.1.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 +19 -1
- package/README.md +102 -13
- package/dist/adonisjs/index.cjs +238 -0
- package/dist/adonisjs/index.cjs.map +1 -1
- package/dist/adonisjs/index.d.cts +2 -1
- package/dist/adonisjs/index.d.ts +2 -1
- package/dist/adonisjs/index.js +238 -0
- package/dist/adonisjs/index.js.map +1 -1
- package/dist/express/index.cjs +236 -1
- package/dist/express/index.cjs.map +1 -1
- package/dist/express/index.d.cts +4 -2
- package/dist/express/index.d.ts +4 -2
- package/dist/express/index.js +236 -1
- package/dist/express/index.js.map +1 -1
- package/dist/fastify/index.cjs +245 -0
- package/dist/fastify/index.cjs.map +1 -1
- package/dist/fastify/index.d.cts +2 -1
- package/dist/fastify/index.d.ts +2 -1
- package/dist/fastify/index.js +245 -0
- package/dist/fastify/index.js.map +1 -1
- package/dist/hono/index.cjs +239 -0
- package/dist/hono/index.cjs.map +1 -1
- package/dist/hono/index.d.cts +2 -1
- package/dist/hono/index.d.ts +2 -1
- package/dist/hono/index.js +239 -0
- package/dist/hono/index.js.map +1 -1
- package/dist/index.cjs +243 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -2
- package/dist/index.d.ts +5 -2
- package/dist/index.js +239 -0
- package/dist/index.js.map +1 -1
- package/dist/koa/index.cjs +238 -0
- package/dist/koa/index.cjs.map +1 -1
- package/dist/koa/index.d.cts +2 -1
- package/dist/koa/index.d.ts +2 -1
- package/dist/koa/index.js +238 -0
- package/dist/koa/index.js.map +1 -1
- package/dist/nestjs/index.cjs +253 -1
- package/dist/nestjs/index.cjs.map +1 -1
- package/dist/nestjs/index.d.cts +21 -7
- package/dist/nestjs/index.d.ts +21 -7
- package/dist/nestjs/index.js +254 -2
- package/dist/nestjs/index.js.map +1 -1
- package/dist/oauth/index.cjs +214 -0
- package/dist/oauth/index.cjs.map +1 -0
- package/dist/oauth/index.d.cts +69 -0
- package/dist/oauth/index.d.ts +69 -0
- package/dist/oauth/index.js +177 -0
- package/dist/oauth/index.js.map +1 -0
- package/dist/oauth-8RmQamGP.d.cts +75 -0
- package/dist/oauth-8RmQamGP.d.ts +75 -0
- package/dist/{server-fzkb-wJo.d.ts → server-BFQ_Hdh9.d.cts} +27 -0
- package/dist/{server-fzkb-wJo.d.cts → server-BRw1E2xy.d.ts} +27 -0
- package/package.json +21 -2
package/dist/express/index.js
CHANGED
|
@@ -369,6 +369,7 @@ function createRouteTool(route, opts, dispatch, serverOpts = {}) {
|
|
|
369
369
|
...defaultAnnotations(method),
|
|
370
370
|
...opts.annotations
|
|
371
371
|
},
|
|
372
|
+
scopes: opts.scopes,
|
|
372
373
|
validate: toValidator(opts.input),
|
|
373
374
|
async handler(args, ctx) {
|
|
374
375
|
const missing = opts.input ? void 0 : checkRequired(schema, args);
|
|
@@ -417,6 +418,115 @@ function createRouteTool(route, opts, dispatch, serverOpts = {}) {
|
|
|
417
418
|
}
|
|
418
419
|
__name(createRouteTool, "createRouteTool");
|
|
419
420
|
|
|
421
|
+
// src/core/oauth.ts
|
|
422
|
+
var PROTECTED_RESOURCE_WELL_KNOWN = "/.well-known/oauth-protected-resource";
|
|
423
|
+
function protectedResourceMetadataPaths(resource) {
|
|
424
|
+
const pathname = new URL(resource).pathname.replace(/\/+$/, "");
|
|
425
|
+
return pathname ? [
|
|
426
|
+
PROTECTED_RESOURCE_WELL_KNOWN + pathname,
|
|
427
|
+
PROTECTED_RESOURCE_WELL_KNOWN
|
|
428
|
+
] : [
|
|
429
|
+
PROTECTED_RESOURCE_WELL_KNOWN
|
|
430
|
+
];
|
|
431
|
+
}
|
|
432
|
+
__name(protectedResourceMetadataPaths, "protectedResourceMetadataPaths");
|
|
433
|
+
function protectedResourceMetadataUrl(resource) {
|
|
434
|
+
return new URL(protectedResourceMetadataPaths(resource)[0], resource).href;
|
|
435
|
+
}
|
|
436
|
+
__name(protectedResourceMetadataUrl, "protectedResourceMetadataUrl");
|
|
437
|
+
function validateOAuthOptions(o) {
|
|
438
|
+
let url;
|
|
439
|
+
try {
|
|
440
|
+
url = new URL(o.resource);
|
|
441
|
+
} catch {
|
|
442
|
+
throw new Error(`mcp-expose: oauth.resource must be an absolute URL, got ${JSON.stringify(o.resource)}`);
|
|
443
|
+
}
|
|
444
|
+
if (url.hash) throw new Error("mcp-expose: oauth.resource must not contain a fragment");
|
|
445
|
+
if (!o.authorizationServers?.length) throw new Error("mcp-expose: oauth.authorizationServers must not be empty");
|
|
446
|
+
if (typeof o.verifyToken !== "function") throw new Error("mcp-expose: oauth.verifyToken is required");
|
|
447
|
+
}
|
|
448
|
+
__name(validateOAuthOptions, "validateOAuthOptions");
|
|
449
|
+
var quote = /* @__PURE__ */ __name((v) => `"${v.replace(/["\\]/g, "\\$&")}"`, "quote");
|
|
450
|
+
function bearerChallenge(resource, params = {}) {
|
|
451
|
+
const parts = [];
|
|
452
|
+
if (params.error) parts.push(`error=${quote(params.error)}`);
|
|
453
|
+
if (params.errorDescription) parts.push(`error_description=${quote(params.errorDescription)}`);
|
|
454
|
+
if (params.scope?.length) parts.push(`scope=${quote(params.scope.join(" "))}`);
|
|
455
|
+
parts.push(`resource_metadata=${quote(protectedResourceMetadataUrl(resource))}`);
|
|
456
|
+
return `Bearer ${parts.join(", ")}`;
|
|
457
|
+
}
|
|
458
|
+
__name(bearerChallenge, "bearerChallenge");
|
|
459
|
+
function bearerToken(headers) {
|
|
460
|
+
const m = /^Bearer[ ]+([^\s,]+)\s*$/i.exec(headers.authorization ?? "");
|
|
461
|
+
return m?.[1];
|
|
462
|
+
}
|
|
463
|
+
__name(bearerToken, "bearerToken");
|
|
464
|
+
async function authenticate(o, headers) {
|
|
465
|
+
const token = bearerToken(headers);
|
|
466
|
+
if (!token) {
|
|
467
|
+
return {
|
|
468
|
+
ok: false,
|
|
469
|
+
status: 401,
|
|
470
|
+
error: "unauthorized",
|
|
471
|
+
description: "Authorization required",
|
|
472
|
+
challenge: bearerChallenge(o.resource, {
|
|
473
|
+
scope: o.requiredScopes
|
|
474
|
+
})
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
let auth;
|
|
478
|
+
try {
|
|
479
|
+
auth = await o.verifyToken(token, {
|
|
480
|
+
resource: o.resource,
|
|
481
|
+
headers
|
|
482
|
+
});
|
|
483
|
+
} catch {
|
|
484
|
+
auth = void 0;
|
|
485
|
+
}
|
|
486
|
+
if (!auth) return deny(o, 401, "invalid_token", "The access token is invalid");
|
|
487
|
+
if (auth.expiresAt !== void 0 && auth.expiresAt * 1e3 <= Date.now()) {
|
|
488
|
+
return deny(o, 401, "invalid_token", "The access token has expired");
|
|
489
|
+
}
|
|
490
|
+
const missing = missingScopes(auth, o.requiredScopes);
|
|
491
|
+
if (missing.length) {
|
|
492
|
+
const scope = [
|
|
493
|
+
.../* @__PURE__ */ new Set([
|
|
494
|
+
...auth.scopes ?? [],
|
|
495
|
+
...o.requiredScopes ?? []
|
|
496
|
+
])
|
|
497
|
+
];
|
|
498
|
+
return deny(o, 403, "insufficient_scope", `Missing scope: ${missing.join(" ")}`, scope);
|
|
499
|
+
}
|
|
500
|
+
return {
|
|
501
|
+
ok: true,
|
|
502
|
+
auth: {
|
|
503
|
+
...auth,
|
|
504
|
+
scopes: auth.scopes ?? []
|
|
505
|
+
}
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
__name(authenticate, "authenticate");
|
|
509
|
+
function deny(o, status, error, description, scope) {
|
|
510
|
+
return {
|
|
511
|
+
ok: false,
|
|
512
|
+
status,
|
|
513
|
+
error,
|
|
514
|
+
description,
|
|
515
|
+
challenge: bearerChallenge(o.resource, {
|
|
516
|
+
error,
|
|
517
|
+
errorDescription: description,
|
|
518
|
+
scope
|
|
519
|
+
})
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
__name(deny, "deny");
|
|
523
|
+
function missingScopes(auth, required) {
|
|
524
|
+
if (!required?.length) return [];
|
|
525
|
+
const granted = new Set(auth?.scopes ?? []);
|
|
526
|
+
return required.filter((s) => !granted.has(s));
|
|
527
|
+
}
|
|
528
|
+
__name(missingScopes, "missingScopes");
|
|
529
|
+
|
|
420
530
|
// src/core/server.ts
|
|
421
531
|
var SUPPORTED_PROTOCOL_VERSIONS = [
|
|
422
532
|
"2025-11-25",
|
|
@@ -451,6 +561,7 @@ var McpServer = class {
|
|
|
451
561
|
loaders = [];
|
|
452
562
|
constructor(options) {
|
|
453
563
|
this.options = options;
|
|
564
|
+
if (options.oauth) validateOAuthOptions(options.oauth);
|
|
454
565
|
}
|
|
455
566
|
/** Register a tool. Throws on duplicate names. */
|
|
456
567
|
addTool(tool) {
|
|
@@ -585,6 +696,79 @@ var McpServer = class {
|
|
|
585
696
|
return (allowed ?? []).includes(origin);
|
|
586
697
|
}
|
|
587
698
|
/**
|
|
699
|
+
* Paths on which adapters serve the OAuth Protected Resource Metadata
|
|
700
|
+
* (empty when `oauth` is not configured). They must be public: no auth middleware.
|
|
701
|
+
*/
|
|
702
|
+
get oauthMetadataPaths() {
|
|
703
|
+
return this.options.oauth ? protectedResourceMetadataPaths(this.options.oauth.resource) : [];
|
|
704
|
+
}
|
|
705
|
+
/** The OAuth Protected Resource Metadata document (RFC 9728). */
|
|
706
|
+
async protectedResourceMetadata() {
|
|
707
|
+
const o = this.options.oauth;
|
|
708
|
+
if (!o) throw new Error("mcp-expose: the oauth option is not configured");
|
|
709
|
+
await this.load();
|
|
710
|
+
const scopes = o.scopesSupported ?? [
|
|
711
|
+
.../* @__PURE__ */ new Set([
|
|
712
|
+
...o.requiredScopes ?? [],
|
|
713
|
+
...[
|
|
714
|
+
...this.tools.values()
|
|
715
|
+
].flatMap((t) => t.scopes ?? [])
|
|
716
|
+
])
|
|
717
|
+
];
|
|
718
|
+
return {
|
|
719
|
+
resource: o.resource,
|
|
720
|
+
authorization_servers: o.authorizationServers,
|
|
721
|
+
...scopes.length ? {
|
|
722
|
+
scopes_supported: scopes
|
|
723
|
+
} : {},
|
|
724
|
+
bearer_methods_supported: [
|
|
725
|
+
"header"
|
|
726
|
+
],
|
|
727
|
+
...o.resourceName ? {
|
|
728
|
+
resource_name: o.resourceName
|
|
729
|
+
} : {},
|
|
730
|
+
...o.resourceDocumentation ? {
|
|
731
|
+
resource_documentation: o.resourceDocumentation
|
|
732
|
+
} : {},
|
|
733
|
+
...o.metadata
|
|
734
|
+
};
|
|
735
|
+
}
|
|
736
|
+
/** HTTP handler for the metadata paths. Public and CORS-enabled so browser-based clients can read it. */
|
|
737
|
+
async handleMetadataHttp(req) {
|
|
738
|
+
const cors = {
|
|
739
|
+
"access-control-allow-origin": "*",
|
|
740
|
+
"access-control-allow-methods": "GET, OPTIONS"
|
|
741
|
+
};
|
|
742
|
+
const method = req.method.toUpperCase();
|
|
743
|
+
if (!this.options.oauth) return {
|
|
744
|
+
status: 404,
|
|
745
|
+
headers: {}
|
|
746
|
+
};
|
|
747
|
+
if (method === "OPTIONS") return {
|
|
748
|
+
status: 204,
|
|
749
|
+
headers: {
|
|
750
|
+
...cors,
|
|
751
|
+
"access-control-allow-headers": "*"
|
|
752
|
+
}
|
|
753
|
+
};
|
|
754
|
+
if (method !== "GET" && method !== "HEAD") return {
|
|
755
|
+
status: 405,
|
|
756
|
+
headers: {
|
|
757
|
+
allow: "GET, OPTIONS"
|
|
758
|
+
}
|
|
759
|
+
};
|
|
760
|
+
const body = JSON.stringify(await this.protectedResourceMetadata());
|
|
761
|
+
return {
|
|
762
|
+
status: 200,
|
|
763
|
+
headers: {
|
|
764
|
+
...cors,
|
|
765
|
+
"content-type": "application/json",
|
|
766
|
+
"cache-control": "public, max-age=3600"
|
|
767
|
+
},
|
|
768
|
+
body: method === "HEAD" ? void 0 : body
|
|
769
|
+
};
|
|
770
|
+
}
|
|
771
|
+
/**
|
|
588
772
|
* Framework-neutral Streamable HTTP handler. Adapters convert their request
|
|
589
773
|
* into McpHttpRequest, call this, and write the McpHttpResponse back.
|
|
590
774
|
*/
|
|
@@ -599,6 +783,15 @@ var McpServer = class {
|
|
|
599
783
|
body: JSON.stringify(fail(null, INVALID_REQUEST, "Origin not allowed"))
|
|
600
784
|
};
|
|
601
785
|
}
|
|
786
|
+
const oauth = this.options.oauth;
|
|
787
|
+
if (oauth) {
|
|
788
|
+
const outcome = await authenticate(oauth, req.headers);
|
|
789
|
+
if (!outcome.ok) return authError(outcome);
|
|
790
|
+
ctx = {
|
|
791
|
+
...ctx,
|
|
792
|
+
auth: outcome.auth
|
|
793
|
+
};
|
|
794
|
+
}
|
|
602
795
|
if (req.method.toUpperCase() !== "POST") {
|
|
603
796
|
return {
|
|
604
797
|
status: 405,
|
|
@@ -619,6 +812,27 @@ var McpServer = class {
|
|
|
619
812
|
};
|
|
620
813
|
}
|
|
621
814
|
}
|
|
815
|
+
if (oauth) {
|
|
816
|
+
await this.load();
|
|
817
|
+
const calls = Array.isArray(body) ? body : [
|
|
818
|
+
body
|
|
819
|
+
];
|
|
820
|
+
for (const m of calls) {
|
|
821
|
+
if (m?.method !== "tools/call") continue;
|
|
822
|
+
const tool = this.tools.get(m.params?.name);
|
|
823
|
+
const missing = missingScopes(ctx.auth, tool?.scopes);
|
|
824
|
+
if (missing.length) {
|
|
825
|
+
const scope = [
|
|
826
|
+
.../* @__PURE__ */ new Set([
|
|
827
|
+
...ctx.auth?.scopes ?? [],
|
|
828
|
+
...oauth.requiredScopes ?? [],
|
|
829
|
+
...tool?.scopes ?? []
|
|
830
|
+
])
|
|
831
|
+
];
|
|
832
|
+
return authError(deny(oauth, 403, "insufficient_scope", `Tool "${tool.name}" needs scope: ${missing.join(" ")}`, scope));
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
}
|
|
622
836
|
const response = await this.handleMessage(body, ctx);
|
|
623
837
|
if (response === null) return {
|
|
624
838
|
status: 202,
|
|
@@ -631,6 +845,20 @@ var McpServer = class {
|
|
|
631
845
|
};
|
|
632
846
|
}
|
|
633
847
|
};
|
|
848
|
+
function authError(o) {
|
|
849
|
+
return {
|
|
850
|
+
status: o.status,
|
|
851
|
+
headers: {
|
|
852
|
+
"content-type": "application/json",
|
|
853
|
+
"www-authenticate": o.challenge
|
|
854
|
+
},
|
|
855
|
+
body: JSON.stringify({
|
|
856
|
+
error: o.error,
|
|
857
|
+
error_description: o.description
|
|
858
|
+
})
|
|
859
|
+
};
|
|
860
|
+
}
|
|
861
|
+
__name(authError, "authError");
|
|
634
862
|
|
|
635
863
|
// src/express/discover.ts
|
|
636
864
|
function collectExpress3(routes, out) {
|
|
@@ -752,7 +980,14 @@ function mountMcp(app, options) {
|
|
|
752
980
|
writeNodeResponse(res, out);
|
|
753
981
|
})().catch(next);
|
|
754
982
|
}, "handler");
|
|
755
|
-
|
|
983
|
+
const all = app.all;
|
|
984
|
+
all.call(app, path, ...options.middleware ?? [], handler);
|
|
985
|
+
const metadata = /* @__PURE__ */ __name((req, res, next) => {
|
|
986
|
+
server.handleMetadataHttp({
|
|
987
|
+
method: req.method ?? "GET"
|
|
988
|
+
}).then((out) => writeNodeResponse(res, out)).catch(next);
|
|
989
|
+
}, "metadata");
|
|
990
|
+
for (const p of server.oauthMetadataPaths) all.call(app, p, metadata);
|
|
756
991
|
return server;
|
|
757
992
|
}
|
|
758
993
|
__name(mountMcp, "mountMcp");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/core/dispatch.ts","../../src/core/marker.ts","../../src/core/node.ts","../../src/core/schema.ts","../../src/core/route-tool.ts","../../src/core/server.ts","../../src/express/discover.ts","../../src/express/index.ts"],"sourcesContent":["import type { Dispatcher, RouteRequest, ToolContext } from './types.js';\n\nexport function buildUrl(base: string, req: Pick<RouteRequest, 'path' | 'query'>): string {\n const url = new URL(base.replace(/\\/+$/, '') + req.path);\n for (const [k, v] of Object.entries(req.query)) {\n for (const item of Array.isArray(v) ? v : [v]) url.searchParams.append(k, item);\n }\n return url.toString();\n}\n\nexport interface FetchDispatcherOptions {\n /**\n * Base URL of the API, e.g. `http://127.0.0.1:3000` or `https://api.example.com/v1`.\n * A function lets adapters resolve it per call (e.g. from the local port).\n */\n baseUrl: string | ((ctx: ToolContext) => string);\n /** Custom fetch implementation. Defaults to the global `fetch`. */\n fetch?: typeof fetch;\n /** Per-request timeout in ms. @default 30_000 */\n timeoutMs?: number;\n}\n\n/**\n * Dispatches tool calls as real HTTP requests. Used for loopback calls into the\n * host app (Express, Koa, NestJS) and for standalone gateways in front of any API.\n */\nexport function createFetchDispatcher(opts: FetchDispatcherOptions): Dispatcher {\n const doFetch = opts.fetch ?? fetch;\n return async (req, ctx) => {\n const base = typeof opts.baseUrl === 'function' ? opts.baseUrl(ctx) : opts.baseUrl;\n const res = await doFetch(buildUrl(base, req), {\n method: req.method,\n headers: req.headers,\n body:\n req.body === undefined || req.method === 'GET' || req.method === 'HEAD' ? undefined : JSON.stringify(req.body),\n signal: AbortSignal.timeout(opts.timeoutMs ?? 30_000),\n });\n const headers: Record<string, string> = {};\n res.headers.forEach((v, k) => (headers[k] = v));\n return { status: res.status, headers, body: await res.text() };\n };\n}\n","import type { RouteToolOptions } from './types.js';\n\n/** Symbol under which marker middleware carries its tool options. */\nexport const MCP_TOOL = Symbol.for('mcp-expose.tool');\n\nexport type Marked<T> = T & { [MCP_TOOL]: RouteToolOptions };\n\nexport function markerOf(fn: unknown): RouteToolOptions | undefined {\n return typeof fn === 'function' ? (fn as Partial<Marked<object>>)[MCP_TOOL] : undefined;\n}\n\nexport function mark<T extends object>(fn: T, opts: RouteToolOptions): Marked<T> {\n Object.defineProperty(fn, MCP_TOOL, { value: opts, enumerable: false });\n return fn as Marked<T>;\n}\n","import type { IncomingMessage, ServerResponse } from 'node:http';\nimport type { McpHttpResponse } from './server.js';\n\n/** Flatten Node's IncomingHttpHeaders into lower-cased single strings. */\nexport function normalizeHeaders(headers: Record<string, string | string[] | undefined>): Record<string, string> {\n const out: Record<string, string> = {};\n for (const [k, v] of Object.entries(headers)) {\n if (v === undefined) continue;\n out[k.toLowerCase()] = Array.isArray(v) ? v.join(', ') : String(v);\n }\n return out;\n}\n\n/** Read and return the raw body of a Node request (for frameworks without a JSON body parser). */\nexport function readRawBody(req: IncomingMessage, limit = 4 * 1024 * 1024): Promise<string> {\n return new Promise((resolve, reject) => {\n let size = 0;\n const chunks: Buffer[] = [];\n req.on('data', (c: Buffer) => {\n size += c.length;\n if (size > limit) {\n reject(new Error('Request body too large'));\n req.destroy();\n } else chunks.push(c);\n });\n req.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));\n req.on('error', reject);\n });\n}\n\n/** Base URL that reaches the same server the MCP request arrived on. */\nexport function loopbackBaseUrl(req: IncomingMessage): string {\n const socket = req.socket as IncomingMessage['socket'] & {\n encrypted?: boolean;\n server?: { address(): { address: string } | string | null };\n };\n const port = socket.localPort;\n const encrypted = socket.encrypted;\n // Prefer the address the server is bound to: when it listens on all\n // interfaces this yields 127.0.0.1, so internal calls come from loopback\n // (which `trust proxy: 'loopback'` style settings rely on).\n const bound = socket.server?.address();\n let host = (typeof bound === 'object' && bound?.address) || socket.localAddress || '127.0.0.1';\n if (host.startsWith('::ffff:')) host = host.slice(7);\n if (host === '::' || host === '0.0.0.0') host = '127.0.0.1';\n if (host.includes(':')) host = `[${host}]`;\n return `${encrypted ? 'https' : 'http'}://${host}:${port}`;\n}\n\nexport function writeNodeResponse(res: ServerResponse, out: McpHttpResponse): void {\n res.statusCode = out.status;\n for (const [k, v] of Object.entries(out.headers)) res.setHeader(k, v);\n res.end(out.body);\n}\n","import type { JsonSchema, McpToolDefinition, SchemaLike } from './types.js';\n\ninterface StandardSchemaV1 {\n '~standard': {\n validate?: (value: unknown) => unknown;\n jsonSchema?: { input?: (opts: { target: string }) => unknown };\n };\n}\n\nfunction isStandard(s: unknown): s is StandardSchemaV1 {\n return typeof s === 'object' && s !== null && '~standard' in s;\n}\n\n/** Convert any supported schema flavour into a plain JSON Schema object. */\nexport function toJsonSchema(schema: SchemaLike | undefined): JsonSchema | undefined {\n if (schema === undefined || schema === null) return undefined;\n let out: unknown;\n if (isStandard(schema) && typeof schema['~standard'].jsonSchema?.input === 'function') {\n out = schema['~standard'].jsonSchema.input({ target: 'draft-2020-12' });\n } else if (typeof (schema as { toJSONSchema?: unknown }).toJSONSchema === 'function') {\n out = (schema as { toJSONSchema(): unknown }).toJSONSchema();\n } else if (isStandard(schema)) {\n throw new Error(\n 'mcp-expose: this schema library does not support JSON Schema export. Pass a plain JSON Schema instead.',\n );\n } else {\n out = schema;\n }\n const { $schema: _ignored, ...rest } = out as JsonSchema;\n return rest;\n}\n\n/** Build a validator from a Standard Schema, if the input is one. */\nexport function toValidator(schema: SchemaLike | undefined): McpToolDefinition['validate'] {\n if (!isStandard(schema) || typeof schema['~standard'].validate !== 'function') return undefined;\n const validate = schema['~standard'].validate;\n return async (value) => {\n const result = (await validate(value)) as {\n value?: unknown;\n issues?: { message: string; path?: (PropertyKey | { key: PropertyKey })[] }[];\n };\n if (!result.issues) return { ok: true, value: result.value };\n const message = result.issues\n .map((i) => {\n const path = (i.path ?? []).map((p) => (typeof p === 'object' ? String(p.key) : String(p))).join('.');\n return path ? `${path}: ${i.message}` : i.message;\n })\n .join('; ');\n return { ok: false, message };\n };\n}\n\n/** Very small structural check used when no Standard Schema validator exists. */\nexport function checkRequired(schema: JsonSchema, args: Record<string, unknown>): string | undefined {\n const missing = (schema.required ?? []).filter((k) => args[k] === undefined);\n return missing.length ? `Missing required argument(s): ${missing.join(', ')}` : undefined;\n}\n","import { checkRequired, toJsonSchema, toValidator } from './schema.js';\nimport type {\n Dispatcher,\n JsonSchema,\n McpServerOptions,\n McpToolDefinition,\n RouteResponse,\n RouteSpec,\n RouteToolOptions,\n ToolAnnotations,\n ToolContext,\n ToolResult,\n} from './types.js';\n\nexport const DEFAULT_FORWARD_HEADERS = ['authorization', 'cookie', 'x-api-key', 'accept-language'];\nconst BODY_METHODS = new Set(['POST', 'PUT', 'PATCH']);\nconst PARAM_RE = /:([A-Za-z0-9_]+)\\??|\\{([A-Za-z0-9_]+)\\}/g;\n\n/** Names of the path parameters in a route path (`/a/:id/{slug}` -> ['id', 'slug']). */\nexport function pathParams(path: string): string[] {\n return [...path.matchAll(PARAM_RE)].map((m) => (m[1] ?? m[2]) as string);\n}\n\n/**\n * Encode one path-parameter value. Rejects empty, `.` and `..` values: after URL\n * normalisation they would change which route is called (e.g. `/orders/../admin`),\n * letting an agent reach routes that were never exposed as tools.\n */\nfunction encodeSegment(name: string, value: unknown): string {\n const raw = String(value);\n if (raw === '' || raw === '.' || raw === '..') {\n throw new Error(`Invalid value for path parameter \"${name}\": ${JSON.stringify(raw)}`);\n }\n return encodeURIComponent(raw);\n}\n\n/** Replace path placeholders with URL-encoded values. Missing optional params drop their segment. */\nexport function interpolatePath(path: string, values: Record<string, unknown>): string {\n return path\n .replace(/\\/:([A-Za-z0-9_]+)\\?/g, (_, k: string) =>\n values[k] === undefined ? '' : `/${encodeSegment(k, values[k])}`,\n )\n .replace(PARAM_RE, (_, a?: string, b?: string) => {\n const k = (a ?? b) as string;\n return encodeSegment(k, values[k]);\n });\n}\n\n/** Join path segments, normalising slashes. */\nexport function joinPaths(...parts: (string | undefined)[]): string {\n const joined = parts\n .filter((p): p is string => !!p && p !== '/')\n .map((p) => p.replace(/^\\/+|\\/+$/g, ''))\n .filter(Boolean)\n .join('/');\n return `/${joined}`;\n}\n\n/** `GET /users/:id/orders` -> `get_users_by_id_orders` */\nexport function toolNameFromRoute(method: string, path: string): string {\n const segments = path\n .split('/')\n .filter(Boolean)\n .map((s) => {\n const m = /^(?::([A-Za-z0-9_]+)\\??|\\{([A-Za-z0-9_]+)\\})$/.exec(s);\n return m ? `by_${m[1] ?? m[2]}` : s;\n });\n return sanitizeToolName([method.toLowerCase(), ...segments].join('_'));\n}\n\nexport function sanitizeToolName(name: string): string {\n return name\n .replace(/[^A-Za-z0-9_-]+/g, '_')\n .replace(/_+/g, '_')\n .replace(/^_|_$/g, '')\n .slice(0, 64);\n}\n\nfunction defaultAnnotations(method: string): ToolAnnotations {\n switch (method) {\n case 'GET':\n case 'HEAD':\n return { readOnlyHint: true, openWorldHint: false };\n case 'DELETE':\n return { readOnlyHint: false, destructiveHint: true, idempotentHint: true, openWorldHint: false };\n case 'PUT':\n return { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: false };\n default:\n return { readOnlyHint: false, destructiveHint: false, openWorldHint: false };\n }\n}\n\ninterface ArgMapping {\n params: string[];\n query: Set<string>;\n body: Set<string>;\n /** Body schema is not an object: the whole body is passed under the `body` key. */\n wholeBody: boolean;\n bodyMethod: boolean;\n}\n\n/**\n * Build the flat input schema an agent sees, and remember which argument goes\n * to the path, query string or body when the tool is called.\n */\nexport function buildInputSchema(\n route: RouteSpec,\n opts: RouteToolOptions,\n): { schema: JsonSchema; mapping: ArgMapping } {\n const method = route.method.toUpperCase();\n const params = pathParams(route.path);\n const mapping: ArgMapping = {\n params,\n query: new Set(),\n body: new Set(),\n wholeBody: false,\n bodyMethod: BODY_METHODS.has(method),\n };\n\n if (opts.input) {\n const schema = toJsonSchema(opts.input) as JsonSchema;\n return { schema: { type: 'object', ...schema }, mapping };\n }\n\n const properties: Record<string, JsonSchema> = {};\n const required = new Set<string>();\n\n const paramSchema = toJsonSchema(opts.params);\n for (const p of params) {\n properties[p] = paramSchema?.properties?.[p] ?? { type: 'string', description: `Path parameter \"${p}\"` };\n if (!route.path.includes(`:${p}?`)) required.add(p);\n }\n\n const querySchema = toJsonSchema(opts.query);\n for (const [k, v] of Object.entries(querySchema?.properties ?? {})) {\n properties[k] = v;\n mapping.query.add(k);\n }\n for (const k of querySchema?.required ?? []) required.add(k);\n\n const bodySchema = toJsonSchema(opts.body);\n if (bodySchema) {\n if (bodySchema.properties && (bodySchema.type === undefined || bodySchema.type === 'object')) {\n for (const [k, v] of Object.entries(bodySchema.properties)) {\n properties[k] = v;\n mapping.body.add(k);\n }\n for (const k of bodySchema.required ?? []) required.add(k);\n } else {\n properties.body = bodySchema;\n required.add('body');\n mapping.wholeBody = true;\n }\n }\n\n const schema: JsonSchema = { type: 'object', properties };\n if (required.size) schema.required = [...required];\n return { schema, mapping };\n}\n\nfunction splitArgs(args: Record<string, unknown>, mapping: ArgMapping) {\n const pathValues: Record<string, unknown> = {};\n const query: Record<string, string | string[]> = {};\n let body: Record<string, unknown> | unknown | undefined;\n\n for (const [k, v] of Object.entries(args)) {\n if (v === undefined) continue;\n if (mapping.params.includes(k)) {\n pathValues[k] = v;\n } else if (mapping.wholeBody && k === 'body') {\n body = v;\n } else if (mapping.query.has(k) || (!mapping.body.has(k) && !mapping.bodyMethod)) {\n query[k] = Array.isArray(v) ? v.map(String) : typeof v === 'object' && v !== null ? JSON.stringify(v) : String(v);\n } else {\n body = { ...((body as Record<string, unknown>) ?? {}), [k]: v };\n }\n }\n if (body === undefined && mapping.bodyMethod) body = {};\n return { pathValues, query, body };\n}\n\nexport function responseToResult(res: RouteResponse, maxChars: number): ToolResult {\n let text = res.body;\n if (text.length > maxChars) text = `${text.slice(0, maxChars)}\\n…[truncated ${text.length - maxChars} chars]`;\n const isError = res.status >= 400;\n const result: ToolResult = {\n content: [{ type: 'text', text: isError ? `HTTP ${res.status}\\n${text}` : text || `HTTP ${res.status}` }],\n };\n if (isError) {\n result.isError = true;\n return result;\n }\n if ((res.headers['content-type'] ?? '').includes('json') && text === res.body) {\n try {\n const parsed = JSON.parse(res.body);\n if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) result.structuredContent = parsed;\n } catch {\n /* not JSON after all */\n }\n }\n return result;\n}\n\nexport function pickForwardHeaders(ctx: ToolContext, names: string[]): Record<string, string> {\n const out: Record<string, string> = {};\n for (const name of names) {\n const v = ctx.headers[name.toLowerCase()];\n if (v !== undefined) out[name.toLowerCase()] = v;\n }\n return out;\n}\n\n/**\n * Turn an HTTP route + options into an MCP tool whose handler calls the route\n * through `dispatch`, so auth guards, validation and rate limits all still apply.\n */\nexport function createRouteTool(\n route: RouteSpec,\n opts: RouteToolOptions,\n dispatch: Dispatcher,\n serverOpts: Pick<McpServerOptions, 'forwardHeaders' | 'maxResponseChars'> = {},\n): McpToolDefinition {\n const method = route.method.toUpperCase();\n const name = sanitizeToolName(opts.name ?? toolNameFromRoute(method, route.path));\n const { schema, mapping } = buildInputSchema(route, opts);\n const forward = serverOpts.forwardHeaders ?? DEFAULT_FORWARD_HEADERS;\n const maxChars = serverOpts.maxResponseChars ?? 100_000;\n\n return {\n name,\n title: opts.title,\n description: opts.description ?? `${method} ${route.path}`,\n inputSchema: schema,\n annotations: { ...defaultAnnotations(method), ...opts.annotations },\n validate: toValidator(opts.input),\n async handler(args, ctx) {\n const missing = opts.input ? undefined : checkRequired(schema, args);\n if (missing) return { isError: true, content: [{ type: 'text', text: missing }] };\n\n const { pathValues, query, body } = splitArgs(args, mapping);\n const headers: Record<string, string> = {\n accept: 'application/json',\n ...pickForwardHeaders(ctx, forward),\n ...opts.headers,\n 'x-mcp-tool': name,\n };\n if (ctx.clientIp) headers['x-forwarded-for'] = ctx.clientIp;\n if (body !== undefined) headers['content-type'] = 'application/json';\n\n let path: string;\n try {\n path = interpolatePath(route.path, pathValues);\n } catch (err) {\n return { isError: true, content: [{ type: 'text', text: (err as Error).message }] };\n }\n const res = await dispatch({ method, path, query, body, headers }, ctx);\n return responseToResult(res, maxChars);\n },\n };\n}\n","import type { McpServerOptions, McpToolDefinition, ToolContext, ToolResult } from './types.js';\n\nexport const SUPPORTED_PROTOCOL_VERSIONS = ['2025-11-25', '2025-06-18', '2025-03-26', '2024-11-05'];\nexport const LATEST_PROTOCOL_VERSION = SUPPORTED_PROTOCOL_VERSIONS[0] as string;\n\ntype JsonRpcId = string | number | null;\ninterface JsonRpcRequest {\n jsonrpc: '2.0';\n id?: JsonRpcId;\n method: string;\n params?: Record<string, unknown>;\n}\nexport interface JsonRpcResponse {\n jsonrpc: '2.0';\n id: JsonRpcId;\n result?: unknown;\n error?: { code: number; message: string; data?: unknown };\n}\n\nconst PARSE_ERROR = -32700;\nconst INVALID_REQUEST = -32600;\nconst METHOD_NOT_FOUND = -32601;\nconst INVALID_PARAMS = -32602;\n\nconst ok = (id: JsonRpcId, result: unknown): JsonRpcResponse => ({ jsonrpc: '2.0', id, result });\nconst fail = (id: JsonRpcId, code: number, message: string): JsonRpcResponse => ({\n jsonrpc: '2.0',\n id,\n error: { code, message },\n});\n\n/** Minimal HTTP request/response shapes, so each adapter can bridge its own framework. */\nexport interface McpHttpRequest {\n method: string;\n headers: Record<string, string>;\n /** Parsed JSON body, or the raw string. */\n body: unknown;\n}\nexport interface McpHttpResponse {\n status: number;\n headers: Record<string, string>;\n body?: string;\n}\n\n/**\n * A stateless MCP server speaking the Streamable HTTP transport (JSON responses).\n * Holds the tool registry and answers JSON-RPC messages.\n */\nexport class McpServer {\n private readonly tools = new Map<string, McpToolDefinition>();\n private loaders: (() => void | Promise<void>)[] = [];\n\n constructor(readonly options: McpServerOptions) {}\n\n /** Register a tool. Throws on duplicate names. */\n addTool(tool: McpToolDefinition): this {\n if (this.tools.has(tool.name)) {\n throw new Error(`mcp-expose: duplicate tool name \"${tool.name}\". Set an explicit \\`name\\`.`);\n }\n this.tools.set(tool.name, tool);\n return this;\n }\n\n /** Register a function that adds tools lazily, right before the first MCP request is served. */\n onLoad(loader: () => void | Promise<void>): this {\n this.loaders.push(loader);\n return this;\n }\n\n async listTools(): Promise<McpToolDefinition[]> {\n await this.load();\n return [...this.tools.values()];\n }\n\n private async load() {\n if (!this.loaders.length) return;\n const loaders = this.loaders;\n this.loaders = [];\n for (const l of loaders) await l();\n }\n\n /** Handle one JSON-RPC message (or a batch). Returns null when no response is due. */\n async handleMessage(message: unknown, ctx: ToolContext): Promise<JsonRpcResponse | JsonRpcResponse[] | null> {\n if (Array.isArray(message)) {\n const results = (await Promise.all(message.map((m) => this.handleOne(m, ctx)))).filter(\n (r): r is JsonRpcResponse => r !== null,\n );\n return results.length ? results : null;\n }\n return this.handleOne(message, ctx);\n }\n\n private async handleOne(message: unknown, ctx: ToolContext): Promise<JsonRpcResponse | null> {\n const msg = message as Partial<JsonRpcRequest>;\n if (!msg || typeof msg !== 'object' || msg.jsonrpc !== '2.0') {\n return fail(null, INVALID_REQUEST, 'Invalid JSON-RPC message');\n }\n // Responses from the client or notifications: acknowledge, no reply.\n if (typeof msg.method !== 'string' || msg.id === undefined) return null;\n const id = msg.id;\n const params = msg.params ?? {};\n\n await this.load();\n switch (msg.method) {\n case 'initialize': {\n const requested = params.protocolVersion as string | undefined;\n const protocolVersion =\n requested && SUPPORTED_PROTOCOL_VERSIONS.includes(requested) ? requested : LATEST_PROTOCOL_VERSION;\n return ok(id, {\n protocolVersion,\n capabilities: { tools: { listChanged: false } },\n serverInfo: { name: this.options.name, version: this.options.version ?? '1.0.0' },\n ...(this.options.instructions ? { instructions: this.options.instructions } : {}),\n });\n }\n case 'ping':\n return ok(id, {});\n case 'tools/list':\n return ok(id, {\n tools: [...this.tools.values()].map((t) => ({\n name: t.name,\n ...(t.title ? { title: t.title } : {}),\n description: t.description,\n inputSchema: t.inputSchema,\n ...(t.annotations ? { annotations: t.annotations } : {}),\n })),\n });\n case 'tools/call': {\n const tool = this.tools.get(params.name as string);\n if (!tool) return fail(id, INVALID_PARAMS, `Unknown tool: ${String(params.name)}`);\n return ok(id, await this.callTool(tool, (params.arguments ?? {}) as Record<string, unknown>, ctx));\n }\n case 'resources/list':\n return ok(id, { resources: [] });\n case 'prompts/list':\n return ok(id, { prompts: [] });\n default:\n return fail(id, METHOD_NOT_FOUND, `Method not found: ${msg.method}`);\n }\n }\n\n private async callTool(\n tool: McpToolDefinition,\n args: Record<string, unknown>,\n ctx: ToolContext,\n ): Promise<ToolResult> {\n try {\n if (tool.validate) {\n const v = await tool.validate(args);\n if (!v.ok) return { isError: true, content: [{ type: 'text', text: `Invalid arguments: ${v.message}` }] };\n args = v.value as Record<string, unknown>;\n }\n return await tool.handler(args, ctx);\n } catch (err) {\n // Tool failures are reported in-band so the model can react to them.\n return { isError: true, content: [{ type: 'text', text: `Tool error: ${(err as Error).message}` }] };\n }\n }\n\n /** Check the Origin header (DNS rebinding protection). */\n isOriginAllowed(origin: string | undefined): boolean {\n if (!origin) return true;\n const allowed = this.options.allowedOrigins;\n if (allowed === '*') return true;\n return (allowed ?? []).includes(origin);\n }\n\n /**\n * Framework-neutral Streamable HTTP handler. Adapters convert their request\n * into McpHttpRequest, call this, and write the McpHttpResponse back.\n */\n async handleHttp(req: McpHttpRequest, ctx: ToolContext): Promise<McpHttpResponse> {\n const json = { 'content-type': 'application/json' };\n if (!this.isOriginAllowed(req.headers.origin)) {\n return { status: 403, headers: json, body: JSON.stringify(fail(null, INVALID_REQUEST, 'Origin not allowed')) };\n }\n if (req.method.toUpperCase() !== 'POST') {\n // Stateless server: no standalone SSE stream and no sessions to delete.\n return { status: 405, headers: { allow: 'POST' } };\n }\n let body = req.body;\n if (typeof body === 'string' || body instanceof Uint8Array) {\n try {\n body = JSON.parse(typeof body === 'string' ? body : new TextDecoder().decode(body));\n } catch {\n return { status: 400, headers: json, body: JSON.stringify(fail(null, PARSE_ERROR, 'Parse error')) };\n }\n }\n const response = await this.handleMessage(body, ctx);\n if (response === null) return { status: 202, headers: {} };\n return { status: 200, headers: json, body: JSON.stringify(response) };\n }\n}\n","// Internal: route discovery for Express 3, 4 and 5 (not part of the public API).\nimport { markerOf } from '../core/marker.js';\nimport { joinPaths } from '../core/route-tool.js';\nimport type { RouteToolOptions } from '../core/types.js';\n\ninterface ExpressLayer {\n route?: { path: string | string[]; methods: Record<string, boolean>; stack: { handle: unknown }[] };\n name?: string;\n handle?: { stack?: ExpressLayer[] };\n regexp?: RegExp & { fast_slash?: boolean };\n}\n\n/** Structural type for an Express 4/5 app (or Router). Kept loose on purpose. */\nexport interface ExpressAppLike {\n all: Function;\n router?: unknown;\n _router?: unknown;\n /** Express 3 route table: `{ get: [{ path, method, callbacks }], ... }`. */\n routes?: unknown;\n}\n\ntype Express3Routes = Record<string, { path: string | RegExp; method: string; callbacks: unknown[] }[]>;\n\n/** Express 3 keeps app-level routes in `app.routes`, keyed by method. */\nfunction collectExpress3(routes: Express3Routes, out: Found[]) {\n for (const list of Object.values(routes)) {\n for (const r of list) {\n const marker = r.callbacks.map(markerOf).find(Boolean);\n if (marker && typeof r.path === 'string')\n out.push({ method: r.method.toUpperCase(), path: r.path, options: marker });\n }\n }\n}\n\nfunction stackOf(app: ExpressAppLike): ExpressLayer[] {\n // Express 4 keeps the router on `_router`; its `app.router` getter throws.\n // Express 5 exposes `app.router`.\n let router = app._router as { stack?: ExpressLayer[] } | undefined;\n if (!router) {\n try {\n router = app.router as typeof router;\n } catch {\n router = undefined;\n }\n }\n return router?.stack ?? [];\n}\n\n/** Recover an Express 4 mount path from a layer regexp (best effort). */\nfunction express4Prefix(layer: ExpressLayer): string | undefined {\n const re = layer.regexp;\n if (!re) return undefined;\n if (re.fast_slash) return '';\n const m = /^\\/\\^((?:\\\\[.*+?^${}()|[\\]\\\\/]|[^.*+?^${}()|[\\]\\\\/])*)\\\\\\/\\?\\(\\?=\\\\\\/\\|\\$\\)\\/i?$/.exec(re.toString());\n return m ? (m[1] as string).replace(/\\\\(.)/g, '$1') : undefined;\n}\n\nexport interface Found {\n method: string;\n path: string;\n options: RouteToolOptions;\n}\n\nfunction collect(\n stack: ExpressLayer[],\n prefix: string,\n routers: Map<unknown, string>,\n out: Found[],\n unresolved: string[],\n) {\n for (const layer of stack) {\n if (layer.route) {\n const marker = layer.route.stack.map((l) => markerOf(l.handle)).find(Boolean);\n if (!marker) continue;\n const paths = Array.isArray(layer.route.path) ? layer.route.path : [layer.route.path];\n for (const method of Object.keys(layer.route.methods).filter((m) => m !== '_all')) {\n for (const p of paths) out.push({ method: method.toUpperCase(), path: joinPaths(prefix, p), options: marker });\n }\n } else if (layer.handle?.stack) {\n const known = routers.get(layer.handle);\n const sub = known ?? express4Prefix(layer);\n if (sub === undefined) {\n // Only complain if this router actually contains MCP tools.\n const inner: Found[] = [];\n collect(layer.handle.stack, '', routers, inner, unresolved);\n if (inner.length) unresolved.push(...inner.map((f) => `${f.method} ${f.path}`));\n continue;\n }\n collect(layer.handle.stack, joinPaths(prefix, sub), routers, out, unresolved);\n }\n }\n}\n\n/** Find every route marked with `mcpTool()` in an Express app. */\nexport function discoverExpressRoutes(app: ExpressAppLike, routers: Record<string, unknown> = {}): Found[] {\n const stack = stackOf(app);\n const byRouter = new Map(Object.entries(routers).map(([prefix, r]) => [r, prefix]));\n const out: Found[] = [];\n const unresolved: string[] = [];\n collect(stack, '', byRouter, out, unresolved);\n if (!stack.length && app.routes && typeof app.routes === 'object') collectExpress3(app.routes as Express3Routes, out);\n if (unresolved.length) {\n throw new Error(\n `mcp-expose: cannot determine the mount path of a router containing MCP tools (${unresolved.join(', ')}). ` +\n `Pass it via the \\`routers\\` option, e.g. mountMcp(app, { routers: { '/api': apiRouter } }).`,\n );\n }\n return out;\n}\n","import type { IncomingMessage, ServerResponse } from 'node:http';\nimport { createFetchDispatcher } from '../core/dispatch.js';\nimport { mark } from '../core/marker.js';\nimport { loopbackBaseUrl, normalizeHeaders, readRawBody, writeNodeResponse } from '../core/node.js';\nimport { createRouteTool } from '../core/route-tool.js';\nimport { McpServer } from '../core/server.js';\nimport type { McpServerOptions, McpToolDefinition, RouteToolOptions, ToolContext } from '../core/types.js';\nimport { discoverExpressRoutes, type ExpressAppLike } from './discover.js';\n\nexport type { ExpressAppLike } from './discover.js';\n\ntype Req = IncomingMessage & { body?: unknown; ip?: string };\ntype Next = (err?: unknown) => void;\ntype Middleware = (req: Req, res: ServerResponse, next: Next) => void;\n\n/**\n * Mark an Express route as an MCP tool. Put it first in the route's handler list:\n *\n * app.get('/users/:id', mcpTool({ description: 'Get a user' }), auth, getUser)\n *\n * It is a no-op at request time; it only carries metadata.\n */\nexport function mcpTool(options: RouteToolOptions = {}): Middleware {\n return mark<Middleware>((_req, _res, next) => next(), options);\n}\n\nexport interface ExpressMcpOptions extends McpServerOptions {\n /** Path of the MCP endpoint. @default '/mcp' */\n path?: string;\n /**\n * Base URL for internal calls. Defaults to the address/port the MCP request\n * arrived on (loopback), so no configuration is needed in most setups.\n */\n baseUrl?: string;\n /**\n * Prefixes of mounted routers, e.g. `{ '/api': apiRouter }`. Express 5 does\n * not keep mount paths, so routers mounted with a path must be listed here.\n * (Express 4 mount paths are detected automatically.)\n */\n routers?: Record<string, unknown>;\n /** Routes to expose without touching the route definitions. */\n routes?: (RouteToolOptions & { method: string; path: string })[];\n /** Extra hand-written tools (see `defineTool`). */\n tools?: McpToolDefinition[];\n /**\n * Middleware run before the MCP endpoint, e.g. your auth middleware, to\n * protect tools/list as well as tools/call.\n */\n middleware?: Middleware[];\n}\n\n/**\n * Add an MCP endpoint (default `POST /mcp`) to an Express app. Tools are\n * discovered lazily on the first MCP request, so call this before or after\n * defining routes. Returns the underlying McpServer.\n */\nexport function mountMcp(app: ExpressAppLike, options: ExpressMcpOptions): McpServer {\n const path = options.path ?? '/mcp';\n const server = new McpServer(options);\n const dispatch = createFetchDispatcher({\n baseUrl: options.baseUrl ?? ((ctx) => loopbackBaseUrl(ctx.raw as IncomingMessage)),\n });\n\n for (const t of options.tools ?? []) server.addTool(t);\n server.onLoad(() => {\n const found = discoverExpressRoutes(app, options.routers);\n for (const r of options.routes ?? []) found.push({ method: r.method, path: r.path, options: r });\n for (const f of found)\n server.addTool(createRouteTool({ method: f.method, path: f.path }, f.options, dispatch, options));\n });\n\n const handler: Middleware = (req, res, next) => {\n (async () => {\n const body = req.method === 'POST' && req.body === undefined ? await readRawBody(req) : req.body;\n const ctx: ToolContext = {\n headers: normalizeHeaders(req.headers),\n clientIp: req.ip ?? req.socket.remoteAddress,\n raw: req,\n };\n const out = await server.handleHttp({ method: req.method ?? 'POST', headers: ctx.headers, body }, ctx);\n writeNodeResponse(res, out);\n })().catch(next);\n };\n (app.all as (path: string, ...h: Middleware[]) => void).call(app, path, ...(options.middleware ?? []), handler);\n return server;\n}\n"],"mappings":";;;;AAEO,SAASA,SAASC,MAAcC,KAAyC;AAC9E,QAAMC,MAAM,IAAIC,IAAIH,KAAKI,QAAQ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,WAAWC,sBAAsB;IACrCC,SAASZ,QAAQY,YAAY,CAACC,QAAQC,gBAAgBD,IAAIE,GAAG;EAC/D,CAAA;AAEA,aAAWC,KAAKhB,QAAQiB,SAAS,CAAA,EAAIT,QAAOU,QAAQF,CAAAA;AACpDR,SAAOW,OAAO,MAAA;AACZ,UAAMC,QAAQC,sBAAsBf,KAAKN,QAAQsB,OAAO;AACxD,eAAWC,KAAKvB,QAAQwB,UAAU,CAAA,EAAIJ,OAAMK,KAAK;MAAEC,QAAQH,EAAEG;MAAQnB,MAAMgB,EAAEhB;MAAMP,SAASuB;IAAE,CAAA;AAC9F,eAAWI,KAAKP,MACdZ,QAAOU,QAAQU,gBAAgB;MAAEF,QAAQC,EAAED;MAAQnB,MAAMoB,EAAEpB;IAAK,GAAGoB,EAAE3B,SAASU,UAAUV,OAAAA,CAAAA;EAC5F,CAAA;AAEA,QAAM6B,UAAsB,wBAACC,KAAKC,KAAK3B,SAAAA;AACpC,KAAA,YAAA;AACC,YAAM4B,OAAOF,IAAIJ,WAAW,UAAUI,IAAIE,SAASC,SAAY,MAAMC,YAAYJ,GAAAA,IAAOA,IAAIE;AAC5F,YAAMnB,MAAmB;QACvBsB,SAASC,iBAAiBN,IAAIK,OAAO;QACrCE,UAAUP,IAAIQ,MAAMR,IAAIS,OAAOC;QAC/BzB,KAAKe;MACP;AACA,YAAMW,MAAM,MAAMjC,OAAOkC,WAAW;QAAEhB,QAAQI,IAAIJ,UAAU;QAAQS,SAAStB,IAAIsB;QAASH;MAAK,GAAGnB,GAAAA;AAClG8B,wBAAkBZ,KAAKU,GAAAA;IACzB,GAAA,EAAKG,MAAMxC,IAAAA;EACb,GAX4B;AAY3BE,MAAIuC,IAAmDC,KAAKxC,KAAKC,MAAAA,GAAUP,QAAQ+C,cAAc,CAAA,GAAKlB,OAAAA;AACvG,SAAOrB;AACT;AA7BgBH;","names":["buildUrl","base","req","url","URL","replace","path","k","v","Object","entries","query","item","Array","isArray","searchParams","append","toString","createFetchDispatcher","opts","doFetch","fetch","ctx","baseUrl","res","method","headers","body","undefined","JSON","stringify","signal","AbortSignal","timeout","timeoutMs","forEach","status","text","MCP_TOOL","Symbol","for","markerOf","fn","undefined","mark","opts","Object","defineProperty","value","enumerable","normalizeHeaders","headers","out","k","v","Object","entries","undefined","toLowerCase","Array","isArray","join","String","readRawBody","req","limit","Promise","resolve","reject","size","chunks","on","c","length","Error","destroy","push","Buffer","concat","toString","loopbackBaseUrl","socket","port","localPort","encrypted","bound","server","address","host","localAddress","startsWith","slice","includes","writeNodeResponse","res","statusCode","status","setHeader","end","body","isStandard","s","toJsonSchema","schema","undefined","out","jsonSchema","input","target","toJSONSchema","Error","$schema","_ignored","rest","toValidator","validate","value","result","issues","ok","message","map","i","path","p","String","key","join","checkRequired","args","missing","required","filter","k","length","DEFAULT_FORWARD_HEADERS","BODY_METHODS","Set","PARAM_RE","pathParams","path","matchAll","map","m","encodeSegment","name","value","raw","String","Error","JSON","stringify","encodeURIComponent","interpolatePath","values","replace","_","k","undefined","a","b","joinPaths","parts","joined","filter","p","Boolean","join","toolNameFromRoute","method","segments","split","s","exec","sanitizeToolName","toLowerCase","slice","defaultAnnotations","readOnlyHint","openWorldHint","destructiveHint","idempotentHint","buildInputSchema","route","opts","toUpperCase","params","mapping","query","body","wholeBody","bodyMethod","has","input","schema","toJsonSchema","type","properties","required","paramSchema","description","includes","add","querySchema","v","Object","entries","bodySchema","size","splitArgs","args","pathValues","Array","isArray","responseToResult","res","maxChars","text","length","isError","status","result","content","headers","parsed","parse","structuredContent","pickForwardHeaders","ctx","names","out","createRouteTool","dispatch","serverOpts","forward","forwardHeaders","maxResponseChars","title","inputSchema","annotations","validate","toValidator","handler","missing","checkRequired","accept","clientIp","err","message","SUPPORTED_PROTOCOL_VERSIONS","LATEST_PROTOCOL_VERSION","PARSE_ERROR","INVALID_REQUEST","METHOD_NOT_FOUND","INVALID_PARAMS","ok","id","result","jsonrpc","fail","code","message","error","McpServer","tools","Map","loaders","options","addTool","tool","has","name","Error","set","onLoad","loader","push","listTools","load","values","length","l","handleMessage","ctx","Array","isArray","results","Promise","all","map","m","handleOne","filter","r","msg","method","undefined","params","requested","protocolVersion","includes","capabilities","listChanged","serverInfo","version","instructions","t","title","description","inputSchema","annotations","get","String","callTool","arguments","resources","prompts","args","validate","v","isError","content","type","text","value","handler","err","isOriginAllowed","origin","allowed","allowedOrigins","handleHttp","req","json","headers","status","body","JSON","stringify","toUpperCase","allow","Uint8Array","parse","TextDecoder","decode","response","collectExpress3","routes","out","list","Object","values","r","marker","callbacks","map","markerOf","find","Boolean","path","push","method","toUpperCase","options","stackOf","app","router","_router","undefined","stack","express4Prefix","layer","re","regexp","fast_slash","m","exec","toString","replace","collect","prefix","routers","unresolved","route","l","handle","paths","Array","isArray","keys","methods","filter","p","joinPaths","known","get","sub","inner","length","f","discoverExpressRoutes","byRouter","Map","entries","Error","join","mcpTool","options","mark","_req","_res","next","mountMcp","app","path","server","McpServer","dispatch","createFetchDispatcher","baseUrl","ctx","loopbackBaseUrl","raw","t","tools","addTool","onLoad","found","discoverExpressRoutes","routers","r","routes","push","method","f","createRouteTool","handler","req","res","body","undefined","readRawBody","headers","normalizeHeaders","clientIp","ip","socket","remoteAddress","out","handleHttp","writeNodeResponse","catch","all","call","middleware"]}
|
|
1
|
+
{"version":3,"sources":["../../src/core/dispatch.ts","../../src/core/marker.ts","../../src/core/node.ts","../../src/core/schema.ts","../../src/core/route-tool.ts","../../src/core/oauth.ts","../../src/core/server.ts","../../src/express/discover.ts","../../src/express/index.ts"],"sourcesContent":["import type { Dispatcher, RouteRequest, ToolContext } from './types.js';\n\nexport function buildUrl(base: string, req: Pick<RouteRequest, 'path' | 'query'>): string {\n const url = new URL(base.replace(/\\/+$/, '') + req.path);\n for (const [k, v] of Object.entries(req.query)) {\n for (const item of Array.isArray(v) ? v : [v]) url.searchParams.append(k, item);\n }\n return url.toString();\n}\n\nexport interface FetchDispatcherOptions {\n /**\n * Base URL of the API, e.g. `http://127.0.0.1:3000` or `https://api.example.com/v1`.\n * A function lets adapters resolve it per call (e.g. from the local port).\n */\n baseUrl: string | ((ctx: ToolContext) => string);\n /** Custom fetch implementation. Defaults to the global `fetch`. */\n fetch?: typeof fetch;\n /** Per-request timeout in ms. @default 30_000 */\n timeoutMs?: number;\n}\n\n/**\n * Dispatches tool calls as real HTTP requests. Used for loopback calls into the\n * host app (Express, Koa, NestJS) and for standalone gateways in front of any API.\n */\nexport function createFetchDispatcher(opts: FetchDispatcherOptions): Dispatcher {\n const doFetch = opts.fetch ?? fetch;\n return async (req, ctx) => {\n const base = typeof opts.baseUrl === 'function' ? opts.baseUrl(ctx) : opts.baseUrl;\n const res = await doFetch(buildUrl(base, req), {\n method: req.method,\n headers: req.headers,\n body:\n req.body === undefined || req.method === 'GET' || req.method === 'HEAD' ? undefined : JSON.stringify(req.body),\n signal: AbortSignal.timeout(opts.timeoutMs ?? 30_000),\n });\n const headers: Record<string, string> = {};\n res.headers.forEach((v, k) => (headers[k] = v));\n return { status: res.status, headers, body: await res.text() };\n };\n}\n","import type { RouteToolOptions } from './types.js';\n\n/** Symbol under which marker middleware carries its tool options. */\nexport const MCP_TOOL = Symbol.for('mcp-expose.tool');\n\nexport type Marked<T> = T & { [MCP_TOOL]: RouteToolOptions };\n\nexport function markerOf(fn: unknown): RouteToolOptions | undefined {\n return typeof fn === 'function' ? (fn as Partial<Marked<object>>)[MCP_TOOL] : undefined;\n}\n\nexport function mark<T extends object>(fn: T, opts: RouteToolOptions): Marked<T> {\n Object.defineProperty(fn, MCP_TOOL, { value: opts, enumerable: false });\n return fn as Marked<T>;\n}\n","import type { IncomingMessage, ServerResponse } from 'node:http';\nimport type { McpHttpResponse } from './server.js';\n\n/** Flatten Node's IncomingHttpHeaders into lower-cased single strings. */\nexport function normalizeHeaders(headers: Record<string, string | string[] | undefined>): Record<string, string> {\n const out: Record<string, string> = {};\n for (const [k, v] of Object.entries(headers)) {\n if (v === undefined) continue;\n out[k.toLowerCase()] = Array.isArray(v) ? v.join(', ') : String(v);\n }\n return out;\n}\n\n/** Read and return the raw body of a Node request (for frameworks without a JSON body parser). */\nexport function readRawBody(req: IncomingMessage, limit = 4 * 1024 * 1024): Promise<string> {\n return new Promise((resolve, reject) => {\n let size = 0;\n const chunks: Buffer[] = [];\n req.on('data', (c: Buffer) => {\n size += c.length;\n if (size > limit) {\n reject(new Error('Request body too large'));\n req.destroy();\n } else chunks.push(c);\n });\n req.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));\n req.on('error', reject);\n });\n}\n\n/** Base URL that reaches the same server the MCP request arrived on. */\nexport function loopbackBaseUrl(req: IncomingMessage): string {\n const socket = req.socket as IncomingMessage['socket'] & {\n encrypted?: boolean;\n server?: { address(): { address: string } | string | null };\n };\n const port = socket.localPort;\n const encrypted = socket.encrypted;\n // Prefer the address the server is bound to: when it listens on all\n // interfaces this yields 127.0.0.1, so internal calls come from loopback\n // (which `trust proxy: 'loopback'` style settings rely on).\n const bound = socket.server?.address();\n let host = (typeof bound === 'object' && bound?.address) || socket.localAddress || '127.0.0.1';\n if (host.startsWith('::ffff:')) host = host.slice(7);\n if (host === '::' || host === '0.0.0.0') host = '127.0.0.1';\n if (host.includes(':')) host = `[${host}]`;\n return `${encrypted ? 'https' : 'http'}://${host}:${port}`;\n}\n\nexport function writeNodeResponse(res: ServerResponse, out: McpHttpResponse): void {\n res.statusCode = out.status;\n for (const [k, v] of Object.entries(out.headers)) res.setHeader(k, v);\n res.end(out.body);\n}\n","import type { JsonSchema, McpToolDefinition, SchemaLike } from './types.js';\n\ninterface StandardSchemaV1 {\n '~standard': {\n validate?: (value: unknown) => unknown;\n jsonSchema?: { input?: (opts: { target: string }) => unknown };\n };\n}\n\nfunction isStandard(s: unknown): s is StandardSchemaV1 {\n return typeof s === 'object' && s !== null && '~standard' in s;\n}\n\n/** Convert any supported schema flavour into a plain JSON Schema object. */\nexport function toJsonSchema(schema: SchemaLike | undefined): JsonSchema | undefined {\n if (schema === undefined || schema === null) return undefined;\n let out: unknown;\n if (isStandard(schema) && typeof schema['~standard'].jsonSchema?.input === 'function') {\n out = schema['~standard'].jsonSchema.input({ target: 'draft-2020-12' });\n } else if (typeof (schema as { toJSONSchema?: unknown }).toJSONSchema === 'function') {\n out = (schema as { toJSONSchema(): unknown }).toJSONSchema();\n } else if (isStandard(schema)) {\n throw new Error(\n 'mcp-expose: this schema library does not support JSON Schema export. Pass a plain JSON Schema instead.',\n );\n } else {\n out = schema;\n }\n const { $schema: _ignored, ...rest } = out as JsonSchema;\n return rest;\n}\n\n/** Build a validator from a Standard Schema, if the input is one. */\nexport function toValidator(schema: SchemaLike | undefined): McpToolDefinition['validate'] {\n if (!isStandard(schema) || typeof schema['~standard'].validate !== 'function') return undefined;\n const validate = schema['~standard'].validate;\n return async (value) => {\n const result = (await validate(value)) as {\n value?: unknown;\n issues?: { message: string; path?: (PropertyKey | { key: PropertyKey })[] }[];\n };\n if (!result.issues) return { ok: true, value: result.value };\n const message = result.issues\n .map((i) => {\n const path = (i.path ?? []).map((p) => (typeof p === 'object' ? String(p.key) : String(p))).join('.');\n return path ? `${path}: ${i.message}` : i.message;\n })\n .join('; ');\n return { ok: false, message };\n };\n}\n\n/** Very small structural check used when no Standard Schema validator exists. */\nexport function checkRequired(schema: JsonSchema, args: Record<string, unknown>): string | undefined {\n const missing = (schema.required ?? []).filter((k) => args[k] === undefined);\n return missing.length ? `Missing required argument(s): ${missing.join(', ')}` : undefined;\n}\n","import { checkRequired, toJsonSchema, toValidator } from './schema.js';\nimport type {\n Dispatcher,\n JsonSchema,\n McpServerOptions,\n McpToolDefinition,\n RouteResponse,\n RouteSpec,\n RouteToolOptions,\n ToolAnnotations,\n ToolContext,\n ToolResult,\n} from './types.js';\n\nexport const DEFAULT_FORWARD_HEADERS = ['authorization', 'cookie', 'x-api-key', 'accept-language'];\nconst BODY_METHODS = new Set(['POST', 'PUT', 'PATCH']);\nconst PARAM_RE = /:([A-Za-z0-9_]+)\\??|\\{([A-Za-z0-9_]+)\\}/g;\n\n/** Names of the path parameters in a route path (`/a/:id/{slug}` -> ['id', 'slug']). */\nexport function pathParams(path: string): string[] {\n return [...path.matchAll(PARAM_RE)].map((m) => (m[1] ?? m[2]) as string);\n}\n\n/**\n * Encode one path-parameter value. Rejects empty, `.` and `..` values: after URL\n * normalisation they would change which route is called (e.g. `/orders/../admin`),\n * letting an agent reach routes that were never exposed as tools.\n */\nfunction encodeSegment(name: string, value: unknown): string {\n const raw = String(value);\n if (raw === '' || raw === '.' || raw === '..') {\n throw new Error(`Invalid value for path parameter \"${name}\": ${JSON.stringify(raw)}`);\n }\n return encodeURIComponent(raw);\n}\n\n/** Replace path placeholders with URL-encoded values. Missing optional params drop their segment. */\nexport function interpolatePath(path: string, values: Record<string, unknown>): string {\n return path\n .replace(/\\/:([A-Za-z0-9_]+)\\?/g, (_, k: string) =>\n values[k] === undefined ? '' : `/${encodeSegment(k, values[k])}`,\n )\n .replace(PARAM_RE, (_, a?: string, b?: string) => {\n const k = (a ?? b) as string;\n return encodeSegment(k, values[k]);\n });\n}\n\n/** Join path segments, normalising slashes. */\nexport function joinPaths(...parts: (string | undefined)[]): string {\n const joined = parts\n .filter((p): p is string => !!p && p !== '/')\n .map((p) => p.replace(/^\\/+|\\/+$/g, ''))\n .filter(Boolean)\n .join('/');\n return `/${joined}`;\n}\n\n/** `GET /users/:id/orders` -> `get_users_by_id_orders` */\nexport function toolNameFromRoute(method: string, path: string): string {\n const segments = path\n .split('/')\n .filter(Boolean)\n .map((s) => {\n const m = /^(?::([A-Za-z0-9_]+)\\??|\\{([A-Za-z0-9_]+)\\})$/.exec(s);\n return m ? `by_${m[1] ?? m[2]}` : s;\n });\n return sanitizeToolName([method.toLowerCase(), ...segments].join('_'));\n}\n\nexport function sanitizeToolName(name: string): string {\n return name\n .replace(/[^A-Za-z0-9_-]+/g, '_')\n .replace(/_+/g, '_')\n .replace(/^_|_$/g, '')\n .slice(0, 64);\n}\n\nfunction defaultAnnotations(method: string): ToolAnnotations {\n switch (method) {\n case 'GET':\n case 'HEAD':\n return { readOnlyHint: true, openWorldHint: false };\n case 'DELETE':\n return { readOnlyHint: false, destructiveHint: true, idempotentHint: true, openWorldHint: false };\n case 'PUT':\n return { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: false };\n default:\n return { readOnlyHint: false, destructiveHint: false, openWorldHint: false };\n }\n}\n\ninterface ArgMapping {\n params: string[];\n query: Set<string>;\n body: Set<string>;\n /** Body schema is not an object: the whole body is passed under the `body` key. */\n wholeBody: boolean;\n bodyMethod: boolean;\n}\n\n/**\n * Build the flat input schema an agent sees, and remember which argument goes\n * to the path, query string or body when the tool is called.\n */\nexport function buildInputSchema(\n route: RouteSpec,\n opts: RouteToolOptions,\n): { schema: JsonSchema; mapping: ArgMapping } {\n const method = route.method.toUpperCase();\n const params = pathParams(route.path);\n const mapping: ArgMapping = {\n params,\n query: new Set(),\n body: new Set(),\n wholeBody: false,\n bodyMethod: BODY_METHODS.has(method),\n };\n\n if (opts.input) {\n const schema = toJsonSchema(opts.input) as JsonSchema;\n return { schema: { type: 'object', ...schema }, mapping };\n }\n\n const properties: Record<string, JsonSchema> = {};\n const required = new Set<string>();\n\n const paramSchema = toJsonSchema(opts.params);\n for (const p of params) {\n properties[p] = paramSchema?.properties?.[p] ?? { type: 'string', description: `Path parameter \"${p}\"` };\n if (!route.path.includes(`:${p}?`)) required.add(p);\n }\n\n const querySchema = toJsonSchema(opts.query);\n for (const [k, v] of Object.entries(querySchema?.properties ?? {})) {\n properties[k] = v;\n mapping.query.add(k);\n }\n for (const k of querySchema?.required ?? []) required.add(k);\n\n const bodySchema = toJsonSchema(opts.body);\n if (bodySchema) {\n if (bodySchema.properties && (bodySchema.type === undefined || bodySchema.type === 'object')) {\n for (const [k, v] of Object.entries(bodySchema.properties)) {\n properties[k] = v;\n mapping.body.add(k);\n }\n for (const k of bodySchema.required ?? []) required.add(k);\n } else {\n properties.body = bodySchema;\n required.add('body');\n mapping.wholeBody = true;\n }\n }\n\n const schema: JsonSchema = { type: 'object', properties };\n if (required.size) schema.required = [...required];\n return { schema, mapping };\n}\n\nfunction splitArgs(args: Record<string, unknown>, mapping: ArgMapping) {\n const pathValues: Record<string, unknown> = {};\n const query: Record<string, string | string[]> = {};\n let body: Record<string, unknown> | unknown | undefined;\n\n for (const [k, v] of Object.entries(args)) {\n if (v === undefined) continue;\n if (mapping.params.includes(k)) {\n pathValues[k] = v;\n } else if (mapping.wholeBody && k === 'body') {\n body = v;\n } else if (mapping.query.has(k) || (!mapping.body.has(k) && !mapping.bodyMethod)) {\n query[k] = Array.isArray(v) ? v.map(String) : typeof v === 'object' && v !== null ? JSON.stringify(v) : String(v);\n } else {\n body = { ...((body as Record<string, unknown>) ?? {}), [k]: v };\n }\n }\n if (body === undefined && mapping.bodyMethod) body = {};\n return { pathValues, query, body };\n}\n\nexport function responseToResult(res: RouteResponse, maxChars: number): ToolResult {\n let text = res.body;\n if (text.length > maxChars) text = `${text.slice(0, maxChars)}\\n…[truncated ${text.length - maxChars} chars]`;\n const isError = res.status >= 400;\n const result: ToolResult = {\n content: [{ type: 'text', text: isError ? `HTTP ${res.status}\\n${text}` : text || `HTTP ${res.status}` }],\n };\n if (isError) {\n result.isError = true;\n return result;\n }\n if ((res.headers['content-type'] ?? '').includes('json') && text === res.body) {\n try {\n const parsed = JSON.parse(res.body);\n if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) result.structuredContent = parsed;\n } catch {\n /* not JSON after all */\n }\n }\n return result;\n}\n\nexport function pickForwardHeaders(ctx: ToolContext, names: string[]): Record<string, string> {\n const out: Record<string, string> = {};\n for (const name of names) {\n const v = ctx.headers[name.toLowerCase()];\n if (v !== undefined) out[name.toLowerCase()] = v;\n }\n return out;\n}\n\n/**\n * Turn an HTTP route + options into an MCP tool whose handler calls the route\n * through `dispatch`, so auth guards, validation and rate limits all still apply.\n */\nexport function createRouteTool(\n route: RouteSpec,\n opts: RouteToolOptions,\n dispatch: Dispatcher,\n serverOpts: Pick<McpServerOptions, 'forwardHeaders' | 'maxResponseChars'> = {},\n): McpToolDefinition {\n const method = route.method.toUpperCase();\n const name = sanitizeToolName(opts.name ?? toolNameFromRoute(method, route.path));\n const { schema, mapping } = buildInputSchema(route, opts);\n const forward = serverOpts.forwardHeaders ?? DEFAULT_FORWARD_HEADERS;\n const maxChars = serverOpts.maxResponseChars ?? 100_000;\n\n return {\n name,\n title: opts.title,\n description: opts.description ?? `${method} ${route.path}`,\n inputSchema: schema,\n annotations: { ...defaultAnnotations(method), ...opts.annotations },\n scopes: opts.scopes,\n validate: toValidator(opts.input),\n async handler(args, ctx) {\n const missing = opts.input ? undefined : checkRequired(schema, args);\n if (missing) return { isError: true, content: [{ type: 'text', text: missing }] };\n\n const { pathValues, query, body } = splitArgs(args, mapping);\n const headers: Record<string, string> = {\n accept: 'application/json',\n ...pickForwardHeaders(ctx, forward),\n ...opts.headers,\n 'x-mcp-tool': name,\n };\n if (ctx.clientIp) headers['x-forwarded-for'] = ctx.clientIp;\n if (body !== undefined) headers['content-type'] = 'application/json';\n\n let path: string;\n try {\n path = interpolatePath(route.path, pathValues);\n } catch (err) {\n return { isError: true, content: [{ type: 'text', text: (err as Error).message }] };\n }\n const res = await dispatch({ method, path, query, body, headers }, ctx);\n return responseToResult(res, maxChars);\n },\n };\n}\n","/**\n * OAuth 2.1 support per the MCP authorization spec: the MCP endpoint acts as an\n * OAuth *resource server*. It publishes Protected Resource Metadata (RFC 9728),\n * answers unauthenticated requests with a `WWW-Authenticate` challenge that\n * tells the client where to log in, and checks bearer tokens with a verifier\n * you provide (see `jwtVerifier` / `introspectionVerifier` in `mcp-expose/oauth`).\n *\n * Runtime-agnostic: no `node:*` imports.\n */\n\n/** Who the access token belongs to and what it allows. Available to tools as `ctx.auth`. */\nexport interface AuthInfo {\n /** The raw access token. */\n token: string;\n /** Scopes granted to the token. */\n scopes: string[];\n /** Subject (user id), e.g. the JWT `sub` claim. */\n subject?: string;\n /** OAuth client the token was issued to. */\n clientId?: string;\n /** Expiry, in seconds since the epoch. Expired tokens are rejected. */\n expiresAt?: number;\n /** All claims / introspection fields. */\n claims?: Record<string, unknown>;\n}\n\nexport interface VerifyTokenContext {\n /** The canonical resource URL (`oauth.resource`). Tokens must be issued for it (audience). */\n resource: string;\n /** Lower-cased headers of the MCP request. */\n headers: Record<string, string>;\n}\n\n/**\n * Checks an access token. Return its AuthInfo, or `undefined` / throw when the\n * token is invalid. It MUST check that the token was issued for this server\n * (audience = `ctx.resource`), or tokens meant for other services would be accepted.\n */\nexport type TokenVerifier = (\n token: string,\n ctx: VerifyTokenContext,\n) => AuthInfo | undefined | null | Promise<AuthInfo | undefined | null>;\n\nexport interface OAuthOptions {\n /**\n * Canonical, public URL of the MCP endpoint, e.g. `https://api.example.com/mcp`.\n * Clients request tokens for this resource, and it must match the URL they connect to.\n */\n resource: string;\n /** Issuer URLs of the authorization servers that issue tokens for this server. */\n authorizationServers: string[];\n /** Checks bearer tokens. */\n verifyToken: TokenVerifier;\n /** Scopes every token needs to use the MCP endpoint at all. */\n requiredScopes?: string[];\n /** Scopes advertised in the metadata. @default requiredScopes plus every tool's `scopes` */\n scopesSupported?: string[];\n /** Human-readable name shown by clients. */\n resourceName?: string;\n /** URL of documentation for developers. */\n resourceDocumentation?: string;\n /** Extra fields merged into the Protected Resource Metadata document. */\n metadata?: Record<string, unknown>;\n}\n\nexport const PROTECTED_RESOURCE_WELL_KNOWN = '/.well-known/oauth-protected-resource';\n\n/**\n * Paths the metadata is served on, per RFC 9728: the well-known prefix with the\n * resource path appended (`/.well-known/oauth-protected-resource/mcp`), plus the\n * bare well-known path that older clients fall back to.\n */\nexport function protectedResourceMetadataPaths(resource: string): string[] {\n const pathname = new URL(resource).pathname.replace(/\\/+$/, '');\n return pathname\n ? [PROTECTED_RESOURCE_WELL_KNOWN + pathname, PROTECTED_RESOURCE_WELL_KNOWN]\n : [PROTECTED_RESOURCE_WELL_KNOWN];\n}\n\n/** Absolute URL of the metadata document, used in `WWW-Authenticate`. */\nexport function protectedResourceMetadataUrl(resource: string): string {\n return new URL(protectedResourceMetadataPaths(resource)[0] as string, resource).href;\n}\n\nexport function validateOAuthOptions(o: OAuthOptions): void {\n let url: URL;\n try {\n url = new URL(o.resource);\n } catch {\n throw new Error(`mcp-expose: oauth.resource must be an absolute URL, got ${JSON.stringify(o.resource)}`);\n }\n if (url.hash) throw new Error('mcp-expose: oauth.resource must not contain a fragment');\n if (!o.authorizationServers?.length) throw new Error('mcp-expose: oauth.authorizationServers must not be empty');\n if (typeof o.verifyToken !== 'function') throw new Error('mcp-expose: oauth.verifyToken is required');\n}\n\nconst quote = (v: string) => `\"${v.replace(/[\"\\\\]/g, '\\\\$&')}\"`;\n\n/** Build a `WWW-Authenticate: Bearer ...` challenge (RFC 6750 + RFC 9728). */\nexport function bearerChallenge(\n resource: string,\n params: { error?: string; errorDescription?: string; scope?: string[] } = {},\n): string {\n const parts: string[] = [];\n if (params.error) parts.push(`error=${quote(params.error)}`);\n if (params.errorDescription) parts.push(`error_description=${quote(params.errorDescription)}`);\n if (params.scope?.length) parts.push(`scope=${quote(params.scope.join(' '))}`);\n parts.push(`resource_metadata=${quote(protectedResourceMetadataUrl(resource))}`);\n return `Bearer ${parts.join(', ')}`;\n}\n\n/** Extract the token from `Authorization: Bearer <token>`. Tokens in the query string are never accepted. */\nexport function bearerToken(headers: Record<string, string>): string | undefined {\n const m = /^Bearer[ ]+([^\\s,]+)\\s*$/i.exec(headers.authorization ?? '');\n return m?.[1];\n}\n\nexport type AuthOutcome =\n | { ok: true; auth: AuthInfo }\n | { ok: false; status: 401 | 403; challenge: string; error: string; description: string };\n\n/** Authenticate one MCP request. */\nexport async function authenticate(o: OAuthOptions, headers: Record<string, string>): Promise<AuthOutcome> {\n const token = bearerToken(headers);\n if (!token) {\n // No credentials: a bare challenge pointing at the metadata starts the client's login flow.\n return {\n ok: false,\n status: 401,\n error: 'unauthorized',\n description: 'Authorization required',\n challenge: bearerChallenge(o.resource, { scope: o.requiredScopes }),\n };\n }\n\n let auth: AuthInfo | undefined | null;\n try {\n auth = await o.verifyToken(token, { resource: o.resource, headers });\n } catch {\n auth = undefined;\n }\n if (!auth) return deny(o, 401, 'invalid_token', 'The access token is invalid');\n if (auth.expiresAt !== undefined && auth.expiresAt * 1000 <= Date.now()) {\n return deny(o, 401, 'invalid_token', 'The access token has expired');\n }\n const missing = missingScopes(auth, o.requiredScopes);\n if (missing.length) {\n // Keep the scopes already granted in the challenge, so re-authorizing never drops access.\n const scope = [...new Set([...(auth.scopes ?? []), ...(o.requiredScopes ?? [])])];\n return deny(o, 403, 'insufficient_scope', `Missing scope: ${missing.join(' ')}`, scope);\n }\n return { ok: true, auth: { ...auth, scopes: auth.scopes ?? [] } };\n}\n\n/** A failed AuthOutcome with an RFC 6750 error challenge. */\nexport function deny(\n o: OAuthOptions,\n status: 401 | 403,\n error: string,\n description: string,\n scope?: string[],\n): AuthOutcome & { ok: false } {\n return {\n ok: false,\n status,\n error,\n description,\n challenge: bearerChallenge(o.resource, { error, errorDescription: description, scope }),\n };\n}\n\nexport function missingScopes(auth: AuthInfo | undefined, required: string[] | undefined): string[] {\n if (!required?.length) return [];\n const granted = new Set(auth?.scopes ?? []);\n return required.filter((s) => !granted.has(s));\n}\n\n/** Parse a space-separated `scope` string or an array of scopes. */\nexport function parseScopes(value: unknown): string[] {\n if (Array.isArray(value)) return value.map(String);\n if (typeof value === 'string') return value.split(' ').filter(Boolean);\n return [];\n}\n","import { authenticate, deny, missingScopes, protectedResourceMetadataPaths, validateOAuthOptions } from './oauth.js';\nimport type { McpServerOptions, McpToolDefinition, ToolContext, ToolResult } from './types.js';\n\nexport const SUPPORTED_PROTOCOL_VERSIONS = ['2025-11-25', '2025-06-18', '2025-03-26', '2024-11-05'];\nexport const LATEST_PROTOCOL_VERSION = SUPPORTED_PROTOCOL_VERSIONS[0] as string;\n\ntype JsonRpcId = string | number | null;\ninterface JsonRpcRequest {\n jsonrpc: '2.0';\n id?: JsonRpcId;\n method: string;\n params?: Record<string, unknown>;\n}\nexport interface JsonRpcResponse {\n jsonrpc: '2.0';\n id: JsonRpcId;\n result?: unknown;\n error?: { code: number; message: string; data?: unknown };\n}\n\nconst PARSE_ERROR = -32700;\nconst INVALID_REQUEST = -32600;\nconst METHOD_NOT_FOUND = -32601;\nconst INVALID_PARAMS = -32602;\n\nconst ok = (id: JsonRpcId, result: unknown): JsonRpcResponse => ({ jsonrpc: '2.0', id, result });\nconst fail = (id: JsonRpcId, code: number, message: string): JsonRpcResponse => ({\n jsonrpc: '2.0',\n id,\n error: { code, message },\n});\n\n/** Minimal HTTP request/response shapes, so each adapter can bridge its own framework. */\nexport interface McpHttpRequest {\n method: string;\n headers: Record<string, string>;\n /** Parsed JSON body, or the raw string. */\n body: unknown;\n}\nexport interface McpHttpResponse {\n status: number;\n headers: Record<string, string>;\n body?: string;\n}\n\n/**\n * A stateless MCP server speaking the Streamable HTTP transport (JSON responses).\n * Holds the tool registry and answers JSON-RPC messages.\n */\nexport class McpServer {\n private readonly tools = new Map<string, McpToolDefinition>();\n private loaders: (() => void | Promise<void>)[] = [];\n\n constructor(readonly options: McpServerOptions) {\n if (options.oauth) validateOAuthOptions(options.oauth);\n }\n\n /** Register a tool. Throws on duplicate names. */\n addTool(tool: McpToolDefinition): this {\n if (this.tools.has(tool.name)) {\n throw new Error(`mcp-expose: duplicate tool name \"${tool.name}\". Set an explicit \\`name\\`.`);\n }\n this.tools.set(tool.name, tool);\n return this;\n }\n\n /** Register a function that adds tools lazily, right before the first MCP request is served. */\n onLoad(loader: () => void | Promise<void>): this {\n this.loaders.push(loader);\n return this;\n }\n\n async listTools(): Promise<McpToolDefinition[]> {\n await this.load();\n return [...this.tools.values()];\n }\n\n private async load() {\n if (!this.loaders.length) return;\n const loaders = this.loaders;\n this.loaders = [];\n for (const l of loaders) await l();\n }\n\n /** Handle one JSON-RPC message (or a batch). Returns null when no response is due. */\n async handleMessage(message: unknown, ctx: ToolContext): Promise<JsonRpcResponse | JsonRpcResponse[] | null> {\n if (Array.isArray(message)) {\n const results = (await Promise.all(message.map((m) => this.handleOne(m, ctx)))).filter(\n (r): r is JsonRpcResponse => r !== null,\n );\n return results.length ? results : null;\n }\n return this.handleOne(message, ctx);\n }\n\n private async handleOne(message: unknown, ctx: ToolContext): Promise<JsonRpcResponse | null> {\n const msg = message as Partial<JsonRpcRequest>;\n if (!msg || typeof msg !== 'object' || msg.jsonrpc !== '2.0') {\n return fail(null, INVALID_REQUEST, 'Invalid JSON-RPC message');\n }\n // Responses from the client or notifications: acknowledge, no reply.\n if (typeof msg.method !== 'string' || msg.id === undefined) return null;\n const id = msg.id;\n const params = msg.params ?? {};\n\n await this.load();\n switch (msg.method) {\n case 'initialize': {\n const requested = params.protocolVersion as string | undefined;\n const protocolVersion =\n requested && SUPPORTED_PROTOCOL_VERSIONS.includes(requested) ? requested : LATEST_PROTOCOL_VERSION;\n return ok(id, {\n protocolVersion,\n capabilities: { tools: { listChanged: false } },\n serverInfo: { name: this.options.name, version: this.options.version ?? '1.0.0' },\n ...(this.options.instructions ? { instructions: this.options.instructions } : {}),\n });\n }\n case 'ping':\n return ok(id, {});\n case 'tools/list':\n return ok(id, {\n tools: [...this.tools.values()].map((t) => ({\n name: t.name,\n ...(t.title ? { title: t.title } : {}),\n description: t.description,\n inputSchema: t.inputSchema,\n ...(t.annotations ? { annotations: t.annotations } : {}),\n })),\n });\n case 'tools/call': {\n const tool = this.tools.get(params.name as string);\n if (!tool) return fail(id, INVALID_PARAMS, `Unknown tool: ${String(params.name)}`);\n return ok(id, await this.callTool(tool, (params.arguments ?? {}) as Record<string, unknown>, ctx));\n }\n case 'resources/list':\n return ok(id, { resources: [] });\n case 'prompts/list':\n return ok(id, { prompts: [] });\n default:\n return fail(id, METHOD_NOT_FOUND, `Method not found: ${msg.method}`);\n }\n }\n\n private async callTool(\n tool: McpToolDefinition,\n args: Record<string, unknown>,\n ctx: ToolContext,\n ): Promise<ToolResult> {\n try {\n if (tool.validate) {\n const v = await tool.validate(args);\n if (!v.ok) return { isError: true, content: [{ type: 'text', text: `Invalid arguments: ${v.message}` }] };\n args = v.value as Record<string, unknown>;\n }\n return await tool.handler(args, ctx);\n } catch (err) {\n // Tool failures are reported in-band so the model can react to them.\n return { isError: true, content: [{ type: 'text', text: `Tool error: ${(err as Error).message}` }] };\n }\n }\n\n /** Check the Origin header (DNS rebinding protection). */\n isOriginAllowed(origin: string | undefined): boolean {\n if (!origin) return true;\n const allowed = this.options.allowedOrigins;\n if (allowed === '*') return true;\n return (allowed ?? []).includes(origin);\n }\n\n /**\n * Paths on which adapters serve the OAuth Protected Resource Metadata\n * (empty when `oauth` is not configured). They must be public: no auth middleware.\n */\n get oauthMetadataPaths(): string[] {\n return this.options.oauth ? protectedResourceMetadataPaths(this.options.oauth.resource) : [];\n }\n\n /** The OAuth Protected Resource Metadata document (RFC 9728). */\n async protectedResourceMetadata(): Promise<Record<string, unknown>> {\n const o = this.options.oauth;\n if (!o) throw new Error('mcp-expose: the oauth option is not configured');\n await this.load();\n const scopes = o.scopesSupported ?? [\n ...new Set([...(o.requiredScopes ?? []), ...[...this.tools.values()].flatMap((t) => t.scopes ?? [])]),\n ];\n return {\n resource: o.resource,\n authorization_servers: o.authorizationServers,\n ...(scopes.length ? { scopes_supported: scopes } : {}),\n bearer_methods_supported: ['header'],\n ...(o.resourceName ? { resource_name: o.resourceName } : {}),\n ...(o.resourceDocumentation ? { resource_documentation: o.resourceDocumentation } : {}),\n ...o.metadata,\n };\n }\n\n /** HTTP handler for the metadata paths. Public and CORS-enabled so browser-based clients can read it. */\n async handleMetadataHttp(req: Pick<McpHttpRequest, 'method'>): Promise<McpHttpResponse> {\n const cors = { 'access-control-allow-origin': '*', 'access-control-allow-methods': 'GET, OPTIONS' };\n const method = req.method.toUpperCase();\n if (!this.options.oauth) return { status: 404, headers: {} };\n if (method === 'OPTIONS') return { status: 204, headers: { ...cors, 'access-control-allow-headers': '*' } };\n if (method !== 'GET' && method !== 'HEAD') return { status: 405, headers: { allow: 'GET, OPTIONS' } };\n const body = JSON.stringify(await this.protectedResourceMetadata());\n return {\n status: 200,\n headers: { ...cors, 'content-type': 'application/json', 'cache-control': 'public, max-age=3600' },\n body: method === 'HEAD' ? undefined : body,\n };\n }\n\n /**\n * Framework-neutral Streamable HTTP handler. Adapters convert their request\n * into McpHttpRequest, call this, and write the McpHttpResponse back.\n */\n async handleHttp(req: McpHttpRequest, ctx: ToolContext): Promise<McpHttpResponse> {\n const json = { 'content-type': 'application/json' };\n if (!this.isOriginAllowed(req.headers.origin)) {\n return { status: 403, headers: json, body: JSON.stringify(fail(null, INVALID_REQUEST, 'Origin not allowed')) };\n }\n const oauth = this.options.oauth;\n if (oauth) {\n const outcome = await authenticate(oauth, req.headers);\n if (!outcome.ok) return authError(outcome);\n ctx = { ...ctx, auth: outcome.auth };\n }\n if (req.method.toUpperCase() !== 'POST') {\n // Stateless server: no standalone SSE stream and no sessions to delete.\n return { status: 405, headers: { allow: 'POST' } };\n }\n let body = req.body;\n if (typeof body === 'string' || body instanceof Uint8Array) {\n try {\n body = JSON.parse(typeof body === 'string' ? body : new TextDecoder().decode(body));\n } catch {\n return { status: 400, headers: json, body: JSON.stringify(fail(null, PARSE_ERROR, 'Parse error')) };\n }\n }\n if (oauth) {\n // Per-tool scopes: answer 403 so the client can ask the user for more access (step-up).\n await this.load();\n const calls = (Array.isArray(body) ? body : [body]) as Partial<JsonRpcRequest>[];\n for (const m of calls) {\n if (m?.method !== 'tools/call') continue;\n const tool = this.tools.get(m.params?.name as string);\n const missing = missingScopes(ctx.auth, tool?.scopes);\n if (missing.length) {\n // Include the scopes already granted, so re-authorizing never drops access the user gave before.\n const scope = [\n ...new Set([...(ctx.auth?.scopes ?? []), ...(oauth.requiredScopes ?? []), ...(tool?.scopes ?? [])]),\n ];\n return authError(\n deny(oauth, 403, 'insufficient_scope', `Tool \"${tool!.name}\" needs scope: ${missing.join(' ')}`, scope),\n );\n }\n }\n }\n const response = await this.handleMessage(body, ctx);\n if (response === null) return { status: 202, headers: {} };\n return { status: 200, headers: json, body: JSON.stringify(response) };\n }\n}\n\nfunction authError(o: { status: number; challenge: string; error: string; description: string }): McpHttpResponse {\n return {\n status: o.status,\n headers: { 'content-type': 'application/json', 'www-authenticate': o.challenge },\n body: JSON.stringify({ error: o.error, error_description: o.description }),\n };\n}\n","// Internal: route discovery for Express 3, 4 and 5 (not part of the public API).\nimport { markerOf } from '../core/marker.js';\nimport { joinPaths } from '../core/route-tool.js';\nimport type { RouteToolOptions } from '../core/types.js';\n\ninterface ExpressLayer {\n route?: { path: string | string[]; methods: Record<string, boolean>; stack: { handle: unknown }[] };\n name?: string;\n handle?: { stack?: ExpressLayer[] };\n regexp?: RegExp & { fast_slash?: boolean };\n}\n\n/** Structural type for an Express 4/5 app (or Router). Kept loose on purpose. */\nexport interface ExpressAppLike {\n all: Function;\n router?: unknown;\n _router?: unknown;\n /** Express 3 route table: `{ get: [{ path, method, callbacks }], ... }`. */\n routes?: unknown;\n}\n\ntype Express3Routes = Record<string, { path: string | RegExp; method: string; callbacks: unknown[] }[]>;\n\n/** Express 3 keeps app-level routes in `app.routes`, keyed by method. */\nfunction collectExpress3(routes: Express3Routes, out: Found[]) {\n for (const list of Object.values(routes)) {\n for (const r of list) {\n const marker = r.callbacks.map(markerOf).find(Boolean);\n if (marker && typeof r.path === 'string')\n out.push({ method: r.method.toUpperCase(), path: r.path, options: marker });\n }\n }\n}\n\nfunction stackOf(app: ExpressAppLike): ExpressLayer[] {\n // Express 4 keeps the router on `_router`; its `app.router` getter throws.\n // Express 5 exposes `app.router`.\n let router = app._router as { stack?: ExpressLayer[] } | undefined;\n if (!router) {\n try {\n router = app.router as typeof router;\n } catch {\n router = undefined;\n }\n }\n return router?.stack ?? [];\n}\n\n/** Recover an Express 4 mount path from a layer regexp (best effort). */\nfunction express4Prefix(layer: ExpressLayer): string | undefined {\n const re = layer.regexp;\n if (!re) return undefined;\n if (re.fast_slash) return '';\n const m = /^\\/\\^((?:\\\\[.*+?^${}()|[\\]\\\\/]|[^.*+?^${}()|[\\]\\\\/])*)\\\\\\/\\?\\(\\?=\\\\\\/\\|\\$\\)\\/i?$/.exec(re.toString());\n return m ? (m[1] as string).replace(/\\\\(.)/g, '$1') : undefined;\n}\n\nexport interface Found {\n method: string;\n path: string;\n options: RouteToolOptions;\n}\n\nfunction collect(\n stack: ExpressLayer[],\n prefix: string,\n routers: Map<unknown, string>,\n out: Found[],\n unresolved: string[],\n) {\n for (const layer of stack) {\n if (layer.route) {\n const marker = layer.route.stack.map((l) => markerOf(l.handle)).find(Boolean);\n if (!marker) continue;\n const paths = Array.isArray(layer.route.path) ? layer.route.path : [layer.route.path];\n for (const method of Object.keys(layer.route.methods).filter((m) => m !== '_all')) {\n for (const p of paths) out.push({ method: method.toUpperCase(), path: joinPaths(prefix, p), options: marker });\n }\n } else if (layer.handle?.stack) {\n const known = routers.get(layer.handle);\n const sub = known ?? express4Prefix(layer);\n if (sub === undefined) {\n // Only complain if this router actually contains MCP tools.\n const inner: Found[] = [];\n collect(layer.handle.stack, '', routers, inner, unresolved);\n if (inner.length) unresolved.push(...inner.map((f) => `${f.method} ${f.path}`));\n continue;\n }\n collect(layer.handle.stack, joinPaths(prefix, sub), routers, out, unresolved);\n }\n }\n}\n\n/** Find every route marked with `mcpTool()` in an Express app. */\nexport function discoverExpressRoutes(app: ExpressAppLike, routers: Record<string, unknown> = {}): Found[] {\n const stack = stackOf(app);\n const byRouter = new Map(Object.entries(routers).map(([prefix, r]) => [r, prefix]));\n const out: Found[] = [];\n const unresolved: string[] = [];\n collect(stack, '', byRouter, out, unresolved);\n if (!stack.length && app.routes && typeof app.routes === 'object') collectExpress3(app.routes as Express3Routes, out);\n if (unresolved.length) {\n throw new Error(\n `mcp-expose: cannot determine the mount path of a router containing MCP tools (${unresolved.join(', ')}). ` +\n `Pass it via the \\`routers\\` option, e.g. mountMcp(app, { routers: { '/api': apiRouter } }).`,\n );\n }\n return out;\n}\n","import type { IncomingMessage, ServerResponse } from 'node:http';\nimport { createFetchDispatcher } from '../core/dispatch.js';\nimport { mark } from '../core/marker.js';\nimport { loopbackBaseUrl, normalizeHeaders, readRawBody, writeNodeResponse } from '../core/node.js';\nimport { createRouteTool } from '../core/route-tool.js';\nimport { McpServer } from '../core/server.js';\nimport type { McpServerOptions, McpToolDefinition, RouteToolOptions, ToolContext } from '../core/types.js';\nimport { discoverExpressRoutes, type ExpressAppLike } from './discover.js';\n\nexport type { ExpressAppLike } from './discover.js';\n\ntype Req = IncomingMessage & { body?: unknown; ip?: string };\ntype Next = (err?: unknown) => void;\ntype Middleware = (req: Req, res: ServerResponse, next: Next) => void;\n\n/**\n * Mark an Express route as an MCP tool. Put it first in the route's handler list:\n *\n * app.get('/users/:id', mcpTool({ description: 'Get a user' }), auth, getUser)\n *\n * It is a no-op at request time; it only carries metadata.\n */\nexport function mcpTool(options: RouteToolOptions = {}): Middleware {\n return mark<Middleware>((_req, _res, next) => next(), options);\n}\n\nexport interface ExpressMcpOptions extends McpServerOptions {\n /** Path of the MCP endpoint. @default '/mcp' */\n path?: string;\n /**\n * Base URL for internal calls. Defaults to the address/port the MCP request\n * arrived on (loopback), so no configuration is needed in most setups.\n */\n baseUrl?: string;\n /**\n * Prefixes of mounted routers, e.g. `{ '/api': apiRouter }`. Express 5 does\n * not keep mount paths, so routers mounted with a path must be listed here.\n * (Express 4 mount paths are detected automatically.)\n */\n routers?: Record<string, unknown>;\n /** Routes to expose without touching the route definitions. */\n routes?: (RouteToolOptions & { method: string; path: string })[];\n /** Extra hand-written tools (see `defineTool`). */\n tools?: McpToolDefinition[];\n /**\n * Middleware run before the MCP endpoint, e.g. your auth middleware, to\n * protect tools/list as well as tools/call. (With the `oauth` option you\n * don't need this: mcp-expose checks the token itself.)\n */\n middleware?: Middleware[];\n}\n\n/**\n * Add an MCP endpoint (default `POST /mcp`) to an Express app. Tools are\n * discovered lazily on the first MCP request, so call this before or after\n * defining routes. Returns the underlying McpServer.\n */\nexport function mountMcp(app: ExpressAppLike, options: ExpressMcpOptions): McpServer {\n const path = options.path ?? '/mcp';\n const server = new McpServer(options);\n const dispatch = createFetchDispatcher({\n baseUrl: options.baseUrl ?? ((ctx) => loopbackBaseUrl(ctx.raw as IncomingMessage)),\n });\n\n for (const t of options.tools ?? []) server.addTool(t);\n server.onLoad(() => {\n const found = discoverExpressRoutes(app, options.routers);\n for (const r of options.routes ?? []) found.push({ method: r.method, path: r.path, options: r });\n for (const f of found)\n server.addTool(createRouteTool({ method: f.method, path: f.path }, f.options, dispatch, options));\n });\n\n const handler: Middleware = (req, res, next) => {\n (async () => {\n const body = req.method === 'POST' && req.body === undefined ? await readRawBody(req) : req.body;\n const ctx: ToolContext = {\n headers: normalizeHeaders(req.headers),\n clientIp: req.ip ?? req.socket.remoteAddress,\n raw: req,\n };\n const out = await server.handleHttp({ method: req.method ?? 'POST', headers: ctx.headers, body }, ctx);\n writeNodeResponse(res, out);\n })().catch(next);\n };\n const all = app.all as (path: string, ...h: Middleware[]) => void;\n all.call(app, path, ...(options.middleware ?? []), handler);\n\n // OAuth Protected Resource Metadata: public, never behind `middleware`.\n const metadata: Middleware = (req, res, next) => {\n server\n .handleMetadataHttp({ method: req.method ?? 'GET' })\n .then((out) => writeNodeResponse(res, out))\n .catch(next);\n };\n for (const p of server.oauthMetadataPaths) all.call(app, p, metadata);\n return server;\n}\n"],"mappings":";;;;AAEO,SAASA,SAASC,MAAcC,KAAyC;AAC9E,QAAMC,MAAM,IAAIC,IAAIH,KAAKI,QAAQ,QAAQ,EAAA,IAAMH,IAAII,IAAI;AACvD,aAAW,CAACC,GAAGC,CAAAA,KAAMC,OAAOC,QAAQR,IAAIS,KAAK,GAAG;AAC9C,eAAWC,QAAQC,MAAMC,QAAQN,CAAAA,IAAKA,IAAI;MAACA;MAAIL,KAAIY,aAAaC,OAAOT,GAAGK,IAAAA;EAC5E;AACA,SAAOT,IAAIc,SAAQ;AACrB;AANgBjB;AAwBT,SAASkB,sBAAsBC,MAA4B;AAChE,QAAMC,UAAUD,K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AAKxC,KAAKC,MAAAA,GAAUP,QAAQ+C,cAAc,CAAA,GAAKlB,OAAAA;AAGnD,QAAMmB,WAAuB,wBAAClB,KAAKC,KAAK3B,SAAAA;AACtCI,WACGyC,mBAAmB;MAAEvB,QAAQI,IAAIJ,UAAU;IAAM,CAAA,EACjDwB,KAAK,CAACT,QAAQE,kBAAkBZ,KAAKU,GAAAA,CAAAA,EACrCG,MAAMxC,IAAAA;EACX,GAL6B;AAM7B,aAAW+C,KAAK3C,OAAO4C,mBAAoBP,KAAIC,KAAKxC,KAAK6C,GAAGH,QAAAA;AAC5D,SAAOxC;AACT;AAvCgBH;","names":["buildUrl","base","req","url","URL","replace","path","k","v","Object","entries","query","item","Array","isArray","searchParams","append","toString","createFetchDispatcher","opts","doFetch","fetch","ctx","baseUrl","res","method","headers","body","undefined","JSON","stringify","signal","AbortSignal","timeout","timeoutMs","forEach","status","text","MCP_TOOL","Symbol","for","markerOf","fn","undefined","mark","opts","Object","defineProperty","value","enumerable","normalizeHeaders","headers","out","k","v","Object","entries","undefined","toLowerCase","Array","isArray","join","String","readRawBody","req","limit","Promise","resolve","reject","size","chunks","on","c","length","Error","destroy","push","Buffer","concat","toString","loopbackBaseUrl","socket","port","localPort","encrypted","bound","server","address","host","localAddress","startsWith","slice","includes","writeNodeResponse","res","statusCode","status","setHeader","end","body","isStandard","s","toJsonSchema","schema","undefined","out","jsonSchema","input","target","toJSONSchema","Error","$schema","_ignored","rest","toValidator","validate","value","result","issues","ok","message","map","i","path","p","String","key","join","checkRequired","args","missing","required","filter","k","length","DEFAULT_FORWARD_HEADERS","BODY_METHODS","Set","PARAM_RE","pathParams","path","matchAll","map","m","encodeSegment","name","value","raw","String","Error","JSON","stringify","encodeURIComponent","interpolatePath","values","replace","_","k","undefined","a","b","joinPaths","parts","joined","filter","p","Boolean","join","toolNameFromRoute","method","segments","split","s","exec","sanitizeToolName","toLowerCase","slice","defaultAnnotations","readOnlyHint","openWorldHint","destructiveHint","idempotentHint","buildInputSchema","route","opts","toUpperCase","params","mapping","query","body","wholeBody","bodyMethod","has","input","schema","toJsonSchema","type","properties","required","paramSchema","description","includes","add","querySchema","v","Object","entries","bodySchema","size","splitArgs","args","pathValues","Array","isArray","responseToResult","res","maxChars","text","length","isError","status","result","content","headers","parsed","parse","structuredContent","pickForwardHeaders","ctx","names","out","createRouteTool","dispatch","serverOpts","forward","forwardHeaders","maxResponseChars","title","inputSchema","annotations","scopes","validate","toValidator","handler","missing","checkRequired","accept","clientIp","err","message","PROTECTED_RESOURCE_WELL_KNOWN","protectedResourceMetadataPaths","resource","pathname","URL","replace","protectedResourceMetadataUrl","href","validateOAuthOptions","o","url","Error","JSON","stringify","hash","authorizationServers","length","verifyToken","quote","v","bearerChallenge","params","parts","error","push","errorDescription","scope","join","bearerToken","headers","m","exec","authorization","authenticate","token","ok","status","description","challenge","requiredScopes","auth","undefined","deny","expiresAt","Date","now","missing","missingScopes","Set","scopes","required","granted","filter","s","has","SUPPORTED_PROTOCOL_VERSIONS","LATEST_PROTOCOL_VERSION","PARSE_ERROR","INVALID_REQUEST","METHOD_NOT_FOUND","INVALID_PARAMS","ok","id","result","jsonrpc","fail","code","message","error","McpServer","tools","Map","loaders","options","oauth","validateOAuthOptions","addTool","tool","has","name","Error","set","onLoad","loader","push","listTools","load","values","length","l","handleMessage","ctx","Array","isArray","results","Promise","all","map","m","handleOne","filter","r","msg","method","undefined","params","requested","protocolVersion","includes","capabilities","listChanged","serverInfo","version","instructions","t","title","description","inputSchema","annotations","get","String","callTool","arguments","resources","prompts","args","validate","v","isError","content","type","text","value","handler","err","isOriginAllowed","origin","allowed","allowedOrigins","oauthMetadataPaths","protectedResourceMetadataPaths","resource","protectedResourceMetadata","o","scopes","scopesSupported","Set","requiredScopes","flatMap","authorization_servers","authorizationServers","scopes_supported","bearer_methods_supported","resourceName","resource_name","resourceDocumentation","resource_documentation","metadata","handleMetadataHttp","req","cors","toUpperCase","status","headers","allow","body","JSON","stringify","handleHttp","json","outcome","authenticate","authError","auth","Uint8Array","parse","TextDecoder","decode","calls","missing","missingScopes","scope","deny","join","response","challenge","error_description","collectExpress3","routes","out","list","Object","values","r","marker","callbacks","map","markerOf","find","Boolean","path","push","method","toUpperCase","options","stackOf","app","router","_router","undefined","stack","express4Prefix","layer","re","regexp","fast_slash","m","exec","toString","replace","collect","prefix","routers","unresolved","route","l","handle","paths","Array","isArray","keys","methods","filter","p","joinPaths","known","get","sub","inner","length","f","discoverExpressRoutes","byRouter","Map","entries","Error","join","mcpTool","options","mark","_req","_res","next","mountMcp","app","path","server","McpServer","dispatch","createFetchDispatcher","baseUrl","ctx","loopbackBaseUrl","raw","t","tools","addTool","onLoad","found","discoverExpressRoutes","routers","r","routes","push","method","f","createRouteTool","handler","req","res","body","undefined","readRawBody","headers","normalizeHeaders","clientIp","ip","socket","remoteAddress","out","handleHttp","writeNodeResponse","catch","all","call","middleware","metadata","handleMetadataHttp","then","p","oauthMetadataPaths"]}
|