@procedures/mcp-server 0.21.0 → 0.22.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 +44 -0
- package/README.md +4 -2
- package/dist/.tsbuildinfo +1 -1
- package/dist/face.d.ts +1 -1
- package/dist/face.d.ts.map +1 -1
- package/dist/host.d.ts +3 -2
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js.map +1 -1
- package/dist/tasks.d.ts +29 -7
- package/dist/tasks.d.ts.map +1 -1
- package/dist/tasks.js +32 -8
- package/dist/tasks.js.map +1 -1
- package/dist/transport/guard.d.ts +11 -4
- package/dist/transport/guard.d.ts.map +1 -1
- package/dist/transport/guard.js +5 -3
- package/dist/transport/guard.js.map +1 -1
- package/dist/transport/http.d.ts +3 -2
- package/dist/transport/http.d.ts.map +1 -1
- package/dist/transport/http.js +2 -1
- package/dist/transport/http.js.map +1 -1
- package/dist/types.d.ts +5 -5
- package/package.json +4 -4
- package/src/face.ts +1 -1
- package/src/host.ts +3 -2
- package/src/tasks.ts +42 -8
- package/src/transport/guard.ts +12 -5
- package/src/transport/http.ts +3 -2
- package/src/types.ts +5 -5
package/src/tasks.ts
CHANGED
|
@@ -11,12 +11,17 @@
|
|
|
11
11
|
* session and each face of a host.
|
|
12
12
|
* - A slot of the limit (`maxRunning`) stays taken until the work of the task ends, not until its
|
|
13
13
|
* status ends: a cancelled task holds its slot until its procedure returns. A task above the
|
|
14
|
-
* limit is created, and then fails at once with the reason
|
|
15
|
-
*
|
|
14
|
+
* limit is created, and then fails at once with the reason: the client gets a task, and reads
|
|
15
|
+
* the reason in the result of the task. A full store (`maxStored`) is different: it creates no
|
|
16
|
+
* task, and the request gets the error `-32600` with the code `TASKS_FULL`.
|
|
16
17
|
* - The time to keep a task (`ttl`) starts when the task ends. The store never removes a task
|
|
17
18
|
* whose work runs, so a short `ttl` does not let work escape the limit or the cancel. A refused
|
|
18
|
-
* task stays 1 minute
|
|
19
|
-
*
|
|
19
|
+
* task stays 1 minute.
|
|
20
|
+
* - Each session keeps at most 100 tasks, and the store keeps at most `maxStored` tasks for all
|
|
21
|
+
* the sessions together: 1000 by default (CLI-51). A new task removes the oldest task of its
|
|
22
|
+
* own session that ended. When the store is full and the session has no task that ended, the
|
|
23
|
+
* request of the new task gets an error, and the store keeps nothing of it. Thus a client
|
|
24
|
+
* cannot remove the results of another session. `Infinity` turns the total limit off.
|
|
20
25
|
* - A cancel of a task (`tasks/cancel`) aborts the signal of its procedure call. The close of a
|
|
21
26
|
* session cancels its tasks, and the stop of the host cancels each task.
|
|
22
27
|
*
|
|
@@ -46,6 +51,11 @@ export const MCP_TASK_LIMITS = {
|
|
|
46
51
|
refusedTtlMs: 60_000,
|
|
47
52
|
/** The most tasks that the store keeps for one session. A new task removes the oldest task of its session that ended. */
|
|
48
53
|
maxStoredPerSession: 100,
|
|
54
|
+
/**
|
|
55
|
+
* The most tasks that the store keeps for all the sessions together (CLI-51): ten sessions with
|
|
56
|
+
* their most tasks. Without it, only the session limit times 100 bounds the store.
|
|
57
|
+
*/
|
|
58
|
+
maxStored: 1_000,
|
|
49
59
|
} as const;
|
|
50
60
|
|
|
51
61
|
/** A task whose work did not end, as a status page of the host shows it. */
|
|
@@ -66,6 +76,12 @@ export interface McpTaskInfo {
|
|
|
66
76
|
export interface McpTaskStoreOptions {
|
|
67
77
|
/** The most tasks whose work runs at one time. The default is 8. */
|
|
68
78
|
readonly maxRunning?: number | undefined;
|
|
79
|
+
/**
|
|
80
|
+
* The most tasks that the store keeps for all the sessions together, also the tasks that ended
|
|
81
|
+
* (CLI-51). The default is 1000. A new task above the limit gets an error. `Infinity` turns the
|
|
82
|
+
* limit off.
|
|
83
|
+
*/
|
|
84
|
+
readonly maxStored?: number | undefined;
|
|
69
85
|
/** The time that a task and its result stay after the task ends, when the client asks for no time. The default is 1 hour. */
|
|
70
86
|
readonly ttl?: Duration | undefined;
|
|
71
87
|
/** This function runs when a task starts, changes its status or ends. An error of it does not stop a task. */
|
|
@@ -100,6 +116,7 @@ const TERMINAL = new Set<Task["status"]>(["completed", "failed", "cancelled"]);
|
|
|
100
116
|
export class McpTaskStore implements TaskStore {
|
|
101
117
|
private readonly entries = new Map<string, Entry>();
|
|
102
118
|
private readonly maxRunning: number;
|
|
119
|
+
private readonly maxStored: number;
|
|
103
120
|
private readonly ttlMs: number;
|
|
104
121
|
private closed = false;
|
|
105
122
|
|
|
@@ -107,6 +124,11 @@ export class McpTaskStore implements TaskStore {
|
|
|
107
124
|
const maxRunning = options.maxRunning ?? MCP_TASK_LIMITS.maxRunning;
|
|
108
125
|
if (!Number.isInteger(maxRunning) || maxRunning < 1) throw new RangeError(`maxRunning must be an integer of 1 or more, not ${String(maxRunning)}`);
|
|
109
126
|
this.maxRunning = maxRunning;
|
|
127
|
+
const maxStored = options.maxStored ?? MCP_TASK_LIMITS.maxStored;
|
|
128
|
+
if (maxStored !== Number.POSITIVE_INFINITY && (!Number.isInteger(maxStored) || maxStored < 1)) {
|
|
129
|
+
throw new RangeError(`maxStored must be an integer of 1 or more, or Infinity, not ${String(maxStored)}`);
|
|
130
|
+
}
|
|
131
|
+
this.maxStored = maxStored;
|
|
110
132
|
this.ttlMs = optionalMs(options.ttl, "ttl") ?? MCP_TASK_LIMITS.ttlMs;
|
|
111
133
|
}
|
|
112
134
|
|
|
@@ -143,12 +165,14 @@ export class McpTaskStore implements TaskStore {
|
|
|
143
165
|
}
|
|
144
166
|
|
|
145
167
|
/**
|
|
146
|
-
* This method removes the oldest tasks of a session that ended, until the session
|
|
147
|
-
* one more task.
|
|
168
|
+
* This method removes the oldest tasks of a session that ended, until the session and the store
|
|
169
|
+
* have room for one more task. It removes only the tasks of the same session, so a client cannot
|
|
170
|
+
* remove the results of another session. A task whose work runs stays: the limit of the running
|
|
171
|
+
* tasks bounds them.
|
|
148
172
|
*/
|
|
149
173
|
private makeRoom(sessionId: string | undefined): void {
|
|
150
174
|
const own = [...this.entries.values()].filter((entry) => entry.sessionId === sessionId);
|
|
151
|
-
let excess = own.length + 1 - MCP_TASK_LIMITS.maxStoredPerSession;
|
|
175
|
+
let excess = Math.max(own.length + 1 - MCP_TASK_LIMITS.maxStoredPerSession, this.entries.size + 1 - this.maxStored);
|
|
152
176
|
for (const entry of own) {
|
|
153
177
|
if (excess <= 0) break;
|
|
154
178
|
if (!entry.settled || !TERMINAL.has(entry.task.status)) continue;
|
|
@@ -165,10 +189,20 @@ export class McpTaskStore implements TaskStore {
|
|
|
165
189
|
return count;
|
|
166
190
|
}
|
|
167
191
|
|
|
168
|
-
/**
|
|
192
|
+
/**
|
|
193
|
+
* The store counts and inserts a task in one step, so a batch of requests cannot pass the limits.
|
|
194
|
+
* When the store is full after `makeRoom`, the request gets an error and the store keeps nothing.
|
|
195
|
+
*/
|
|
169
196
|
async createTask(params: CreateTaskOptions, _requestId: RequestId, request: Request, sessionId?: string): Promise<Task> {
|
|
170
197
|
if (this.closed) throw new McpError(ErrorCode.InvalidRequest, "The MCP host is stopping: it starts no task");
|
|
171
198
|
this.makeRoom(sessionId);
|
|
199
|
+
if (this.entries.size >= this.maxStored) {
|
|
200
|
+
throw new McpError(
|
|
201
|
+
ErrorCode.InvalidRequest,
|
|
202
|
+
`The host stores ${String(this.entries.size)} tasks, its limit of stored tasks. Try again when the time of a task ends.`,
|
|
203
|
+
{ code: "TASKS_FULL" },
|
|
204
|
+
);
|
|
205
|
+
}
|
|
172
206
|
const running = this.runningCount();
|
|
173
207
|
const refused =
|
|
174
208
|
running >= this.maxRunning ? `The host runs ${String(running)} tasks, its limit. Wait for a task to end, or cancel one.` : undefined;
|
package/src/transport/guard.ts
CHANGED
|
@@ -14,8 +14,10 @@
|
|
|
14
14
|
* - The face reads the body of a request that starts a session to a limit (4 MiB, the limit of
|
|
15
15
|
* the SDK), with `readJsonBody` of the core. Only an `initialize` request starts a session, so
|
|
16
16
|
* a stray POST makes no MCP server.
|
|
17
|
-
* -
|
|
18
|
-
* closes the session that was idle longest: no task in progress and no open response.
|
|
17
|
+
* - The face keeps at most `maxSessions` sessions: 256 by default (CLI-51). A new session above
|
|
18
|
+
* the limit closes the session that was idle longest: no task in progress and no open response.
|
|
19
|
+
* When each session is busy, the new session gets 503 with `Retry-After`. `Infinity` turns the
|
|
20
|
+
* limit off.
|
|
19
21
|
* - `assertLoopbackBind` refuses a listener on an address that is not loopback, unless the
|
|
20
22
|
* operator allows it. The owner of the listener calls it before it listens.
|
|
21
23
|
*/
|
|
@@ -27,13 +29,18 @@ import { bearerOf, checkBrowserRequest, isLoopbackHost, tokenMatches, type Brows
|
|
|
27
29
|
export const MCP_HTTP_LIMITS: {
|
|
28
30
|
/** The largest body of a request: 4 MiB, the limit of the SDK. */
|
|
29
31
|
readonly maxBodyBytes: 4_194_304;
|
|
30
|
-
/**
|
|
31
|
-
|
|
32
|
+
/**
|
|
33
|
+
* The most sessions at one time: 256 (CLI-51). Each live client of the MCP SDK keeps its GET
|
|
34
|
+
* stream open, so the limit does not close it. Thus the limit is much more than the clients of
|
|
35
|
+
* one host. Each live session also holds a socket, and 256 is much less than the usual limit of
|
|
36
|
+
* 1024 open files of a process. `maxSessions: Infinity` turns the limit off.
|
|
37
|
+
*/
|
|
38
|
+
readonly maxSessions: 256;
|
|
32
39
|
/** The seconds in the `Retry-After` header of a 503, when each session is busy. */
|
|
33
40
|
readonly retryAfterSeconds: 5;
|
|
34
41
|
} = {
|
|
35
42
|
maxBodyBytes: 4_194_304,
|
|
36
|
-
maxSessions:
|
|
43
|
+
maxSessions: 256,
|
|
37
44
|
retryAfterSeconds: 5,
|
|
38
45
|
};
|
|
39
46
|
|
package/src/transport/http.ts
CHANGED
|
@@ -18,7 +18,8 @@
|
|
|
18
18
|
* Thus the runner of the process owns the port, the stop in stages and the exit.
|
|
19
19
|
*
|
|
20
20
|
* The checks of each request are in `guard.ts` (CLI-35). Only an `initialize` request starts a
|
|
21
|
-
* session, each body has a limit
|
|
21
|
+
* session, and each body has a limit. The endpoint keeps at most `maxSessions` sessions: 256 by
|
|
22
|
+
* default (CLI-51), or no limit with `Infinity`.
|
|
22
23
|
*/
|
|
23
24
|
|
|
24
25
|
import { randomUUID } from "node:crypto";
|
|
@@ -32,7 +33,7 @@ import { MCP_HTTP_LIMITS, guardMcpRequest } from "./guard.js";
|
|
|
32
33
|
|
|
33
34
|
/** The limits of the sessions of the endpoint (CLI-35). The HTTP options win over them. */
|
|
34
35
|
export interface McpHttpSessionOptions {
|
|
35
|
-
/** The most sessions at one time. The default is
|
|
36
|
+
/** The most sessions at one time. The default is 256 (CLI-51). `Infinity` turns the limit off. */
|
|
36
37
|
readonly maxSessions?: number | undefined;
|
|
37
38
|
/** The largest body of a request. The default is 4 MiB, the limit of the SDK. */
|
|
38
39
|
readonly maxBodyBytes?: number | undefined;
|
package/src/types.ts
CHANGED
|
@@ -83,8 +83,8 @@ export interface HttpTransportOptions {
|
|
|
83
83
|
/**
|
|
84
84
|
* The most sessions at one time (CLI-35). A new session above the limit closes the session that
|
|
85
85
|
* was idle longest: no task in progress and no open response. When each session is busy, the
|
|
86
|
-
* new session gets 503 with `Retry-After`.
|
|
87
|
-
* @default
|
|
86
|
+
* new session gets 503 with `Retry-After`. `Infinity` turns the limit off.
|
|
87
|
+
* @default 256 (CLI-51, `MCP_HTTP_LIMITS.maxSessions`)
|
|
88
88
|
*/
|
|
89
89
|
maxSessions?: number | undefined;
|
|
90
90
|
/**
|
|
@@ -187,9 +187,9 @@ export interface McpServerTransportOptions {
|
|
|
187
187
|
streamLimits?: { items?: number; time?: Duration } | undefined;
|
|
188
188
|
|
|
189
189
|
/**
|
|
190
|
-
* MCP tasks (CLI-35): a store, or the options of a new store (`maxRunning`, `ttl`).
|
|
191
|
-
* tool whose procedure has `metadata.taskSupport` can run as a task. Without it, each
|
|
192
|
-
* is a normal request. The stop of the transport closes a store that it made.
|
|
190
|
+
* MCP tasks (CLI-35): a store, or the options of a new store (`maxRunning`, `maxStored`, `ttl`).
|
|
191
|
+
* With it, a tool whose procedure has `metadata.taskSupport` can run as a task. Without it, each
|
|
192
|
+
* tool call is a normal request. The stop of the transport closes a store that it made.
|
|
193
193
|
*/
|
|
194
194
|
tasks?: McpTasksOptions | undefined;
|
|
195
195
|
}
|