@modelcontextprotocol/sdk 1.24.3 → 1.25.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/dist/cjs/client/auth-extensions.js +5 -9
- package/dist/cjs/client/auth-extensions.js.map +1 -1
- package/dist/cjs/client/auth.js +30 -37
- package/dist/cjs/client/auth.js.map +1 -1
- package/dist/cjs/client/index.d.ts +61 -14
- package/dist/cjs/client/index.d.ts.map +1 -1
- package/dist/cjs/client/index.js +117 -36
- package/dist/cjs/client/index.js.map +1 -1
- package/dist/cjs/client/middleware.js +3 -3
- package/dist/cjs/client/middleware.js.map +1 -1
- package/dist/cjs/client/sse.js +19 -27
- package/dist/cjs/client/sse.js.map +1 -1
- package/dist/cjs/client/stdio.js +18 -28
- package/dist/cjs/client/stdio.js.map +1 -1
- package/dist/cjs/client/streamableHttp.d.ts.map +1 -1
- package/dist/cjs/client/streamableHttp.js +39 -49
- package/dist/cjs/client/streamableHttp.js.map +1 -1
- package/dist/cjs/client/websocket.js +6 -11
- package/dist/cjs/client/websocket.js.map +1 -1
- package/dist/cjs/examples/client/elicitationUrlExample.js +4 -5
- package/dist/cjs/examples/client/elicitationUrlExample.js.map +1 -1
- package/dist/cjs/examples/client/simpleOAuthClient.js +3 -3
- package/dist/cjs/examples/client/simpleOAuthClient.js.map +1 -1
- package/dist/cjs/examples/client/simpleStreamableHttp.js +7 -9
- package/dist/cjs/examples/client/simpleStreamableHttp.js.map +1 -1
- package/dist/cjs/examples/client/simpleTaskInteractiveClient.js +1 -2
- package/dist/cjs/examples/client/simpleTaskInteractiveClient.js.map +1 -1
- package/dist/cjs/examples/server/honoWebStandardStreamableHttp.d.ts +10 -0
- package/dist/cjs/examples/server/honoWebStandardStreamableHttp.d.ts.map +1 -0
- package/dist/cjs/examples/server/honoWebStandardStreamableHttp.js +81 -0
- package/dist/cjs/examples/server/honoWebStandardStreamableHttp.js.map +1 -0
- package/dist/cjs/examples/server/jsonResponseStreamableHttp.js +10 -4
- package/dist/cjs/examples/server/jsonResponseStreamableHttp.js.map +1 -1
- package/dist/cjs/examples/server/simpleSseServer.js +6 -3
- package/dist/cjs/examples/server/simpleSseServer.js.map +1 -1
- package/dist/cjs/examples/server/simpleStatelessStreamableHttp.js +12 -6
- package/dist/cjs/examples/server/simpleStatelessStreamableHttp.js.map +1 -1
- package/dist/cjs/examples/server/simpleStreamableHttp.js +21 -11
- package/dist/cjs/examples/server/simpleStreamableHttp.js.map +1 -1
- package/dist/cjs/examples/server/simpleTaskInteractive.js +7 -11
- package/dist/cjs/examples/server/simpleTaskInteractive.js.map +1 -1
- package/dist/cjs/examples/server/sseAndStreamableHttpCompatibleServer.js +10 -7
- package/dist/cjs/examples/server/sseAndStreamableHttpCompatibleServer.js.map +1 -1
- package/dist/cjs/examples/server/standaloneSseWithGetStreamableHttp.js +1 -1
- package/dist/cjs/examples/server/standaloneSseWithGetStreamableHttp.js.map +1 -1
- package/dist/cjs/experimental/tasks/client.js +1 -2
- package/dist/cjs/experimental/tasks/client.js.map +1 -1
- package/dist/cjs/experimental/tasks/helpers.js +3 -5
- package/dist/cjs/experimental/tasks/helpers.js.map +1 -1
- package/dist/cjs/experimental/tasks/interfaces.d.ts +3 -3
- package/dist/cjs/experimental/tasks/interfaces.d.ts.map +1 -1
- package/dist/cjs/experimental/tasks/stores/in-memory.d.ts +1 -1
- package/dist/cjs/experimental/tasks/stores/in-memory.d.ts.map +1 -1
- package/dist/cjs/experimental/tasks/stores/in-memory.js +3 -5
- package/dist/cjs/experimental/tasks/stores/in-memory.js.map +1 -1
- package/dist/cjs/inMemory.js +5 -7
- package/dist/cjs/inMemory.js.map +1 -1
- package/dist/cjs/server/auth/handlers/token.js +1 -1
- package/dist/cjs/server/auth/handlers/token.js.map +1 -1
- package/dist/cjs/server/auth/providers/proxyProvider.js +11 -17
- package/dist/cjs/server/auth/providers/proxyProvider.js.map +1 -1
- package/dist/cjs/server/auth/router.js +3 -6
- package/dist/cjs/server/auth/router.js.map +1 -1
- package/dist/cjs/server/completable.js +1 -1
- package/dist/cjs/server/completable.js.map +1 -1
- package/dist/cjs/server/index.d.ts +3 -3
- package/dist/cjs/server/index.d.ts.map +1 -1
- package/dist/cjs/server/index.js +20 -30
- package/dist/cjs/server/index.js.map +1 -1
- package/dist/cjs/server/mcp.d.ts.map +1 -1
- package/dist/cjs/server/mcp.js +25 -11
- package/dist/cjs/server/mcp.js.map +1 -1
- package/dist/cjs/server/middleware/hostHeaderValidation.js +1 -1
- package/dist/cjs/server/middleware/hostHeaderValidation.js.map +1 -1
- package/dist/cjs/server/sse.js +14 -17
- package/dist/cjs/server/sse.js.map +1 -1
- package/dist/cjs/server/stdio.js +4 -7
- package/dist/cjs/server/stdio.js.map +1 -1
- package/dist/cjs/server/streamableHttp.d.ts +52 -154
- package/dist/cjs/server/streamableHttp.d.ts.map +1 -1
- package/dist/cjs/server/streamableHttp.js +78 -653
- package/dist/cjs/server/streamableHttp.js.map +1 -1
- package/dist/cjs/server/webStandardStreamableHttp.d.ts +267 -0
- package/dist/cjs/server/webStandardStreamableHttp.d.ts.map +1 -0
- package/dist/cjs/server/webStandardStreamableHttp.js +729 -0
- package/dist/cjs/server/webStandardStreamableHttp.js.map +1 -0
- package/dist/cjs/server/zod-compat.d.ts +3 -1
- package/dist/cjs/server/zod-compat.d.ts.map +1 -1
- package/dist/cjs/server/zod-compat.js +11 -19
- package/dist/cjs/server/zod-compat.js.map +1 -1
- package/dist/cjs/server/zod-json-schema-compat.js +5 -6
- package/dist/cjs/server/zod-json-schema-compat.js.map +1 -1
- package/dist/cjs/shared/metadataUtils.js +1 -2
- package/dist/cjs/shared/metadataUtils.js.map +1 -1
- package/dist/cjs/shared/protocol.d.ts +1 -1
- package/dist/cjs/shared/protocol.d.ts.map +1 -1
- package/dist/cjs/shared/protocol.js +76 -88
- package/dist/cjs/shared/protocol.js.map +1 -1
- package/dist/cjs/shared/transport.js +1 -1
- package/dist/cjs/shared/transport.js.map +1 -1
- package/dist/cjs/spec.types.d.ts +315 -26
- package/dist/cjs/spec.types.d.ts.map +1 -1
- package/dist/cjs/spec.types.js +2 -2
- package/dist/cjs/spec.types.js.map +1 -1
- package/dist/cjs/types.d.ts +1424 -1240
- package/dist/cjs/types.d.ts.map +1 -1
- package/dist/cjs/types.js +227 -136
- package/dist/cjs/types.js.map +1 -1
- package/dist/cjs/validation/ajv-provider.d.ts +1 -1
- package/dist/cjs/validation/ajv-provider.d.ts.map +1 -1
- package/dist/cjs/validation/ajv-provider.js +4 -5
- package/dist/cjs/validation/ajv-provider.js.map +1 -1
- package/dist/cjs/validation/cfworker-provider.js +4 -5
- package/dist/cjs/validation/cfworker-provider.js.map +1 -1
- package/dist/cjs/validation/types.d.ts +12 -2
- package/dist/cjs/validation/types.d.ts.map +1 -1
- package/dist/esm/client/auth-extensions.js +5 -9
- package/dist/esm/client/auth-extensions.js.map +1 -1
- package/dist/esm/client/auth.js +30 -37
- package/dist/esm/client/auth.js.map +1 -1
- package/dist/esm/client/index.d.ts +61 -14
- package/dist/esm/client/index.d.ts.map +1 -1
- package/dist/esm/client/index.js +118 -37
- package/dist/esm/client/index.js.map +1 -1
- package/dist/esm/client/middleware.js +3 -3
- package/dist/esm/client/middleware.js.map +1 -1
- package/dist/esm/client/sse.js +19 -27
- package/dist/esm/client/sse.js.map +1 -1
- package/dist/esm/client/stdio.js +18 -28
- package/dist/esm/client/stdio.js.map +1 -1
- package/dist/esm/client/streamableHttp.d.ts.map +1 -1
- package/dist/esm/client/streamableHttp.js +40 -50
- package/dist/esm/client/streamableHttp.js.map +1 -1
- package/dist/esm/client/websocket.js +6 -11
- package/dist/esm/client/websocket.js.map +1 -1
- package/dist/esm/examples/client/elicitationUrlExample.js +4 -5
- package/dist/esm/examples/client/elicitationUrlExample.js.map +1 -1
- package/dist/esm/examples/client/simpleOAuthClient.js +3 -3
- package/dist/esm/examples/client/simpleOAuthClient.js.map +1 -1
- package/dist/esm/examples/client/simpleStreamableHttp.js +7 -9
- package/dist/esm/examples/client/simpleStreamableHttp.js.map +1 -1
- package/dist/esm/examples/client/simpleTaskInteractiveClient.js +1 -2
- package/dist/esm/examples/client/simpleTaskInteractiveClient.js.map +1 -1
- package/dist/esm/examples/server/honoWebStandardStreamableHttp.d.ts +10 -0
- package/dist/esm/examples/server/honoWebStandardStreamableHttp.d.ts.map +1 -0
- package/dist/esm/examples/server/honoWebStandardStreamableHttp.js +56 -0
- package/dist/esm/examples/server/honoWebStandardStreamableHttp.js.map +1 -0
- package/dist/esm/examples/server/jsonResponseStreamableHttp.js +10 -4
- package/dist/esm/examples/server/jsonResponseStreamableHttp.js.map +1 -1
- package/dist/esm/examples/server/simpleSseServer.js +6 -3
- package/dist/esm/examples/server/simpleSseServer.js.map +1 -1
- package/dist/esm/examples/server/simpleStatelessStreamableHttp.js +12 -6
- package/dist/esm/examples/server/simpleStatelessStreamableHttp.js.map +1 -1
- package/dist/esm/examples/server/simpleStreamableHttp.js +21 -11
- package/dist/esm/examples/server/simpleStreamableHttp.js.map +1 -1
- package/dist/esm/examples/server/simpleTaskInteractive.js +7 -11
- package/dist/esm/examples/server/simpleTaskInteractive.js.map +1 -1
- package/dist/esm/examples/server/sseAndStreamableHttpCompatibleServer.js +10 -7
- package/dist/esm/examples/server/sseAndStreamableHttpCompatibleServer.js.map +1 -1
- package/dist/esm/examples/server/standaloneSseWithGetStreamableHttp.js +1 -1
- package/dist/esm/examples/server/standaloneSseWithGetStreamableHttp.js.map +1 -1
- package/dist/esm/experimental/tasks/client.js +1 -2
- package/dist/esm/experimental/tasks/client.js.map +1 -1
- package/dist/esm/experimental/tasks/helpers.js +3 -5
- package/dist/esm/experimental/tasks/helpers.js.map +1 -1
- package/dist/esm/experimental/tasks/interfaces.d.ts +3 -3
- package/dist/esm/experimental/tasks/interfaces.d.ts.map +1 -1
- package/dist/esm/experimental/tasks/stores/in-memory.d.ts +1 -1
- package/dist/esm/experimental/tasks/stores/in-memory.d.ts.map +1 -1
- package/dist/esm/experimental/tasks/stores/in-memory.js +3 -5
- package/dist/esm/experimental/tasks/stores/in-memory.js.map +1 -1
- package/dist/esm/inMemory.js +5 -7
- package/dist/esm/inMemory.js.map +1 -1
- package/dist/esm/server/auth/handlers/token.js +1 -1
- package/dist/esm/server/auth/handlers/token.js.map +1 -1
- package/dist/esm/server/auth/providers/proxyProvider.js +11 -17
- package/dist/esm/server/auth/providers/proxyProvider.js.map +1 -1
- package/dist/esm/server/auth/router.js +3 -6
- package/dist/esm/server/auth/router.js.map +1 -1
- package/dist/esm/server/completable.js +1 -1
- package/dist/esm/server/completable.js.map +1 -1
- package/dist/esm/server/index.d.ts +3 -3
- package/dist/esm/server/index.d.ts.map +1 -1
- package/dist/esm/server/index.js +20 -30
- package/dist/esm/server/index.js.map +1 -1
- package/dist/esm/server/mcp.d.ts.map +1 -1
- package/dist/esm/server/mcp.js +25 -11
- package/dist/esm/server/mcp.js.map +1 -1
- package/dist/esm/server/middleware/hostHeaderValidation.js +1 -1
- package/dist/esm/server/middleware/hostHeaderValidation.js.map +1 -1
- package/dist/esm/server/sse.js +14 -17
- package/dist/esm/server/sse.js.map +1 -1
- package/dist/esm/server/stdio.js +4 -7
- package/dist/esm/server/stdio.js.map +1 -1
- package/dist/esm/server/streamableHttp.d.ts +52 -154
- package/dist/esm/server/streamableHttp.d.ts.map +1 -1
- package/dist/esm/server/streamableHttp.js +78 -650
- package/dist/esm/server/streamableHttp.js.map +1 -1
- package/dist/esm/server/webStandardStreamableHttp.d.ts +267 -0
- package/dist/esm/server/webStandardStreamableHttp.d.ts.map +1 -0
- package/dist/esm/server/webStandardStreamableHttp.js +725 -0
- package/dist/esm/server/webStandardStreamableHttp.js.map +1 -0
- package/dist/esm/server/zod-compat.d.ts +3 -1
- package/dist/esm/server/zod-compat.d.ts.map +1 -1
- package/dist/esm/server/zod-compat.js +11 -19
- package/dist/esm/server/zod-compat.js.map +1 -1
- package/dist/esm/server/zod-json-schema-compat.js +5 -6
- package/dist/esm/server/zod-json-schema-compat.js.map +1 -1
- package/dist/esm/shared/metadataUtils.js +1 -2
- package/dist/esm/shared/metadataUtils.js.map +1 -1
- package/dist/esm/shared/protocol.d.ts +1 -1
- package/dist/esm/shared/protocol.d.ts.map +1 -1
- package/dist/esm/shared/protocol.js +77 -89
- package/dist/esm/shared/protocol.js.map +1 -1
- package/dist/esm/shared/transport.js +1 -1
- package/dist/esm/shared/transport.js.map +1 -1
- package/dist/esm/spec.types.d.ts +315 -26
- package/dist/esm/spec.types.d.ts.map +1 -1
- package/dist/esm/spec.types.js +2 -2
- package/dist/esm/spec.types.js.map +1 -1
- package/dist/esm/types.d.ts +1430 -1246
- package/dist/esm/types.d.ts.map +1 -1
- package/dist/esm/types.js +220 -130
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/validation/ajv-provider.d.ts +1 -1
- package/dist/esm/validation/ajv-provider.d.ts.map +1 -1
- package/dist/esm/validation/ajv-provider.js +3 -4
- package/dist/esm/validation/ajv-provider.js.map +1 -1
- package/dist/esm/validation/cfworker-provider.js +4 -5
- package/dist/esm/validation/cfworker-provider.js.map +1 -1
- package/dist/esm/validation/types.d.ts +12 -2
- package/dist/esm/validation/types.d.ts.map +1 -1
- package/package.json +3 -1
|
@@ -0,0 +1,729 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Web Standards Streamable HTTP Server Transport
|
|
4
|
+
*
|
|
5
|
+
* This is the core transport implementation using Web Standard APIs (Request, Response, ReadableStream).
|
|
6
|
+
* It can run on any runtime that supports Web Standards: Node.js 18+, Cloudflare Workers, Deno, Bun, etc.
|
|
7
|
+
*
|
|
8
|
+
* For Node.js Express/HTTP compatibility, use `StreamableHTTPServerTransport` which wraps this transport.
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.WebStandardStreamableHTTPServerTransport = void 0;
|
|
12
|
+
const types_js_1 = require("../types.js");
|
|
13
|
+
/**
|
|
14
|
+
* Server transport for Web Standards Streamable HTTP: this implements the MCP Streamable HTTP transport specification
|
|
15
|
+
* using Web Standard APIs (Request, Response, ReadableStream).
|
|
16
|
+
*
|
|
17
|
+
* This transport works on any runtime that supports Web Standards: Node.js 18+, Cloudflare Workers, Deno, Bun, etc.
|
|
18
|
+
*
|
|
19
|
+
* Usage example:
|
|
20
|
+
*
|
|
21
|
+
* ```typescript
|
|
22
|
+
* // Stateful mode - server sets the session ID
|
|
23
|
+
* const statefulTransport = new WebStandardStreamableHTTPServerTransport({
|
|
24
|
+
* sessionIdGenerator: () => crypto.randomUUID(),
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* // Stateless mode - explicitly set session ID to undefined
|
|
28
|
+
* const statelessTransport = new WebStandardStreamableHTTPServerTransport({
|
|
29
|
+
* sessionIdGenerator: undefined,
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* // Hono.js usage
|
|
33
|
+
* app.all('/mcp', async (c) => {
|
|
34
|
+
* return transport.handleRequest(c.req.raw);
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* // Cloudflare Workers usage
|
|
38
|
+
* export default {
|
|
39
|
+
* async fetch(request: Request): Promise<Response> {
|
|
40
|
+
* return transport.handleRequest(request);
|
|
41
|
+
* }
|
|
42
|
+
* };
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* In stateful mode:
|
|
46
|
+
* - Session ID is generated and included in response headers
|
|
47
|
+
* - Session ID is always included in initialization responses
|
|
48
|
+
* - Requests with invalid session IDs are rejected with 404 Not Found
|
|
49
|
+
* - Non-initialization requests without a session ID are rejected with 400 Bad Request
|
|
50
|
+
* - State is maintained in-memory (connections, message history)
|
|
51
|
+
*
|
|
52
|
+
* In stateless mode:
|
|
53
|
+
* - No Session ID is included in any responses
|
|
54
|
+
* - No session validation is performed
|
|
55
|
+
*/
|
|
56
|
+
class WebStandardStreamableHTTPServerTransport {
|
|
57
|
+
constructor(options = {}) {
|
|
58
|
+
this._started = false;
|
|
59
|
+
this._streamMapping = new Map();
|
|
60
|
+
this._requestToStreamMapping = new Map();
|
|
61
|
+
this._requestResponseMap = new Map();
|
|
62
|
+
this._initialized = false;
|
|
63
|
+
this._enableJsonResponse = false;
|
|
64
|
+
this._standaloneSseStreamId = '_GET_stream';
|
|
65
|
+
this.sessionIdGenerator = options.sessionIdGenerator;
|
|
66
|
+
this._enableJsonResponse = options.enableJsonResponse ?? false;
|
|
67
|
+
this._eventStore = options.eventStore;
|
|
68
|
+
this._onsessioninitialized = options.onsessioninitialized;
|
|
69
|
+
this._onsessionclosed = options.onsessionclosed;
|
|
70
|
+
this._allowedHosts = options.allowedHosts;
|
|
71
|
+
this._allowedOrigins = options.allowedOrigins;
|
|
72
|
+
this._enableDnsRebindingProtection = options.enableDnsRebindingProtection ?? false;
|
|
73
|
+
this._retryInterval = options.retryInterval;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Starts the transport. This is required by the Transport interface but is a no-op
|
|
77
|
+
* for the Streamable HTTP transport as connections are managed per-request.
|
|
78
|
+
*/
|
|
79
|
+
async start() {
|
|
80
|
+
if (this._started) {
|
|
81
|
+
throw new Error('Transport already started');
|
|
82
|
+
}
|
|
83
|
+
this._started = true;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Helper to create a JSON error response
|
|
87
|
+
*/
|
|
88
|
+
createJsonErrorResponse(status, code, message, options) {
|
|
89
|
+
const error = { code, message };
|
|
90
|
+
if (options?.data !== undefined) {
|
|
91
|
+
error.data = options.data;
|
|
92
|
+
}
|
|
93
|
+
return new Response(JSON.stringify({
|
|
94
|
+
jsonrpc: '2.0',
|
|
95
|
+
error,
|
|
96
|
+
id: null
|
|
97
|
+
}), {
|
|
98
|
+
status,
|
|
99
|
+
headers: {
|
|
100
|
+
'Content-Type': 'application/json',
|
|
101
|
+
...options?.headers
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Validates request headers for DNS rebinding protection.
|
|
107
|
+
* @returns Error response if validation fails, undefined if validation passes.
|
|
108
|
+
*/
|
|
109
|
+
validateRequestHeaders(req) {
|
|
110
|
+
// Skip validation if protection is not enabled
|
|
111
|
+
if (!this._enableDnsRebindingProtection) {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
// Validate Host header if allowedHosts is configured
|
|
115
|
+
if (this._allowedHosts && this._allowedHosts.length > 0) {
|
|
116
|
+
const hostHeader = req.headers.get('host');
|
|
117
|
+
if (!hostHeader || !this._allowedHosts.includes(hostHeader)) {
|
|
118
|
+
const error = `Invalid Host header: ${hostHeader}`;
|
|
119
|
+
this.onerror?.(new Error(error));
|
|
120
|
+
return this.createJsonErrorResponse(403, -32000, error);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// Validate Origin header if allowedOrigins is configured
|
|
124
|
+
if (this._allowedOrigins && this._allowedOrigins.length > 0) {
|
|
125
|
+
const originHeader = req.headers.get('origin');
|
|
126
|
+
if (originHeader && !this._allowedOrigins.includes(originHeader)) {
|
|
127
|
+
const error = `Invalid Origin header: ${originHeader}`;
|
|
128
|
+
this.onerror?.(new Error(error));
|
|
129
|
+
return this.createJsonErrorResponse(403, -32000, error);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Handles an incoming HTTP request, whether GET, POST, or DELETE
|
|
136
|
+
* Returns a Response object (Web Standard)
|
|
137
|
+
*/
|
|
138
|
+
async handleRequest(req, options) {
|
|
139
|
+
// Validate request headers for DNS rebinding protection
|
|
140
|
+
const validationError = this.validateRequestHeaders(req);
|
|
141
|
+
if (validationError) {
|
|
142
|
+
return validationError;
|
|
143
|
+
}
|
|
144
|
+
switch (req.method) {
|
|
145
|
+
case 'POST':
|
|
146
|
+
return this.handlePostRequest(req, options);
|
|
147
|
+
case 'GET':
|
|
148
|
+
return this.handleGetRequest(req);
|
|
149
|
+
case 'DELETE':
|
|
150
|
+
return this.handleDeleteRequest(req);
|
|
151
|
+
default:
|
|
152
|
+
return this.handleUnsupportedRequest();
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Writes a priming event to establish resumption capability.
|
|
157
|
+
* Only sends if eventStore is configured (opt-in for resumability) and
|
|
158
|
+
* the client's protocol version supports empty SSE data (>= 2025-11-25).
|
|
159
|
+
*/
|
|
160
|
+
async writePrimingEvent(controller, encoder, streamId, protocolVersion) {
|
|
161
|
+
if (!this._eventStore) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
// Priming events have empty data which older clients cannot handle.
|
|
165
|
+
// Only send priming events to clients with protocol version >= 2025-11-25
|
|
166
|
+
// which includes the fix for handling empty SSE data.
|
|
167
|
+
if (protocolVersion < '2025-11-25') {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
const primingEventId = await this._eventStore.storeEvent(streamId, {});
|
|
171
|
+
let primingEvent = `id: ${primingEventId}\ndata: \n\n`;
|
|
172
|
+
if (this._retryInterval !== undefined) {
|
|
173
|
+
primingEvent = `id: ${primingEventId}\nretry: ${this._retryInterval}\ndata: \n\n`;
|
|
174
|
+
}
|
|
175
|
+
controller.enqueue(encoder.encode(primingEvent));
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Handles GET requests for SSE stream
|
|
179
|
+
*/
|
|
180
|
+
async handleGetRequest(req) {
|
|
181
|
+
// The client MUST include an Accept header, listing text/event-stream as a supported content type.
|
|
182
|
+
const acceptHeader = req.headers.get('accept');
|
|
183
|
+
if (!acceptHeader?.includes('text/event-stream')) {
|
|
184
|
+
return this.createJsonErrorResponse(406, -32000, 'Not Acceptable: Client must accept text/event-stream');
|
|
185
|
+
}
|
|
186
|
+
// If an Mcp-Session-Id is returned by the server during initialization,
|
|
187
|
+
// clients using the Streamable HTTP transport MUST include it
|
|
188
|
+
// in the Mcp-Session-Id header on all of their subsequent HTTP requests.
|
|
189
|
+
const sessionError = this.validateSession(req);
|
|
190
|
+
if (sessionError) {
|
|
191
|
+
return sessionError;
|
|
192
|
+
}
|
|
193
|
+
const protocolError = this.validateProtocolVersion(req);
|
|
194
|
+
if (protocolError) {
|
|
195
|
+
return protocolError;
|
|
196
|
+
}
|
|
197
|
+
// Handle resumability: check for Last-Event-ID header
|
|
198
|
+
if (this._eventStore) {
|
|
199
|
+
const lastEventId = req.headers.get('last-event-id');
|
|
200
|
+
if (lastEventId) {
|
|
201
|
+
return this.replayEvents(lastEventId);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
// Check if there's already an active standalone SSE stream for this session
|
|
205
|
+
if (this._streamMapping.get(this._standaloneSseStreamId) !== undefined) {
|
|
206
|
+
// Only one GET SSE stream is allowed per session
|
|
207
|
+
return this.createJsonErrorResponse(409, -32000, 'Conflict: Only one SSE stream is allowed per session');
|
|
208
|
+
}
|
|
209
|
+
const encoder = new TextEncoder();
|
|
210
|
+
let streamController;
|
|
211
|
+
// Create a ReadableStream with a controller we can use to push SSE events
|
|
212
|
+
const readable = new ReadableStream({
|
|
213
|
+
start: controller => {
|
|
214
|
+
streamController = controller;
|
|
215
|
+
},
|
|
216
|
+
cancel: () => {
|
|
217
|
+
// Stream was cancelled by client
|
|
218
|
+
this._streamMapping.delete(this._standaloneSseStreamId);
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
const headers = {
|
|
222
|
+
'Content-Type': 'text/event-stream',
|
|
223
|
+
'Cache-Control': 'no-cache, no-transform',
|
|
224
|
+
Connection: 'keep-alive'
|
|
225
|
+
};
|
|
226
|
+
// After initialization, always include the session ID if we have one
|
|
227
|
+
if (this.sessionId !== undefined) {
|
|
228
|
+
headers['mcp-session-id'] = this.sessionId;
|
|
229
|
+
}
|
|
230
|
+
// Store the stream mapping with the controller for pushing data
|
|
231
|
+
this._streamMapping.set(this._standaloneSseStreamId, {
|
|
232
|
+
controller: streamController,
|
|
233
|
+
encoder,
|
|
234
|
+
cleanup: () => {
|
|
235
|
+
this._streamMapping.delete(this._standaloneSseStreamId);
|
|
236
|
+
try {
|
|
237
|
+
streamController.close();
|
|
238
|
+
}
|
|
239
|
+
catch {
|
|
240
|
+
// Controller might already be closed
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
return new Response(readable, { headers });
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Replays events that would have been sent after the specified event ID
|
|
248
|
+
* Only used when resumability is enabled
|
|
249
|
+
*/
|
|
250
|
+
async replayEvents(lastEventId) {
|
|
251
|
+
if (!this._eventStore) {
|
|
252
|
+
return this.createJsonErrorResponse(400, -32000, 'Event store not configured');
|
|
253
|
+
}
|
|
254
|
+
try {
|
|
255
|
+
// If getStreamIdForEventId is available, use it for conflict checking
|
|
256
|
+
let streamId;
|
|
257
|
+
if (this._eventStore.getStreamIdForEventId) {
|
|
258
|
+
streamId = await this._eventStore.getStreamIdForEventId(lastEventId);
|
|
259
|
+
if (!streamId) {
|
|
260
|
+
return this.createJsonErrorResponse(400, -32000, 'Invalid event ID format');
|
|
261
|
+
}
|
|
262
|
+
// Check conflict with the SAME streamId we'll use for mapping
|
|
263
|
+
if (this._streamMapping.get(streamId) !== undefined) {
|
|
264
|
+
return this.createJsonErrorResponse(409, -32000, 'Conflict: Stream already has an active connection');
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
const headers = {
|
|
268
|
+
'Content-Type': 'text/event-stream',
|
|
269
|
+
'Cache-Control': 'no-cache, no-transform',
|
|
270
|
+
Connection: 'keep-alive'
|
|
271
|
+
};
|
|
272
|
+
if (this.sessionId !== undefined) {
|
|
273
|
+
headers['mcp-session-id'] = this.sessionId;
|
|
274
|
+
}
|
|
275
|
+
// Create a ReadableStream with controller for SSE
|
|
276
|
+
const encoder = new TextEncoder();
|
|
277
|
+
let streamController;
|
|
278
|
+
const readable = new ReadableStream({
|
|
279
|
+
start: controller => {
|
|
280
|
+
streamController = controller;
|
|
281
|
+
},
|
|
282
|
+
cancel: () => {
|
|
283
|
+
// Stream was cancelled by client
|
|
284
|
+
// Cleanup will be handled by the mapping
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
// Replay events - returns the streamId for backwards compatibility
|
|
288
|
+
const replayedStreamId = await this._eventStore.replayEventsAfter(lastEventId, {
|
|
289
|
+
send: async (eventId, message) => {
|
|
290
|
+
const success = this.writeSSEEvent(streamController, encoder, message, eventId);
|
|
291
|
+
if (!success) {
|
|
292
|
+
this.onerror?.(new Error('Failed replay events'));
|
|
293
|
+
try {
|
|
294
|
+
streamController.close();
|
|
295
|
+
}
|
|
296
|
+
catch {
|
|
297
|
+
// Controller might already be closed
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
this._streamMapping.set(replayedStreamId, {
|
|
303
|
+
controller: streamController,
|
|
304
|
+
encoder,
|
|
305
|
+
cleanup: () => {
|
|
306
|
+
this._streamMapping.delete(replayedStreamId);
|
|
307
|
+
try {
|
|
308
|
+
streamController.close();
|
|
309
|
+
}
|
|
310
|
+
catch {
|
|
311
|
+
// Controller might already be closed
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
return new Response(readable, { headers });
|
|
316
|
+
}
|
|
317
|
+
catch (error) {
|
|
318
|
+
this.onerror?.(error);
|
|
319
|
+
return this.createJsonErrorResponse(500, -32000, 'Error replaying events');
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Writes an event to an SSE stream via controller with proper formatting
|
|
324
|
+
*/
|
|
325
|
+
writeSSEEvent(controller, encoder, message, eventId) {
|
|
326
|
+
try {
|
|
327
|
+
let eventData = `event: message\n`;
|
|
328
|
+
// Include event ID if provided - this is important for resumability
|
|
329
|
+
if (eventId) {
|
|
330
|
+
eventData += `id: ${eventId}\n`;
|
|
331
|
+
}
|
|
332
|
+
eventData += `data: ${JSON.stringify(message)}\n\n`;
|
|
333
|
+
controller.enqueue(encoder.encode(eventData));
|
|
334
|
+
return true;
|
|
335
|
+
}
|
|
336
|
+
catch {
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Handles unsupported requests (PUT, PATCH, etc.)
|
|
342
|
+
*/
|
|
343
|
+
handleUnsupportedRequest() {
|
|
344
|
+
return new Response(JSON.stringify({
|
|
345
|
+
jsonrpc: '2.0',
|
|
346
|
+
error: {
|
|
347
|
+
code: -32000,
|
|
348
|
+
message: 'Method not allowed.'
|
|
349
|
+
},
|
|
350
|
+
id: null
|
|
351
|
+
}), {
|
|
352
|
+
status: 405,
|
|
353
|
+
headers: {
|
|
354
|
+
Allow: 'GET, POST, DELETE',
|
|
355
|
+
'Content-Type': 'application/json'
|
|
356
|
+
}
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Handles POST requests containing JSON-RPC messages
|
|
361
|
+
*/
|
|
362
|
+
async handlePostRequest(req, options) {
|
|
363
|
+
try {
|
|
364
|
+
// Validate the Accept header
|
|
365
|
+
const acceptHeader = req.headers.get('accept');
|
|
366
|
+
// The client MUST include an Accept header, listing both application/json and text/event-stream as supported content types.
|
|
367
|
+
if (!acceptHeader?.includes('application/json') || !acceptHeader.includes('text/event-stream')) {
|
|
368
|
+
return this.createJsonErrorResponse(406, -32000, 'Not Acceptable: Client must accept both application/json and text/event-stream');
|
|
369
|
+
}
|
|
370
|
+
const ct = req.headers.get('content-type');
|
|
371
|
+
if (!ct || !ct.includes('application/json')) {
|
|
372
|
+
return this.createJsonErrorResponse(415, -32000, 'Unsupported Media Type: Content-Type must be application/json');
|
|
373
|
+
}
|
|
374
|
+
// Build request info from headers
|
|
375
|
+
const requestInfo = {
|
|
376
|
+
headers: Object.fromEntries(req.headers.entries())
|
|
377
|
+
};
|
|
378
|
+
let rawMessage;
|
|
379
|
+
if (options?.parsedBody !== undefined) {
|
|
380
|
+
rawMessage = options.parsedBody;
|
|
381
|
+
}
|
|
382
|
+
else {
|
|
383
|
+
try {
|
|
384
|
+
rawMessage = await req.json();
|
|
385
|
+
}
|
|
386
|
+
catch {
|
|
387
|
+
return this.createJsonErrorResponse(400, -32700, 'Parse error: Invalid JSON');
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
let messages;
|
|
391
|
+
// handle batch and single messages
|
|
392
|
+
try {
|
|
393
|
+
if (Array.isArray(rawMessage)) {
|
|
394
|
+
messages = rawMessage.map(msg => types_js_1.JSONRPCMessageSchema.parse(msg));
|
|
395
|
+
}
|
|
396
|
+
else {
|
|
397
|
+
messages = [types_js_1.JSONRPCMessageSchema.parse(rawMessage)];
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
catch {
|
|
401
|
+
return this.createJsonErrorResponse(400, -32700, 'Parse error: Invalid JSON-RPC message');
|
|
402
|
+
}
|
|
403
|
+
// Check if this is an initialization request
|
|
404
|
+
// https://spec.modelcontextprotocol.io/specification/2025-03-26/basic/lifecycle/
|
|
405
|
+
const isInitializationRequest = messages.some(types_js_1.isInitializeRequest);
|
|
406
|
+
if (isInitializationRequest) {
|
|
407
|
+
// If it's a server with session management and the session ID is already set we should reject the request
|
|
408
|
+
// to avoid re-initialization.
|
|
409
|
+
if (this._initialized && this.sessionId !== undefined) {
|
|
410
|
+
return this.createJsonErrorResponse(400, -32600, 'Invalid Request: Server already initialized');
|
|
411
|
+
}
|
|
412
|
+
if (messages.length > 1) {
|
|
413
|
+
return this.createJsonErrorResponse(400, -32600, 'Invalid Request: Only one initialization request is allowed');
|
|
414
|
+
}
|
|
415
|
+
this.sessionId = this.sessionIdGenerator?.();
|
|
416
|
+
this._initialized = true;
|
|
417
|
+
// If we have a session ID and an onsessioninitialized handler, call it immediately
|
|
418
|
+
// This is needed in cases where the server needs to keep track of multiple sessions
|
|
419
|
+
if (this.sessionId && this._onsessioninitialized) {
|
|
420
|
+
await Promise.resolve(this._onsessioninitialized(this.sessionId));
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
if (!isInitializationRequest) {
|
|
424
|
+
// If an Mcp-Session-Id is returned by the server during initialization,
|
|
425
|
+
// clients using the Streamable HTTP transport MUST include it
|
|
426
|
+
// in the Mcp-Session-Id header on all of their subsequent HTTP requests.
|
|
427
|
+
const sessionError = this.validateSession(req);
|
|
428
|
+
if (sessionError) {
|
|
429
|
+
return sessionError;
|
|
430
|
+
}
|
|
431
|
+
// Mcp-Protocol-Version header is required for all requests after initialization.
|
|
432
|
+
const protocolError = this.validateProtocolVersion(req);
|
|
433
|
+
if (protocolError) {
|
|
434
|
+
return protocolError;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
// check if it contains requests
|
|
438
|
+
const hasRequests = messages.some(types_js_1.isJSONRPCRequest);
|
|
439
|
+
if (!hasRequests) {
|
|
440
|
+
// if it only contains notifications or responses, return 202
|
|
441
|
+
for (const message of messages) {
|
|
442
|
+
this.onmessage?.(message, { authInfo: options?.authInfo, requestInfo });
|
|
443
|
+
}
|
|
444
|
+
return new Response(null, { status: 202 });
|
|
445
|
+
}
|
|
446
|
+
// The default behavior is to use SSE streaming
|
|
447
|
+
// but in some cases server will return JSON responses
|
|
448
|
+
const streamId = crypto.randomUUID();
|
|
449
|
+
// Extract protocol version for priming event decision.
|
|
450
|
+
// For initialize requests, get from request params.
|
|
451
|
+
// For other requests, get from header (already validated).
|
|
452
|
+
const initRequest = messages.find(m => (0, types_js_1.isInitializeRequest)(m));
|
|
453
|
+
const clientProtocolVersion = initRequest
|
|
454
|
+
? initRequest.params.protocolVersion
|
|
455
|
+
: (req.headers.get('mcp-protocol-version') ?? types_js_1.DEFAULT_NEGOTIATED_PROTOCOL_VERSION);
|
|
456
|
+
if (this._enableJsonResponse) {
|
|
457
|
+
// For JSON response mode, return a Promise that resolves when all responses are ready
|
|
458
|
+
return new Promise(resolve => {
|
|
459
|
+
this._streamMapping.set(streamId, {
|
|
460
|
+
resolveJson: resolve,
|
|
461
|
+
cleanup: () => {
|
|
462
|
+
this._streamMapping.delete(streamId);
|
|
463
|
+
}
|
|
464
|
+
});
|
|
465
|
+
for (const message of messages) {
|
|
466
|
+
if ((0, types_js_1.isJSONRPCRequest)(message)) {
|
|
467
|
+
this._requestToStreamMapping.set(message.id, streamId);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
for (const message of messages) {
|
|
471
|
+
this.onmessage?.(message, { authInfo: options?.authInfo, requestInfo });
|
|
472
|
+
}
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
// SSE streaming mode - use ReadableStream with controller for more reliable data pushing
|
|
476
|
+
const encoder = new TextEncoder();
|
|
477
|
+
let streamController;
|
|
478
|
+
const readable = new ReadableStream({
|
|
479
|
+
start: controller => {
|
|
480
|
+
streamController = controller;
|
|
481
|
+
},
|
|
482
|
+
cancel: () => {
|
|
483
|
+
// Stream was cancelled by client
|
|
484
|
+
this._streamMapping.delete(streamId);
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
const headers = {
|
|
488
|
+
'Content-Type': 'text/event-stream',
|
|
489
|
+
'Cache-Control': 'no-cache',
|
|
490
|
+
Connection: 'keep-alive'
|
|
491
|
+
};
|
|
492
|
+
// After initialization, always include the session ID if we have one
|
|
493
|
+
if (this.sessionId !== undefined) {
|
|
494
|
+
headers['mcp-session-id'] = this.sessionId;
|
|
495
|
+
}
|
|
496
|
+
// Store the response for this request to send messages back through this connection
|
|
497
|
+
// We need to track by request ID to maintain the connection
|
|
498
|
+
for (const message of messages) {
|
|
499
|
+
if ((0, types_js_1.isJSONRPCRequest)(message)) {
|
|
500
|
+
this._streamMapping.set(streamId, {
|
|
501
|
+
controller: streamController,
|
|
502
|
+
encoder,
|
|
503
|
+
cleanup: () => {
|
|
504
|
+
this._streamMapping.delete(streamId);
|
|
505
|
+
try {
|
|
506
|
+
streamController.close();
|
|
507
|
+
}
|
|
508
|
+
catch {
|
|
509
|
+
// Controller might already be closed
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
this._requestToStreamMapping.set(message.id, streamId);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
// Write priming event if event store is configured (after mapping is set up)
|
|
517
|
+
await this.writePrimingEvent(streamController, encoder, streamId, clientProtocolVersion);
|
|
518
|
+
// handle each message
|
|
519
|
+
for (const message of messages) {
|
|
520
|
+
// Build closeSSEStream callback for requests when eventStore is configured
|
|
521
|
+
// AND client supports resumability (protocol version >= 2025-11-25).
|
|
522
|
+
// Old clients can't resume if the stream is closed early because they
|
|
523
|
+
// didn't receive a priming event with an event ID.
|
|
524
|
+
let closeSSEStream;
|
|
525
|
+
let closeStandaloneSSEStream;
|
|
526
|
+
if ((0, types_js_1.isJSONRPCRequest)(message) && this._eventStore && clientProtocolVersion >= '2025-11-25') {
|
|
527
|
+
closeSSEStream = () => {
|
|
528
|
+
this.closeSSEStream(message.id);
|
|
529
|
+
};
|
|
530
|
+
closeStandaloneSSEStream = () => {
|
|
531
|
+
this.closeStandaloneSSEStream();
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
this.onmessage?.(message, { authInfo: options?.authInfo, requestInfo, closeSSEStream, closeStandaloneSSEStream });
|
|
535
|
+
}
|
|
536
|
+
// The server SHOULD NOT close the SSE stream before sending all JSON-RPC responses
|
|
537
|
+
// This will be handled by the send() method when responses are ready
|
|
538
|
+
return new Response(readable, { status: 200, headers });
|
|
539
|
+
}
|
|
540
|
+
catch (error) {
|
|
541
|
+
// return JSON-RPC formatted error
|
|
542
|
+
this.onerror?.(error);
|
|
543
|
+
return this.createJsonErrorResponse(400, -32700, 'Parse error', { data: String(error) });
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
/**
|
|
547
|
+
* Handles DELETE requests to terminate sessions
|
|
548
|
+
*/
|
|
549
|
+
async handleDeleteRequest(req) {
|
|
550
|
+
const sessionError = this.validateSession(req);
|
|
551
|
+
if (sessionError) {
|
|
552
|
+
return sessionError;
|
|
553
|
+
}
|
|
554
|
+
const protocolError = this.validateProtocolVersion(req);
|
|
555
|
+
if (protocolError) {
|
|
556
|
+
return protocolError;
|
|
557
|
+
}
|
|
558
|
+
await Promise.resolve(this._onsessionclosed?.(this.sessionId));
|
|
559
|
+
await this.close();
|
|
560
|
+
return new Response(null, { status: 200 });
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Validates session ID for non-initialization requests.
|
|
564
|
+
* Returns Response error if invalid, undefined otherwise
|
|
565
|
+
*/
|
|
566
|
+
validateSession(req) {
|
|
567
|
+
if (this.sessionIdGenerator === undefined) {
|
|
568
|
+
// If the sessionIdGenerator ID is not set, the session management is disabled
|
|
569
|
+
// and we don't need to validate the session ID
|
|
570
|
+
return undefined;
|
|
571
|
+
}
|
|
572
|
+
if (!this._initialized) {
|
|
573
|
+
// If the server has not been initialized yet, reject all requests
|
|
574
|
+
return this.createJsonErrorResponse(400, -32000, 'Bad Request: Server not initialized');
|
|
575
|
+
}
|
|
576
|
+
const sessionId = req.headers.get('mcp-session-id');
|
|
577
|
+
if (!sessionId) {
|
|
578
|
+
// Non-initialization requests without a session ID should return 400 Bad Request
|
|
579
|
+
return this.createJsonErrorResponse(400, -32000, 'Bad Request: Mcp-Session-Id header is required');
|
|
580
|
+
}
|
|
581
|
+
if (sessionId !== this.sessionId) {
|
|
582
|
+
// Reject requests with invalid session ID with 404 Not Found
|
|
583
|
+
return this.createJsonErrorResponse(404, -32001, 'Session not found');
|
|
584
|
+
}
|
|
585
|
+
return undefined;
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
588
|
+
* Validates the MCP-Protocol-Version header on incoming requests.
|
|
589
|
+
*
|
|
590
|
+
* For initialization: Version negotiation handles unknown versions gracefully
|
|
591
|
+
* (server responds with its supported version).
|
|
592
|
+
*
|
|
593
|
+
* For subsequent requests with MCP-Protocol-Version header:
|
|
594
|
+
* - Accept if in supported list
|
|
595
|
+
* - 400 if unsupported
|
|
596
|
+
*
|
|
597
|
+
* For HTTP requests without the MCP-Protocol-Version header:
|
|
598
|
+
* - Accept and default to the version negotiated at initialization
|
|
599
|
+
*/
|
|
600
|
+
validateProtocolVersion(req) {
|
|
601
|
+
const protocolVersion = req.headers.get('mcp-protocol-version');
|
|
602
|
+
if (protocolVersion !== null && !types_js_1.SUPPORTED_PROTOCOL_VERSIONS.includes(protocolVersion)) {
|
|
603
|
+
return this.createJsonErrorResponse(400, -32000, `Bad Request: Unsupported protocol version: ${protocolVersion} (supported versions: ${types_js_1.SUPPORTED_PROTOCOL_VERSIONS.join(', ')})`);
|
|
604
|
+
}
|
|
605
|
+
return undefined;
|
|
606
|
+
}
|
|
607
|
+
async close() {
|
|
608
|
+
// Close all SSE connections
|
|
609
|
+
this._streamMapping.forEach(({ cleanup }) => {
|
|
610
|
+
cleanup();
|
|
611
|
+
});
|
|
612
|
+
this._streamMapping.clear();
|
|
613
|
+
// Clear any pending responses
|
|
614
|
+
this._requestResponseMap.clear();
|
|
615
|
+
this.onclose?.();
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Close an SSE stream for a specific request, triggering client reconnection.
|
|
619
|
+
* Use this to implement polling behavior during long-running operations -
|
|
620
|
+
* client will reconnect after the retry interval specified in the priming event.
|
|
621
|
+
*/
|
|
622
|
+
closeSSEStream(requestId) {
|
|
623
|
+
const streamId = this._requestToStreamMapping.get(requestId);
|
|
624
|
+
if (!streamId)
|
|
625
|
+
return;
|
|
626
|
+
const stream = this._streamMapping.get(streamId);
|
|
627
|
+
if (stream) {
|
|
628
|
+
stream.cleanup();
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
/**
|
|
632
|
+
* Close the standalone GET SSE stream, triggering client reconnection.
|
|
633
|
+
* Use this to implement polling behavior for server-initiated notifications.
|
|
634
|
+
*/
|
|
635
|
+
closeStandaloneSSEStream() {
|
|
636
|
+
const stream = this._streamMapping.get(this._standaloneSseStreamId);
|
|
637
|
+
if (stream) {
|
|
638
|
+
stream.cleanup();
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
async send(message, options) {
|
|
642
|
+
let requestId = options?.relatedRequestId;
|
|
643
|
+
if ((0, types_js_1.isJSONRPCResultResponse)(message) || (0, types_js_1.isJSONRPCErrorResponse)(message)) {
|
|
644
|
+
// If the message is a response, use the request ID from the message
|
|
645
|
+
requestId = message.id;
|
|
646
|
+
}
|
|
647
|
+
// Check if this message should be sent on the standalone SSE stream (no request ID)
|
|
648
|
+
// Ignore notifications from tools (which have relatedRequestId set)
|
|
649
|
+
// Those will be sent via dedicated response SSE streams
|
|
650
|
+
if (requestId === undefined) {
|
|
651
|
+
// For standalone SSE streams, we can only send requests and notifications
|
|
652
|
+
if ((0, types_js_1.isJSONRPCResultResponse)(message) || (0, types_js_1.isJSONRPCErrorResponse)(message)) {
|
|
653
|
+
throw new Error('Cannot send a response on a standalone SSE stream unless resuming a previous client request');
|
|
654
|
+
}
|
|
655
|
+
// Generate and store event ID if event store is provided
|
|
656
|
+
// Store even if stream is disconnected so events can be replayed on reconnect
|
|
657
|
+
let eventId;
|
|
658
|
+
if (this._eventStore) {
|
|
659
|
+
// Stores the event and gets the generated event ID
|
|
660
|
+
eventId = await this._eventStore.storeEvent(this._standaloneSseStreamId, message);
|
|
661
|
+
}
|
|
662
|
+
const standaloneSse = this._streamMapping.get(this._standaloneSseStreamId);
|
|
663
|
+
if (standaloneSse === undefined) {
|
|
664
|
+
// Stream is disconnected - event is stored for replay, nothing more to do
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
// Send the message to the standalone SSE stream
|
|
668
|
+
if (standaloneSse.controller && standaloneSse.encoder) {
|
|
669
|
+
this.writeSSEEvent(standaloneSse.controller, standaloneSse.encoder, message, eventId);
|
|
670
|
+
}
|
|
671
|
+
return;
|
|
672
|
+
}
|
|
673
|
+
// Get the response for this request
|
|
674
|
+
const streamId = this._requestToStreamMapping.get(requestId);
|
|
675
|
+
if (!streamId) {
|
|
676
|
+
throw new Error(`No connection established for request ID: ${String(requestId)}`);
|
|
677
|
+
}
|
|
678
|
+
const stream = this._streamMapping.get(streamId);
|
|
679
|
+
if (!this._enableJsonResponse && stream?.controller && stream?.encoder) {
|
|
680
|
+
// For SSE responses, generate event ID if event store is provided
|
|
681
|
+
let eventId;
|
|
682
|
+
if (this._eventStore) {
|
|
683
|
+
eventId = await this._eventStore.storeEvent(streamId, message);
|
|
684
|
+
}
|
|
685
|
+
// Write the event to the response stream
|
|
686
|
+
this.writeSSEEvent(stream.controller, stream.encoder, message, eventId);
|
|
687
|
+
}
|
|
688
|
+
if ((0, types_js_1.isJSONRPCResultResponse)(message) || (0, types_js_1.isJSONRPCErrorResponse)(message)) {
|
|
689
|
+
this._requestResponseMap.set(requestId, message);
|
|
690
|
+
const relatedIds = Array.from(this._requestToStreamMapping.entries())
|
|
691
|
+
.filter(([_, sid]) => sid === streamId)
|
|
692
|
+
.map(([id]) => id);
|
|
693
|
+
// Check if we have responses for all requests using this connection
|
|
694
|
+
const allResponsesReady = relatedIds.every(id => this._requestResponseMap.has(id));
|
|
695
|
+
if (allResponsesReady) {
|
|
696
|
+
if (!stream) {
|
|
697
|
+
throw new Error(`No connection established for request ID: ${String(requestId)}`);
|
|
698
|
+
}
|
|
699
|
+
if (this._enableJsonResponse && stream.resolveJson) {
|
|
700
|
+
// All responses ready, send as JSON
|
|
701
|
+
const headers = {
|
|
702
|
+
'Content-Type': 'application/json'
|
|
703
|
+
};
|
|
704
|
+
if (this.sessionId !== undefined) {
|
|
705
|
+
headers['mcp-session-id'] = this.sessionId;
|
|
706
|
+
}
|
|
707
|
+
const responses = relatedIds.map(id => this._requestResponseMap.get(id));
|
|
708
|
+
if (responses.length === 1) {
|
|
709
|
+
stream.resolveJson(new Response(JSON.stringify(responses[0]), { status: 200, headers }));
|
|
710
|
+
}
|
|
711
|
+
else {
|
|
712
|
+
stream.resolveJson(new Response(JSON.stringify(responses), { status: 200, headers }));
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
else {
|
|
716
|
+
// End the SSE stream
|
|
717
|
+
stream.cleanup();
|
|
718
|
+
}
|
|
719
|
+
// Clean up
|
|
720
|
+
for (const id of relatedIds) {
|
|
721
|
+
this._requestResponseMap.delete(id);
|
|
722
|
+
this._requestToStreamMapping.delete(id);
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
exports.WebStandardStreamableHTTPServerTransport = WebStandardStreamableHTTPServerTransport;
|
|
729
|
+
//# sourceMappingURL=webStandardStreamableHttp.js.map
|