@procedures/mcp-server 0.27.6 → 0.27.8
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 +25 -0
- package/README.md +9 -0
- package/dist/.tsbuildinfo +1 -1
- package/dist/face.d.ts +2 -0
- package/dist/face.d.ts.map +1 -1
- package/dist/face.js +12 -4
- package/dist/face.js.map +1 -1
- package/dist/host.d.ts +2 -1
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +8 -2
- package/dist/host.js.map +1 -1
- package/dist/transport/http.d.ts +13 -0
- package/dist/transport/http.d.ts.map +1 -1
- package/dist/transport/http.js +39 -0
- package/dist/transport/http.js.map +1 -1
- package/dist/transport/mcp-transport.d.ts +3 -0
- package/dist/transport/mcp-transport.d.ts.map +1 -1
- package/dist/transport/mcp-transport.js +17 -1
- package/dist/transport/mcp-transport.js.map +1 -1
- package/package.json +4 -4
- package/src/face.ts +12 -4
- package/src/host.ts +9 -3
- package/src/transport/http.ts +52 -0
- package/src/transport/mcp-transport.ts +19 -1
package/src/face.ts
CHANGED
|
@@ -13,6 +13,8 @@
|
|
|
13
13
|
* authenticate function. The face has no token check of its own.
|
|
14
14
|
* - With `narrowing`, each call of the face has a narrower capability (CLI-43). Refer to
|
|
15
15
|
* `McpFaceOptions.narrowing`.
|
|
16
|
+
* - The stop of the intake ends the GET stream of each session without a task in progress
|
|
17
|
+
* (CLI-72). Then a new session and a new GET stream get 503. A call in progress completes.
|
|
16
18
|
* - The types are structural: this package does not depend on `@procedures/peer`.
|
|
17
19
|
*/
|
|
18
20
|
|
|
@@ -31,7 +33,7 @@ import {
|
|
|
31
33
|
type RequestHandler,
|
|
32
34
|
} from "@procedures/client";
|
|
33
35
|
import type { McpToolFilter } from "@procedures/mcp";
|
|
34
|
-
import { McpServerTransport } from "./transport/mcp-transport.js";
|
|
36
|
+
import { McpServerTransport, httpEndpointOf } from "./transport/mcp-transport.js";
|
|
35
37
|
import type { McpServerTransportOptions } from "./types.js";
|
|
36
38
|
|
|
37
39
|
/** The options of `mcpFace()`: the options of an MCP transport, with the path of its endpoint. */
|
|
@@ -180,10 +182,16 @@ export function mcpFace(options: McpFaceOptions = {}): (context: McpFaceContext)
|
|
|
180
182
|
return {
|
|
181
183
|
handler: (req, res) => transport.handleHttp(req, res),
|
|
182
184
|
// The tool calls go through the request tracking of the host: the face counts no work of its own.
|
|
183
|
-
//
|
|
185
|
+
// The stop of the intake ends the GET stream of each session without a task in progress, so the
|
|
186
|
+
// drain of the listener waits only for the calls and the tasks (CLI-72). Before, each GET stream
|
|
187
|
+
// stayed open until the close of the face, and each stop waited for the full cap of the drain.
|
|
184
188
|
parts: {
|
|
185
|
-
start: () =>
|
|
186
|
-
|
|
189
|
+
start: async () => {
|
|
190
|
+
await transport.start();
|
|
191
|
+
// A start after the stop of the intake accepts new sessions again
|
|
192
|
+
httpEndpointOf(transport)?.startIntake();
|
|
193
|
+
},
|
|
194
|
+
stopIntake: () => httpEndpointOf(transport)?.stopIntake(),
|
|
187
195
|
drain: async () => undefined,
|
|
188
196
|
inFlight: () => 0,
|
|
189
197
|
close: () => transport.stop(),
|
package/src/host.ts
CHANGED
|
@@ -46,7 +46,7 @@ import {
|
|
|
46
46
|
type Duration,
|
|
47
47
|
} from "@procedures/client";
|
|
48
48
|
import { proceduresToMcpTools, type McpTool, type McpToolFilter } from "@procedures/mcp";
|
|
49
|
-
import { McpServerTransport, distinctTools, namedTools } from "./transport/mcp-transport.js";
|
|
49
|
+
import { McpServerTransport, distinctTools, httpEndpointOf, namedTools } from "./transport/mcp-transport.js";
|
|
50
50
|
import type {
|
|
51
51
|
HttpTransportOptions,
|
|
52
52
|
McpServerTransportOptions,
|
|
@@ -226,7 +226,8 @@ export interface McpHost {
|
|
|
226
226
|
* `@mark1russell7/service` (CLI-11). The server is a transport part, and each storage is a
|
|
227
227
|
* dependency. A host with a stdio face declares the trigger `stdin-close`. Then the runner owns
|
|
228
228
|
* the stop: the end of the input starts the staged stop, and the stdio face does not stop the
|
|
229
|
-
* host by itself. A host declares one time.
|
|
229
|
+
* host by itself. A host declares one time. At the stop of the intake, the HTTP face ends the GET
|
|
230
|
+
* stream of each session without a task in progress (CLI-72).
|
|
230
231
|
*/
|
|
231
232
|
declare<S extends McpServiceBuilder>(svc: S): S;
|
|
232
233
|
/**
|
|
@@ -451,7 +452,12 @@ export function createMcpHost(options: McpHostOptions = {}): McpHost {
|
|
|
451
452
|
}
|
|
452
453
|
svc.server("mcp", {}, {
|
|
453
454
|
start: () => begin(),
|
|
454
|
-
stopIntake: () =>
|
|
455
|
+
stopIntake: () => {
|
|
456
|
+
server.stopIntake();
|
|
457
|
+
// The HTTP face ends the GET stream of each session without a task in progress, so the drain
|
|
458
|
+
// of a listener waits only for the calls and the tasks (CLI-72)
|
|
459
|
+
if (httpFace) httpEndpointOf(httpFace)?.stopIntake();
|
|
460
|
+
},
|
|
455
461
|
drain: (deadline, signal) => server.drain(deadline, signal),
|
|
456
462
|
inFlight: () => server.inFlight(),
|
|
457
463
|
close: async () => {
|
package/src/transport/http.ts
CHANGED
|
@@ -95,6 +95,19 @@ export interface McpHttpTransport {
|
|
|
95
95
|
handle(req: IncomingMessage, res: ServerResponse): boolean;
|
|
96
96
|
/** The number of open sessions. */
|
|
97
97
|
readonly sessions: number;
|
|
98
|
+
/**
|
|
99
|
+
* This function stops the intake (CLI-72): a new session and a new GET stream get 503. It ends
|
|
100
|
+
* the GET stream of each session without a task in progress. Thus the drain of a listener waits
|
|
101
|
+
* only for the calls and the tasks, not for the idle streams.
|
|
102
|
+
*/
|
|
103
|
+
stopIntake(): void;
|
|
104
|
+
/** This function accepts new sessions and new GET streams again, after `stopIntake()`. */
|
|
105
|
+
startIntake(): void;
|
|
106
|
+
/**
|
|
107
|
+
* After the stop of the intake, this function ends the GET stream of each session without a task
|
|
108
|
+
* in progress. The end of the work of a task calls it.
|
|
109
|
+
*/
|
|
110
|
+
endIdleStreams(): void;
|
|
98
111
|
/** This function closes each session. The listener stays with its owner. */
|
|
99
112
|
close(): Promise<void>;
|
|
100
113
|
}
|
|
@@ -121,6 +134,8 @@ export function createHttpTransport(
|
|
|
121
134
|
const openResponses = new Map<string, number>();
|
|
122
135
|
/** The new sessions that do not have their id yet: the session limit counts them */
|
|
123
136
|
let pending = 0;
|
|
137
|
+
/** False from the stop of the intake (CLI-72): a new session and a new GET stream get 503 */
|
|
138
|
+
let intakeOpen = true;
|
|
124
139
|
const maxSessions = options.maxSessions ?? limits.maxSessions ?? MCP_HTTP_LIMITS.maxSessions;
|
|
125
140
|
const maxBodyBytes = options.maxBodyBytes ?? limits.maxBodyBytes ?? MCP_HTTP_LIMITS.maxBodyBytes;
|
|
126
141
|
if (!isLimit(maxSessions)) throw new RangeError(`maxSessions must be an integer of 1 or more, or Infinity, not ${String(maxSessions)}`);
|
|
@@ -141,6 +156,22 @@ export function createHttpTransport(
|
|
|
141
156
|
res.end(JSON.stringify({ jsonrpc: "2.0", error: { code: -32001, message, data: { code, ...(reason !== undefined ? { reason } : {}) } }, id: null }));
|
|
142
157
|
};
|
|
143
158
|
|
|
159
|
+
/** The refusal of new work after the stop of the intake, as on the other faces: 503 with `UNAVAILABLE` and `Retry-After` */
|
|
160
|
+
const refuseStopping = (res: ServerResponse): void =>
|
|
161
|
+
refuse(res, 503, "UNAVAILABLE", "The server is stopping", { "retry-after": String(MCP_HTTP_LIMITS.retryAfterSeconds) });
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* After the stop of the intake, this function ends the GET stream of each session without a task
|
|
165
|
+
* in progress (CLI-72). The SDK ends the response in its protocol, so the client can connect
|
|
166
|
+
* again, to the next server. A session with a task in progress keeps its stream: the progress and
|
|
167
|
+
* the end of the task go on it.
|
|
168
|
+
*/
|
|
169
|
+
const endIdleStreams = (): void => {
|
|
170
|
+
if (intakeOpen) return;
|
|
171
|
+
const busy = limits.busySessions?.() ?? new Set<string>();
|
|
172
|
+
for (const [id, transport] of sessions) if (!busy.has(id)) transport.closeStandaloneSSEStream();
|
|
173
|
+
};
|
|
174
|
+
|
|
144
175
|
/** This function counts an open response of a session until the response closes. */
|
|
145
176
|
const track = (sessionId: string, res: ServerResponse): void => {
|
|
146
177
|
openResponses.set(sessionId, (openResponses.get(sessionId) ?? 0) + 1);
|
|
@@ -178,6 +209,11 @@ export function createHttpTransport(
|
|
|
178
209
|
|
|
179
210
|
/** This function starts a session for an `initialize` request whose body it read. */
|
|
180
211
|
const open = async (req: IncomingMessage, res: ServerResponse, body: unknown): Promise<void> => {
|
|
212
|
+
// After the stop of the intake, a new session goes to another server (CLI-72)
|
|
213
|
+
if (!intakeOpen) {
|
|
214
|
+
refuseStopping(res);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
181
217
|
// The limit counts the sessions and the new sessions without an id, so the awaits below cannot
|
|
182
218
|
// pass it. A new session leaves `pending` when it gets its id, or when it gets no session
|
|
183
219
|
pending++;
|
|
@@ -233,6 +269,14 @@ export function createHttpTransport(
|
|
|
233
269
|
refuse(res, 404, "SESSION_NOT_FOUND", "No session: start one with an initialize request");
|
|
234
270
|
return;
|
|
235
271
|
}
|
|
272
|
+
// After the stop of the intake, a new GET stream goes to another server (CLI-72). A POST and
|
|
273
|
+
// a DELETE of the session go on: a call in progress can need them, for example for the
|
|
274
|
+
// answer of an elicitation.
|
|
275
|
+
// The check and the start of the stream are in one turn, so the stop cannot come between them.
|
|
276
|
+
if (!intakeOpen && req.method === "GET") {
|
|
277
|
+
refuseStopping(res);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
236
280
|
lastUsed.set(sessionId, Date.now());
|
|
237
281
|
// A call or a GET stream keeps its session busy until its response closes
|
|
238
282
|
track(sessionId, res);
|
|
@@ -312,6 +356,14 @@ export function createHttpTransport(
|
|
|
312
356
|
get sessions() {
|
|
313
357
|
return sessions.size;
|
|
314
358
|
},
|
|
359
|
+
stopIntake: () => {
|
|
360
|
+
intakeOpen = false;
|
|
361
|
+
endIdleStreams();
|
|
362
|
+
},
|
|
363
|
+
startIntake: () => {
|
|
364
|
+
intakeOpen = true;
|
|
365
|
+
},
|
|
366
|
+
endIdleStreams,
|
|
315
367
|
close: async () => {
|
|
316
368
|
const open = [...sessions.values()];
|
|
317
369
|
sessions.clear();
|
|
@@ -180,6 +180,17 @@ function generateRequestId(): string {
|
|
|
180
180
|
return `mcp-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
|
|
181
181
|
}
|
|
182
182
|
|
|
183
|
+
/**
|
|
184
|
+
* The Streamable HTTP endpoint of each started transport. The stop parts of `mcpFace()` read it,
|
|
185
|
+
* for the stop of the intake (CLI-72). The root of the package does not export it.
|
|
186
|
+
*/
|
|
187
|
+
const httpEndpoints = new WeakMap<McpServerTransport, McpHttpTransport>();
|
|
188
|
+
|
|
189
|
+
/** This function gives the Streamable HTTP endpoint of a started transport, else undefined. Only the face of this package uses it. */
|
|
190
|
+
export function httpEndpointOf(transport: McpServerTransport): McpHttpTransport | undefined {
|
|
191
|
+
return httpEndpoints.get(transport);
|
|
192
|
+
}
|
|
193
|
+
|
|
183
194
|
/**
|
|
184
195
|
* MCP Server Transport
|
|
185
196
|
*
|
|
@@ -384,7 +395,11 @@ export class McpServerTransport implements ServerTransport {
|
|
|
384
395
|
void this.callTool(server, tool, args, background, false)
|
|
385
396
|
.then(finish)
|
|
386
397
|
.catch(() => undefined)
|
|
387
|
-
.finally(() =>
|
|
398
|
+
.finally(() => {
|
|
399
|
+
store.settle(id);
|
|
400
|
+
// After the stop of the intake, the GET stream of the session ends with the work of its last task (CLI-72)
|
|
401
|
+
this.http?.endIdleStreams();
|
|
402
|
+
});
|
|
388
403
|
return { task };
|
|
389
404
|
}
|
|
390
405
|
|
|
@@ -814,6 +829,7 @@ export class McpServerTransport implements ServerTransport {
|
|
|
814
829
|
if (transport) await this.mcpServer.connect(transport);
|
|
815
830
|
if (this.http) {
|
|
816
831
|
// The endpoint has no listener (CLI-11): the owner of a listener mounts handleHttp
|
|
832
|
+
httpEndpoints.set(this, this.http);
|
|
817
833
|
this.httpUrl = this.http.path;
|
|
818
834
|
this.log(`Using Streamable HTTP at the path ${this.httpUrl}`);
|
|
819
835
|
}
|
|
@@ -821,6 +837,7 @@ export class McpServerTransport implements ServerTransport {
|
|
|
821
837
|
this.removeRegistryListeners();
|
|
822
838
|
this.stdinEnd?.();
|
|
823
839
|
this.stdinEnd = undefined;
|
|
840
|
+
httpEndpoints.delete(this);
|
|
824
841
|
await this.http?.close().catch(() => undefined);
|
|
825
842
|
this.http = undefined;
|
|
826
843
|
throw error;
|
|
@@ -848,6 +865,7 @@ export class McpServerTransport implements ServerTransport {
|
|
|
848
865
|
}
|
|
849
866
|
|
|
850
867
|
await this.mcpServer.close();
|
|
868
|
+
httpEndpoints.delete(this);
|
|
851
869
|
await this.http?.close();
|
|
852
870
|
for (const sessionServer of this.sessionServers) await sessionServer.close().catch(() => undefined);
|
|
853
871
|
this.sessionServers.clear();
|