@orkestrel/mcp 0.0.28 → 0.0.29
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/README.md +11 -14
- package/dist/src/browser/index.d.ts +63 -65
- package/dist/src/browser/index.js +44 -44
- package/dist/src/browser/index.js.map +1 -1
- package/dist/src/core/index.cjs +233 -176
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +442 -366
- package/dist/src/core/index.d.ts +442 -366
- package/dist/src/core/index.js +233 -176
- package/dist/src/core/index.js.map +1 -1
- package/dist/src/server/index.cjs +109 -109
- package/dist/src/server/index.cjs.map +1 -1
- package/dist/src/server/index.d.cts +209 -203
- package/dist/src/server/index.d.ts +209 -203
- package/dist/src/server/index.js +109 -109
- package/dist/src/server/index.js.map +1 -1
- package/package.json +24 -25
|
@@ -29,9 +29,9 @@ var DEFAULT_MCP_KEEPALIVE_INTERVAL = 15e3;
|
|
|
29
29
|
/** Names the comment text written by the held-open MCP response keepalive. */
|
|
30
30
|
var SSE_KEEPALIVE_COMMENT = "keepalive";
|
|
31
31
|
/**
|
|
32
|
-
* Sets the default capacity of a session's
|
|
32
|
+
* Sets the default capacity of a session's folded resumable event log (the per-{@link
|
|
33
33
|
* import('./MCPSession.js').MCPSession} replay log) — the maximum number of pushed
|
|
34
|
-
* server→client messages retained for replay before the
|
|
34
|
+
* server→client messages retained for replay before the oldest is evicted.
|
|
35
35
|
*
|
|
36
36
|
* @remarks
|
|
37
37
|
* Bounds the replay log's memory: only the most-recent {@link DEFAULT_MCP_SESSION_CAPACITY}
|
|
@@ -57,7 +57,7 @@ var DEFAULT_MCP_SESSION_TTL = 3e5;
|
|
|
57
57
|
*
|
|
58
58
|
* @remarks
|
|
59
59
|
* Ten seconds. The load-bearing property is the ordering, not the magnitude: this bound stays
|
|
60
|
-
*
|
|
60
|
+
* below {@link import('@orkestrel/mcp').DEFAULT_MCP_REQUEST_TIMEOUT}, so a write the child never
|
|
61
61
|
* reads fails as an undeliverable message while the request that carried it is still open,
|
|
62
62
|
* rather than being masked by that request's own deadline expiring first. Override per
|
|
63
63
|
* transport with `delivery`; an explicit `0` there removes the bound.
|
|
@@ -67,16 +67,16 @@ var DEFAULT_MCP_DELIVERY = 1e4;
|
|
|
67
67
|
//#region src/server/helpers.ts
|
|
68
68
|
/**
|
|
69
69
|
* Pumps a controlled held-open exchange onto an open SSE stream — one `data:` event per
|
|
70
|
-
* notification in order, then the terminating response — and
|
|
70
|
+
* notification in order, then the terminating response — and end the exchange however the
|
|
71
71
|
* pump leaves.
|
|
72
72
|
*
|
|
73
73
|
* @remarks
|
|
74
74
|
* The Streamable-HTTP twin of {@link import('@orkestrel/mcp').sendStream}, and it owns exactly what
|
|
75
|
-
* that owns. The `finally` releases the exchange on
|
|
75
|
+
* that owns. The `finally` releases the exchange on every exit — the normal terminal, a
|
|
76
76
|
* producer that threw, a `write` that threw, and an abort alike — because nothing else will:
|
|
77
77
|
* a request whose client vanished cancels nothing by itself, so an exchange this pump walks
|
|
78
78
|
* away from keeps its producer, its request lifetime, and its live subscription slot forever.
|
|
79
|
-
* The exchange is released
|
|
79
|
+
* The exchange is released before the body ends, so the slot is already back when the response
|
|
80
80
|
* completes.
|
|
81
81
|
*
|
|
82
82
|
* Total — never throws and never rejects. A held-open SSE response has already sent its
|
|
@@ -203,7 +203,7 @@ function readLastEventId(request) {
|
|
|
203
203
|
* @remarks
|
|
204
204
|
* Returns `Response.json(buildJSONRPCError(undefined, JSONRPC_INVALID_REQUEST, 'Session not
|
|
205
205
|
* found'), { status: 404 })`, mirroring `createMCPRoutes`'s `400` transport-failure shape (a
|
|
206
|
-
* JSON-RPC error
|
|
206
|
+
* JSON-RPC error body with no id) but at the session-not-found status. Shared by
|
|
207
207
|
* every {@link import('./middlewares.js').createMCPSession} validation site — the
|
|
208
208
|
* non-`initialize` `POST` path, the resumable `GET {path}` open, and the `DELETE {path}`
|
|
209
209
|
* session-end (each a missing / unknown / TTL-evicted id) — so the single `404` envelope
|
|
@@ -219,7 +219,7 @@ function rejectUnknownSession() {
|
|
|
219
219
|
* the `createWebSocketServer` upgrade-path match.
|
|
220
220
|
*
|
|
221
221
|
* @remarks
|
|
222
|
-
* A `node:http` {@link import('node:http').IncomingMessage}'s `url` is the request
|
|
222
|
+
* A `node:http` {@link import('node:http').IncomingMessage}'s `url` is the request target
|
|
223
223
|
* (`'/mcp?x=1'`), narrowed with `isString` (never `as`) and defaulting to `'/'` for an
|
|
224
224
|
* absent target; it is parsed against a placeholder base (only the pathname matters for the upgrade
|
|
225
225
|
* decision) and the `pathname` returned. The upgrade handler compares this against its
|
|
@@ -240,7 +240,7 @@ function upgradeRequestPath(request) {
|
|
|
240
240
|
*
|
|
241
241
|
* @remarks
|
|
242
242
|
* Concatenates `buffer` (the carried-forward partial line from the previous call)
|
|
243
|
-
* with `chunk`, splits on `'\n'`, and returns every
|
|
243
|
+
* with `chunk`, splits on `'\n'`, and returns every complete line (a `'\r'` trailing
|
|
244
244
|
* a line, from a CRLF-framed peer, is trimmed) plus the final, possibly-empty
|
|
245
245
|
* fragment as the new `remainder` — the caller threads it back in as the next call's
|
|
246
246
|
* `buffer`. A chunk containing no `'\n'` yields no lines and the whole (buffer +
|
|
@@ -265,12 +265,12 @@ function extractLines(buffer, chunk) {
|
|
|
265
265
|
* The completion callback is the writable channel's backpressure boundary. A callback error and
|
|
266
266
|
* a synchronous `write` throw reject the returned promise with the original value.
|
|
267
267
|
*
|
|
268
|
-
* That callback is the
|
|
268
|
+
* That callback is the only thing that settles the promise: this helper holds no timer and no
|
|
269
269
|
* abort, so an output that neither confirms nor fails the write parks the promise for as long as
|
|
270
270
|
* the caller-owned stream holds the callback. A caller wanting a bound races this promise against
|
|
271
271
|
* one it owns — {@link import('./transports/StdioServerTransport.js').StdioServerTransport}
|
|
272
272
|
* registers such a bound per send and rejects it on `close()`, so closing the transport settles
|
|
273
|
-
* the
|
|
273
|
+
* the caller's `send` while the abandoned write stays with the stream that still holds its
|
|
274
274
|
* callback, reachable from nothing the transport retains.
|
|
275
275
|
*
|
|
276
276
|
* @param output - The writable stream that receives the line
|
|
@@ -365,7 +365,7 @@ function inferHeaderTarget(request) {
|
|
|
365
365
|
* body requires a protocol header after initialization. Messages name the expected value but
|
|
366
366
|
* never echo the client-supplied one.
|
|
367
367
|
*
|
|
368
|
-
* The expectation a
|
|
368
|
+
* The expectation a live session supplies is a different rule over a different input, so it
|
|
369
369
|
* is {@link inferSessionHeaderIssue} rather than a second arm of this one.
|
|
370
370
|
*
|
|
371
371
|
* @param request - The HTTP request carrying the headers
|
|
@@ -433,7 +433,7 @@ function inferHeaderIssue(request, invocation) {
|
|
|
433
433
|
* The session layer's rule, distinct from the body-derived one {@link inferHeaderIssue} owns:
|
|
434
434
|
* a live legacy session pinned its revision at `initialize`, so every later request on that
|
|
435
435
|
* session must name the same one. An absent header reads as `missing`, which the session
|
|
436
|
-
* middleware answers by
|
|
436
|
+
* middleware answers by supplying the pinned revision rather than refusing; a present header
|
|
437
437
|
* naming another revision reads as `mismatched` and is refused. The message names the session's
|
|
438
438
|
* revision and never echoes the client-supplied value.
|
|
439
439
|
*
|
|
@@ -465,8 +465,8 @@ function inferSessionHeaderIssue(request, version) {
|
|
|
465
465
|
*
|
|
466
466
|
* @remarks
|
|
467
467
|
* The custom-header half of the standard-header seam {@link inferHeaderIssue} owns, and it
|
|
468
|
-
* takes the
|
|
469
|
-
* rule to the `Mcp-Param-*` names the server's
|
|
468
|
+
* takes the served definition's projections rather than a header issue: SEP-2243 scopes the
|
|
469
|
+
* rule to the `Mcp-Param-*` names the server's own tool definitions annotate, so a name no
|
|
470
470
|
* parameter claims is another party's header and travels through untouched.
|
|
471
471
|
*
|
|
472
472
|
* For each recognized parameter the body's value at the parameter's own property path fixes
|
|
@@ -517,7 +517,7 @@ function inferParameterRefusal(request, parameters, values) {
|
|
|
517
517
|
*
|
|
518
518
|
* @remarks
|
|
519
519
|
* A supported legacy request is pinned exactly. A modern, malformed, absent, or unsupported
|
|
520
|
-
* request selects the newest supported legacy revision. The read is deliberately the
|
|
520
|
+
* request selects the newest supported legacy revision. The read is deliberately the same one
|
|
521
521
|
* {@link import('@orkestrel/mcp').buildInitializeResult} performs — `isMCPLegacyVersion` over
|
|
522
522
|
* the requested revision — because the session version this pins and the version that result
|
|
523
523
|
* echoes must be the one value. Routing through `inferVersion` cannot do it: that inferer is
|
|
@@ -558,22 +558,22 @@ function inferStatus(response, era) {
|
|
|
558
558
|
* Composes one incoming HTTP request lifetime with one MCP-owned SSE response lifetime.
|
|
559
559
|
*
|
|
560
560
|
* @remarks
|
|
561
|
-
* The composed {@link signal} observes request abort and
|
|
561
|
+
* The composed {@link signal} observes request abort and every way this response can end
|
|
562
562
|
* without one: consumer cancellation of the bridged body, a forwarding failure mid-pump, and a
|
|
563
563
|
* keepalive tick that finds the SSE stream already closed. That last pair is the whole point of
|
|
564
564
|
* the composition — a client that vanishes mid-stream aborts nothing by itself, so unless this
|
|
565
565
|
* object raises the signal on its own failure paths, the handler, the controlled stream, and
|
|
566
566
|
* the producer behind them all keep running for a response that can no longer be written.
|
|
567
|
-
* Graceful upstream completion is the one terminal that does
|
|
567
|
+
* Graceful upstream completion is the one terminal that does not abort: the body closes,
|
|
568
568
|
* because the exchange finished rather than ended.
|
|
569
569
|
*
|
|
570
570
|
* {@link bridge} preserves the source response status and headers, forwards its body bytes, and
|
|
571
571
|
* owns keepalive comments plus listener/timer cleanup until upstream completion, request abort,
|
|
572
572
|
* or consumer cancellation. This is a single-response lifecycle object, not a reusable bridge:
|
|
573
|
-
* a second {@link bridge} call
|
|
573
|
+
* a second {@link bridge} call throws rather than arming a second keepalive over one lifecycle.
|
|
574
574
|
* It supplies no handler or session policy.
|
|
575
575
|
*
|
|
576
|
-
* The keepalive interval is a
|
|
576
|
+
* The keepalive interval is a budget, sanitized like every other numeric knob in this package:
|
|
577
577
|
* anything that is not a positive integer — `0`, a negative, a fractional value, `NaN`,
|
|
578
578
|
* `Infinity` — falls back to {@link DEFAULT_MCP_KEEPALIVE_INTERVAL}, and a larger value clamps
|
|
579
579
|
* to Node's `2_147_483_647` ms timer maximum. None may reach the platform's timer floor, where
|
|
@@ -707,7 +707,7 @@ var HTTPDisconnect = class {
|
|
|
707
707
|
* method carrying a named target — `tools/call` and `prompts/get` against `params.name`,
|
|
708
708
|
* `resources/read` against `params.uri` — with a Base64-sentinel value decoded before the
|
|
709
709
|
* comparison; a missing, mismatched, or invalidly encoded value returns HTTP `400` + `-32020`.
|
|
710
|
-
* A protocol header naming a
|
|
710
|
+
* A protocol header naming a modern revision holds the request to that revision whatever shape
|
|
711
711
|
* its body arrived in, so a body with no parsable modern `_meta` returns HTTP `400` + `-32602`.
|
|
712
712
|
* Headerless `initialize` is accepted, while every other headerless request needs a live legacy
|
|
713
713
|
* session to supply its pinned version. A legacy-shaped request carrying a protocol header is
|
|
@@ -829,39 +829,39 @@ function createMCPPostHandler(mcp, options) {
|
|
|
829
829
|
/**
|
|
830
830
|
* Represents one MCP transport session — the per-session entity a {@link
|
|
831
831
|
* import('./middlewares.js').createMCPSession} middleware owns, keyed by its `id`, carrying the
|
|
832
|
-
* resumable server→client push channel with its bounded replay log
|
|
832
|
+
* resumable server→client push channel with its bounded replay log folded in.
|
|
833
833
|
*
|
|
834
834
|
* @remarks
|
|
835
835
|
* One entity carries the whole session: it holds the
|
|
836
|
-
* session `id`, its
|
|
836
|
+
* session `id`, its own bounded, replayable log of pushed server→client messages (the
|
|
837
837
|
* resumable GET-SSE channel — a private `#events` `Map` + a monotone `#counter`, with
|
|
838
838
|
* `capacity` / `ttl` eviction, not a separate store), and the set of open
|
|
839
839
|
* server→client SSE streams (a resumable `GET {path}` registers through `attach`, unregisters through
|
|
840
840
|
* `detach` on disconnect). Still a small entity (not a record), built minimal + extensible.
|
|
841
841
|
*
|
|
842
|
-
* - **`push` is the server-initiated primitive.** It
|
|
843
|
-
* a monotone base36 event id) and
|
|
844
|
-
* SSE event (`stream.write({ id, data })`). A push with
|
|
845
|
-
* so a client that connects (or reconnects with a `Last-Event-ID`)
|
|
842
|
+
* - **`push` is the server-initiated primitive.** It appends the message to the log (assigning
|
|
843
|
+
* a monotone base36 event id) and fans it out to every attached stream as one `id:`-tagged
|
|
844
|
+
* SSE event (`stream.write({ id, data })`). A push with no attached stream is still logged,
|
|
845
|
+
* so a client that connects (or reconnects with a `Last-Event-ID`) later replays it from the
|
|
846
846
|
* log. A `write` to a closed stream is a safe no-op (the {@link
|
|
847
847
|
* `@orkestrel/server`'s `createStream` contract), so a just-disconnected stream that
|
|
848
848
|
* has not yet been `detach`ed never throws. A replayed event and the live one carry the
|
|
849
|
-
*
|
|
849
|
+
* identical id (the log assigns it once).
|
|
850
850
|
*
|
|
851
851
|
* - **`replay(afterId)` is strictly-after.** It returns every retained log entry whose id sorts
|
|
852
|
-
*
|
|
853
|
-
* before attaching the stream for live pushes. The decision for an
|
|
852
|
+
* after `afterId` in append order — the missed-events list the `GET {path}` handler writes
|
|
853
|
+
* before attaching the stream for live pushes. The decision for an unknown / already-evicted
|
|
854
854
|
* `afterId` (the client's cursor fell off the back of the capacity window, or never existed):
|
|
855
|
-
* replay
|
|
856
|
-
* lost (its cursor is
|
|
855
|
+
* replay nothing. Replaying the whole retained log would re-deliver events the client never
|
|
856
|
+
* lost (its cursor is older than everything retained); returning `[]` lets the handler then
|
|
857
857
|
* stream only the fresh pushes that follow `attach` — the spec-sane resume.
|
|
858
858
|
*
|
|
859
|
-
* - **Bounded, append-ordered, plain `Map`.** The log lives in
|
|
860
|
-
* `Map<id, entry>` — insertion order
|
|
861
|
-
* eviction both walk the map directly.
|
|
859
|
+
* - **Bounded, append-ordered, plain `Map`.** The log lives in one insertion-ordered
|
|
860
|
+
* `Map<id, entry>` — insertion order is append order is id order, so `replay` and capacity
|
|
861
|
+
* eviction both walk the map directly. No database mirror — the log is process-local
|
|
862
862
|
* transport mechanics, not durable state. `push` first drops every entry older than `ttl`
|
|
863
863
|
* (lazy TTL — no background timer, the middleware's lazy-window idiom), appends, then evicts
|
|
864
|
-
* the
|
|
864
|
+
* the oldest entries until at most `capacity` remain; `replay` also runs the lazy TTL sweep
|
|
865
865
|
* first, so a stale entry is never replayed.
|
|
866
866
|
*
|
|
867
867
|
* - **No transport coupling beyond the SSE seam.** It holds session state + the generic {@link
|
|
@@ -950,15 +950,15 @@ var MCPSession = class {
|
|
|
950
950
|
//#region src/server/transports/WebSocketServerTransport.ts
|
|
951
951
|
/**
|
|
952
952
|
* Wraps a {@link NodeWebSocketInterface} (the RFC 6455 wire wrapper) as a
|
|
953
|
-
* {@link MCPMessageTransportInterface} — the per-connection JSON-RPC-over-WebSocket
|
|
953
|
+
* {@link MCPMessageTransportInterface} — the per-connection JSON-RPC-over-WebSocket server
|
|
954
954
|
* bridge, the bidirectional JSON-RPC message channel
|
|
955
955
|
* `createWebSocketServer` pumps `mcp.dispatch` over and the egress mirror's
|
|
956
956
|
* {@link import('./WebSocketClientTransport.js').WebSocketClientTransport} reuses.
|
|
957
957
|
*
|
|
958
958
|
* @remarks
|
|
959
|
-
* - **Reuses `MCPMessageTransportInterface`.** It
|
|
959
|
+
* - **Reuses `MCPMessageTransportInterface`.** It is the same generic carrier the HTTP
|
|
960
960
|
* client transport implements — `emitter` (`message` / `close` / `error`), `start`,
|
|
961
|
-
* `send`, `close` — so the WebSocket server and client both speak
|
|
961
|
+
* `send`, `close` — so the WebSocket server and client both speak one transport contract,
|
|
962
962
|
* no near-duplicate sibling interface. `session` is `undefined` (the stateless v1; a
|
|
963
963
|
* session id is the deferred sessions tier). The name keeps the role explicit even though
|
|
964
964
|
* the shape is shared.
|
|
@@ -966,13 +966,13 @@ var MCPSession = class {
|
|
|
966
966
|
* frame runs through the shared `deliverMessage` fold (parse, then narrow) — a
|
|
967
967
|
* well-formed {@link JSONRPCMessage} is re-emitted on this transport's `message` event (the
|
|
968
968
|
* parsed envelope the {@link import('@orkestrel/mcp').MCPServerInterface} pump dispatches), while
|
|
969
|
-
* a non-JSON or non-message frame is surfaced on `error` and
|
|
969
|
+
* a non-JSON or non-message frame is surfaced on `error` and dropped, never thrown. It
|
|
970
970
|
* also bridges the socket's `close` → this transport's `close`, and the socket's `error`.
|
|
971
971
|
* - **Outbound (`send`).** `send(message)` writes one text frame
|
|
972
972
|
* (`nodeWs.send(JSON.stringify(message))`). The underlying wrapper no-ops a write on a
|
|
973
973
|
* non-open socket and confirms nothing, so this bridge answers a closed channel from its own
|
|
974
974
|
* state and the socket's `readyState`: a `send` after `close()`, after the peer's close, or on
|
|
975
|
-
* a socket that is not `OPEN`
|
|
975
|
+
* a socket that is not `OPEN` rejects with `WebSocket transport is not connected` rather than
|
|
976
976
|
* resolving on a frame nobody wrote. `bindServer` catches that rejection and routes it to the
|
|
977
977
|
* dispatcher's `error` event, and it aborts every in-flight request the moment this transport's
|
|
978
978
|
* `close` fires — so a peer that disconnects mid-request is answered by no write at all.
|
|
@@ -983,7 +983,7 @@ var MCPSession = class {
|
|
|
983
983
|
* releases the same way, so a closed transport is never subscribed to a live socket.
|
|
984
984
|
* - **Observable.** Owns the `emitter` ({@link MCPMessageTransportEventMap}); the emitter
|
|
985
985
|
* isolates a listener throw (a buggy observer never corrupts the bridge). `error` is a
|
|
986
|
-
*
|
|
986
|
+
* domain event (a transport-level fault), distinct from the emitter's listener-error channel.
|
|
987
987
|
*/
|
|
988
988
|
var WebSocketServerTransport = class {
|
|
989
989
|
#emitter;
|
|
@@ -1040,7 +1040,7 @@ var WebSocketServerTransport = class {
|
|
|
1040
1040
|
//#endregion
|
|
1041
1041
|
//#region src/server/transports/WebSocketClientTransport.ts
|
|
1042
1042
|
/**
|
|
1043
|
-
* Drives a
|
|
1043
|
+
* Drives a remote MCP server over a WebSocket — a client
|
|
1044
1044
|
* {@link MCPMessageTransportInterface} for the Model Context Protocol, the
|
|
1045
1045
|
* egress mirror of {@link import('./factories.js').createWebSocketServer} and the WebSocket
|
|
1046
1046
|
* sibling of {@link import('@orkestrel/mcp').HTTPClientTransport}.
|
|
@@ -1050,16 +1050,16 @@ var WebSocketServerTransport = class {
|
|
|
1050
1050
|
* RFC 6455 client handshake: it opens a `node:http`(`s`) `GET` carrying `Connection: Upgrade`
|
|
1051
1051
|
* / `Upgrade: websocket` / a random `Sec-WebSocket-Key` / `Sec-WebSocket-Version: 13` /
|
|
1052
1052
|
* `Sec-WebSocket-Protocol: mcp` (plus any `options.headers`), awaits the client `'upgrade'`
|
|
1053
|
-
* event, and
|
|
1054
|
-
* — a mismatch (or a non-`101` response, or a request error)
|
|
1053
|
+
* event, and validates `Sec-WebSocket-Accept === computeWebSocketAccept(key)` (the D2 helper)
|
|
1054
|
+
* — a mismatch (or a non-`101` response, or a request error) rejects `start()` and the socket
|
|
1055
1055
|
* is destroyed. On success it wraps the raw upgraded socket in `createNodeWebSocket({ socket,
|
|
1056
|
-
* head })` (
|
|
1056
|
+
* head })` (client mode — no key → frames are masked per RFC 6455 §5.3) and bridges its
|
|
1057
1057
|
* `message`.
|
|
1058
1058
|
* - **The arriving socket is RE-ASKED for, never assumed.** `start()` suspends across that
|
|
1059
1059
|
* connect and upgrade, so it re-checks the transport's state before installing anything: a
|
|
1060
1060
|
* concurrent `start()` that already installed a socket, or a {@link close} that ended the
|
|
1061
|
-
* transport while the handshake was on the wire, both
|
|
1062
|
-
*
|
|
1061
|
+
* transport while the handshake was on the wire, both win — the socket that arrives late is
|
|
1062
|
+
* destroyed and never bound, so no orphan is left re-emitting frames at nobody. Both
|
|
1063
1063
|
* `start()` calls still resolve; exactly one socket is ever bound.
|
|
1064
1064
|
* - **Inbound (`message`).** Each decoded text frame runs through the shared `deliverMessage`
|
|
1065
1065
|
* fold (parse, then narrow) — a {@link JSONRPCMessage} re-emits on this transport's `message`
|
|
@@ -1067,14 +1067,14 @@ var WebSocketServerTransport = class {
|
|
|
1067
1067
|
* non-JSON / non-message frame surfaces on `error` and is dropped. The socket's `close`
|
|
1068
1068
|
* / `error` bridge to this transport's events.
|
|
1069
1069
|
* - **Outbound (`send`).** `send(message)` writes one masked text frame. A socket write is not
|
|
1070
|
-
* confirmed, so this transport answers a closed channel from its own state
|
|
1070
|
+
* confirmed, so this transport answers a closed channel from its own state and the socket's
|
|
1071
1071
|
* `readyState`: a `send` with no bound socket — before `start()`, after `close()`, or after the
|
|
1072
|
-
* peer ended the socket — and a `send` on a bound socket that is not `OPEN` both
|
|
1072
|
+
* peer ended the socket — and a `send` on a bound socket that is not `OPEN` both reject with
|
|
1073
1073
|
* `WebSocket transport is not connected`. It neither drops the message nor queues it for a
|
|
1074
1074
|
* connection this transport is not holding — the browser face queues a pre-open send, and this
|
|
1075
1075
|
* one, holding no connection to flush it onto, rejects that too.
|
|
1076
1076
|
* - **`close()`** unsubscribes from the socket, closes it, and fires `close` (idempotent). An
|
|
1077
|
-
* upgrade still on the wire is
|
|
1077
|
+
* upgrade still on the wire is destroyed, so a `close()` during the handshake ends the
|
|
1078
1078
|
* transport at once instead of waiting for a peer that may never answer — the suspended
|
|
1079
1079
|
* `start()` resolves, because the close is the outcome its caller asked for.
|
|
1080
1080
|
* - **URL scheme.** `options.url` accepts a `ws://` / `wss://` URL or an `http://` / `https://`
|
|
@@ -1082,7 +1082,7 @@ var WebSocketServerTransport = class {
|
|
|
1082
1082
|
* → TLS through `node:https`). Either reaches the same endpoint.
|
|
1083
1083
|
* - **Observable.** Owns the `emitter` ({@link MCPMessageTransportEventMap}); every emit
|
|
1084
1084
|
* the emitter isolates a listener throw (a buggy observer never corrupts the transport);
|
|
1085
|
-
* `error` is a
|
|
1085
|
+
* `error` is a domain event (a transport-level fault).
|
|
1086
1086
|
*
|
|
1087
1087
|
* @example
|
|
1088
1088
|
* ```ts
|
|
@@ -1222,7 +1222,7 @@ var WebSocketClientTransport = class {
|
|
|
1222
1222
|
//#endregion
|
|
1223
1223
|
//#region src/server/transports/StdioClientTransport.ts
|
|
1224
1224
|
/**
|
|
1225
|
-
* Drives a
|
|
1225
|
+
* Drives a child process MCP server over newline-delimited JSON-RPC on `stdin`/`stdout` —
|
|
1226
1226
|
* a {@link StdioClientTransportInterface}, the stdio sibling of {@link
|
|
1227
1227
|
* import('@orkestrel/mcp').HTTPClientTransport} and {@link
|
|
1228
1228
|
* import('./WebSocketClientTransport.js').WebSocketClientTransport}.
|
|
@@ -1238,14 +1238,14 @@ var WebSocketClientTransport = class {
|
|
|
1238
1238
|
* line is decoded and delivered through the shared {@link dispatchLines} helper — a well-formed
|
|
1239
1239
|
* {@link JSONRPCMessage} emits `message`, a malformed line emits `error` (never throws).
|
|
1240
1240
|
* - **Outbound (`send`).** `send(message)` writes one newline-terminated `JSON.stringify`d line
|
|
1241
|
-
* through the supervisor's `send` and
|
|
1241
|
+
* through the supervisor's `send` and awaits its answer, so this promise settles only after the
|
|
1242
1242
|
* host reports the line handled rather than the moment the write is queued. The supervisor never
|
|
1243
1243
|
* rejects — it answers `false` for a channel that was closed, destroyed, or ended, for a write
|
|
1244
1244
|
* that failed, or for one that remained unconfirmed through `delivery`. A call made without a
|
|
1245
1245
|
* live child rejects as not connected; a `false` answer from a live child rejects as unable to
|
|
1246
1246
|
* deliver. The supervisor does not disclose which cause produced that answer.
|
|
1247
1247
|
* - **`close()`** runs the supervisor's bounded termination and teardown, then fires `close` once
|
|
1248
|
-
* (idempotent). That teardown reaches the child's
|
|
1248
|
+
* (idempotent). That teardown reaches the child's terminal moment, where the supervisor freezes
|
|
1249
1249
|
* `evidence`, ends `lines`, and settles `exit` together, so this transport needs no release of
|
|
1250
1250
|
* its own to get its line pump back: the stream ends under the pump rather than throwing at it.
|
|
1251
1251
|
* A line the supervisor had already framed behind the one being delivered is dropped rather than
|
|
@@ -1258,7 +1258,7 @@ var WebSocketClientTransport = class {
|
|
|
1258
1258
|
* child's own process group `SIGTERM`, waits the grace window, then `SIGKILL`s through the same
|
|
1259
1259
|
* route, so the kill reaches grandchildren rather than orphaning them, while Windows ends the
|
|
1260
1260
|
* tree with `taskkill /F /T`, which nothing in the child can intercept.
|
|
1261
|
-
* - **Evidence.** `evidence` reports that retained stderr tail off the
|
|
1261
|
+
* - **Evidence.** `evidence` reports that retained stderr tail off the held child — its live tail
|
|
1262
1262
|
* while the child runs, and the value the supervisor froze at that child's terminal moment
|
|
1263
1263
|
* afterwards. The reference is held past that moment and replaced only by the next `start()`,
|
|
1264
1264
|
* which is what keeps a post-`close()` read stable without a private copy: the frozen value
|
|
@@ -1266,7 +1266,7 @@ var WebSocketClientTransport = class {
|
|
|
1266
1266
|
* cannot grow it. See {@link StdioClientTransportInterface.evidence} for the readings and the
|
|
1267
1267
|
* byte bound.
|
|
1268
1268
|
* - **Observable.** Owns the `emitter` ({@link MCPMessageTransportEventMap}); the
|
|
1269
|
-
* emitter isolates a listener throw; `error` is a
|
|
1269
|
+
* emitter isolates a listener throw; `error` is a domain event (a transport-level
|
|
1270
1270
|
* fault, including the child spawn cause the supervisor surfaces and the notice that this
|
|
1271
1271
|
* lifetime's `evidence` was cut off at the `drain` bound), distinct from the emitter's own
|
|
1272
1272
|
* listener-error channel.
|
|
@@ -1410,7 +1410,7 @@ var StdioClientTransport = class {
|
|
|
1410
1410
|
* - **`close()`** removes this transport's input and output subscriptions, rejects every
|
|
1411
1411
|
* pending send, and fires its `close`
|
|
1412
1412
|
* event (idempotent). It pauses the input only when the caller was not already reading
|
|
1413
|
-
* it at `start` (`readableFlowing !== true`)
|
|
1413
|
+
* it at `start` (`readableFlowing !== true`) and no `data` listener remains once this
|
|
1414
1414
|
* transport's own is removed — so a process holding `process.stdin` can exit, and a
|
|
1415
1415
|
* caller's own flow is never stopped underneath it. The transport preserves flowing versus
|
|
1416
1416
|
* non-flowing state and restores every caller-owned listener. A Node stream that had never been
|
|
@@ -1421,7 +1421,7 @@ var StdioClientTransport = class {
|
|
|
1421
1421
|
* `process.stdin`/`process.stdout`), so the transport never destroys, ends, or blanket-clears
|
|
1422
1422
|
* them.
|
|
1423
1423
|
* - **Observable.** Owns the `emitter` ({@link MCPMessageTransportEventMap}); the
|
|
1424
|
-
* emitter isolates a listener throw; `error` is a
|
|
1424
|
+
* emitter isolates a listener throw; `error` is a domain event (a transport-level
|
|
1425
1425
|
* fault), distinct from the emitter's own listener-error channel.
|
|
1426
1426
|
*/
|
|
1427
1427
|
var StdioServerTransport = class {
|
|
@@ -1529,37 +1529,37 @@ function createMCPContinuation(secret) {
|
|
|
1529
1529
|
* Creates the server-side mirror of
|
|
1530
1530
|
* {@link import('@orkestrel/mcp').createDuplexClientTransport}: the adapter that bridges a
|
|
1531
1531
|
* message-channel {@link MCPMessageTransportInterface}
|
|
1532
|
-
* (the shape the stdio and WebSocket
|
|
1532
|
+
* (the shape the stdio and WebSocket server transports already implement) onto the
|
|
1533
1533
|
* environment-agnostic {@link import('@orkestrel/mcp').MCPTransportInterface} port — what
|
|
1534
1534
|
* {@link createStdioServer} and {@link createWebSocketServer} pipe through `bindServer`, so
|
|
1535
|
-
* the request/reply/error pump those factories used to hand-roll identically now lives
|
|
1535
|
+
* the request/reply/error pump those factories used to hand-roll identically now lives once
|
|
1536
1536
|
* in the core binder. {@link import('@orkestrel/mcp').createDuplexClientTransport} adapts the
|
|
1537
|
-
* same
|
|
1537
|
+
* same contracts the other way.
|
|
1538
1538
|
*
|
|
1539
1539
|
* @remarks
|
|
1540
1540
|
* `send` decodes the already-serialized reply string back to a {@link JSONRPCMessage}
|
|
1541
1541
|
* and writes it through `transport.send` (the same `JSON.stringify` the underlying
|
|
1542
1542
|
* transport already performs, so the wire bytes are unchanged). `listen` filters
|
|
1543
|
-
* `transport`'s `message` event to
|
|
1543
|
+
* `transport`'s `message` event to invocations only — requests and notifications, never a
|
|
1544
1544
|
* stray response, exactly as the prior hand-rolled pumps did — and re-serializes each one
|
|
1545
1545
|
* back to a string for `bindServer`. `closed` bridges `transport`'s `close` event. `close`
|
|
1546
1546
|
* closes the underlying `transport`.
|
|
1547
1547
|
*
|
|
1548
|
-
* @remarks A message crossing this bridge is decoded and re-encoded
|
|
1549
|
-
*
|
|
1548
|
+
* @remarks A message crossing this bridge is decoded and re-encoded twice, and that is
|
|
1549
|
+
* accepted rather than accidental. Inbound: the carrier already parsed the frame into a
|
|
1550
1550
|
* {@link JSONRPCMessage}, and `listen` re-serializes it so `bindServer` can decode it again
|
|
1551
1551
|
* under the server's own `limit`. Outbound: `bindServer` serialized the reply, `send` parses
|
|
1552
1552
|
* it back, and the carrier stringifies it once more. The cost is two extra `JSON.parse` /
|
|
1553
|
-
* `JSON.stringify` round trips per message, paid to keep
|
|
1554
|
-
* of a hand-rolled one per carrier. It is
|
|
1553
|
+
* `JSON.stringify` round trips per message, paid to keep one pump in the core binder instead
|
|
1554
|
+
* of a hand-rolled one per carrier. It is bounded rather than unbounded because the binder
|
|
1555
1555
|
* decodes within `server.limit.message`, so an oversized frame is refused before the second
|
|
1556
1556
|
* decode rather than after it. Removing the cost means giving `MCPTransportInterface` a
|
|
1557
1557
|
* message-shaped face beside its string one, which every transport would then carry.
|
|
1558
1558
|
*
|
|
1559
1559
|
* @remarks Per {@link import('@orkestrel/mcp').MCPTransportInterface}, `listen`/`closed`
|
|
1560
|
-
* each hold
|
|
1560
|
+
* each hold the single current handler (a second call replaces the first, never adds).
|
|
1561
1561
|
* Because the underlying `transport.emitter` is ADD-based (`on` subscribes, never
|
|
1562
|
-
* replaces), this bridge installs
|
|
1562
|
+
* replaces), this bridge installs one stable emitter listener per event on first use
|
|
1563
1563
|
* and re-routes it to whichever handler is active (`undefined` while
|
|
1564
1564
|
* none is), so rebinding never double-dispatches.
|
|
1565
1565
|
*
|
|
@@ -1613,14 +1613,14 @@ function createDuplexServerTransport(transport) {
|
|
|
1613
1613
|
* hand to `router.add(...)`.
|
|
1614
1614
|
*
|
|
1615
1615
|
* @remarks
|
|
1616
|
-
* A
|
|
1616
|
+
* A single `POST {path}` route — `createMCPRoutes` is stateless. The handler reads its own
|
|
1617
1617
|
* request body (its own JSON parse try/catch), so it works with or without a session
|
|
1618
1618
|
* middleware mounted in front. It draws a sharp line between TRANSPORT-level and
|
|
1619
1619
|
* DISPATCH-level outcomes:
|
|
1620
1620
|
*
|
|
1621
1621
|
* - A **transport** failure — a malformed JSON body, or a parsed value that is not a
|
|
1622
|
-
* JSON-RPC
|
|
1623
|
-
* error / `-32600` Invalid Request), with the `id` it could not read
|
|
1622
|
+
* JSON-RPC invocation — is an HTTP `400` carrying a JSON-RPC error body (`-32700` Parse
|
|
1623
|
+
* error / `-32600` Invalid Request), with the `id` it could not read omitted.
|
|
1624
1624
|
* - Modern protocol/method/name headers are validated against the body; a mismatch is
|
|
1625
1625
|
* HTTP `400` + `-32020`. Headerless initialize is accepted, a live legacy session supplies
|
|
1626
1626
|
* its pinned revision, and every other headerless request is rejected.
|
|
@@ -1634,14 +1634,14 @@ function createDuplexServerTransport(transport) {
|
|
|
1634
1634
|
* the JSON-RPC envelope, then the stream ends) through `@orkestrel/server`'s generic
|
|
1635
1635
|
* {@link import('@orkestrel/server').createStream} seam; otherwise it is a plain JSON body.
|
|
1636
1636
|
*
|
|
1637
|
-
* **Sessions are a
|
|
1638
|
-
* session id. To make the transport
|
|
1639
|
-
* import('./middlewares.js').createMCPSession}
|
|
1637
|
+
* **Sessions are a separate, plug-and-play middleware.** `createMCPRoutes` mints / reads no
|
|
1638
|
+
* session id. To make the transport stateful, mount {@link
|
|
1639
|
+
* import('./middlewares.js').createMCPSession} in front — it owns the same `path`, mints +
|
|
1640
1640
|
* validates the `mcp-session-id`, and serves the resumable `GET {path}` + `DELETE {path}`,
|
|
1641
1641
|
* leaving this route to dispatch the validated `POST`.
|
|
1642
1642
|
*
|
|
1643
|
-
* This is
|
|
1644
|
-
*
|
|
1643
|
+
* This is mechanism, not policy: compose auth / rate-limiting (and the session middleware)
|
|
1644
|
+
* in front as ordinary middleware; the optional `origin` group carries the deployment's shared
|
|
1645
1645
|
* allowlist or explicitly delegates validation to an upstream layer.
|
|
1646
1646
|
*
|
|
1647
1647
|
* @typeParam TState - The consumer's opaque per-request state type
|
|
@@ -1670,8 +1670,8 @@ function createMCPRoutes(mcp, options) {
|
|
|
1670
1670
|
}];
|
|
1671
1671
|
}
|
|
1672
1672
|
/**
|
|
1673
|
-
* Creates the HTTP
|
|
1674
|
-
* — a {@link MCPMessageTransportInterface} that drives a
|
|
1673
|
+
* Creates the HTTP client transport for an {@link import('@orkestrel/mcp').MCPClientInterface}
|
|
1674
|
+
* — a {@link MCPMessageTransportInterface} that drives a remote Streamable-HTTP MCP server
|
|
1675
1675
|
* over `fetch`. The egress mirror of {@link createMCPRoutes}.
|
|
1676
1676
|
*
|
|
1677
1677
|
* @remarks
|
|
@@ -1682,17 +1682,17 @@ function createMCPRoutes(mcp, options) {
|
|
|
1682
1682
|
* @remarks
|
|
1683
1683
|
* Hand it to `createMCPClient({ transport })`: each JSON-RPC message the client sends is
|
|
1684
1684
|
* `POST`ed to `options.url` with `content-type: application/json` and an `Accept` of
|
|
1685
|
-
* both `application/json` and `text/event-stream` (the server answers with
|
|
1685
|
+
* both `application/json` and `text/event-stream` (the server answers with either — a
|
|
1686
1686
|
* plain JSON envelope or a Streamable-HTTP SSE `data:` event, decoded with `@orkestrel/sse`),
|
|
1687
1687
|
* and the reply is surfaced on the transport's `message` event for the client's id
|
|
1688
1688
|
* correlation. Add `options.headers` (for example, an `Authorization` bearer) to reach a guarded
|
|
1689
|
-
* server. `start` / `close` hold no connection; against a
|
|
1689
|
+
* server. `start` / `close` hold no connection; against a stateful server it captures the
|
|
1690
1690
|
* `mcp-session-id` from `initialize` and echoes it on later requests. It also captures
|
|
1691
1691
|
* the initialize result's `protocolVersion` and sends `mcp-protocol-version` alone on each
|
|
1692
1692
|
* subsequent legacy request. Modern requests derive protocol and method headers directly
|
|
1693
1693
|
* from the message, plus a name header only for `tools/call`.
|
|
1694
1694
|
*
|
|
1695
|
-
* @param options - `url` (the remote endpoint;
|
|
1695
|
+
* @param options - `url` (the remote endpoint; required), optional `headers` merged onto
|
|
1696
1696
|
* every request, optional `fetch` (default `globalThis.fetch`), and optional `timeout`
|
|
1697
1697
|
* (ms, applied with `AbortSignal.timeout`); see {@link HTTPClientTransportOptions}
|
|
1698
1698
|
* @returns A working {@link MCPMessageTransportInterface} over `fetch`
|
|
@@ -1713,7 +1713,7 @@ function createHTTPClientTransport(options) {
|
|
|
1713
1713
|
return new _src_core.HTTPClientTransport(options);
|
|
1714
1714
|
}
|
|
1715
1715
|
/**
|
|
1716
|
-
* Creates the MCP WebSocket transport
|
|
1716
|
+
* Creates the MCP WebSocket transport ingress — an {@link UpgradeHandler} that exposes a
|
|
1717
1717
|
* transport-agnostic {@link MCPDispatcherInterface} over a WebSocket, the WebSocket mirror of
|
|
1718
1718
|
* {@link createMCPRoutes}. Register it on the spine's upgrade seam.
|
|
1719
1719
|
*
|
|
@@ -1725,16 +1725,16 @@ function createHTTPClientTransport(options) {
|
|
|
1725
1725
|
* socket to the next handler (or destroys an unclaimed one): the `Upgrade` header is not
|
|
1726
1726
|
* `websocket`, the request path is not `options.path` (default {@link DEFAULT_MCP_PATH},
|
|
1727
1727
|
* `'/mcp'`), the `Sec-WebSocket-Key` is absent, or the `Sec-WebSocket-Version` is not `13`.
|
|
1728
|
-
* A decline
|
|
1728
|
+
* A decline never writes to the socket (it is not yet ours) — the spine owns the unclaimed
|
|
1729
1729
|
* outcome.
|
|
1730
1730
|
* - **Claims (returns `true`)** otherwise: it builds `createNodeWebSocket({ socket, key, head,
|
|
1731
1731
|
* protocol })` (SERVER mode → writes the `101` handshake, selects the configured subprotocol
|
|
1732
|
-
* only when the client's offer contains it, and sends
|
|
1732
|
+
* only when the client's offer contains it, and sends unmasked frames), wraps it in a
|
|
1733
1733
|
* {@link WebSocketServerTransport}, and pipes it through the core {@link
|
|
1734
1734
|
* import('@orkestrel/mcp').MCPTransportInterface} port through {@link
|
|
1735
1735
|
* createDuplexServerTransport} + {@link import('@orkestrel/mcp').bindServer}:
|
|
1736
|
-
* each inbound
|
|
1737
|
-
* as a frame — a
|
|
1736
|
+
* each inbound request runs through `mcp.dispatch`, and a defined response is written back
|
|
1737
|
+
* as a frame — a notification sends nothing, and a non-request message (a stray response) is
|
|
1738
1738
|
* ignored. A `dispatch` / `send` fault surfaces on `mcp.emitter`'s `error` event rather than
|
|
1739
1739
|
* escaping the (async) message pump.
|
|
1740
1740
|
* - **Closes on the spine's `stop`.** It holds every socket it claimed and, on `options.emitter`'s
|
|
@@ -1745,12 +1745,12 @@ function createHTTPClientTransport(options) {
|
|
|
1745
1745
|
* then have the connection cut mid-protocol. A socket the peer already dropped is gone from
|
|
1746
1746
|
* the set (its transport's `close` removes it), and closing a dead one is a no-op either way.
|
|
1747
1747
|
*
|
|
1748
|
-
* It is
|
|
1749
|
-
* handler
|
|
1748
|
+
* It is mechanism, not policy: compose an auth guard in front by registering an upgrade
|
|
1749
|
+
* handler before this one — that handler can claim (decline + destroy) an unauthenticated
|
|
1750
1750
|
* upgrade so it never reaches this pump.
|
|
1751
1751
|
*
|
|
1752
1752
|
* @param mcp - The transport-agnostic {@link MCPDispatcherInterface} to expose over WebSocket
|
|
1753
|
-
* @param options - The spine's `emitter` (
|
|
1753
|
+
* @param options - The spine's `emitter` (required — the `stop` event this ingress closes its
|
|
1754
1754
|
* sockets on), plus optional `path` (default {@link DEFAULT_MCP_PATH}) and `subprotocol`
|
|
1755
1755
|
* (default {@link MCP_WEBSOCKET_SUBPROTOCOL}); see {@link WebSocketServerOptions}
|
|
1756
1756
|
* @returns An {@link UpgradeHandler} to register with the spine's `upgrade` seam
|
|
@@ -1803,8 +1803,8 @@ function createWebSocketServer(mcp, options) {
|
|
|
1803
1803
|
};
|
|
1804
1804
|
}
|
|
1805
1805
|
/**
|
|
1806
|
-
* Creates the WebSocket
|
|
1807
|
-
* — a {@link MCPMessageTransportInterface} that drives a
|
|
1806
|
+
* Creates the WebSocket client transport for an {@link import('@orkestrel/mcp').MCPClientInterface}
|
|
1807
|
+
* — a {@link MCPMessageTransportInterface} that drives a remote MCP server over a WebSocket. The
|
|
1808
1808
|
* egress mirror of {@link createWebSocketServer} and the WebSocket sibling of {@link
|
|
1809
1809
|
* createHTTPClientTransport}.
|
|
1810
1810
|
*
|
|
@@ -1818,7 +1818,7 @@ function createWebSocketServer(mcp, options) {
|
|
|
1818
1818
|
* surfaced on the transport's `message` event for the client's id correlation. Add
|
|
1819
1819
|
* `options.headers` (for example, an `Authorization` bearer) to reach a guarded server.
|
|
1820
1820
|
*
|
|
1821
|
-
* @param options - `url` (the remote WebSocket endpoint;
|
|
1821
|
+
* @param options - `url` (the remote WebSocket endpoint; required) and optional `headers`
|
|
1822
1822
|
* merged onto the upgrade request; see {@link WebSocketClientTransportOptions}
|
|
1823
1823
|
* @returns A working {@link MCPMessageTransportInterface} over a WebSocket
|
|
1824
1824
|
*
|
|
@@ -1838,8 +1838,8 @@ function createWebSocketClientTransport(options) {
|
|
|
1838
1838
|
return new WebSocketClientTransport(options);
|
|
1839
1839
|
}
|
|
1840
1840
|
/**
|
|
1841
|
-
* Creates the stdio
|
|
1842
|
-
* — a {@link StdioClientTransportInterface} that spawns and drives a
|
|
1841
|
+
* Creates the stdio client transport for an {@link import('@orkestrel/mcp').MCPClientInterface}
|
|
1842
|
+
* — a {@link StdioClientTransportInterface} that spawns and drives a child process MCP server
|
|
1843
1843
|
* over newline-delimited JSON-RPC on `stdin`/`stdout`, the stdio sibling of {@link
|
|
1844
1844
|
* createHTTPClientTransport} and {@link createWebSocketClientTransport}.
|
|
1845
1845
|
*
|
|
@@ -1856,7 +1856,7 @@ function createWebSocketClientTransport(options) {
|
|
|
1856
1856
|
* waits before the `send` rejects. An omitted `delivery` selects {@link
|
|
1857
1857
|
* import('./constants.js').DEFAULT_MCP_DELIVERY}; an explicit `0` removes the bound.
|
|
1858
1858
|
*
|
|
1859
|
-
* @param options - `command` (the executable to spawn;
|
|
1859
|
+
* @param options - `command` (the executable to spawn; required), optional `args`,
|
|
1860
1860
|
* optional `env`, and an optional `delivery` bound in milliseconds on an unconfirmed
|
|
1861
1861
|
* `stdin` write; see {@link StdioClientTransportOptions}
|
|
1862
1862
|
* @returns A working {@link StdioClientTransportInterface} over a child process's stdio,
|
|
@@ -1878,7 +1878,7 @@ function createStdioClientTransport(options) {
|
|
|
1878
1878
|
return new StdioClientTransport(options);
|
|
1879
1879
|
}
|
|
1880
1880
|
/**
|
|
1881
|
-
* Creates the MCP stdio transport
|
|
1881
|
+
* Creates the MCP stdio transport ingress — pumps a transport-agnostic {@link
|
|
1882
1882
|
* MCPDispatcherInterface} over newline-delimited JSON-RPC on `stdin`/`stdout` (or an
|
|
1883
1883
|
* injected stream pair), the stdio mirror of {@link createWebSocketServer}.
|
|
1884
1884
|
*
|
|
@@ -1887,8 +1887,8 @@ function createStdioClientTransport(options) {
|
|
|
1887
1887
|
* `process.stdout`) in a {@link import('./transports/StdioServerTransport.js').StdioServerTransport}
|
|
1888
1888
|
* and pipes it through the core {@link import('@orkestrel/mcp').MCPTransportInterface} port
|
|
1889
1889
|
* through {@link createDuplexServerTransport} + {@link
|
|
1890
|
-
* import('@orkestrel/mcp').bindServer}: each inbound
|
|
1891
|
-
* a defined response is written back as a newline-terminated line — a
|
|
1890
|
+
* import('@orkestrel/mcp').bindServer}: each inbound request runs through `mcp.dispatch`, and
|
|
1891
|
+
* a defined response is written back as a newline-terminated line — a notification
|
|
1892
1892
|
* writes nothing, and a non-request message is ignored. A `dispatch` / `send` fault
|
|
1893
1893
|
* surfaces on `mcp.emitter`'s `error` event rather than escaping the (async) message
|
|
1894
1894
|
* pump.
|
|
@@ -1929,7 +1929,7 @@ function createStdioServer(mcp, options) {
|
|
|
1929
1929
|
* Creates the native MCP session {@link MiddlewareHandler} — the plug-and-play stateful layer
|
|
1930
1930
|
* that fronts a session-agnostic {@link import('./factories.js').createMCPRoutes}. Compose it
|
|
1931
1931
|
* with `router.use(createMCPSession())` (or the equivalent middleware seam), mirroring any
|
|
1932
|
-
* other closure-scoped stateful middleware. Has
|
|
1932
|
+
* other closure-scoped stateful middleware. Has no dependency on `@orkestrel/middleware` — the
|
|
1933
1933
|
* session store, mint-on-`initialize`, and resumable stream are all native to this package.
|
|
1934
1934
|
*
|
|
1935
1935
|
* @remarks
|
|
@@ -1942,32 +1942,32 @@ function createStdioServer(mcp, options) {
|
|
|
1942
1942
|
*
|
|
1943
1943
|
* - **`POST {path}`.** Buffers `const text = await request.text()` (so the downstream route
|
|
1944
1944
|
* can re-read it from a freshly-built forwarded `Request`). Resolves a session through {@link
|
|
1945
|
-
* readSessionHeader}: a
|
|
1946
|
-
*
|
|
1947
|
-
* isInitializeRequest})
|
|
1945
|
+
* readSessionHeader}: a valid id touches the entry and sets `context.state.session`; an
|
|
1946
|
+
* absent / unknown id whose (guarded) body parses to an `initialize` request ({@link
|
|
1947
|
+
* isInitializeRequest}) mints a fresh {@link MCPSession} (`crypto.randomUUID()`, the `session`
|
|
1948
1948
|
* options group) and sets `context.state.session`; neither → {@link rejectUnknownSession}
|
|
1949
1949
|
* (`404`). The
|
|
1950
1950
|
* minted entry pins the negotiated legacy revision, which is supplied to a later headerless
|
|
1951
1951
|
* live-session request. It then
|
|
1952
|
-
*
|
|
1952
|
+
* forwards a fresh `Request` carrying the buffered `text` (`next(forwarded)`) — never the
|
|
1953
1953
|
* already-consumed original — so the route re-reads the same body, and stamps the response
|
|
1954
|
-
* with {@link MCP_SESSION_HEADER}. The entry's `touched` instant is read
|
|
1955
|
-
* downstream response, because it means the
|
|
1954
|
+
* with {@link MCP_SESSION_HEADER}. The entry's `touched` instant is read after that
|
|
1955
|
+
* downstream response, because it means the last access: a request slower than `ttl` would
|
|
1956
1956
|
* otherwise store a session that is already expired, and the write-back RE-ASKS the store, so
|
|
1957
1957
|
* a `DELETE` arriving while the request was suspended is not undone.
|
|
1958
1958
|
* - **`GET {path}`.** Resolves the session the same way (no mint — only `initialize` mints);
|
|
1959
1959
|
* an invalid / unknown id is the same `404`. A valid session opens the resumable
|
|
1960
1960
|
* server→client stream through `@orkestrel/server`'s {@link import('@orkestrel/server').createStream}:
|
|
1961
|
-
* replays every event after the client's `Last-Event-ID` ({@link readLastEventId})
|
|
1961
|
+
* replays every event after the client's `Last-Event-ID` ({@link readLastEventId}) before
|
|
1962
1962
|
* attaching the stream for live pushes, then attaches; cancellation of the streamed response
|
|
1963
1963
|
* body composes with `request.signal` and detaches it. Long-lived — never `end()`ed here.
|
|
1964
1964
|
* - **`DELETE {path}`.** Resolves the session; a valid id deletes it from the store and answers
|
|
1965
1965
|
* `204`; an invalid / unknown id is the same `404`.
|
|
1966
1966
|
*
|
|
1967
|
-
* It is
|
|
1967
|
+
* It is mechanism, not policy, and additive: omit it entirely for the stateless default
|
|
1968
1968
|
* ({@link import('./factories.js').createMCPRoutes}'s only behavior). The `path` MUST match the
|
|
1969
1969
|
* `createMCPRoutes` `path` it fronts. The WebSocket transport is inherently one session per
|
|
1970
|
-
* connection (the socket
|
|
1970
|
+
* connection (the socket is the session), so this middleware does not apply to it.
|
|
1971
1971
|
*
|
|
1972
1972
|
* @typeParam TState - The consumer's `TState`, which MUST extend {@link MCPSessionState} so
|
|
1973
1973
|
* the resolved session can be threaded through `context.state.session`
|