@procedures/mcp-server 0.16.0 → 0.17.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 +35 -0
- package/README.md +4 -5
- package/dist/.tsbuildinfo +1 -1
- package/dist/face.d.ts.map +1 -1
- package/dist/face.js +0 -1
- package/dist/face.js.map +1 -1
- package/dist/host.d.ts +5 -5
- package/dist/transport/http.d.ts +10 -12
- package/dist/transport/http.d.ts.map +1 -1
- package/dist/transport/http.js +8 -39
- package/dist/transport/http.js.map +1 -1
- package/dist/transport/mcp-transport.d.ts +1 -1
- package/dist/transport/mcp-transport.d.ts.map +1 -1
- package/dist/transport/mcp-transport.js +4 -3
- package/dist/transport/mcp-transport.js.map +1 -1
- package/dist/types.d.ts +7 -21
- package/dist/types.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/face.ts +0 -1
- package/src/host.ts +5 -5
- package/src/transport/http.ts +12 -49
- package/src/transport/mcp-transport.ts +4 -3
- package/src/types.ts +7 -21
package/src/transport/http.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
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
|
|
8
|
+
* a local server apply: the listener of the service listens on loopback by default, the endpoint refuses a web page of
|
|
9
9
|
* another host or origin (DNS rebinding, cross-site requests), and with a token it refuses a
|
|
10
10
|
* request without the token.
|
|
11
11
|
*
|
|
@@ -13,13 +13,16 @@
|
|
|
13
13
|
* server (`principalOf`) and its token check apply. A server that reads cookies trusts a request
|
|
14
14
|
* with a cookie only from a named origin (CSRF).
|
|
15
15
|
*
|
|
16
|
+
* The endpoint has no listener of its own (CLI-11). The owner of a listener mounts `handle`: a
|
|
17
|
+
* listener of the service (`httpTransport` of `@mark1russell7/service/http`) or the procedures host.
|
|
18
|
+
* Thus the runner of the process owns the port, the stop in stages and the exit.
|
|
19
|
+
*
|
|
16
20
|
* The checks of each request are in `guard.ts` (CLI-35). Only an `initialize` request starts a
|
|
17
21
|
* session, each body has a limit, and with `maxSessions` the endpoint keeps at most that many sessions.
|
|
18
22
|
*/
|
|
19
23
|
|
|
20
24
|
import { randomUUID } from "node:crypto";
|
|
21
|
-
import {
|
|
22
|
-
import type { AddressInfo } from "node:net";
|
|
25
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
23
26
|
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
24
27
|
import { isInitializeRequest } from "@modelcontextprotocol/sdk/types.js";
|
|
25
28
|
import { hasAmbientCredentials } from "@procedures/client";
|
|
@@ -39,19 +42,13 @@ export interface McpHttpSessionOptions {
|
|
|
39
42
|
|
|
40
43
|
/** The HTTP endpoint of an MCP server, with a session for each client. */
|
|
41
44
|
export interface McpHttpTransport {
|
|
42
|
-
/**
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
*/
|
|
46
|
-
listen(): Promise<string>;
|
|
47
|
-
/**
|
|
48
|
-
* This function answers a request of the path of the endpoint and gives true. It gives false for
|
|
49
|
-
* another path, when the endpoint does not own its server.
|
|
50
|
-
*/
|
|
45
|
+
/** The path of the endpoint, for example `/mcp`. */
|
|
46
|
+
readonly path: string;
|
|
47
|
+
/** This function answers a request of the path of the endpoint and gives true. It gives false for another path. */
|
|
51
48
|
handle(req: IncomingMessage, res: ServerResponse): boolean;
|
|
52
49
|
/** The number of open sessions. */
|
|
53
50
|
readonly sessions: number;
|
|
54
|
-
/** This function closes each session
|
|
51
|
+
/** This function closes each session. The listener stays with its owner. */
|
|
55
52
|
close(): Promise<void>;
|
|
56
53
|
}
|
|
57
54
|
|
|
@@ -70,10 +67,6 @@ export function createHttpTransport(
|
|
|
70
67
|
limits: McpHttpSessionOptions = {},
|
|
71
68
|
): McpHttpTransport {
|
|
72
69
|
const path = options.path ?? "/mcp";
|
|
73
|
-
const host = options.host ?? "127.0.0.1";
|
|
74
|
-
const mounted = options.mount === true;
|
|
75
|
-
const owned = !mounted && options.server === undefined;
|
|
76
|
-
const server: HttpServer | undefined = mounted ? undefined : (options.server ?? createServer());
|
|
77
70
|
const sessions = new Map<string, StreamableHTTPServerTransport>();
|
|
78
71
|
/** The time of the last request of each session: the session limit closes the session that was idle longest */
|
|
79
72
|
const lastUsed = new Map<string, number>();
|
|
@@ -209,11 +202,7 @@ export function createHttpTransport(
|
|
|
209
202
|
|
|
210
203
|
const handle = (req: IncomingMessage, res: ServerResponse): boolean => {
|
|
211
204
|
const url = new URL(req.url ?? "/", "http://localhost");
|
|
212
|
-
if (url.pathname !== path)
|
|
213
|
-
if (!owned) return false;
|
|
214
|
-
refuse(res, 404, "NOT_FOUND", `No MCP endpoint at ${url.pathname}`);
|
|
215
|
-
return true;
|
|
216
|
-
}
|
|
205
|
+
if (url.pathname !== path) return false;
|
|
217
206
|
// A web page must not reach the tools: the host and the origin of the request, then the token
|
|
218
207
|
// (guard.ts). A server that reads cookies trusts a request with ambient credentials only from a
|
|
219
208
|
// named origin (CSRF)
|
|
@@ -233,44 +222,18 @@ export function createHttpTransport(
|
|
|
233
222
|
});
|
|
234
223
|
return true;
|
|
235
224
|
};
|
|
236
|
-
const onRequest = (req: IncomingMessage, res: ServerResponse): void => {
|
|
237
|
-
handle(req, res);
|
|
238
|
-
};
|
|
239
|
-
server?.on("request", onRequest);
|
|
240
|
-
|
|
241
225
|
return {
|
|
226
|
+
path,
|
|
242
227
|
handle,
|
|
243
|
-
listen: async () => {
|
|
244
|
-
if (!server) return path;
|
|
245
|
-
if (owned && !server.listening) {
|
|
246
|
-
await new Promise<void>((resolve, reject) => {
|
|
247
|
-
server.once("error", reject);
|
|
248
|
-
server.listen(options.port ?? 3003, host, () => {
|
|
249
|
-
server.off("error", reject);
|
|
250
|
-
resolve();
|
|
251
|
-
});
|
|
252
|
-
});
|
|
253
|
-
}
|
|
254
|
-
const address = server.address() as AddressInfo | null;
|
|
255
|
-
const shown = host === "0.0.0.0" ? "localhost" : host;
|
|
256
|
-
return `http://${shown}:${address?.port ?? options.port ?? 3003}${path}`;
|
|
257
|
-
},
|
|
258
228
|
get sessions() {
|
|
259
229
|
return sessions.size;
|
|
260
230
|
},
|
|
261
231
|
close: async () => {
|
|
262
|
-
server?.off("request", onRequest);
|
|
263
232
|
const open = [...sessions.values()];
|
|
264
233
|
sessions.clear();
|
|
265
234
|
lastUsed.clear();
|
|
266
235
|
openResponses.clear();
|
|
267
236
|
await Promise.all(open.map((transport) => transport.close().catch(() => undefined)));
|
|
268
|
-
if (owned && server?.listening) {
|
|
269
|
-
await new Promise<void>((resolve) => {
|
|
270
|
-
server.close(() => resolve());
|
|
271
|
-
server.closeAllConnections();
|
|
272
|
-
});
|
|
273
|
-
}
|
|
274
237
|
},
|
|
275
238
|
};
|
|
276
239
|
}
|
|
@@ -759,8 +759,9 @@ export class McpServerTransport implements ServerTransport {
|
|
|
759
759
|
// Connect MCP server to transport
|
|
760
760
|
if (transport) await this.mcpServer.connect(transport);
|
|
761
761
|
if (this.http) {
|
|
762
|
-
|
|
763
|
-
this.
|
|
762
|
+
// The endpoint has no listener (CLI-11): the owner of a listener mounts handleHttp
|
|
763
|
+
this.httpUrl = this.http.path;
|
|
764
|
+
this.log(`Using Streamable HTTP at the path ${this.httpUrl}`);
|
|
764
765
|
}
|
|
765
766
|
} catch (error) {
|
|
766
767
|
this.removeRegistryListeners();
|
|
@@ -806,7 +807,7 @@ export class McpServerTransport implements ServerTransport {
|
|
|
806
807
|
|
|
807
808
|
/**
|
|
808
809
|
* This method answers a request of the Streamable HTTP endpoint on a listener that another part
|
|
809
|
-
* owns (
|
|
810
|
+
* owns (CLI-7, CLI-11). It gives false for another path, and before `start()`.
|
|
810
811
|
*/
|
|
811
812
|
handleHttp(req: IncomingMessage, res: ServerResponse): boolean {
|
|
812
813
|
return this.http?.handle(req, res) ?? false;
|
package/src/types.ts
CHANGED
|
@@ -53,21 +53,11 @@ export type McpToolContentHook = (result: McpToolContentInput) => McpToolContent
|
|
|
53
53
|
* SSE transport configuration options.
|
|
54
54
|
*/
|
|
55
55
|
/**
|
|
56
|
-
* The options of the Streamable HTTP transport (roadmap R26).
|
|
57
|
-
*
|
|
58
|
-
* `
|
|
56
|
+
* The options of the Streamable HTTP transport (roadmap R26). The endpoint has no listener of its
|
|
57
|
+
* own (CLI-11): the owner of a listener mounts its request handler (`McpServerTransport.handleHttp`
|
|
58
|
+
* or `McpHost.handleHttp`), for example a listener of the service or the procedures host.
|
|
59
59
|
*/
|
|
60
60
|
export interface HttpTransportOptions {
|
|
61
|
-
/**
|
|
62
|
-
* Port to listen on (default: 3003)
|
|
63
|
-
* @deprecated The next MINOR removes the listener of the endpoint (CLI-11). Give `mount: true`, and mount `McpHost.handleHttp` on a listener of the service.
|
|
64
|
-
*/
|
|
65
|
-
port?: number | undefined;
|
|
66
|
-
/**
|
|
67
|
-
* Host to bind to (default: "127.0.0.1": only this computer can reach the tools)
|
|
68
|
-
* @deprecated The next MINOR removes the listener of the endpoint (CLI-11). The listener of the service sets its address.
|
|
69
|
-
*/
|
|
70
|
-
host?: string | undefined;
|
|
71
61
|
/** The path of the MCP endpoint (default: "/mcp") */
|
|
72
62
|
path?: string | undefined;
|
|
73
63
|
/** A secret that each request must send as `Authorization: Bearer <token>` */
|
|
@@ -80,13 +70,9 @@ export interface HttpTransportOptions {
|
|
|
80
70
|
*/
|
|
81
71
|
allowedHosts?: readonly string[] | "*" | undefined;
|
|
82
72
|
/**
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
|
|
86
|
-
server?: import("node:http").Server | undefined;
|
|
87
|
-
/**
|
|
88
|
-
* True: the endpoint has no server and attaches to none. The owner of a listener mounts its
|
|
89
|
-
* request handler (`McpServerTransport.handleHttp`), for example the procedures host (CLI-7).
|
|
73
|
+
* This option has no effect: the endpoint is always mounted (CLI-11). Code that followed the
|
|
74
|
+
* migration of 0.14.x gives `mount: true`, so the option stays one more release.
|
|
75
|
+
* @deprecated Remove it. A later MINOR removes the option.
|
|
90
76
|
*/
|
|
91
77
|
mount?: boolean | undefined;
|
|
92
78
|
/**
|
|
@@ -214,7 +200,7 @@ export interface McpServerTransportState {
|
|
|
214
200
|
toolCount: number;
|
|
215
201
|
/** Transport type in use */
|
|
216
202
|
transport: "stdio" | "sse" | "http";
|
|
217
|
-
/** The
|
|
203
|
+
/** The path of the Streamable HTTP endpoint, when the transport is "http". The owner of the listener gives the address (CLI-11). */
|
|
218
204
|
url?: string;
|
|
219
205
|
/** The open sessions of the Streamable HTTP endpoint, when the transport is "http" (CLI-35) */
|
|
220
206
|
sessions?: number;
|