@procedures/mcp-server 0.14.0 → 0.15.0
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 +48 -0
- package/README.md +99 -2
- package/dist/.tsbuildinfo +1 -1
- package/dist/face.d.ts +20 -1
- package/dist/face.d.ts.map +1 -1
- package/dist/face.js +49 -3
- package/dist/face.js.map +1 -1
- package/dist/host.d.ts +64 -4
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +73 -11
- package/dist/host.js.map +1 -1
- package/dist/index.d.ts +7 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/tasks.d.ts +131 -0
- package/dist/tasks.d.ts.map +1 -0
- package/dist/tasks.js +276 -0
- package/dist/tasks.js.map +1 -0
- package/dist/transport/guard.d.ts +61 -0
- package/dist/transport/guard.d.ts.map +1 -0
- package/dist/transport/guard.js +58 -0
- package/dist/transport/guard.js.map +1 -0
- package/dist/transport/http.d.ts +17 -2
- package/dist/transport/http.d.ts.map +1 -1
- package/dist/transport/http.js +157 -37
- package/dist/transport/http.js.map +1 -1
- package/dist/transport/mcp-transport.d.ts +19 -0
- package/dist/transport/mcp-transport.d.ts.map +1 -1
- package/dist/transport/mcp-transport.js +183 -76
- package/dist/transport/mcp-transport.js.map +1 -1
- package/dist/types.d.ts +43 -4
- package/dist/types.d.ts.map +1 -1
- package/package.json +5 -4
- package/src/face.ts +82 -4
- package/src/host.ts +133 -21
- package/src/index.ts +23 -1
- package/src/tasks.ts +318 -0
- package/src/transport/guard.ts +90 -0
- package/src/transport/http.ts +164 -36
- package/src/transport/mcp-transport.ts +200 -81
- package/src/types.ts +44 -4
package/src/face.ts
CHANGED
|
@@ -9,11 +9,24 @@
|
|
|
9
9
|
* - The tools are the procedures of the registry of the host that its policy exposes.
|
|
10
10
|
* - Without a token of its own, the face takes the token of the policy. Without origins of its
|
|
11
11
|
* own, it takes the permitted origins of the policy.
|
|
12
|
+
* - With `narrowing`, each call of the face has a narrower capability (CLI-43). Refer to
|
|
13
|
+
* `McpFaceOptions.narrowing`.
|
|
12
14
|
* - The types are structural: this package does not depend on `@procedures/peer`.
|
|
13
15
|
*/
|
|
14
16
|
|
|
15
17
|
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
16
|
-
import
|
|
18
|
+
import {
|
|
19
|
+
NARROWING_METADATA_KEY,
|
|
20
|
+
addNarrowing,
|
|
21
|
+
capabilityRefusal,
|
|
22
|
+
narrowWithin,
|
|
23
|
+
narrowingOf,
|
|
24
|
+
type CapabilityRequest,
|
|
25
|
+
type HostPolicy,
|
|
26
|
+
type ProcedurePath,
|
|
27
|
+
type ProcedureRegistry,
|
|
28
|
+
type RequestHandler,
|
|
29
|
+
} from "@procedures/client";
|
|
17
30
|
import type { McpToolFilter } from "@procedures/mcp";
|
|
18
31
|
import { McpServerTransport } from "./transport/mcp-transport.js";
|
|
19
32
|
import type { McpServerTransportOptions } from "./types.js";
|
|
@@ -26,6 +39,21 @@ export interface McpFaceOptions extends Omit<McpServerTransportOptions, "transpo
|
|
|
26
39
|
readonly token?: string | undefined;
|
|
27
40
|
/** The browser origins that can call the endpoint, besides loopback origins. The default is the list of the policy. */
|
|
28
41
|
readonly allowedOrigins?: string[] | undefined;
|
|
42
|
+
/** The host names that a request on a loopback address can name. The default is `browser.allowedHosts` of the policy. */
|
|
43
|
+
readonly allowedHosts?: readonly string[] | "*" | undefined;
|
|
44
|
+
/** The most sessions at one time (CLI-35). The default is no limit. */
|
|
45
|
+
readonly maxSessions?: number | undefined;
|
|
46
|
+
/** The largest body of a request, in bytes (CLI-35). The default is 4 MiB. */
|
|
47
|
+
readonly maxBodyBytes?: number | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* A narrower capability for each call of the face (CLI-43), for example the read-only mode
|
|
50
|
+
* `{ effects: { writes: [] } }`. The face adds it as one more source of the narrowing key
|
|
51
|
+
* (`procedure-narrowing`, CLI-30). Thus the server applies the meet of all the sources: the
|
|
52
|
+
* capability of the policy, the request hook and the key of the caller. A narrowing can only
|
|
53
|
+
* narrow. The face lists only the tools that the narrowed capability permits. A narrowing that
|
|
54
|
+
* is not valid stops the factory with an error. Without it, the face does not narrow.
|
|
55
|
+
*/
|
|
56
|
+
readonly narrowing?: CapabilityRequest<unknown> | readonly CapabilityRequest<unknown>[] | undefined;
|
|
29
57
|
}
|
|
30
58
|
|
|
31
59
|
/** What the MCP face needs of its host: the handler of its calls, the registry and the policy. */
|
|
@@ -49,23 +77,70 @@ export interface McpFace {
|
|
|
49
77
|
readonly transport: McpServerTransport;
|
|
50
78
|
}
|
|
51
79
|
|
|
80
|
+
/** The narrowing of a face: the handler that adds it to each call, and the rule of the tools that it permits. */
|
|
81
|
+
interface FaceNarrowing {
|
|
82
|
+
readonly handler: RequestHandler;
|
|
83
|
+
readonly permits: (path: ProcedurePath) => boolean;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* This function reads the narrowing of a face with the decoder and the meet of the wire (CLI-30).
|
|
88
|
+
* It throws for a part that is not valid, because each call of the face then gets a refusal. The
|
|
89
|
+
* capability of the tool list is the meet of the policy capability and the narrowing, without the
|
|
90
|
+
* time: the time limits each call, not the tools.
|
|
91
|
+
*/
|
|
92
|
+
function faceNarrowing(narrowing: NonNullable<McpFaceOptions["narrowing"]>, context: McpFaceContext): FaceNarrowing {
|
|
93
|
+
const wire = narrowingOf(narrowing);
|
|
94
|
+
// The sources as the decoder read them, frozen: a change of the options or of the metadata of a call does not change them
|
|
95
|
+
const sources = Object.freeze(wire.narrowings.map((source) => Object.freeze({ ...source })));
|
|
96
|
+
const untimed = sources.map((source): CapabilityRequest<unknown> => ({
|
|
97
|
+
...(source.clearance !== undefined ? { clearance: source.clearance } : {}),
|
|
98
|
+
...(source.effects !== undefined ? { effects: source.effects } : {}),
|
|
99
|
+
}));
|
|
100
|
+
const listed = narrowWithin(context.policy.capabilityOf?.(), untimed, context.policy.clearanceOrder);
|
|
101
|
+
const refusal = [...wire.notes, ...listed.notes].find((note) => note.kind === "refused");
|
|
102
|
+
if (refusal) throw new Error(`The narrowing of the MCP face is not valid: ${refusal.message}`);
|
|
103
|
+
const { capability } = listed;
|
|
104
|
+
const { handler: inner, registry } = context;
|
|
105
|
+
return {
|
|
106
|
+
handler: {
|
|
107
|
+
handle: (request) =>
|
|
108
|
+
inner.handle({ ...request, metadata: { ...request.metadata, [NARROWING_METADATA_KEY]: addNarrowing(request.metadata[NARROWING_METADATA_KEY], sources) } }),
|
|
109
|
+
...(inner.ambientCredentials ? { ambientCredentials: inner.ambientCredentials } : {}),
|
|
110
|
+
},
|
|
111
|
+
permits: (path) => {
|
|
112
|
+
const procedure = registry.get(path);
|
|
113
|
+
return procedure !== undefined && capabilityRefusal(procedure, capability) === undefined;
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
52
118
|
/**
|
|
53
119
|
* This function gives the MCP face of a procedures host. The host calls the factory one time.
|
|
54
120
|
*
|
|
55
121
|
* @example
|
|
56
122
|
* ```typescript
|
|
57
123
|
* const host = createProcedureHost({ registry, policy, faces: { http: true, mcp: mcpFace({ serverInfo }) }, listen });
|
|
124
|
+
* // A read-only MCP face next to the HTTP face of the same server
|
|
125
|
+
* const readOnly = mcpFace({ serverInfo, narrowing: { effects: { writes: [] } } });
|
|
58
126
|
* ```
|
|
59
127
|
*/
|
|
60
128
|
export function mcpFace(options: McpFaceOptions = {}): (context: McpFaceContext) => McpFace {
|
|
61
129
|
return (context) => {
|
|
62
|
-
const { path: givenPath, token: givenToken, allowedOrigins: givenOrigins, ...common } = options;
|
|
130
|
+
const { path: givenPath, token: givenToken, allowedOrigins: givenOrigins, allowedHosts: givenHosts, maxSessions, maxBodyBytes, narrowing: givenNarrowing, ...common } = options;
|
|
63
131
|
const path = givenPath ?? "/mcp";
|
|
64
132
|
const token = givenToken ?? context.policy.token;
|
|
65
133
|
const policyOrigins = context.policy.browser.allowedOrigins;
|
|
66
134
|
const allowedOrigins = givenOrigins ?? (Array.isArray(policyOrigins) ? [...policyOrigins] : undefined);
|
|
135
|
+
const allowedHosts = givenHosts ?? context.policy.browser.allowedHosts;
|
|
136
|
+
const narrowed = givenNarrowing !== undefined ? faceNarrowing(givenNarrowing, context) : undefined;
|
|
67
137
|
const toolFilter: McpToolFilter = { ...common.toolFilter, ...(context.policy.expose ? { include: context.policy.expose } : {}) };
|
|
68
|
-
|
|
138
|
+
if (narrowed) {
|
|
139
|
+
const included = toolFilter.include;
|
|
140
|
+
toolFilter.include = included ? (tool) => included(tool) && narrowed.permits(tool) : narrowed.permits;
|
|
141
|
+
}
|
|
142
|
+
const handler = narrowed?.handler ?? context.handler;
|
|
143
|
+
const transport = new McpServerTransport(handler, {
|
|
69
144
|
...common,
|
|
70
145
|
registry: context.registry,
|
|
71
146
|
toolFilter,
|
|
@@ -75,7 +150,10 @@ export function mcpFace(options: McpFaceOptions = {}): (context: McpFaceContext)
|
|
|
75
150
|
path,
|
|
76
151
|
...(token !== undefined ? { token } : {}),
|
|
77
152
|
...(allowedOrigins ? { allowedOrigins } : {}),
|
|
78
|
-
...(
|
|
153
|
+
...(allowedHosts !== undefined ? { allowedHosts } : {}),
|
|
154
|
+
...(maxSessions !== undefined ? { maxSessions } : {}),
|
|
155
|
+
...(maxBodyBytes !== undefined ? { maxBodyBytes } : {}),
|
|
156
|
+
...(handler.ambientCredentials ? { ambientCredentials: handler.ambientCredentials } : {}),
|
|
79
157
|
},
|
|
80
158
|
});
|
|
81
159
|
return {
|
package/src/host.ts
CHANGED
|
@@ -14,11 +14,17 @@
|
|
|
14
14
|
* limit, the interceptors and the capability. The host does not build the options of
|
|
15
15
|
* `ProcedureServer` by hand.
|
|
16
16
|
* - The faces: stdio, Streamable HTTP, and SDK transports. The stdio face stops the host when its
|
|
17
|
-
* input ends.
|
|
17
|
+
* input ends. The HTTP face mounts on a listener of the app (`handleHttp`).
|
|
18
|
+
* - The runner (CLI-11): `declare(svc)` gives the host to a service of `@mark1russell7/service`.
|
|
19
|
+
* Then `run()` starts the host and stops it in stages, and the end of the stdio input is the
|
|
20
|
+
* trigger `stdin-close` of the runner.
|
|
18
21
|
* - What each tool call gets: the questions to the person (elicitation, with the consents of the
|
|
19
22
|
* operator), the progress, the envelopes of untrusted text, and the instructions of the server.
|
|
23
|
+
* - The tasks (CLI-35): one store for each face, with a limit of tasks that run at one time. A
|
|
24
|
+
* procedure with `metadata.taskSupport` runs as a task when the client asks for one.
|
|
20
25
|
*/
|
|
21
26
|
|
|
27
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
22
28
|
import type { Transport as SdkTransport } from "@modelcontextprotocol/sdk/shared/transport.js";
|
|
23
29
|
import {
|
|
24
30
|
PROCEDURE_REGISTRY,
|
|
@@ -49,6 +55,7 @@ import type {
|
|
|
49
55
|
StdioTransportOptions,
|
|
50
56
|
} from "./types.js";
|
|
51
57
|
import type { UntrustedContentOptions } from "./untrusted.js";
|
|
58
|
+
import { taskStoreOf, type McpTaskStore, type McpTasksOptions } from "./tasks.js";
|
|
52
59
|
|
|
53
60
|
/** The stdio face of a host. */
|
|
54
61
|
export interface McpStdioFace extends Omit<StdioTransportOptions, "onEnd"> {
|
|
@@ -64,7 +71,8 @@ export interface McpHostFaces {
|
|
|
64
71
|
stdio?: boolean | McpStdioFace | undefined;
|
|
65
72
|
/**
|
|
66
73
|
* The face over Streamable HTTP. Without a token of its own, it takes the token of the policy,
|
|
67
|
-
* and without origins of its own, the permitted origins of the policy.
|
|
74
|
+
* and without origins of its own, the permitted origins of the policy. With `mount: true`, mount
|
|
75
|
+
* `McpHost.handleHttp` on a listener. A face with a listener of its own is deprecated (CLI-11).
|
|
68
76
|
*/
|
|
69
77
|
http?: HttpTransportOptions | undefined;
|
|
70
78
|
/** SDK transports to connect: the in-memory transport of a test, or an embedding. */
|
|
@@ -148,6 +156,11 @@ export interface McpHostOptions {
|
|
|
148
156
|
streamLimits?: { items?: number; time?: Duration } | undefined;
|
|
149
157
|
/** True: debug lines go to stderr. */
|
|
150
158
|
debug?: boolean | undefined;
|
|
159
|
+
/**
|
|
160
|
+
* MCP tasks (CLI-35): a store, or the options of a new store (`maxRunning`, the default 8, and
|
|
161
|
+
* `ttl`). Each face shares the store. Without it, the host has no tasks.
|
|
162
|
+
*/
|
|
163
|
+
tasks?: McpTasksOptions | undefined;
|
|
151
164
|
}
|
|
152
165
|
|
|
153
166
|
/** The state of a host: if it operates, and the state of each face. */
|
|
@@ -156,6 +169,35 @@ export interface McpHostState {
|
|
|
156
169
|
readonly faces: readonly McpServerTransportState[];
|
|
157
170
|
}
|
|
158
171
|
|
|
172
|
+
/**
|
|
173
|
+
* A transport part of the runner: the shape of `TransportImpl` of `@mark1russell7/service`. The
|
|
174
|
+
* types are structural, so this package imports no code of the runner.
|
|
175
|
+
*/
|
|
176
|
+
export interface McpRunnerPart {
|
|
177
|
+
start(signal: AbortSignal, life: unknown): Promise<void> | void;
|
|
178
|
+
stopIntake(): Promise<void> | void;
|
|
179
|
+
drain(deadline: number, signal: AbortSignal): Promise<void> | void;
|
|
180
|
+
inFlight(): number;
|
|
181
|
+
close(): Promise<void> | void;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** A storage of the server as a dependency of the runner: the shape of `DependencyHandle`. */
|
|
185
|
+
export type McpRunnerDependency = ReturnType<ProcedureServer["storageHandles"]>[number];
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* What `McpHost.declare()` uses of the builder of a service (`ServiceBuilder` of
|
|
189
|
+
* `@mark1russell7/service`). The types are structural.
|
|
190
|
+
*/
|
|
191
|
+
export interface McpServiceBuilder {
|
|
192
|
+
server(name: string, knobs: Readonly<Record<string, never>>, impl: McpRunnerPart): unknown;
|
|
193
|
+
dependency(
|
|
194
|
+
name: string,
|
|
195
|
+
spec: { system: null; knobs: Readonly<Record<string, never>>; operations: Readonly<Record<string, { idempotent: boolean }>> },
|
|
196
|
+
handle: McpRunnerDependency,
|
|
197
|
+
): unknown;
|
|
198
|
+
trigger(trigger: "stdin-close"): unknown;
|
|
199
|
+
}
|
|
200
|
+
|
|
159
201
|
/** An MCP host: its registry, its policy, its server and its faces. */
|
|
160
202
|
export interface McpHost {
|
|
161
203
|
readonly registry: ProcedureRegistry;
|
|
@@ -165,12 +207,31 @@ export interface McpHost {
|
|
|
165
207
|
readonly expose: ((path: ProcedurePath) => boolean) | undefined;
|
|
166
208
|
/** The MCP transports of the faces, in the order stdio, Streamable HTTP, SDK transports. */
|
|
167
209
|
readonly transports: readonly McpServerTransport[];
|
|
168
|
-
/**
|
|
210
|
+
/** The store of the tasks of each face, or undefined when the host has no tasks (CLI-35). */
|
|
211
|
+
readonly tasks: McpTaskStore | undefined;
|
|
212
|
+
/**
|
|
213
|
+
* A promise that resolves when the host stopped: by `stop()`, at the end of the stdio input, or
|
|
214
|
+
* at the close of its part under `run()`.
|
|
215
|
+
*/
|
|
169
216
|
readonly closed: Promise<void>;
|
|
170
|
-
/** This function starts each face. */
|
|
217
|
+
/** This function starts each face. A host that `declare()` gave to a service starts under `run()`. */
|
|
171
218
|
start(): Promise<void>;
|
|
172
219
|
/** This function stops the server and each face. The calls in flight get the grace time. */
|
|
173
220
|
stop(options?: { grace?: Duration | undefined }): Promise<void>;
|
|
221
|
+
/**
|
|
222
|
+
* This function declares the host on the builder of a service, for `run()` of
|
|
223
|
+
* `@mark1russell7/service` (CLI-11). The server is a transport part, and each storage is a
|
|
224
|
+
* dependency. A host with a stdio face declares the trigger `stdin-close`. Then the runner owns
|
|
225
|
+
* the stop: the end of the input starts the staged stop, and the stdio face does not stop the
|
|
226
|
+
* host by itself. A host declares one time.
|
|
227
|
+
*/
|
|
228
|
+
declare<S extends McpServiceBuilder>(svc: S): S;
|
|
229
|
+
/**
|
|
230
|
+
* This function answers a request of the Streamable HTTP face (`faces.http` with `mount: true`)
|
|
231
|
+
* and gives true. It gives false for another path, or without that face. Mount it on a listener,
|
|
232
|
+
* for example an `httpTransport` of `@mark1russell7/service/http`.
|
|
233
|
+
*/
|
|
234
|
+
handleHttp(req: IncomingMessage, res: ServerResponse): boolean;
|
|
174
235
|
/** The tools of the host at this time, from the registry and the expose rule. */
|
|
175
236
|
tools(): McpTool[];
|
|
176
237
|
/** The state of the host and of its faces. */
|
|
@@ -197,6 +258,9 @@ function isTransportList(value: SdkTransport | readonly SdkTransport[]): value i
|
|
|
197
258
|
return Array.isArray(value);
|
|
198
259
|
}
|
|
199
260
|
|
|
261
|
+
/** The operations of a storage as a dependency of the runner: a read can go again, a write cannot. */
|
|
262
|
+
const STORAGE_OPERATIONS = Object.freeze({ read: { idempotent: true }, write: { idempotent: false } });
|
|
263
|
+
|
|
200
264
|
/** The read-only capability of a host: no effect can write. */
|
|
201
265
|
const READ_ONLY_REQUEST = { effects: { writes: [] as readonly string[] } };
|
|
202
266
|
|
|
@@ -255,16 +319,26 @@ export function createMcpHost(options: McpHostOptions = {}): McpHost {
|
|
|
255
319
|
});
|
|
256
320
|
const server = new ProcedureServer({ ...procedureServerOptions(policy, registry), ...options.serverOptions });
|
|
257
321
|
|
|
322
|
+
// One store of the tasks for each face. The stop of the host closes a store that the host made
|
|
323
|
+
const tasks = taskStoreOf(options.tasks);
|
|
324
|
+
|
|
258
325
|
let resolveClosed: () => void = () => undefined;
|
|
259
326
|
const closed = new Promise<void>((resolve) => {
|
|
260
327
|
resolveClosed = resolve;
|
|
261
328
|
});
|
|
262
329
|
let stopping: Promise<void> | undefined;
|
|
263
330
|
let running = false;
|
|
331
|
+
// True after declare(): the runner starts and stops the host
|
|
332
|
+
let managed = false;
|
|
333
|
+
// Both stop paths close a task store that the host made: stop(), and the close of the runner part
|
|
334
|
+
const closeTasks = async (): Promise<void> => {
|
|
335
|
+
if (tasks?.owned) await tasks.store.close().catch(() => undefined);
|
|
336
|
+
};
|
|
264
337
|
const stop = (stopOptions: { grace?: Duration | undefined } = {}): Promise<void> => {
|
|
265
338
|
stopping ??= server
|
|
266
339
|
.stop(stopOptions.grace !== undefined ? { grace: stopOptions.grace } : {})
|
|
267
|
-
.finally(() => {
|
|
340
|
+
.finally(async () => {
|
|
341
|
+
await closeTasks();
|
|
268
342
|
running = false;
|
|
269
343
|
resolveClosed();
|
|
270
344
|
});
|
|
@@ -285,6 +359,7 @@ export function createMcpHost(options: McpHostOptions = {}): McpHost {
|
|
|
285
359
|
...(options.json ? { json: options.json } : {}),
|
|
286
360
|
...(options.streamLimits ? { streamLimits: options.streamLimits } : {}),
|
|
287
361
|
...(options.debug !== undefined ? { debug: options.debug } : {}),
|
|
362
|
+
...(tasks ? { tasks: { store: tasks.store } } : {}),
|
|
288
363
|
};
|
|
289
364
|
|
|
290
365
|
const faces = options.faces ?? { stdio: true };
|
|
@@ -298,27 +373,30 @@ export function createMcpHost(options: McpHostOptions = {}): McpHost {
|
|
|
298
373
|
stdio: {
|
|
299
374
|
...(stdio.stdin ? { stdin: stdio.stdin } : {}),
|
|
300
375
|
...(stdio.stdout ? { stdout: stdio.stdout } : {}),
|
|
301
|
-
|
|
376
|
+
// Under run(), the trigger stdin-close of the runner starts the stop
|
|
377
|
+
...(stdio.stopOnEnd !== false ? { onEnd: () => void (managed ? undefined : stop()) } : {}),
|
|
302
378
|
},
|
|
303
379
|
}),
|
|
304
380
|
);
|
|
305
381
|
}
|
|
382
|
+
let httpFace: McpServerTransport | undefined;
|
|
306
383
|
if (faces.http) {
|
|
307
384
|
// The HTTP face keeps the token and the origins of the policy (CLI-11 moves the checks to the host)
|
|
308
385
|
const policyOrigins = policy.browser.allowedOrigins;
|
|
309
386
|
const allowedOrigins = faces.http.allowedOrigins ?? (Array.isArray(policyOrigins) ? policyOrigins : undefined);
|
|
387
|
+
const allowedHosts = faces.http.allowedHosts ?? policy.browser.allowedHosts;
|
|
310
388
|
const token = faces.http.token ?? policy.token;
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
);
|
|
389
|
+
httpFace = new McpServerTransport(server, {
|
|
390
|
+
...common,
|
|
391
|
+
transport: "http",
|
|
392
|
+
httpOptions: {
|
|
393
|
+
...faces.http,
|
|
394
|
+
...(token !== undefined ? { token } : {}),
|
|
395
|
+
...(allowedOrigins ? { allowedOrigins: [...allowedOrigins] } : {}),
|
|
396
|
+
...(allowedHosts !== undefined ? { allowedHosts } : {}),
|
|
397
|
+
},
|
|
398
|
+
});
|
|
399
|
+
transports.push(httpFace);
|
|
322
400
|
}
|
|
323
401
|
const sdkTransports: readonly SdkTransport[] = faces.sdk === undefined ? [] : isTransportList(faces.sdk) ? faces.sdk : [faces.sdk];
|
|
324
402
|
for (const sdkTransport of sdkTransports) {
|
|
@@ -326,20 +404,54 @@ export function createMcpHost(options: McpHostOptions = {}): McpHost {
|
|
|
326
404
|
}
|
|
327
405
|
for (const transport of transports) server.addTransport(transport);
|
|
328
406
|
|
|
407
|
+
/** This function starts the server and its faces: by `start()`, or by the part of the runner. */
|
|
408
|
+
const begin = async (): Promise<void> => {
|
|
409
|
+
if (stopping) throw new Error("This MCP host stopped. A host stops one time: create a new host.");
|
|
410
|
+
if (stdio && stdio.redirectConsole !== false) redirectConsoleToStderr();
|
|
411
|
+
await server.start();
|
|
412
|
+
running = true;
|
|
413
|
+
};
|
|
414
|
+
|
|
329
415
|
return {
|
|
330
416
|
registry,
|
|
331
417
|
policy,
|
|
332
418
|
server,
|
|
333
419
|
expose: policy.expose,
|
|
334
420
|
transports,
|
|
421
|
+
tasks: tasks?.store,
|
|
335
422
|
closed,
|
|
336
423
|
async start() {
|
|
337
|
-
if (
|
|
338
|
-
|
|
339
|
-
await server.start();
|
|
340
|
-
running = true;
|
|
424
|
+
if (managed) throw new Error("This MCP host is declared on a service: run() of the service starts it.");
|
|
425
|
+
await begin();
|
|
341
426
|
},
|
|
342
427
|
stop,
|
|
428
|
+
declare(svc) {
|
|
429
|
+
if (managed) throw new Error("This MCP host is declared on a service already. A host declares one time.");
|
|
430
|
+
if (running || stopping) throw new Error("This MCP host started without a service. Declare it before it starts.");
|
|
431
|
+
managed = true;
|
|
432
|
+
// The storages connect before the server starts, and close after it (as the procedures host does)
|
|
433
|
+
for (const handle of server.storageHandles()) {
|
|
434
|
+
svc.dependency(handle.name, { system: null, knobs: {}, operations: STORAGE_OPERATIONS }, handle);
|
|
435
|
+
}
|
|
436
|
+
svc.server("mcp", {}, {
|
|
437
|
+
start: () => begin(),
|
|
438
|
+
stopIntake: () => server.stopIntake(),
|
|
439
|
+
drain: (deadline, signal) => server.drain(deadline, signal),
|
|
440
|
+
inFlight: () => server.inFlight(),
|
|
441
|
+
close: async () => {
|
|
442
|
+
try {
|
|
443
|
+
await server.close();
|
|
444
|
+
} finally {
|
|
445
|
+
await closeTasks();
|
|
446
|
+
running = false;
|
|
447
|
+
resolveClosed();
|
|
448
|
+
}
|
|
449
|
+
},
|
|
450
|
+
});
|
|
451
|
+
if (stdio) svc.trigger("stdin-close");
|
|
452
|
+
return svc;
|
|
453
|
+
},
|
|
454
|
+
handleHttp: (req, res) => httpFace?.handleHttp(req, res) ?? false,
|
|
343
455
|
tools: () => distinctTools(namedTools(proceduresToMcpTools(registry.getAll(), toolFilter), options.toolName)),
|
|
344
456
|
state: () => ({ running, faces: transports.map((transport) => transport.getState()) }),
|
|
345
457
|
};
|
package/src/index.ts
CHANGED
|
@@ -17,9 +17,28 @@ export { createSseTransport } from "./transport/sse.js";
|
|
|
17
17
|
|
|
18
18
|
// The host factory (CLI-10): one MCP host for each server of procedures
|
|
19
19
|
export { createMcpHost, mcpExposeRule, redirectConsoleToStderr } from "./host.js";
|
|
20
|
-
export type {
|
|
20
|
+
export type {
|
|
21
|
+
McpHost,
|
|
22
|
+
McpHostOptions,
|
|
23
|
+
McpHostFaces,
|
|
24
|
+
McpStdioFace,
|
|
25
|
+
McpHostServerOptions,
|
|
26
|
+
McpHostParts,
|
|
27
|
+
McpHostState,
|
|
28
|
+
McpRunnerPart,
|
|
29
|
+
McpRunnerDependency,
|
|
30
|
+
McpServiceBuilder,
|
|
31
|
+
} from "./host.js";
|
|
21
32
|
export { DEFAULT_ELICIT_TIMEOUT_MS } from "./transport/mcp-transport.js";
|
|
22
33
|
|
|
34
|
+
// MCP tasks (CLI-35): a long tool call runs as a task, with a limit of tasks that run at one time
|
|
35
|
+
export { McpTaskStore, MCP_TASK_LIMITS } from "./tasks.js";
|
|
36
|
+
export type { McpTaskInfo, McpTaskStoreOptions, McpTasksOptions } from "./tasks.js";
|
|
37
|
+
|
|
38
|
+
// The checks of a request to the Streamable HTTP face (CLI-35), for the face and a listener of a host
|
|
39
|
+
export { guardMcpRequest, assertLoopbackBind, MCP_HTTP_LIMITS } from "./transport/guard.js";
|
|
40
|
+
export type { McpRequestGuardOptions, McpGuardResult } from "./transport/guard.js";
|
|
41
|
+
|
|
23
42
|
// The MCP face of a procedures host (CLI-7): Streamable HTTP on a listener of the host
|
|
24
43
|
export { mcpFace } from "./face.js";
|
|
25
44
|
export type { McpFace, McpFaceContext, McpFaceOptions } from "./face.js";
|
|
@@ -52,6 +71,8 @@ export { mcpListToolsProcedure } from "./procedures/mcp/list-tools.js";
|
|
|
52
71
|
export type {
|
|
53
72
|
McpTool,
|
|
54
73
|
McpToolDefinition,
|
|
74
|
+
McpToolExecution,
|
|
75
|
+
McpTaskSupport,
|
|
55
76
|
McpToolFilter,
|
|
56
77
|
McpServerInfo,
|
|
57
78
|
McpTransportType,
|
|
@@ -61,6 +82,7 @@ export type {
|
|
|
61
82
|
export {
|
|
62
83
|
proceduresToMcpTools,
|
|
63
84
|
procedureToMcpTool,
|
|
85
|
+
taskSupportOf,
|
|
64
86
|
encodePath,
|
|
65
87
|
decodePath,
|
|
66
88
|
} from "@procedures/mcp";
|