@procedures/mcp-server 0.26.2 → 0.27.1
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 +73 -0
- package/README.md +14 -11
- package/dist/.tsbuildinfo +1 -1
- package/dist/face.d.ts +14 -10
- package/dist/face.d.ts.map +1 -1
- package/dist/face.js +18 -8
- package/dist/face.js.map +1 -1
- package/dist/host.d.ts +4 -7
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +20 -8
- package/dist/host.js.map +1 -1
- package/dist/transport/guard.d.ts +17 -16
- package/dist/transport/guard.d.ts.map +1 -1
- package/dist/transport/guard.js +24 -17
- package/dist/transport/guard.js.map +1 -1
- package/dist/transport/http.d.ts +29 -8
- package/dist/transport/http.d.ts.map +1 -1
- package/dist/transport/http.js +66 -15
- package/dist/transport/http.js.map +1 -1
- package/dist/transport/mcp-transport.d.ts.map +1 -1
- package/dist/transport/mcp-transport.js +17 -6
- package/dist/transport/mcp-transport.js.map +1 -1
- package/dist/types.d.ts +12 -11
- package/dist/types.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/face.ts +28 -14
- package/src/host.ts +25 -15
- package/src/transport/guard.ts +33 -26
- package/src/transport/http.ts +89 -16
- package/src/transport/mcp-transport.ts +18 -5
- package/src/types.ts +12 -11
package/src/host.ts
CHANGED
|
@@ -70,13 +70,10 @@ export interface McpHostFaces {
|
|
|
70
70
|
/** The face over the standard input and output. `true`: `process.stdin` and `process.stdout`. */
|
|
71
71
|
stdio?: boolean | McpStdioFace | undefined;
|
|
72
72
|
/**
|
|
73
|
-
* The face over Streamable HTTP. Without
|
|
74
|
-
*
|
|
75
|
-
* (CLI-
|
|
76
|
-
*
|
|
77
|
-
* Deprecated (CLI-2b): `http.token`. Give the token in `policy.token`. From the next MINOR,
|
|
78
|
-
* `http.token` becomes the token of the policy, and the server checks it. A different token in
|
|
79
|
-
* the policy throws an error.
|
|
73
|
+
* The face over Streamable HTTP. Without origins and hosts of its own, it takes the browser
|
|
74
|
+
* checks of the policy. The server of the host admits each request one time, with the one token
|
|
75
|
+
* of the host (CLI-2b). A `token` of the face becomes the token of the policy. The face has no
|
|
76
|
+
* listener (CLI-11): mount `McpHost.handleHttp` on a listener.
|
|
80
77
|
*/
|
|
81
78
|
http?: HttpTransportOptions | undefined;
|
|
82
79
|
/** SDK transports to connect: the in-memory transport of a test, or an embedding. */
|
|
@@ -292,6 +289,17 @@ export function mcpExposeRule(
|
|
|
292
289
|
};
|
|
293
290
|
}
|
|
294
291
|
|
|
292
|
+
/**
|
|
293
|
+
* The one token of a host (CLI-2b): the token of the policy, or the token of the HTTP face. The
|
|
294
|
+
* server of the host checks it for each network request, so two different tokens throw an error.
|
|
295
|
+
*/
|
|
296
|
+
function hostToken(policyToken: string | undefined, faceToken: string | undefined): string | undefined {
|
|
297
|
+
if (policyToken !== undefined && faceToken !== undefined && policyToken !== faceToken) {
|
|
298
|
+
throw new Error("An MCP host has one token: give the same token to the policy and to the HTTP face, or give it only in the policy");
|
|
299
|
+
}
|
|
300
|
+
return policyToken ?? faceToken;
|
|
301
|
+
}
|
|
302
|
+
|
|
295
303
|
/**
|
|
296
304
|
* This function builds an MCP host. It starts nothing: `start()` starts the faces. A host
|
|
297
305
|
* stops one time.
|
|
@@ -316,9 +324,12 @@ export function createMcpHost(options: McpHostOptions = {}): McpHost {
|
|
|
316
324
|
const parts: McpHostParts = { registry, expose };
|
|
317
325
|
const hostInterceptors = typeof options.interceptors === "function" ? options.interceptors(parts) : (options.interceptors ?? []);
|
|
318
326
|
const interceptors = [...(options.policy?.interceptors ?? []), ...hostInterceptors];
|
|
327
|
+
// A host has one token, and its server checks it (CLI-2b): the token of the HTTP face goes to the policy
|
|
328
|
+
const token = hostToken(options.policy?.token, options.faces?.http?.token);
|
|
319
329
|
const policy = hostPolicy({
|
|
320
330
|
strictCalls: true,
|
|
321
331
|
...options.policy,
|
|
332
|
+
...(token !== undefined ? { token } : {}),
|
|
322
333
|
...(expose ? { expose } : {}),
|
|
323
334
|
...(options.readOnly === true ? { readOnly: true } : {}),
|
|
324
335
|
...(interceptors.length > 0 ? { interceptors } : {}),
|
|
@@ -387,18 +398,17 @@ export function createMcpHost(options: McpHostOptions = {}): McpHost {
|
|
|
387
398
|
}
|
|
388
399
|
let httpFace: McpServerTransport | undefined;
|
|
389
400
|
if (faces.http) {
|
|
390
|
-
//
|
|
391
|
-
|
|
392
|
-
const
|
|
393
|
-
const
|
|
394
|
-
const
|
|
401
|
+
// One check for each request (CLI-2b): the browser checks of the policy, and the admission of
|
|
402
|
+
// the server with the one token of the host. The face has no token check of its own.
|
|
403
|
+
const { token: _token, ...http } = faces.http;
|
|
404
|
+
const allowedOrigins = http.allowedOrigins ?? policy.browser.allowedOrigins;
|
|
405
|
+
const allowedHosts = http.allowedHosts ?? policy.browser.allowedHosts;
|
|
395
406
|
httpFace = new McpServerTransport(server, {
|
|
396
407
|
...common,
|
|
397
408
|
transport: "http",
|
|
398
409
|
httpOptions: {
|
|
399
|
-
...
|
|
400
|
-
...(
|
|
401
|
-
...(allowedOrigins ? { allowedOrigins: [...allowedOrigins] } : {}),
|
|
410
|
+
...http,
|
|
411
|
+
...(allowedOrigins !== undefined ? { allowedOrigins } : {}),
|
|
402
412
|
...(allowedHosts !== undefined ? { allowedHosts } : {}),
|
|
403
413
|
},
|
|
404
414
|
});
|
package/src/transport/guard.ts
CHANGED
|
@@ -7,9 +7,10 @@
|
|
|
7
7
|
* - The checks of a web browser (`checkBrowserRequest` of the core): a request on a loopback
|
|
8
8
|
* address names a loopback host or an allowed host (DNS rebinding), and an `Origin` header names
|
|
9
9
|
* the server or a permitted origin. A request with cookies needs a named origin (CSRF).
|
|
10
|
-
* - The
|
|
11
|
-
*
|
|
12
|
-
*
|
|
10
|
+
* - The credentials are not checked here (CLI-2b). After the browser checks, the face gives each
|
|
11
|
+
* request to the admission of its server (`admission()` of the handler): the request hook, the
|
|
12
|
+
* token check and the authenticate function, one time for each request. Before, this function
|
|
13
|
+
* compared the token itself, next to the token check of the server.
|
|
13
14
|
*
|
|
14
15
|
* The other guards of the face:
|
|
15
16
|
*
|
|
@@ -25,7 +26,7 @@
|
|
|
25
26
|
*/
|
|
26
27
|
|
|
27
28
|
import type { IncomingMessage } from "node:http";
|
|
28
|
-
import {
|
|
29
|
+
import { checkBrowserRequest, isLoopbackHost, type BrowserGuardOptions } from "@procedures/client";
|
|
29
30
|
|
|
30
31
|
/** The limits of the Streamable HTTP face. `maxBodyBytes` and `maxSessions` of the HTTP options change them. */
|
|
31
32
|
export const MCP_HTTP_LIMITS: {
|
|
@@ -46,15 +47,11 @@ export const MCP_HTTP_LIMITS: {
|
|
|
46
47
|
retryAfterSeconds: 5,
|
|
47
48
|
};
|
|
48
49
|
|
|
49
|
-
/**
|
|
50
|
+
/**
|
|
51
|
+
* The options of `guardMcpRequest`: the browser checks. Since CLI-2b they have no token. The
|
|
52
|
+
* server of the face checks the token in its admission, one time for each request.
|
|
53
|
+
*/
|
|
50
54
|
export interface McpRequestGuardOptions {
|
|
51
|
-
/**
|
|
52
|
-
* The token that each request sends as `Authorization: Bearer <token>`. Without it, the face checks no token.
|
|
53
|
-
* @deprecated Give the token to the server, and let the server check the credentials of each
|
|
54
|
-
* request (CLI-2b). From the next MINOR, the option is removed, a `token` option throws an
|
|
55
|
-
* error, and the owner of a listener admits each request with `admission()` of the server.
|
|
56
|
-
*/
|
|
57
|
-
readonly token?: string | undefined;
|
|
58
55
|
/** The browser origins that can call the face, besides its own origin. */
|
|
59
56
|
readonly allowedOrigins?: BrowserGuardOptions["allowedOrigins"];
|
|
60
57
|
/** The host names that a request on a loopback address can name, besides the loopback names. */
|
|
@@ -68,7 +65,8 @@ export type McpGuardResult =
|
|
|
68
65
|
| { readonly ok: true }
|
|
69
66
|
| {
|
|
70
67
|
readonly ok: false;
|
|
71
|
-
|
|
68
|
+
/** 500 only for a removed `token` option, after its first error (CLI-2b). */
|
|
69
|
+
readonly status: 401 | 403 | 405 | 415 | 500;
|
|
72
70
|
readonly code: string;
|
|
73
71
|
/** The reason of a FORBIDDEN refusal of wire v1, for example `origin-not-allowed` (CLI-4). */
|
|
74
72
|
readonly reason?: string | undefined;
|
|
@@ -76,29 +74,38 @@ export type McpGuardResult =
|
|
|
76
74
|
readonly headers?: Readonly<Record<string, string>> | undefined;
|
|
77
75
|
};
|
|
78
76
|
|
|
77
|
+
/** The message for a `token` option of `guardMcpRequest` (CLI-2b). It names the migration. */
|
|
78
|
+
const TOKEN_OPTION_REMOVED =
|
|
79
|
+
"guardMcpRequest checks no token since CLI-2b: remove its token option. Give the token to the server, hostPolicy({ token }) or new ProcedureServer({ token }). " +
|
|
80
|
+
"After guardMcpRequest, admit each request with server.admission(credentials), and answer 401 when ok is false.";
|
|
81
|
+
|
|
82
|
+
/** True after the first call with a `token` option threw its error. */
|
|
83
|
+
let tokenOptionReported = false;
|
|
84
|
+
|
|
79
85
|
/**
|
|
80
|
-
* The checks of a request to the MCP endpoint: the
|
|
86
|
+
* The browser checks of a request to the MCP endpoint: one check of the host and the origin. The
|
|
87
|
+
* credentials are the job of the admission of the server (CLI-2b).
|
|
81
88
|
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
89
|
+
* Before, this function compared a `token` option. A token without its check opens the endpoint,
|
|
90
|
+
* so a `token` option fails. The function has no point of creation: the first call with a
|
|
91
|
+
* `token` option throws a `TypeError` with the migration. Each later call with it gives a refusal
|
|
92
|
+
* with the status 500 and the same message, without an error. Thus the log of the app gets one
|
|
93
|
+
* error, and each request stays refused.
|
|
85
94
|
*/
|
|
86
95
|
export function guardMcpRequest(req: Pick<IncomingMessage, "method" | "headers" | "socket">, options: McpRequestGuardOptions = {}): McpGuardResult {
|
|
96
|
+
if (Reflect.get(options, "token") !== undefined) {
|
|
97
|
+
if (!tokenOptionReported) {
|
|
98
|
+
tokenOptionReported = true;
|
|
99
|
+
throw new TypeError(TOKEN_OPTION_REMOVED);
|
|
100
|
+
}
|
|
101
|
+
return { ok: false, status: 500, code: "INTERNAL_ERROR", message: TOKEN_OPTION_REMOVED };
|
|
102
|
+
}
|
|
87
103
|
const browser: BrowserGuardOptions = {
|
|
88
104
|
...(options.allowedOrigins !== undefined ? { allowedOrigins: options.allowedOrigins } : {}),
|
|
89
105
|
...(options.allowedHosts !== undefined ? { allowedHosts: options.allowedHosts } : {}),
|
|
90
106
|
};
|
|
91
107
|
const checked = checkBrowserRequest(req, browser, false, options.ambient === true);
|
|
92
108
|
if (!checked.ok) return checked;
|
|
93
|
-
if (options.token !== undefined && !tokenMatches(options.token, bearerOf(req.headers.authorization))) {
|
|
94
|
-
return {
|
|
95
|
-
ok: false,
|
|
96
|
-
status: 401,
|
|
97
|
-
code: "UNAUTHORIZED",
|
|
98
|
-
message: "Unauthorized: this server requires its token",
|
|
99
|
-
headers: { "www-authenticate": 'Bearer realm="mcp"' },
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
109
|
return { ok: true };
|
|
103
110
|
}
|
|
104
111
|
|
package/src/transport/http.ts
CHANGED
|
@@ -5,19 +5,22 @@
|
|
|
5
5
|
* Each client gets a session: its `initialize` request makes a transport and an MCP server of its
|
|
6
6
|
* own, and the later requests of the client carry the `mcp-session-id` header of that session.
|
|
7
7
|
* A session gets the progress of a tool call and the `list_changed` notifications. The checks of
|
|
8
|
-
* a local server apply: the listener of the service listens on loopback by default, the
|
|
9
|
-
* another host or origin (DNS rebinding, cross-site requests)
|
|
10
|
-
* request without the token.
|
|
8
|
+
* a local server apply: the listener of the service listens on loopback by default, and the
|
|
9
|
+
* endpoint refuses a web page of another host or origin (DNS rebinding, cross-site requests).
|
|
11
10
|
*
|
|
12
|
-
* Each
|
|
13
|
-
*
|
|
14
|
-
*
|
|
11
|
+
* Each request has one check of its credentials (CLI-2b): the admission of the server of the
|
|
12
|
+
* face (`admission()` of its handler). The admission runs the request hook (`principalOf`), the
|
|
13
|
+
* token check and the authenticate function of the server, for each request: `initialize`, a
|
|
14
|
+
* call, the GET stream and the DELETE of a session. The endpoint has no token check of its own.
|
|
15
|
+
* The tool calls of the request get the admitted credentials (CLI-10), so the server does not
|
|
16
|
+
* run its checks again for them. A server that reads cookies trusts a request with a cookie only
|
|
17
|
+
* from a named origin (CSRF).
|
|
15
18
|
*
|
|
16
19
|
* The endpoint has no listener of its own (CLI-11). The owner of a listener mounts `handle`: a
|
|
17
20
|
* listener of the service (`httpTransport` of `@mark1russell7/service/http`) or the procedures host.
|
|
18
21
|
* Thus the runner of the process owns the port, the stop in stages and the exit.
|
|
19
22
|
*
|
|
20
|
-
* The checks of each request are in `guard.ts` (CLI-35). Only an `initialize` request starts a
|
|
23
|
+
* The browser checks of each request are in `guard.ts` (CLI-35). Only an `initialize` request starts a
|
|
21
24
|
* session, and each body has a limit. The endpoint keeps at most `maxSessions` sessions: 256 by
|
|
22
25
|
* default (CLI-51), or no limit with `Infinity`.
|
|
23
26
|
*/
|
|
@@ -26,12 +29,46 @@ import { randomUUID } from "node:crypto";
|
|
|
26
29
|
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
27
30
|
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
28
31
|
import { isInitializeRequest } from "@modelcontextprotocol/sdk/types.js";
|
|
29
|
-
import { hasAmbientCredentials } from "@procedures/client";
|
|
32
|
+
import { hasAmbientCredentials, requestCredentials, type Admission, type Credentials } from "@procedures/client";
|
|
30
33
|
import { BodyError, readJsonBody } from "@procedures/client/node";
|
|
31
34
|
import type { HttpTransportOptions } from "../types.js";
|
|
32
35
|
import { MCP_HTTP_LIMITS, guardMcpRequest } from "./guard.js";
|
|
33
36
|
|
|
34
|
-
/**
|
|
37
|
+
/**
|
|
38
|
+
* The key of the admitted credentials of a request in the auth info of the MCP SDK (CLI-2b). The
|
|
39
|
+
* endpoint sets it on `req.auth`, and the SDK gives it to each message of that request.
|
|
40
|
+
*/
|
|
41
|
+
const ADMISSION_KEY = "procedures.admission";
|
|
42
|
+
|
|
43
|
+
/** The auth info of the MCP SDK on a request (`req.auth`), as far as the endpoint uses it. */
|
|
44
|
+
interface AuthInfoOf {
|
|
45
|
+
token: string;
|
|
46
|
+
clientId: string;
|
|
47
|
+
scopes: string[];
|
|
48
|
+
extra?: Record<string, unknown>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* This function gives the admitted credentials of the HTTP request of a message, from the auth
|
|
53
|
+
* info that the SDK gives the message (CLI-2b). It gives undefined when the endpoint admitted no
|
|
54
|
+
* credentials for the request.
|
|
55
|
+
*/
|
|
56
|
+
export function admittedCredentials(authInfo: { readonly extra?: Record<string, unknown> | undefined } | undefined): Credentials | undefined {
|
|
57
|
+
const value = authInfo?.extra?.[ADMISSION_KEY];
|
|
58
|
+
return typeof value === "object" && value !== null ? (value as Credentials) : undefined;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** This function puts the admitted credentials on the request, for the SDK and each message of the request. */
|
|
62
|
+
function attachAdmission(req: IncomingMessage, credentials: Credentials): void {
|
|
63
|
+
const carrier = req as IncomingMessage & { auth?: AuthInfoOf };
|
|
64
|
+
const auth: AuthInfoOf = carrier.auth ?? { token: "", clientId: "", scopes: [] };
|
|
65
|
+
carrier.auth = { ...auth, extra: { ...auth.extra, [ADMISSION_KEY]: credentials } };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The parts of the endpoint that its owner gives: the limits of the sessions (CLI-35) and the
|
|
70
|
+
* admission of each request (CLI-2b). The HTTP options win over the limits.
|
|
71
|
+
*/
|
|
35
72
|
export interface McpHttpSessionOptions {
|
|
36
73
|
/** The most sessions at one time. The default is 256 (CLI-51). `Infinity` turns the limit off. */
|
|
37
74
|
readonly maxSessions?: number | undefined;
|
|
@@ -39,6 +76,12 @@ export interface McpHttpSessionOptions {
|
|
|
39
76
|
readonly maxBodyBytes?: number | undefined;
|
|
40
77
|
/** The sessions that have a task in progress: the session limit does not close them. */
|
|
41
78
|
readonly busySessions?: (() => ReadonlySet<string>) | undefined;
|
|
79
|
+
/**
|
|
80
|
+
* The one check of the credentials of each request (CLI-2b): `admission()` of the handler of the
|
|
81
|
+
* face. A refusal gets 401 with `WWW-Authenticate`. Without it, the endpoint checks no
|
|
82
|
+
* credentials, and each call goes to the handler with the credentials of its request.
|
|
83
|
+
*/
|
|
84
|
+
readonly admission?: ((credentials: Credentials) => Promise<Admission>) | undefined;
|
|
42
85
|
}
|
|
43
86
|
|
|
44
87
|
/** The HTTP endpoint of an MCP server, with a session for each client. */
|
|
@@ -79,6 +122,13 @@ export function createHttpTransport(
|
|
|
79
122
|
const maxBodyBytes = options.maxBodyBytes ?? limits.maxBodyBytes ?? MCP_HTTP_LIMITS.maxBodyBytes;
|
|
80
123
|
if (!isLimit(maxSessions)) throw new RangeError(`maxSessions must be an integer of 1 or more, or Infinity, not ${String(maxSessions)}`);
|
|
81
124
|
if (!Number.isInteger(maxBodyBytes) || maxBodyBytes < 1) throw new RangeError(`maxBodyBytes must be an integer of 1 or more, not ${String(maxBodyBytes)}`);
|
|
125
|
+
// The endpoint has no token check of its own (CLI-2b). A token here has no check, so it stops the
|
|
126
|
+
// endpoint: the token goes to the server, and the admission of the server checks it
|
|
127
|
+
if (options.token !== undefined) {
|
|
128
|
+
throw new TypeError(
|
|
129
|
+
"The MCP endpoint checks no token of its own (CLI-2b). Give the token to the server of the face: hostPolicy({ token }), ProcedureServer({ token }), or policy.token of createMcpHost.",
|
|
130
|
+
);
|
|
131
|
+
}
|
|
82
132
|
|
|
83
133
|
const refuse = (res: ServerResponse, status: number, code: string, message: string, headers: Readonly<Record<string, string>> = {}, reason?: string): void => {
|
|
84
134
|
res.statusCode = status;
|
|
@@ -202,24 +252,47 @@ export function createHttpTransport(
|
|
|
202
252
|
await open(req, res, body);
|
|
203
253
|
};
|
|
204
254
|
|
|
255
|
+
/**
|
|
256
|
+
* The one check of the credentials of a request (CLI-2b): the admission of the server of the
|
|
257
|
+
* face. It gives the message of a refusal, or undefined when the request passes. The calls of
|
|
258
|
+
* the request get the admitted credentials through the auth info of the SDK.
|
|
259
|
+
*/
|
|
260
|
+
const admit = async (req: IncomingMessage): Promise<string | undefined> => {
|
|
261
|
+
const admission = limits.admission;
|
|
262
|
+
if (!admission) return undefined;
|
|
263
|
+
const admitted = await admission(requestCredentials({ headers: req.headers as Record<string, unknown>, url: req.url }));
|
|
264
|
+
if (!admitted.ok) return admitted.message;
|
|
265
|
+
attachAdmission(req, admitted.credentials);
|
|
266
|
+
return undefined;
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
/** This function admits a request, and then answers it: in its session, or in a new session. */
|
|
270
|
+
const admitAndServe = async (req: IncomingMessage, res: ServerResponse): Promise<void> => {
|
|
271
|
+
const refused = await admit(req);
|
|
272
|
+
if (refused !== undefined) {
|
|
273
|
+
refuse(res, 401, "UNAUTHORIZED", refused, { "www-authenticate": 'Bearer realm="mcp"' });
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
await serve(req, res);
|
|
277
|
+
};
|
|
278
|
+
|
|
205
279
|
const handle = (req: IncomingMessage, res: ServerResponse): boolean => {
|
|
206
280
|
const url = new URL(req.url ?? "/", "http://localhost");
|
|
207
281
|
if (url.pathname !== path) return false;
|
|
208
|
-
// A web page must not reach the tools: the host and the origin of the request
|
|
209
|
-
// (guard.ts)
|
|
210
|
-
// named origin (CSRF)
|
|
282
|
+
// A web page must not reach the tools: one check of the host and the origin of the request
|
|
283
|
+
// (guard.ts), then one admission of its credentials by the server. A server that reads
|
|
284
|
+
// cookies trusts a request with ambient credentials only from a named origin (CSRF)
|
|
211
285
|
const ambient = options.ambientCredentials === "always" || hasAmbientCredentials(req.headers);
|
|
212
286
|
const guard = guardMcpRequest(req, {
|
|
213
|
-
...(options.
|
|
214
|
-
...(options.
|
|
215
|
-
...(options.allowedHosts ? { allowedHosts: options.allowedHosts } : {}),
|
|
287
|
+
...(options.allowedOrigins !== undefined ? { allowedOrigins: options.allowedOrigins } : {}),
|
|
288
|
+
...(options.allowedHosts !== undefined ? { allowedHosts: options.allowedHosts } : {}),
|
|
216
289
|
ambient,
|
|
217
290
|
});
|
|
218
291
|
if (!guard.ok) {
|
|
219
292
|
refuse(res, guard.status, guard.code, guard.message, guard.headers, guard.reason);
|
|
220
293
|
return true;
|
|
221
294
|
}
|
|
222
|
-
|
|
295
|
+
admitAndServe(req, res).catch((error: unknown) => {
|
|
223
296
|
if (!res.headersSent) refuse(res, 500, "INTERNAL_ERROR", error instanceof Error ? error.message : String(error));
|
|
224
297
|
});
|
|
225
298
|
return true;
|
|
@@ -41,7 +41,7 @@ import {
|
|
|
41
41
|
import type { McpServerTransportOptions, McpServerTransportState, McpTextBlock, McpToolContent } from "../types.js";
|
|
42
42
|
import { createStdioTransport } from "./stdio.js";
|
|
43
43
|
import { createSseTransport } from "./sse.js";
|
|
44
|
-
import { createHttpTransport, type McpHttpTransport } from "./http.js";
|
|
44
|
+
import { admittedCredentials, createHttpTransport, type McpHttpTransport } from "./http.js";
|
|
45
45
|
import { optionalMs } from "@procedures/client";
|
|
46
46
|
import { createUntrustedEnvelopes } from "../untrusted.js";
|
|
47
47
|
import { taskStoreOf, type McpTaskStore } from "../tasks.js";
|
|
@@ -87,6 +87,8 @@ interface CallExtra {
|
|
|
87
87
|
sendNotification: (notification: ServerNotification) => Promise<void>;
|
|
88
88
|
/** The HTTP request of the message: present only for a network transport (Streamable HTTP) */
|
|
89
89
|
requestInfo?: { headers: Record<string, string | string[] | undefined>; url?: URL | undefined } | undefined;
|
|
90
|
+
/** The auth info of the HTTP request: the Streamable HTTP face puts the admitted credentials of the request in it (CLI-2b) */
|
|
91
|
+
authInfo?: { readonly extra?: Record<string, unknown> | undefined } | undefined;
|
|
90
92
|
/** The task store of the request, when the server has tasks (CLI-35) */
|
|
91
93
|
taskStore?: RequestTaskStore | undefined;
|
|
92
94
|
/** The time to keep the task that the client asked for */
|
|
@@ -106,9 +108,13 @@ function progressMessage(progress: number, total: number | undefined): string {
|
|
|
106
108
|
* cookies, the headers and the query of its HTTP request (CLI-10). A call of a local face without
|
|
107
109
|
* an HTTP request has the local marker (CLI-1). The local faces are stdio, whose peer is the parent
|
|
108
110
|
* process, and a given SDK transport, for example the in-memory transport. A session of Streamable
|
|
109
|
-
* HTTP is never local: without its HTTP request, the call presents no credentials.
|
|
111
|
+
* HTTP is never local: without its HTTP request, the call presents no credentials. The face
|
|
112
|
+
* admitted the HTTP request of a call one time (CLI-2b): then the call has the admitted
|
|
113
|
+
* credentials of that request, and the server does not run its checks again.
|
|
110
114
|
*/
|
|
111
115
|
function callCredentials(extra: CallExtra, localFace: boolean): Credentials {
|
|
116
|
+
const admitted = admittedCredentials(extra.authInfo);
|
|
117
|
+
if (admitted) return admitted;
|
|
112
118
|
const info = extra.requestInfo;
|
|
113
119
|
if (info) return requestCredentials({ headers: info.headers, url: info.url ? `${info.url.pathname}${info.url.search}` : undefined });
|
|
114
120
|
return localFace ? LOCAL_CREDENTIALS : {};
|
|
@@ -766,9 +772,13 @@ export class McpServerTransport implements ServerTransport {
|
|
|
766
772
|
} else if (this.options.transport === "http") {
|
|
767
773
|
// Streamable HTTP (roadmap R26): an MCP server of its own for each session
|
|
768
774
|
// The face trusts cookies only as the server reads them (CSRF)
|
|
769
|
-
const
|
|
775
|
+
const handler: Partial<RequestHandler> = this.server;
|
|
776
|
+
const ambientCredentials = handler.ambientCredentials;
|
|
770
777
|
const httpOptions = { ...this.options.httpOptions, ...(ambientCredentials ? { ambientCredentials } : {}) };
|
|
771
778
|
const taskStore = this.taskStore;
|
|
779
|
+
// The one admission of each HTTP request is the admission of the server (CLI-2b): its
|
|
780
|
+
// request hook, its token check and its authenticate function
|
|
781
|
+
const admission = handler.admission?.bind(handler);
|
|
772
782
|
this.http = createHttpTransport(
|
|
773
783
|
httpOptions,
|
|
774
784
|
async (sessionTransport) => {
|
|
@@ -781,8 +791,11 @@ export class McpServerTransport implements ServerTransport {
|
|
|
781
791
|
};
|
|
782
792
|
await sessionServer.connect(sessionTransport as unknown as SdkTransport);
|
|
783
793
|
},
|
|
784
|
-
|
|
785
|
-
|
|
794
|
+
{
|
|
795
|
+
// The session limit keeps each session with a task in progress open
|
|
796
|
+
...(taskStore ? { busySessions: () => taskStore.busySessions() } : {}),
|
|
797
|
+
...(admission ? { admission } : {}),
|
|
798
|
+
},
|
|
786
799
|
);
|
|
787
800
|
transport = undefined;
|
|
788
801
|
} else if (this.options.transport === "sse") {
|
package/src/types.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import type { Readable, Writable } from "node:stream";
|
|
8
8
|
import type { Transport as SdkTransport } from "@modelcontextprotocol/sdk/shared/transport.js";
|
|
9
|
-
import type { ElicitationPolicy, ProcedureRegistry } from "@procedures/client";
|
|
9
|
+
import type { BrowserGuardOptions, ElicitationPolicy, ProcedureRegistry } from "@procedures/client";
|
|
10
10
|
import type { McpServerInfo, McpTool, McpToolFilter } from "@procedures/mcp";
|
|
11
11
|
import type { Duration } from "@procedures/client";
|
|
12
12
|
import type { UntrustedContentOptions } from "./untrusted.js";
|
|
@@ -61,18 +61,19 @@ export interface HttpTransportOptions {
|
|
|
61
61
|
/** The path of the MCP endpoint (default: "/mcp") */
|
|
62
62
|
path?: string | undefined;
|
|
63
63
|
/**
|
|
64
|
-
* A secret that each request must send as `Authorization: Bearer <token>`. The endpoint
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
* `McpServerTransport` throws an error at `start()` for `httpOptions.token`. In `faces.http` of
|
|
70
|
-
* `createMcpHost()`, the token becomes the token of the policy, and a different token of the
|
|
71
|
-
* policy throws an error.
|
|
64
|
+
* A secret that each request must send as `Authorization: Bearer <token>`. The endpoint has no
|
|
65
|
+
* token check of its own (CLI-2b): the server of the face checks the token in its admission of
|
|
66
|
+
* each request. `createMcpHost` gives this token to its policy, as `policy.token`. Elsewhere the
|
|
67
|
+
* option throws an error, because a token without its check opens the endpoint.
|
|
68
|
+
* @deprecated Give the token to the server: `policy.token` of `createMcpHost`, or `hostPolicy({ token })`.
|
|
72
69
|
*/
|
|
73
70
|
token?: string | undefined;
|
|
74
|
-
/**
|
|
75
|
-
|
|
71
|
+
/**
|
|
72
|
+
* The browser origins that can call the server, besides its own origin: a list, `"*"` or a
|
|
73
|
+
* function, as `browser.allowedOrigins` of the host policy. `"*"` does not permit a request with
|
|
74
|
+
* cookies (CSRF).
|
|
75
|
+
*/
|
|
76
|
+
allowedOrigins?: BrowserGuardOptions["allowedOrigins"];
|
|
76
77
|
/**
|
|
77
78
|
* The host names that a request on a loopback address can name in its `Host` header, besides
|
|
78
79
|
* the loopback names (`browser.allowedHosts` of the host policy). `"*"` turns the check off.
|