@j0hanz/filesystem-mcp 2.1.1 → 2.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/cli-help.js +1 -1
- package/dist/cli-help.js.map +1 -1
- package/dist/core/cursor.d.ts +37 -0
- package/dist/core/cursor.d.ts.map +1 -1
- package/dist/core/cursor.js +34 -0
- package/dist/core/cursor.js.map +1 -1
- package/dist/tools/batch.d.ts +20 -0
- package/dist/tools/batch.d.ts.map +1 -1
- package/dist/tools/batch.js +17 -0
- package/dist/tools/batch.js.map +1 -1
- package/dist/tools/create.js +3 -3
- package/dist/tools/create.js.map +1 -1
- package/dist/tools/define.d.ts +6 -0
- package/dist/tools/define.d.ts.map +1 -1
- package/dist/tools/define.js +1 -29
- package/dist/tools/define.js.map +1 -1
- package/dist/tools/delete-file.d.ts.map +1 -1
- package/dist/tools/delete-file.js +4 -1
- package/dist/tools/delete-file.js.map +1 -1
- package/dist/tools/edit.d.ts.map +1 -1
- package/dist/tools/edit.js +2 -1
- package/dist/tools/edit.js.map +1 -1
- package/dist/tools/list.d.ts.map +1 -1
- package/dist/tools/list.js +54 -66
- package/dist/tools/list.js.map +1 -1
- package/dist/tools/move.d.ts.map +1 -1
- package/dist/tools/move.js +5 -2
- package/dist/tools/move.js.map +1 -1
- package/dist/tools/read.d.ts.map +1 -1
- package/dist/tools/read.js +2 -1
- package/dist/tools/read.js.map +1 -1
- package/dist/tools/replace-in-files.d.ts.map +1 -1
- package/dist/tools/replace-in-files.js +4 -2
- package/dist/tools/replace-in-files.js.map +1 -1
- package/dist/tools/search-content.d.ts.map +1 -1
- package/dist/tools/search-content.js +58 -87
- package/dist/tools/search-content.js.map +1 -1
- package/dist/tools/search-files.d.ts.map +1 -1
- package/dist/tools/search-files.js +41 -59
- package/dist/tools/search-files.js.map +1 -1
- package/dist/tools/stat.d.ts.map +1 -1
- package/dist/tools/stat.js +2 -1
- package/dist/tools/stat.js.map +1 -1
- package/dist/{http-policy.d.ts → transport/http-policy.d.ts} +4 -4
- package/dist/transport/http-policy.d.ts.map +1 -0
- package/dist/{http-policy.js → transport/http-policy.js} +13 -9
- package/dist/transport/http-policy.js.map +1 -0
- package/dist/transport/http.d.ts.map +1 -1
- package/dist/transport/http.js +12 -2
- package/dist/transport/http.js.map +1 -1
- package/dist/transport/shared.d.ts +14 -0
- package/dist/transport/shared.d.ts.map +1 -1
- package/dist/transport/shared.js +13 -5
- package/dist/transport/shared.js.map +1 -1
- package/dist/transport/stdio.d.ts.map +1 -1
- package/dist/transport/stdio.js +58 -20
- package/dist/transport/stdio.js.map +1 -1
- package/package.json +1 -1
- package/dist/http-policy.d.ts.map +0 -1
- package/dist/http-policy.js.map +0 -1
package/dist/transport/stdio.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ProtocolErrorCode } from '@modelcontextprotocol/server';
|
|
2
2
|
import { serveStdio, StdioServerTransport, } from '@modelcontextprotocol/server/stdio';
|
|
3
3
|
import { fileURLToPath } from 'node:url';
|
|
4
4
|
import { formatUnknownErrorMessage } from '../core/errors.js';
|
|
@@ -6,7 +6,7 @@ import { Logger } from '../core/observability.js';
|
|
|
6
6
|
import { PathGuard } from '../core/path.js';
|
|
7
7
|
import { createWatcherRegistry } from '../core/watcher-registry.js';
|
|
8
8
|
import { createServer } from '../server.js';
|
|
9
|
-
import { isStructurallyValidListen, jsonRpcRequestId, listenSubscriptionUris, prepareListenWatchers, } from './shared.js';
|
|
9
|
+
import { isStructurallyValidListen, jsonRpcError, jsonRpcRequestId, listenSubscriptionUris, prepareListenWatchers, } from './shared.js';
|
|
10
10
|
// Read through a function, not `state.cancelled` directly: a cancellation lands
|
|
11
11
|
// between the checks, and control-flow narrowing from the first one would
|
|
12
12
|
// otherwise make the second read a compile-time constant.
|
|
@@ -91,6 +91,7 @@ export async function seedRootsFromClient(ctx) {
|
|
|
91
91
|
* rejection, graceful completion, or connection close.
|
|
92
92
|
*/
|
|
93
93
|
export function startServer(options, config = {}) {
|
|
94
|
+
let closed = false;
|
|
94
95
|
let activeCtx;
|
|
95
96
|
const pathGuard = new PathGuard(options, true);
|
|
96
97
|
let pathGuardReady;
|
|
@@ -109,6 +110,14 @@ export function startServer(options, config = {}) {
|
|
|
109
110
|
era,
|
|
110
111
|
...(config.apiKey !== undefined ? { apiKey: config.apiKey } : {}),
|
|
111
112
|
});
|
|
113
|
+
if (closed) {
|
|
114
|
+
// The connection went away while this instance was being built.
|
|
115
|
+
// `cleanupConnection` already ran and its `closed` guard makes every later
|
|
116
|
+
// call a no-op, so publishing `c` would strand it: nothing would dispose
|
|
117
|
+
// it. Return it unactivated — the wire is gone, so nothing serves it.
|
|
118
|
+
c.disposeRuntimeState();
|
|
119
|
+
return c.mcp;
|
|
120
|
+
}
|
|
112
121
|
activeCtx = c;
|
|
113
122
|
if (era === 'legacy') {
|
|
114
123
|
// Fires when the client's `notifications/initialized` lands. Safe to own:
|
|
@@ -152,6 +161,39 @@ export function startServer(options, config = {}) {
|
|
|
152
161
|
for (const uri of state.acquiredUris)
|
|
153
162
|
registry.release(uri);
|
|
154
163
|
};
|
|
164
|
+
const cleanupConnection = () => {
|
|
165
|
+
if (closed)
|
|
166
|
+
return;
|
|
167
|
+
closed = true;
|
|
168
|
+
for (const state of listens.values())
|
|
169
|
+
state.cancelled = true;
|
|
170
|
+
listens.clear();
|
|
171
|
+
registry.destroy();
|
|
172
|
+
const ctx = activeCtx;
|
|
173
|
+
activeCtx = undefined;
|
|
174
|
+
// Both steps below are guarded separately, and for the same reason: this
|
|
175
|
+
// also runs from `wire.onclose`, where a throw is an uncaught exception on
|
|
176
|
+
// the stdin close event rather than a rejected close(). One shared catch
|
|
177
|
+
// would let a failed disposal skip the unref and strand the process.
|
|
178
|
+
try {
|
|
179
|
+
ctx?.disposeRuntimeState();
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
/* idempotent — disposeRuntimeState guards cleanedUp */
|
|
183
|
+
}
|
|
184
|
+
try {
|
|
185
|
+
// The SDK's transport close pauses stdin only when no other 'data'
|
|
186
|
+
// listener is left, so a fatal read error (a ReadBuffer overflow) tears
|
|
187
|
+
// the connection down but leaves the handle referenced and this process
|
|
188
|
+
// alive with nothing left to serve. Unref rather than pause: a listener
|
|
189
|
+
// the SDK still owns keeps receiving data, the loop just stops being held
|
|
190
|
+
// open for it.
|
|
191
|
+
process.stdin.unref();
|
|
192
|
+
}
|
|
193
|
+
catch {
|
|
194
|
+
/* a file-backed stdin is an `fs.ReadStream`, which has no `unref` */
|
|
195
|
+
}
|
|
196
|
+
};
|
|
155
197
|
const send = wire.send.bind(wire);
|
|
156
198
|
wire.send = async (message) => {
|
|
157
199
|
if ('id' in message && ('result' in message || 'error' in message)) {
|
|
@@ -168,6 +210,15 @@ export function startServer(options, config = {}) {
|
|
|
168
210
|
Logger.error('[Stdio] serve error:', formatUnknownErrorMessage(error));
|
|
169
211
|
},
|
|
170
212
|
});
|
|
213
|
+
const onclose = wire.onclose;
|
|
214
|
+
wire.onclose = () => {
|
|
215
|
+
try {
|
|
216
|
+
cleanupConnection();
|
|
217
|
+
}
|
|
218
|
+
finally {
|
|
219
|
+
onclose?.();
|
|
220
|
+
}
|
|
221
|
+
};
|
|
171
222
|
// Wrap the callback `serveStdio` just installed. Listen requests are admitted
|
|
172
223
|
// in order so two requests naming the same URI cannot race the registry's
|
|
173
224
|
// ref-count. Cancellation is serialized with pending admission by request ID:
|
|
@@ -228,11 +279,7 @@ export function startServer(options, config = {}) {
|
|
|
228
279
|
const prepared = await prepareListenWatchers(message, pathGuard, registry, sink);
|
|
229
280
|
if (!prepared.ok) {
|
|
230
281
|
listens.delete(id);
|
|
231
|
-
await wire.send(
|
|
232
|
-
jsonrpc: JSONRPC_VERSION,
|
|
233
|
-
id,
|
|
234
|
-
error: { code: ProtocolErrorCode.InvalidParams, message: prepared.message },
|
|
235
|
-
});
|
|
282
|
+
await wire.send(jsonRpcError(ProtocolErrorCode.InvalidParams, prepared.message, id));
|
|
236
283
|
return;
|
|
237
284
|
}
|
|
238
285
|
state.acquiredUris = prepared.acquiredUris;
|
|
@@ -250,11 +297,7 @@ export function startServer(options, config = {}) {
|
|
|
250
297
|
// The gate swallowed the message, so nothing downstream will answer it.
|
|
251
298
|
// Without this the client waits out its whole request timeout.
|
|
252
299
|
await wire
|
|
253
|
-
.send(
|
|
254
|
-
jsonrpc: JSONRPC_VERSION,
|
|
255
|
-
id,
|
|
256
|
-
error: { code: ProtocolErrorCode.InternalError, message: failure.message },
|
|
257
|
-
})
|
|
300
|
+
.send(jsonRpcError(ProtocolErrorCode.InternalError, failure.message, id))
|
|
258
301
|
.catch(() => {
|
|
259
302
|
/* the wire is gone; onerror above already reported it */
|
|
260
303
|
});
|
|
@@ -262,17 +305,12 @@ export function startServer(options, config = {}) {
|
|
|
262
305
|
};
|
|
263
306
|
return {
|
|
264
307
|
close: async () => {
|
|
265
|
-
for (const id of [...listens.keys()])
|
|
266
|
-
releaseListen(id);
|
|
267
|
-
registry.destroy();
|
|
268
308
|
try {
|
|
269
|
-
|
|
309
|
+
cleanupConnection();
|
|
270
310
|
}
|
|
271
|
-
|
|
272
|
-
|
|
311
|
+
finally {
|
|
312
|
+
await handle.close();
|
|
273
313
|
}
|
|
274
|
-
activeCtx = undefined;
|
|
275
|
-
await handle.close();
|
|
276
314
|
},
|
|
277
315
|
};
|
|
278
316
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stdio.js","sourceRoot":"","sources":["../../src/transport/stdio.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AAClF,OAAO,EACL,UAAU,EAEV,oBAAoB,GACrB,MAAM,oCAAoC,CAAC;AAE5C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEzC,OAAO,EAAE,yBAAyB,EAAE,MAAM,mBAAmB,CAAC;AAC9D,OAAO,E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QACnB,CAAC,CAAC;aACD,KAAK,CAAC,KAAK,EAAE,KAAc,EAAE,EAAE;YAC9B,aAAa,CAAC,EAAE,CAAC,CAAC;YAClB,MAAM,OAAO,GACX,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,yBAAyB,CAAC,KAAK,CAAC,CAAC,CAAC;YAC/E,IAAI,CAAC,OAAO,EAAE,CAAC,OAAO,CAAC,CAAC;YACxB,wEAAwE;YACxE,+DAA+D;YAC/D,MAAM,IAAI;iBACP,IAAI,CAAC;gBACJ,OAAO,EAAE,eAAe;gBACxB,EAAE;gBACF,KAAK,EAAE,EAAE,IAAI,EAAE,iBAAiB,CAAC,aAAa,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE;aAC3E,CAAC;iBACD,KAAK,CAAC,GAAG,EAAE;gBACV,yDAAyD;YAC3D,CAAC,CAAC,CAAC;QACP,CAAC,CAAC,CAAC;IACP,CAAC,CAAC;IAEF,OAAO;QACL,KAAK,EAAE,KAAK,IAAI,EAAE;YAChB,KAAK,MAAM,EAAE,IAAI,CAAC,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC;gBAAE,aAAa,CAAC,EAAE,CAAC,CAAC;YACxD,QAAQ,CAAC,OAAO,EAAE,CAAC;YACnB,IAAI,CAAC;gBACH,SAAS,EAAE,mBAAmB,EAAE,CAAC;YACnC,CAAC;YAAC,MAAM,CAAC;gBACP,uDAAuD;YACzD,CAAC;YACD,SAAS,GAAG,SAAS,CAAC;YACtB,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC;QACvB,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["// stdio hosting: the pinned-instance factory, legacy roots seeding, and the\n// gated transport that attaches listen-filter watchers before the SDK sees the\n// message.\nimport type { McpServerFactory } from '@modelcontextprotocol/server';\nimport { JSONRPC_VERSION, ProtocolErrorCode } from '@modelcontextprotocol/server';\nimport {\n serveStdio,\n type StdioServerHandle,\n StdioServerTransport,\n} from '@modelcontextprotocol/server/stdio';\n\nimport { fileURLToPath } from 'node:url';\n\nimport { formatUnknownErrorMessage } from '../core/errors.js';\nimport { Logger } from '../core/observability.js';\nimport type { ServerOptions } from '../core/path.js';\nimport { PathGuard } from '../core/path.js';\nimport { createWatcherRegistry } from '../core/watcher-registry.js';\nimport type { FilesystemServerContext } from '../server.js';\nimport { createServer } from '../server.js';\nimport type { RuntimeConfig } from './shared.js';\nimport {\n isStructurallyValidListen,\n jsonRpcRequestId,\n listenSubscriptionUris,\n prepareListenWatchers,\n} from './shared.js';\n\ninterface StdioListenState {\n acquiredUris: string[];\n cancelled: boolean;\n delivered: boolean;\n}\n\n// Read through a function, not `state.cancelled` directly: a cancellation lands\n// between the checks, and control-flow narrowing from the first one would\n// otherwise make the second read a compile-time constant.\nfunction isListenCancelled(state: StdioListenState): boolean {\n return state.cancelled;\n}\n\nfunction cancelledRequestId(message: unknown): string | number | null {\n if (typeof message !== 'object' || message === null) return null;\n const notification = message as {\n method?: unknown;\n params?: { requestId?: unknown };\n };\n if (notification.method !== 'notifications/cancelled') return null;\n const requestId = notification.params?.requestId;\n return typeof requestId === 'string' || typeof requestId === 'number' ? requestId : null;\n}\n\n/**\n * Seed the guard's allowed roots from the client's declared workspace roots.\n * Legacy-era only: push-style `roots/list` is deprecated (SEP-2577) and throws\n * on a 2026-07-28 connection, where clients pass paths as tool arguments and\n * the access-grant round-trip covers out-of-root paths instead. Every root\n * still passes `applyGrant`'s boundary and unsafe-path guards, so a client\n * cannot root-declare its way into $HOME or past FS_ROOT_BOUNDARY — a refused\n * root is skipped and its paths fail closed at validateAccess like any other.\n */\nexport async function seedRootsFromClient(ctx: FilesystemServerContext): Promise<number> {\n let roots: readonly { uri: string }[];\n try {\n // eslint-disable-next-line @typescript-eslint/no-deprecated -- legacy-era-only; the modern era never reaches the hooks that call this.\n ({ roots } = await ctx.mcp.server.listRoots());\n } catch (err: unknown) {\n // Client without the roots capability (strict-capabilities throw), or the\n // request failed — either way there is nothing to seed.\n Logger.debug('[Stdio] roots/list unavailable', { error: formatUnknownErrorMessage(err) });\n return 0;\n }\n let granted = 0;\n for (const root of roots) {\n let dir: string;\n try {\n dir = fileURLToPath(root.uri);\n } catch {\n continue; // not a file:// root\n }\n try {\n if (await ctx.pathGuard.applyGrant(dir)) granted += 1;\n else Logger.debug('[Stdio] client root refused by grant policy', { dir });\n } catch (err: unknown) {\n Logger.debug('[Stdio] client root grant failed', {\n dir,\n error: formatUnknownErrorMessage(err),\n });\n }\n }\n // No list-changed notification follows a grant. Allowed roots were once listed\n // as concrete resources, and are not any more: the file template's `list()`\n // returns nothing and the other two lists never read the roots at all (see\n // resources.ts). Notifying bought the client a re-fetch of an unchanged list.\n if (granted > 0) {\n Logger.info(`[Stdio] allowed ${granted} client-declared workspace root(s)`);\n }\n return granted;\n}\n\n/**\n * Serve filesystem-mcp over stdio using modern protocol revision 2026-07-28.\n *\n * The SDK's `StdioListenRouter` acknowledges `subscriptions/listen` and routes\n * the pinned instance's outbound change notifications onto the matching\n * streams, but it exposes no listen-filter hook, so nothing attaches the\n * filesystem watcher that would produce those notifications. `serveStdio`'s\n * `transport` option is the seam: it installs its own `onmessage` synchronously\n * and only then starts the wire, so wrapping that callback afterwards gives the\n * same view of the inbound `resourceSubscriptions` filter the HTTP leg reads off\n * `req.body`. The watcher's notify sink calls `sendResourceUpdated` on the\n * pinned instance, which the router then delivers to the listening stream.\n *\n * Watcher leases are tracked by listen request ID and released on cancellation,\n * rejection, graceful completion, or connection close.\n */\nexport function startServer(options: ServerOptions, config: RuntimeConfig = {}): StdioServerHandle {\n let activeCtx: FilesystemServerContext | undefined;\n const pathGuard = new PathGuard(options, true);\n let pathGuardReady: Promise<void> | undefined;\n const ensurePathGuard = (): Promise<void> => {\n pathGuardReady ??= pathGuard.recomputeAllowedDirectories();\n return pathGuardReady;\n };\n // Shared with the resource contract so a `resources/subscribe` and a\n // `subscriptions/listen` naming the same URI reuse one watcher.\n const registry = createWatcherRegistry();\n const factory: McpServerFactory = async ({ era }) => {\n await ensurePathGuard();\n const c = await createServer(options, {\n watcherRegistry: registry,\n pathGuard,\n era,\n ...(config.apiKey !== undefined ? { apiKey: config.apiKey } : {}),\n });\n activeCtx = c;\n if (era === 'legacy') {\n // Fires when the client's `notifications/initialized` lands. Safe to own:\n // the SDK's only touchpoint is its own initialized handler reading it.\n c.mcp.server.oninitialized = () => {\n void seedRootsFromClient(c);\n };\n // Re-list and grant anything new. Grants are session-additive (R8): a\n // root the client withdrew is not revoked mid-session.\n c.mcp.server.setNotificationHandler('notifications/roots/list_changed', () => {\n void seedRootsFromClient(c);\n });\n }\n return c.mcp;\n };\n\n // One sink for the whole connection. `addCallback` de-duplicates by function\n // identity, so re-using this closure makes a repeat `subscriptions/listen` on\n // an already-watched URI a no-op; a fresh closure per listen would instead\n // stack a callback every time, double-notifying and growing an\n // `activeCallbacks` set that MAX_WATCHERS does not bound (it caps watchers,\n // not callbacks). Reads `activeCtx` when it fires rather than capturing it, so\n // it never pins a disposed instance.\n const sink = (uri: string): void => {\n // A failed notify means the connection went away; nothing to recover.\n void activeCtx?.mcp.server.sendResourceUpdated({ uri }).catch((err: unknown) => {\n Logger.debug('[Stdio] resource update not delivered', {\n uri,\n error: formatUnknownErrorMessage(err),\n });\n });\n };\n\n const wire = new StdioServerTransport();\n const listens = new Map<string | number, StdioListenState>();\n\n // Deleting the entry is what makes this idempotent: a second call for the\n // same id finds nothing to release.\n const releaseListen = (id: string | number): void => {\n const state = listens.get(id);\n if (!state) return;\n listens.delete(id);\n for (const uri of state.acquiredUris) registry.release(uri);\n };\n\n const send = wire.send.bind(wire);\n wire.send = async (message) => {\n if ('id' in message && ('result' in message || 'error' in message)) {\n const id = message.id;\n if (id !== undefined) releaseListen(id);\n }\n await send(message);\n };\n\n const handle = serveStdio(factory, {\n legacy: 'serve',\n transport: wire,\n onerror: (error: unknown) => {\n Logger.error('[Stdio] serve error:', formatUnknownErrorMessage(error));\n },\n });\n\n // Wrap the callback `serveStdio` just installed. Listen requests are admitted\n // in order so two requests naming the same URI cannot race the registry's\n // ref-count. Cancellation is serialized with pending admission by request ID:\n // a pending request is suppressed and releases after preparation, while an\n // active request releases before the SDK closes its stream.\n //\n // serveStdio installs its onmessage synchronously and only then starts the\n // wire; this wrapper is only correct because of that ordering. Assert it\n // rather than degrade to a silent no-op if a future SDK release changes it.\n const deliver = wire.onmessage;\n if (!deliver) {\n throw new Error(\n 'serveStdio did not install a synchronous onmessage handler; the subscriptions/listen watcher gate cannot attach. This is an SDK contract change, not a configuration error.',\n );\n }\n let gated = Promise.resolve();\n wire.onmessage = (message) => {\n const cancelId = cancelledRequestId(message);\n if (cancelId !== null) {\n const state = listens.get(cancelId);\n if (!state) {\n deliver(message);\n return;\n }\n state.cancelled = true;\n if (state.delivered) {\n releaseListen(cancelId);\n deliver(message);\n }\n return;\n }\n\n const subscriptionUris = listenSubscriptionUris(message);\n if (subscriptionUris.length === 0) {\n deliver(message);\n return;\n }\n const id = jsonRpcRequestId(message);\n if (id === null || listens.has(id)) {\n deliver(message);\n return;\n }\n const state: StdioListenState = {\n acquiredUris: [],\n cancelled: false,\n delivered: false,\n };\n listens.set(id, state);\n gated = gated\n .then(async () => {\n if (!isStructurallyValidListen(message)) {\n listens.delete(id);\n deliver(message);\n return;\n }\n if (isListenCancelled(state)) {\n listens.delete(id);\n return;\n }\n await ensurePathGuard();\n const prepared = await prepareListenWatchers(message, pathGuard, registry, sink);\n if (!prepared.ok) {\n listens.delete(id);\n await wire.send({\n jsonrpc: JSONRPC_VERSION,\n id,\n error: { code: ProtocolErrorCode.InvalidParams, message: prepared.message },\n });\n return;\n }\n state.acquiredUris = prepared.acquiredUris;\n if (isListenCancelled(state)) {\n releaseListen(id);\n return;\n }\n state.delivered = true;\n deliver(message);\n })\n .catch(async (error: unknown) => {\n releaseListen(id);\n const failure =\n error instanceof Error ? error : new Error(formatUnknownErrorMessage(error));\n wire.onerror?.(failure);\n // The gate swallowed the message, so nothing downstream will answer it.\n // Without this the client waits out its whole request timeout.\n await wire\n .send({\n jsonrpc: JSONRPC_VERSION,\n id,\n error: { code: ProtocolErrorCode.InternalError, message: failure.message },\n })\n .catch(() => {\n /* the wire is gone; onerror above already reported it */\n });\n });\n };\n\n return {\n close: async () => {\n for (const id of [...listens.keys()]) releaseListen(id);\n registry.destroy();\n try {\n activeCtx?.disposeRuntimeState();\n } catch {\n /* idempotent — disposeRuntimeState guards cleanedUp */\n }\n activeCtx = undefined;\n await handle.close();\n },\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"stdio.js","sourceRoot":"","sources":["../../src/transport/stdio.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,EACL,UAAU,EAEV,oBAAoB,GACrB,MAAM,oCAAoC,CAAC;AAE5C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEzC,OAAO,EAAE,yBAAyB,EAAE,MAAM,mBAAmB,CAAC;AAC9D,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAC;AAElD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,qBAAqB,EAAE,MAAM,6BAA6B,CAAC;AAEpE,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,OAAO,EACL,yBAAyB,EACzB,YAAY,EACZ,gBAAgB,EAChB,sBAAsB,EACtB,qBAAqB,GACtB,MAAM,aAAa,CAAC;AAQrB,gFAAgF;AAChF,0EAA0E;AAC1E,0DAA0D;AAC1D,SAAS,iBAAiB,CAAC,KAAuB;IAChD,OAAO,KAAK,CAAC,SAAS,CAAC;AACzB,CAAC;AAED,SAAS,kBAAkB,CAAC,OAAgB;IAC1C,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,KAAK,IAAI;QAAE,OAAO,IAAI,CAAC;IACjE,MAAM,YAAY,GAAG,OAGpB,CAAC;IACF,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ACH,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["// stdio hosting: the pinned-instance factory, legacy roots seeding, and the\n// gated transport that attaches listen-filter watchers before the SDK sees the\n// message.\nimport type { McpServerFactory } from '@modelcontextprotocol/server';\nimport { ProtocolErrorCode } from '@modelcontextprotocol/server';\nimport {\n serveStdio,\n type StdioServerHandle,\n StdioServerTransport,\n} from '@modelcontextprotocol/server/stdio';\n\nimport { fileURLToPath } from 'node:url';\n\nimport { formatUnknownErrorMessage } from '../core/errors.js';\nimport { Logger } from '../core/observability.js';\nimport type { ServerOptions } from '../core/path.js';\nimport { PathGuard } from '../core/path.js';\nimport { createWatcherRegistry } from '../core/watcher-registry.js';\nimport type { FilesystemServerContext } from '../server.js';\nimport { createServer } from '../server.js';\nimport type { RuntimeConfig } from './shared.js';\nimport {\n isStructurallyValidListen,\n jsonRpcError,\n jsonRpcRequestId,\n listenSubscriptionUris,\n prepareListenWatchers,\n} from './shared.js';\n\ninterface StdioListenState {\n acquiredUris: string[];\n cancelled: boolean;\n delivered: boolean;\n}\n\n// Read through a function, not `state.cancelled` directly: a cancellation lands\n// between the checks, and control-flow narrowing from the first one would\n// otherwise make the second read a compile-time constant.\nfunction isListenCancelled(state: StdioListenState): boolean {\n return state.cancelled;\n}\n\nfunction cancelledRequestId(message: unknown): string | number | null {\n if (typeof message !== 'object' || message === null) return null;\n const notification = message as {\n method?: unknown;\n params?: { requestId?: unknown };\n };\n if (notification.method !== 'notifications/cancelled') return null;\n const requestId = notification.params?.requestId;\n return typeof requestId === 'string' || typeof requestId === 'number' ? requestId : null;\n}\n\n/**\n * Seed the guard's allowed roots from the client's declared workspace roots.\n * Legacy-era only: push-style `roots/list` is deprecated (SEP-2577) and throws\n * on a 2026-07-28 connection, where clients pass paths as tool arguments and\n * the access-grant round-trip covers out-of-root paths instead. Every root\n * still passes `applyGrant`'s boundary and unsafe-path guards, so a client\n * cannot root-declare its way into $HOME or past FS_ROOT_BOUNDARY — a refused\n * root is skipped and its paths fail closed at validateAccess like any other.\n */\nexport async function seedRootsFromClient(ctx: FilesystemServerContext): Promise<number> {\n let roots: readonly { uri: string }[];\n try {\n // eslint-disable-next-line @typescript-eslint/no-deprecated -- legacy-era-only; the modern era never reaches the hooks that call this.\n ({ roots } = await ctx.mcp.server.listRoots());\n } catch (err: unknown) {\n // Client without the roots capability (strict-capabilities throw), or the\n // request failed — either way there is nothing to seed.\n Logger.debug('[Stdio] roots/list unavailable', { error: formatUnknownErrorMessage(err) });\n return 0;\n }\n let granted = 0;\n for (const root of roots) {\n let dir: string;\n try {\n dir = fileURLToPath(root.uri);\n } catch {\n continue; // not a file:// root\n }\n try {\n if (await ctx.pathGuard.applyGrant(dir)) granted += 1;\n else Logger.debug('[Stdio] client root refused by grant policy', { dir });\n } catch (err: unknown) {\n Logger.debug('[Stdio] client root grant failed', {\n dir,\n error: formatUnknownErrorMessage(err),\n });\n }\n }\n // No list-changed notification follows a grant. Allowed roots were once listed\n // as concrete resources, and are not any more: the file template's `list()`\n // returns nothing and the other two lists never read the roots at all (see\n // resources.ts). Notifying bought the client a re-fetch of an unchanged list.\n if (granted > 0) {\n Logger.info(`[Stdio] allowed ${granted} client-declared workspace root(s)`);\n }\n return granted;\n}\n\n/**\n * Serve filesystem-mcp over stdio using modern protocol revision 2026-07-28.\n *\n * The SDK's `StdioListenRouter` acknowledges `subscriptions/listen` and routes\n * the pinned instance's outbound change notifications onto the matching\n * streams, but it exposes no listen-filter hook, so nothing attaches the\n * filesystem watcher that would produce those notifications. `serveStdio`'s\n * `transport` option is the seam: it installs its own `onmessage` synchronously\n * and only then starts the wire, so wrapping that callback afterwards gives the\n * same view of the inbound `resourceSubscriptions` filter the HTTP leg reads off\n * `req.body`. The watcher's notify sink calls `sendResourceUpdated` on the\n * pinned instance, which the router then delivers to the listening stream.\n *\n * Watcher leases are tracked by listen request ID and released on cancellation,\n * rejection, graceful completion, or connection close.\n */\nexport function startServer(options: ServerOptions, config: RuntimeConfig = {}): StdioServerHandle {\n let closed = false;\n let activeCtx: FilesystemServerContext | undefined;\n const pathGuard = new PathGuard(options, true);\n let pathGuardReady: Promise<void> | undefined;\n const ensurePathGuard = (): Promise<void> => {\n pathGuardReady ??= pathGuard.recomputeAllowedDirectories();\n return pathGuardReady;\n };\n // Shared with the resource contract so a `resources/subscribe` and a\n // `subscriptions/listen` naming the same URI reuse one watcher.\n const registry = createWatcherRegistry();\n const factory: McpServerFactory = async ({ era }) => {\n await ensurePathGuard();\n const c = await createServer(options, {\n watcherRegistry: registry,\n pathGuard,\n era,\n ...(config.apiKey !== undefined ? { apiKey: config.apiKey } : {}),\n });\n if (closed) {\n // The connection went away while this instance was being built.\n // `cleanupConnection` already ran and its `closed` guard makes every later\n // call a no-op, so publishing `c` would strand it: nothing would dispose\n // it. Return it unactivated — the wire is gone, so nothing serves it.\n c.disposeRuntimeState();\n return c.mcp;\n }\n activeCtx = c;\n if (era === 'legacy') {\n // Fires when the client's `notifications/initialized` lands. Safe to own:\n // the SDK's only touchpoint is its own initialized handler reading it.\n c.mcp.server.oninitialized = () => {\n void seedRootsFromClient(c);\n };\n // Re-list and grant anything new. Grants are session-additive (R8): a\n // root the client withdrew is not revoked mid-session.\n c.mcp.server.setNotificationHandler('notifications/roots/list_changed', () => {\n void seedRootsFromClient(c);\n });\n }\n return c.mcp;\n };\n\n // One sink for the whole connection. `addCallback` de-duplicates by function\n // identity, so re-using this closure makes a repeat `subscriptions/listen` on\n // an already-watched URI a no-op; a fresh closure per listen would instead\n // stack a callback every time, double-notifying and growing an\n // `activeCallbacks` set that MAX_WATCHERS does not bound (it caps watchers,\n // not callbacks). Reads `activeCtx` when it fires rather than capturing it, so\n // it never pins a disposed instance.\n const sink = (uri: string): void => {\n // A failed notify means the connection went away; nothing to recover.\n void activeCtx?.mcp.server.sendResourceUpdated({ uri }).catch((err: unknown) => {\n Logger.debug('[Stdio] resource update not delivered', {\n uri,\n error: formatUnknownErrorMessage(err),\n });\n });\n };\n\n const wire = new StdioServerTransport();\n const listens = new Map<string | number, StdioListenState>();\n\n // Deleting the entry is what makes this idempotent: a second call for the\n // same id finds nothing to release.\n const releaseListen = (id: string | number): void => {\n const state = listens.get(id);\n if (!state) return;\n listens.delete(id);\n for (const uri of state.acquiredUris) registry.release(uri);\n };\n\n const cleanupConnection = (): void => {\n if (closed) return;\n closed = true;\n for (const state of listens.values()) state.cancelled = true;\n listens.clear();\n registry.destroy();\n const ctx = activeCtx;\n activeCtx = undefined;\n // Both steps below are guarded separately, and for the same reason: this\n // also runs from `wire.onclose`, where a throw is an uncaught exception on\n // the stdin close event rather than a rejected close(). One shared catch\n // would let a failed disposal skip the unref and strand the process.\n try {\n ctx?.disposeRuntimeState();\n } catch {\n /* idempotent — disposeRuntimeState guards cleanedUp */\n }\n try {\n // The SDK's transport close pauses stdin only when no other 'data'\n // listener is left, so a fatal read error (a ReadBuffer overflow) tears\n // the connection down but leaves the handle referenced and this process\n // alive with nothing left to serve. Unref rather than pause: a listener\n // the SDK still owns keeps receiving data, the loop just stops being held\n // open for it.\n process.stdin.unref();\n } catch {\n /* a file-backed stdin is an `fs.ReadStream`, which has no `unref` */\n }\n };\n\n const send = wire.send.bind(wire);\n wire.send = async (message) => {\n if ('id' in message && ('result' in message || 'error' in message)) {\n const id = message.id;\n if (id !== undefined) releaseListen(id);\n }\n await send(message);\n };\n\n const handle = serveStdio(factory, {\n legacy: 'serve',\n transport: wire,\n onerror: (error: unknown) => {\n Logger.error('[Stdio] serve error:', formatUnknownErrorMessage(error));\n },\n });\n const onclose = wire.onclose;\n wire.onclose = () => {\n try {\n cleanupConnection();\n } finally {\n onclose?.();\n }\n };\n\n // Wrap the callback `serveStdio` just installed. Listen requests are admitted\n // in order so two requests naming the same URI cannot race the registry's\n // ref-count. Cancellation is serialized with pending admission by request ID:\n // a pending request is suppressed and releases after preparation, while an\n // active request releases before the SDK closes its stream.\n //\n // serveStdio installs its onmessage synchronously and only then starts the\n // wire; this wrapper is only correct because of that ordering. Assert it\n // rather than degrade to a silent no-op if a future SDK release changes it.\n const deliver = wire.onmessage;\n if (!deliver) {\n throw new Error(\n 'serveStdio did not install a synchronous onmessage handler; the subscriptions/listen watcher gate cannot attach. This is an SDK contract change, not a configuration error.',\n );\n }\n let gated = Promise.resolve();\n wire.onmessage = (message) => {\n const cancelId = cancelledRequestId(message);\n if (cancelId !== null) {\n const state = listens.get(cancelId);\n if (!state) {\n deliver(message);\n return;\n }\n state.cancelled = true;\n if (state.delivered) {\n releaseListen(cancelId);\n deliver(message);\n }\n return;\n }\n\n const subscriptionUris = listenSubscriptionUris(message);\n if (subscriptionUris.length === 0) {\n deliver(message);\n return;\n }\n const id = jsonRpcRequestId(message);\n if (id === null || listens.has(id)) {\n deliver(message);\n return;\n }\n const state: StdioListenState = {\n acquiredUris: [],\n cancelled: false,\n delivered: false,\n };\n listens.set(id, state);\n gated = gated\n .then(async () => {\n if (!isStructurallyValidListen(message)) {\n listens.delete(id);\n deliver(message);\n return;\n }\n if (isListenCancelled(state)) {\n listens.delete(id);\n return;\n }\n await ensurePathGuard();\n const prepared = await prepareListenWatchers(message, pathGuard, registry, sink);\n if (!prepared.ok) {\n listens.delete(id);\n await wire.send(jsonRpcError(ProtocolErrorCode.InvalidParams, prepared.message, id));\n return;\n }\n state.acquiredUris = prepared.acquiredUris;\n if (isListenCancelled(state)) {\n releaseListen(id);\n return;\n }\n state.delivered = true;\n deliver(message);\n })\n .catch(async (error: unknown) => {\n releaseListen(id);\n const failure =\n error instanceof Error ? error : new Error(formatUnknownErrorMessage(error));\n wire.onerror?.(failure);\n // The gate swallowed the message, so nothing downstream will answer it.\n // Without this the client waits out its whole request timeout.\n await wire\n .send(jsonRpcError(ProtocolErrorCode.InternalError, failure.message, id))\n .catch(() => {\n /* the wire is gone; onerror above already reported it */\n });\n });\n };\n\n return {\n close: async () => {\n try {\n cleanupConnection();\n } finally {\n await handle.close();\n }\n },\n };\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,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CAAC,EACD,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,WAAW,GAAG,oBAAoB,GAAG,GAAG,CAAC,GAAG,IAAI,CAAC,CACpE,CAAC;QACF,gBAAgB,CAAC,GAAG,EAAE,GAAG,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,IAAI,EAAE;YAC5E,aAAa,EAAE,MAAM,CAAC,aAAa,CAAC;SACrC,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import { getOAuthProtectedResourceMetadataUrl } from '@modelcontextprotocol/express';\nimport { JSONRPC_VERSION, localhostAllowedHostnames } from '@modelcontextprotocol/server';\nimport type { AuthInfo } from '@modelcontextprotocol/server';\n\nimport { createHash, timingSafeEqual } from 'node:crypto';\n\nimport type { NextFunction, Request, RequestHandler, Response } from 'express';\n\nimport { ErrorCode, FsError } from './core/errors.js';\nimport { Logger } from './core/observability.js';\nimport { splitCsvList } from './core/util.js';\n\nconst MAX_BEARER_TOKEN_LENGTH = 4096;\n\n/**\n * JSON-RPC's server-defined error range. No SDK enum maps to \"Unauthorized\" or\n * \"Rate limit exceeded\", both of which this module refuses before any handler\n * has parsed a request.\n */\nconst JSONRPC_SERVER_ERROR = -32000;\n\n/**\n * The one JSON-RPC error envelope every HTTP refusal goes out in — this\n * module's pre-handler rejections and `transport.ts`'s. It lives here because\n * `transport.ts` already imports this module and not the other way round; a\n * second hand-built literal is how the two drift apart.\n *\n * `headers` carries what a particular refusal owes the client on top of the\n * JSON content type — `WWW-Authenticate` on a 401, `Retry-After` on a 429.\n * Goes out through `res.json` so every refusal keeps the `charset=utf-8` and\n * `Content-Length` a hand-rolled `writeHead`/`end` pair drops.\n */\nexport function sendJsonRpcError(\n res: Response,\n status: number,\n code: number,\n message: string,\n id: string | number | null = null,\n headers: Record<string, string> = {},\n): void {\n res.status(status).set(headers).json({ jsonrpc: JSONRPC_VERSION, id, error: { code, message } });\n}\n\nconst LOCALHOST_ORIGIN_RE = /^https?:\\/\\/(localhost|127\\.0\\.0\\.1|\\[::1\\])(:\\d+)?$/u;\n\n/**\n * Pure HTTP auth and binding policy. Holds no state; all functions are\n * directly testable without spinning up a server.\n */\nexport function isLoopbackHttpHost(host: string): boolean {\n const normalizedHost = host.trim().toLowerCase();\n return (\n normalizedHost === '127.0.0.1' ||\n normalizedHost === 'localhost' ||\n normalizedHost === '[::1]' ||\n normalizedHost === '::1'\n );\n}\n\nexport function isAllowedLocalhostOrigin(origin: string): boolean {\n return LOCALHOST_ORIGIN_RE.test(origin);\n}\n\n/** A bind to every interface. Clients never send `Host: 0.0.0.0`, so this bind\n * cannot derive its allowed-host set from itself. */\nfunction isWildcardHttpHost(host: string): boolean {\n return host === '0.0.0.0' || host === '::';\n}\n\nfunction originHostname(origin: string): string | undefined {\n try {\n return new URL(origin).hostname;\n } catch {\n return undefined;\n }\n}\n\n/**\n * True if `origin` (a raw `Origin` request header) is allowed given the\n * env-derived `allowedHostnames` set (hostname-form, no scheme/port). Localhost\n * origins are always accepted via {@link isAllowedLocalhostOrigin}; a remote\n * origin is accepted iff its parsed hostname is in the set. Both the SDK app's\n * `allowedOrigins` and this OPTIONS-handler check consume hostname-form, so a\n * remote origin allowed via `FS_ALLOWED_ORIGINS` is reflected\n * end-to-end in `Access-Control-Allow-Origin`.\n */\nexport function isOriginAllowed(origin: string, allowedHostnames: readonly string[]): boolean {\n if (isAllowedLocalhostOrigin(origin)) return true;\n const host = originHostname(origin);\n return host !== undefined && allowedHostnames.includes(host);\n}\n\nexport function validateBearerAuthorization(\n apiKey: string,\n authHeader: unknown,\n): authHeader is string {\n const bearerPrefix = 'Bearer ';\n if (typeof authHeader !== 'string' || !authHeader.startsWith(bearerPrefix)) {\n return false;\n }\n const userKey = authHeader.slice(bearerPrefix.length).trim();\n const normalizedApiKey = apiKey.trim();\n if (userKey.length > MAX_BEARER_TOKEN_LENGTH || normalizedApiKey.length === 0) {\n return false;\n }\n\n // Pure: hash per call. createHash is negligible next to the timingSafeEqual\n // already done per request, and avoiding module-level cache state keeps this\n // testable without post-import env-mutation footguns.\n const expectedHash = createHash('sha256').update(normalizedApiKey).digest();\n const actualHash = createHash('sha256').update(userKey).digest();\n return timingSafeEqual(expectedHash, actualHash);\n}\n\nfunction isSecureApiKey(key: string | undefined): boolean {\n return typeof key === 'string' && key.trim().length >= 16;\n}\n\n/**\n * Refuse to bind to a non-loopback host without an API key. Throws on\n * policy violation; returns silently when allowed.\n */\nexport function assertHttpBindingPolicy(host: string, apiKey: string | undefined): void {\n if (isLoopbackHttpHost(host)) {\n if (apiKey !== undefined && !isSecureApiKey(apiKey)) {\n throw new FsError(\n ErrorCode.PERMISSION_DENIED,\n 'API_KEY is configured but is insecure (minimum 16 characters).',\n );\n }\n return;\n }\n if (isSecureApiKey(apiKey)) return;\n throw new FsError(\n ErrorCode.PERMISSION_DENIED,\n `Refusing to bind HTTP server to non-loopback host '${host}' without a secure API_KEY (minimum 16 characters).`,\n );\n}\n\n/**\n * The Host header values this bind accepts. `FS_ALLOWED_HOSTS` wins\n * when set; otherwise a loopback bind takes the whole localhost hostname set (a\n * client dialing http://localhost:<port> sends `Host: localhost`, which a bare\n * ['127.0.0.1'] list would 403), a wildcard bind takes none, and a concrete\n * non-loopback bind takes itself. An empty result means no Host validation is\n * mounted — only reachable under FS_ALLOW_UNRESTRICTED_HOSTS=1.\n */\nexport function resolveAllowedHosts(\n httpHost: string,\n allowedHostsEnv: string | undefined,\n): string[] {\n const configured = splitCsvList(allowedHostsEnv);\n if (configured.length > 0) return configured;\n if (isLoopbackHttpHost(httpHost)) return localhostAllowedHostnames();\n if (isWildcardHttpHost(httpHost)) return [];\n return [httpHost];\n}\n\n/**\n * Express's `trust proxy` setting from `FS_TRUST_PROXY`. A\n * non-negative integer hop count parses to a number (Express counts hops from\n * the socket); anything else (a subnet name/expression, or a negative/\n * non-integer string) passes through unchanged for Express to interpret.\n * `undefined` means the env var was unset or empty — leave Express's default\n * (disabled) in place.\n */\nexport function resolveTrustProxySetting(value: string | undefined): number | string | undefined {\n if (!value) return undefined;\n const hops = Number(value);\n return Number.isInteger(hops) && hops >= 0 ? hops : value;\n}\n\n/**\n * Refuse to bind a wildcard host (`0.0.0.0` / `::`) without an explicit\n * `FS_ALLOWED_HOSTS` list. Clients never send `Host: 0.0.0.0`, so\n * defaulting the allowed-host set to the wildcard string would reject all real\n * traffic. Operators who accept the risk can set\n * `FS_ALLOW_UNRESTRICTED_HOSTS=1` to restore warn-and-bind.\n * Loopback and concrete non-loopback hosts are unaffected.\n */\nexport function assertHttpHostPolicy(\n host: string,\n allowedHosts: readonly string[],\n allowUnrestricted: boolean,\n): void {\n if (isLoopbackHttpHost(host)) return;\n // concrete non-loopback: Host validated against the bind host.\n if (!isWildcardHttpHost(host)) return;\n if (allowUnrestricted) return;\n if (allowedHosts.length > 0) return;\n throw new FsError(\n ErrorCode.PERMISSION_DENIED,\n `Refusing to bind wildcard host '${host}' without FS_ALLOWED_HOSTS. Set it to the public hostname(s) clients send, or set FS_ALLOW_UNRESTRICTED_HOSTS=true to accept the risk.`,\n );\n}\n\n// ─── Protected-resource discovery (RFC 9728 / RFC 6750) ──────────────────────\n\n/**\n * This server is a resource server with no authorization server: `API_KEY` is a\n * static secret the operator hands out of band, not an issued token. So the\n * metadata document deliberately omits `authorization_servers` — RFC 9728 §2\n * makes it optional, and its absence is the accurate statement that a token\n * cannot be obtained from an endpoint. `mcpAuthMetadataRouter` from\n * `@modelcontextprotocol/express` is the tool for the IdP-backed case: it\n * requires RFC 8414 authorization-server metadata, and inventing an issuer with\n * endpoints that answer nothing would send clients into a flow that cannot\n * complete. Adopt it if this ever moves to a real IdP.\n *\n * What discovery buys here: a client hitting 401 learns the resource identifier\n * and that the credential goes in the Authorization header, instead of a bare\n * challenge plus a 404 on the well-known path.\n */\nexport function protectedResourceUrl(\n req: Request,\n hostValidated: boolean,\n configured: string | undefined,\n): URL | null {\n if (configured) {\n const parsed = URL.parse(configured);\n if (parsed) return parsed;\n Logger.warn(\n `[HTTP] Ignoring unparseable FS_PUBLIC_URL: ${configured}. Deriving the resource identifier from the Host header instead.`,\n );\n }\n // With no Host validator mounted (FS_ALLOW_UNRESTRICTED_HOSTS=1)\n // the Host header is attacker-controlled, and naming a resource from it would\n // publish an identifier the operator does not own. Answer with nothing instead.\n if (!hostValidated) return null;\n const host = req.headers.host ?? '127.0.0.1';\n return URL.parse(`${req.secure ? 'https' : 'http'}://${host}/mcp`);\n}\n\n/**\n * RFC 6750 §3: a request with no credentials gets a bare challenge, while one\n * that presented something invalid gets `error=\"invalid_token\"`. Clients use\n * the difference to tell \"you must authenticate\" from \"your token is wrong\".\n */\nfunction buildAuthChallenge(\n req: Request,\n hasCredentials: boolean,\n hostValidated: boolean,\n publicUrl: string | undefined,\n): string {\n const resource = protectedResourceUrl(req, hostValidated, publicUrl);\n const params: string[] = [];\n if (resource) {\n params.push(`resource_metadata=\"${getOAuthProtectedResourceMetadataUrl(resource)}\"`);\n }\n if (hasCredentials) {\n params.push('error=\"invalid_token\"', 'error_description=\"Invalid or expired bearer token\"');\n }\n // A bare `Bearer` is a complete challenge; avoid emitting a trailing space.\n return params.length > 0 ? `Bearer ${params.join(', ')}` : 'Bearer';\n}\n\n/**\n * Express middleware: when `apiKey` is set, require a matching bearer token.\n * No key set = open access (loopback dev mode). `apiKey` is captured once per\n * app setup (passed in from startHttpServer) so the middleware and\n * assertHttpBindingPolicy share one source of truth.\n *\n * `requireBearerAuth` + `verifyAccessToken` from `@modelcontextprotocol/express`\n * is the tool for verifier-backed tokens; this middleware stays hand-rolled\n * because the credential is a static operator key with no verifier, the 401 body\n * must stay a JSON-RPC envelope, and the RFC 6750 bare-vs-`invalid_token`\n * challenge split is implemented here. Adopt `requireBearerAuth` if this ever\n * verifies issued tokens.\n */\nexport function bearerAuthMiddleware(\n apiKey: string | undefined,\n hostValidated: boolean,\n publicUrl?: string,\n): RequestHandler {\n return (req: Request, res: Response, next: NextFunction): void => {\n if (!apiKey) {\n next();\n return;\n }\n const authHeader = req.headers.authorization;\n if (isSecureApiKey(apiKey) && validateBearerAuthorization(apiKey, authHeader)) {\n // Forward the validated caller to the SDK pipeline: toNodeHandler reads\n // req.auth and passes it as the handler's pass-through authInfo, which the\n // per-request factory receives and tool handlers read as ctx.http.authInfo.\n // `expiresAt` is deliberately omitted: a static API key has no expiry and\n // there is no `exp` claim or introspection response to read one from.\n // Populate it from the token if this ever moves to issued tokens.\n const presented = authHeader.slice('Bearer '.length).trim();\n (req as Request & { auth?: AuthInfo }).auth = {\n token: presented,\n clientId: 'api-key',\n scopes: [],\n };\n next();\n return;\n }\n sendJsonRpcError(res, 401, JSONRPC_SERVER_ERROR, 'Unauthorized', null, {\n 'WWW-Authenticate': buildAuthChallenge(\n req,\n req.headers.authorization !== undefined,\n hostValidated,\n publicUrl,\n ),\n });\n };\n}\n\n/**\n * Env-derived CORS origins (hostname-form, no scheme/port — matches the SDK\n * app's `allowedOrigins` consumer). Defaults to the SDK's loopback hostname set\n * so loopback browser clients keep working; operators set\n * `FS_ALLOWED_ORIGINS` to allow remote clients on non-loopback\n * binds. An empty value reads as unset, matching how parseAllowedHostsEnv\n * treats an all-empty list. The same set is consulted by corsPreflightHandler\n * so a remote origin is reflected end-to-end in Access-Control-Allow-Origin.\n */\nexport function computeAllowedOriginHostnames(originsEnv: string | undefined): string[] {\n // Truthiness, not list length: an all-blank value (\" , \") is a configured but\n // empty allow-list — deny every remote origin — while an unset or \"\" value\n // falls back to the loopback defaults.\n return originsEnv ? splitCsvList(originsEnv) : localhostAllowedHostnames();\n}\n\n/**\n * Reflect a present Origin on the response if it is allowed — localhost, or\n * in the env-derived `FS_ALLOWED_ORIGINS` set — and avoid\n * emitting a wildcard fallback. Shared by the OPTIONS preflight and the real\n * POST response: `createMcpExpressApp`'s `allowedOrigins` only gates which\n * Origins are accepted, it never sets `Access-Control-Allow-Origin` on the\n * actual response, so without this a browser client would pass preflight and\n * then have the POST response body blocked by CORS.\n */\nfunction reflectAllowedOrigin(\n req: Request,\n res: Response,\n allowedOriginHostnames: readonly string[],\n): void {\n const origin = req.headers.origin;\n if (origin && isOriginAllowed(origin, allowedOriginHostnames)) {\n res.header('Access-Control-Allow-Origin', origin);\n // Key the response by Origin so a CDN/proxy caching one origin's response\n // cannot replay it for a different origin (cache-poison).\n res.header('Vary', 'Origin');\n }\n}\n\n/** Mounted ahead of the `/mcp` handlers so every response — not just the\n * OPTIONS preflight — carries `Access-Control-Allow-Origin` for an allowed\n * Origin. */\nexport function corsOriginMiddleware(allowedOriginHostnames: readonly string[]): RequestHandler {\n return (req: Request, res: Response, next: NextFunction): void => {\n reflectAllowedOrigin(req, res, allowedOriginHostnames);\n next();\n };\n}\n\n/**\n * OPTIONS preflight for `/mcp`. The allow-list carries the SEP-2243 standard\n * headers (`mcp-protocol-version`, `mcp-method`, `mcp-name`): SDK clients send\n * all three on every modern request POST and `createMcpHandler` requires them\n * (400 / -32020), so omitting them here would fail every browser preflight —\n * and the HTTP leg is modern-only, so there is no legacy path to fall back to.\n * `mcp-session-id` is deliberately absent: it is 2025-era only and this\n * endpoint rejects legacy traffic.\n */\nexport function corsPreflightHandler(allowedOriginHostnames: readonly string[]): RequestHandler {\n return (req: Request, res: Response): void => {\n reflectAllowedOrigin(req, res, allowedOriginHostnames);\n res.header('Access-Control-Allow-Methods', 'POST, OPTIONS');\n res.header(\n 'Access-Control-Allow-Headers',\n 'Content-Type, Authorization, mcp-protocol-version, mcp-method, mcp-name',\n );\n res.status(204).end();\n };\n}\n\n// ─── Rate limiting (public bind only) ────────────────────────────────────────\n\ninterface RateBucket {\n windowStart: number;\n count: number;\n}\n\n// Fixed 60s window; never tuned, so it is a constant rather than config plumbing.\nconst RATE_LIMIT_WINDOW_MS = 60_000;\n\n/**\n * Fixed-window per-client-IP rate limiter.\n */\nexport function createRateLimiter(max: number): RequestHandler {\n const buckets = new Map<string, RateBucket>();\n const sweep = setInterval(() => {\n const cutoff = Date.now() - RATE_LIMIT_WINDOW_MS * 2;\n for (const [ip, bucket] of buckets) {\n if (bucket.windowStart < cutoff) buckets.delete(ip);\n }\n }, RATE_LIMIT_WINDOW_MS);\n sweep.unref();\n\n return (req: Request, res: Response, next: NextFunction): void => {\n const ip = req.ip ?? req.socket.remoteAddress ?? 'unknown';\n const now = Date.now();\n let bucket = buckets.get(ip);\n if (!bucket || now - bucket.windowStart >= RATE_LIMIT_WINDOW_MS) {\n bucket = { windowStart: now, count: 0 };\n buckets.set(ip, bucket);\n }\n bucket.count += 1;\n if (bucket.count <= max) {\n next();\n return;\n }\n const retryAfterSec = Math.max(\n 1,\n Math.ceil((bucket.windowStart + RATE_LIMIT_WINDOW_MS - now) / 1000),\n );\n sendJsonRpcError(res, 429, JSONRPC_SERVER_ERROR, 'Rate limit exceeded', null, {\n 'Retry-After': String(retryAfterSec),\n });\n };\n}\n"]}
|