@mongodb-js/mcp-http-runners 3.0.0-prerelease.1 → 3.0.0-prerelease.3

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/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  export type { StreamableHttpRunnerOptions } from "./streamableHttpRunner.js";
2
2
  export { StreamableHttpRunner } from "./streamableHttpRunner.js";
3
3
  export { MCPHttpServer, type MCPHttpServerOptions } from "./mcpHttpServer.js";
4
+ export { LegacyMcpHttpHandler, type LegacyMcpHttpHandlerOptions, type LegacySessionOptions, } from "./legacyMcpHttpHandler.js";
5
+ export { SessionStore, SessionLimitExceededError, SessionRejectedError, createDefaultSessionStore, type ISessionStore, type SessionStoreConstructorArgs, type CreateSessionStoreFn, } from "@mongodb-js/mcp-core";
4
6
  export { MonitoringServer, type MonitoringServerOptions } from "./monitoringServer.js";
5
7
  export { ExpressBasedHttpServer, type ExpressBasedHttpServerOptions } from "./expressBasedHttpServer.js";
6
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,2BAA2B,EAAE,MAAM,2BAA2B,CAAC;AAG7E,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAGjE,OAAO,EAAE,aAAa,EAAE,KAAK,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAC9E,OAAO,EAAE,gBAAgB,EAAE,KAAK,uBAAuB,EAAE,MAAM,uBAAuB,CAAC;AAGvF,OAAO,EAAE,sBAAsB,EAAE,KAAK,6BAA6B,EAAE,MAAM,6BAA6B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,2BAA2B,EAAE,MAAM,2BAA2B,CAAC;AAG7E,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAGjE,OAAO,EAAE,aAAa,EAAE,KAAK,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAC9E,OAAO,EACH,oBAAoB,EACpB,KAAK,2BAA2B,EAChC,KAAK,oBAAoB,GAC5B,MAAM,2BAA2B,CAAC;AAEnC,OAAO,EACH,YAAY,EACZ,yBAAyB,EACzB,oBAAoB,EACpB,yBAAyB,EACzB,KAAK,aAAa,EAClB,KAAK,2BAA2B,EAChC,KAAK,oBAAoB,GAC5B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,gBAAgB,EAAE,KAAK,uBAAuB,EAAE,MAAM,uBAAuB,CAAC;AAGvF,OAAO,EAAE,sBAAsB,EAAE,KAAK,6BAA6B,EAAE,MAAM,6BAA6B,CAAC"}
package/dist/index.js CHANGED
@@ -2,6 +2,9 @@
2
2
  export { StreamableHttpRunner } from "./streamableHttpRunner.js";
3
3
  // HTTP Servers
4
4
  export { MCPHttpServer } from "./mcpHttpServer.js";
5
+ export { LegacyMcpHttpHandler, } from "./legacyMcpHttpHandler.js";
6
+ // Session lifecycle primitives live in @mongodb-js/mcp-core; re-export for convenience.
7
+ export { SessionStore, SessionLimitExceededError, SessionRejectedError, createDefaultSessionStore, } from "@mongodb-js/mcp-core";
5
8
  export { MonitoringServer } from "./monitoringServer.js";
6
9
  // Express HTTP Server base
7
10
  export { ExpressBasedHttpServer } from "./expressBasedHttpServer.js";
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,qCAAqC;AACrC,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAEjE,eAAe;AACf,OAAO,EAAE,aAAa,EAA6B,MAAM,oBAAoB,CAAC;AAC9E,OAAO,EAAE,gBAAgB,EAAgC,MAAM,uBAAuB,CAAC;AAEvF,2BAA2B;AAC3B,OAAO,EAAE,sBAAsB,EAAsC,MAAM,6BAA6B,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,qCAAqC;AACrC,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAEjE,eAAe;AACf,OAAO,EAAE,aAAa,EAA6B,MAAM,oBAAoB,CAAC;AAC9E,OAAO,EACH,oBAAoB,GAGvB,MAAM,2BAA2B,CAAC;AACnC,wFAAwF;AACxF,OAAO,EACH,YAAY,EACZ,yBAAyB,EACzB,oBAAoB,EACpB,yBAAyB,GAI5B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,gBAAgB,EAAgC,MAAM,uBAAuB,CAAC;AAEvF,2BAA2B;AAC3B,OAAO,EAAE,sBAAsB,EAAsC,MAAM,6BAA6B,CAAC"}
@@ -0,0 +1,88 @@
1
+ import { NodeStreamableHTTPServerTransport } from "@modelcontextprotocol/node";
2
+ import type express from "express";
3
+ import type { ILogger, TransportRequestContext, HttpServerOptions, BaseServer } from "@mongodb-js/mcp-types";
4
+ import { type ISessionStore } from "@mongodb-js/mcp-core";
5
+ /** Builds a fresh request-scoped server for a legacy request. */
6
+ export type LegacyServerFactory = (request: TransportRequestContext) => Promise<BaseServer>;
7
+ /** Tunables forwarded to the {@link SessionStore}. */
8
+ export type LegacySessionOptions = {
9
+ maxSessions?: number;
10
+ idleTimeoutMS?: number;
11
+ notificationTimeoutMS?: number;
12
+ evictionIdleGraceMS?: number;
13
+ };
14
+ export type LegacyMcpHttpHandlerOptions = {
15
+ /** Builds a fresh request-scoped server for the legacy session. */
16
+ createServer: LegacyServerFactory;
17
+ logger: ILogger;
18
+ http: HttpServerOptions;
19
+ /** Session store backing the legacy sessionful path (auth-aware or durable). */
20
+ sessionStore: ISessionStore<NodeStreamableHTTPServerTransport>;
21
+ };
22
+ /**
23
+ * The contract {@link MCPHttpServer.createLegacyHandler} returns for 2025-era
24
+ * serving. It is deliberately transport-shaped so alternative implementations
25
+ * (a different transport, a distributed session store, ...) can be returned by
26
+ * overriding `createLegacyHandler`.
27
+ */
28
+ export interface LegacyMcpHandler {
29
+ /** Serves one 2025-era HTTP request (POST/GET/DELETE) to `/mcp`. */
30
+ handle(req: express.Request, res: express.Response): Promise<void>;
31
+ /** Releases any held resources (e.g. live sessions) on shutdown. */
32
+ close(): Promise<void>;
33
+ }
34
+ /**
35
+ * Serves 2025-era (legacy) MCP requests over streamable HTTP **sessionfully**.
36
+ *
37
+ * The 2026-07-28 protocol is stateless and carries client identity per request
38
+ * in the `_meta` envelope. The 2025 protocol is not: it declares the client's
39
+ * negotiated capabilities (incl. `elicitation`) in the `initialize` handshake
40
+ * and delivers elicitation as real server→client requests. Both need a live,
41
+ * correlated transport, which is exactly what the SDK's legacy elicitation
42
+ * shim relies on. The SDK's `legacy: 'stateless'` mode cannot do that (each
43
+ * request gets a fresh instance: no capabilities, no return channel).
44
+ *
45
+ * This handler owns that sessionful lifecycle: an `initialize` POST creates a
46
+ * session (a connected transport/server pair), later requests and the SSE idle
47
+ * stream reuse the session's transport (the shim's return channel), and a
48
+ * DELETE closes it. Sessions are bounded by the injected `sessionStore`.
49
+ * Keeping it isolated from {@link MCPHttpServer} leaves the modern stateless
50
+ * path clean.
51
+ */
52
+ export declare class LegacyMcpHttpHandler implements LegacyMcpHandler {
53
+ private readonly createServer;
54
+ private readonly logger;
55
+ private readonly http;
56
+ private readonly sessions;
57
+ constructor({ createServer, logger, http, sessionStore }: LegacyMcpHttpHandlerOptions);
58
+ /** Closes every live session (e.g. on server shutdown). */
59
+ close(): Promise<void>;
60
+ private buildTransportContext;
61
+ /** Reports a session error the way main did: same codes, messages, and statuses. */
62
+ private reportSessionError;
63
+ /**
64
+ * Keeps the session's SSE idle stream alive through proxies and reaps dead
65
+ * clients: pings every 30s and closes the transport after 3 consecutive
66
+ * failures. Not started in JSON response mode, where connections are
67
+ * short-lived and pings aren't needed.
68
+ */
69
+ private startKeepAliveLoop;
70
+ /**
71
+ * Creates a fresh session: builds a request-scoped server, connects it to a
72
+ * sessionful streamable HTTP transport (so `initialize` negotiates and the
73
+ * SDK keeps the client's capabilities on the connected server), and stores
74
+ * the transport so later requests and the SSE idle stream reuse it (the
75
+ * return channel the legacy elicitation shim needs).
76
+ */
77
+ private createSession;
78
+ /**
79
+ * Serves a 2025-era request. POSTs that carry no session begin a new session
80
+ * (the `initialize` handshake); POSTs/GETs/DELETEs that carry a session id
81
+ * are routed to the stored transport, preserving the client's negotiated
82
+ * state and providing the SSE return channel for server→client requests.
83
+ */
84
+ handle(req: express.Request, res: express.Response): Promise<void>;
85
+ /** Routes a session-carrying request to its live transport; reports a session error when missing. */
86
+ private handleSessionRequest;
87
+ }
88
+ //# sourceMappingURL=legacyMcpHttpHandler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"legacyMcpHttpHandler.d.ts","sourceRoot":"","sources":["../src/legacyMcpHttpHandler.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iCAAiC,EAAE,MAAM,4BAA4B,CAAC;AAC/E,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AACnC,OAAO,KAAK,EACR,OAAO,EACP,uBAAuB,EAEvB,iBAAiB,EACjB,UAAU,EACb,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAA6B,KAAK,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAwBrF,iEAAiE;AACjE,MAAM,MAAM,mBAAmB,GAAG,CAAC,OAAO,EAAE,uBAAuB,KAAK,OAAO,CAAC,UAAU,CAAC,CAAC;AAE5F,sDAAsD;AACtD,MAAM,MAAM,oBAAoB,GAAG;IAC/B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAChC,CAAC;AAE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Transport } from "@modelcontextprotocol/node";
3
+ import { LogId, getRandomUUID, requestIdAttr } from "@mongodb-js/mcp-core";
4
+ import { SessionLimitExceededError } from "@mongodb-js/mcp-core";
5
+ import { sleep } from "./utils.js";
6
+ /**
7
+ * Session error codes as they were served to 2025-era clients on main; the
8
+ * shared constants were removed from mcp-core with the session surface, so
9
+ * the legacy path keeps its wire contract locally.
10
+ */
11
+ const JSON_RPC_ERROR_CODE_SESSION_ID_REQUIRED = -32001;
12
+ const JSON_RPC_ERROR_CODE_SESSION_ID_INVALID = -32002;
13
+ const JSON_RPC_ERROR_CODE_SESSION_NOT_FOUND = -32003;
14
+ const JSON_RPC_ERROR_CODE_INVALID_REQUEST = -32004;
15
+ const JSON_RPC_ERROR_CODE_DISALLOWED_EXTERNAL_SESSION = -32005;
16
+ const JSON_RPC_ERROR_CODE_SESSION_LIMIT_EXCEEDED = -32006;
17
+ /**
18
+ * Serves 2025-era (legacy) MCP requests over streamable HTTP **sessionfully**.
19
+ *
20
+ * The 2026-07-28 protocol is stateless and carries client identity per request
21
+ * in the `_meta` envelope. The 2025 protocol is not: it declares the client's
22
+ * negotiated capabilities (incl. `elicitation`) in the `initialize` handshake
23
+ * and delivers elicitation as real server→client requests. Both need a live,
24
+ * correlated transport, which is exactly what the SDK's legacy elicitation
25
+ * shim relies on. The SDK's `legacy: 'stateless'` mode cannot do that (each
26
+ * request gets a fresh instance: no capabilities, no return channel).
27
+ *
28
+ * This handler owns that sessionful lifecycle: an `initialize` POST creates a
29
+ * session (a connected transport/server pair), later requests and the SSE idle
30
+ * stream reuse the session's transport (the shim's return channel), and a
31
+ * DELETE closes it. Sessions are bounded by the injected `sessionStore`.
32
+ * Keeping it isolated from {@link MCPHttpServer} leaves the modern stateless
33
+ * path clean.
34
+ */
35
+ export class LegacyMcpHttpHandler {
36
+ constructor({ createServer, logger, http, sessionStore }) {
37
+ this.createServer = createServer;
38
+ this.logger = logger;
39
+ this.http = http;
40
+ this.sessions = sessionStore;
41
+ }
42
+ /** Closes every live session (e.g. on server shutdown). */
43
+ async close() {
44
+ await this.sessions.closeAllSessions();
45
+ }
46
+ buildTransportContext(req) {
47
+ return {
48
+ headers: req.headers,
49
+ query: req.query,
50
+ // The explicit auth state of this request. Legacy serving does not
51
+ // normalize authInfo itself; hosts supply it via `req.auth` when
52
+ // using authenticated mode.
53
+ authInfo: req.auth
54
+ ? { mode: "authenticated", state: req.auth }
55
+ : { mode: "unauthenticated" },
56
+ };
57
+ }
58
+ /** Reports a session error the way main did: same codes, messages, and statuses. */
59
+ reportSessionError(res, errorCode) {
60
+ let message;
61
+ let statusCode = 400;
62
+ switch (errorCode) {
63
+ case JSON_RPC_ERROR_CODE_SESSION_ID_REQUIRED:
64
+ message = "session id is required";
65
+ break;
66
+ case JSON_RPC_ERROR_CODE_SESSION_ID_INVALID:
67
+ message = "session id is invalid";
68
+ break;
69
+ case JSON_RPC_ERROR_CODE_INVALID_REQUEST:
70
+ message = "invalid request";
71
+ break;
72
+ case JSON_RPC_ERROR_CODE_SESSION_NOT_FOUND:
73
+ message = "session not found";
74
+ statusCode = 404;
75
+ break;
76
+ case JSON_RPC_ERROR_CODE_DISALLOWED_EXTERNAL_SESSION:
77
+ message = "cannot provide sessionId when externally managed sessions are disabled";
78
+ break;
79
+ case JSON_RPC_ERROR_CODE_SESSION_LIMIT_EXCEEDED:
80
+ message = "server has reached the maximum number of concurrent sessions, try again later";
81
+ statusCode = 503;
82
+ break;
83
+ default:
84
+ message = "unknown error";
85
+ statusCode = 500;
86
+ }
87
+ res.status(statusCode).json({
88
+ jsonrpc: "2.0",
89
+ error: {
90
+ code: errorCode,
91
+ message,
92
+ },
93
+ });
94
+ }
95
+ /**
96
+ * Keeps the session's SSE idle stream alive through proxies and reaps dead
97
+ * clients: pings every 30s and closes the transport after 3 consecutive
98
+ * failures. Not started in JSON response mode, where connections are
99
+ * short-lived and pings aren't needed.
100
+ */
101
+ async startKeepAliveLoop({ transport, signal, }) {
102
+ if (this.http.responseType === "json") {
103
+ return;
104
+ }
105
+ let failedPings = 0;
106
+ while (!signal.aborted) {
107
+ try {
108
+ this.logger.debug({
109
+ id: LogId.streamableHttpTransportKeepAlive,
110
+ context: "streamableHttpTransport",
111
+ message: "Sending ping",
112
+ });
113
+ await transport.send({
114
+ jsonrpc: "2.0",
115
+ method: "ping",
116
+ });
117
+ failedPings = 0;
118
+ }
119
+ catch (err) {
120
+ try {
121
+ failedPings++;
122
+ this.logger.warning({
123
+ id: LogId.streamableHttpTransportKeepAliveFailure,
124
+ context: "streamableHttpTransport",
125
+ message: `Error sending ping (attempt #${failedPings}): ${err instanceof Error ? err.message : String(err)}`,
126
+ });
127
+ if (failedPings > 3) {
128
+ await transport.close();
129
+ return;
130
+ }
131
+ }
132
+ catch {
133
+ // Ignore the error of the transport close
134
+ }
135
+ }
136
+ await sleep(30000, { signal });
137
+ }
138
+ }
139
+ /**
140
+ * Creates a fresh session: builds a request-scoped server, connects it to a
141
+ * sessionful streamable HTTP transport (so `initialize` negotiates and the
142
+ * SDK keeps the client's capabilities on the connected server), and stores
143
+ * the transport so later requests and the SSE idle stream reuse it (the
144
+ * return channel the legacy elicitation shim needs).
145
+ */
146
+ async createSession(req) {
147
+ const sessionId = getRandomUUID();
148
+ const transport = new NodeStreamableHTTPServerTransport({
149
+ sessionIdGenerator: () => sessionId,
150
+ enableJsonResponse: this.http.responseType === "json",
151
+ });
152
+ const server = (await this.createServer(this.buildTransportContext(req)));
153
+ // Admit (check cap, evict LRU idle victim). A rejection admits nothing.
154
+ try {
155
+ await this.sessions.addSession({ sessionId, transport, logger: this.logger, headers: req.headers });
156
+ }
157
+ catch (error) {
158
+ // Dispose the un-connected server we built for a rejected session.
159
+ await server.close().catch(() => undefined);
160
+ throw error;
161
+ }
162
+ const keepAliveController = new AbortController();
163
+ void this.startKeepAliveLoop({ transport, signal: keepAliveController.signal });
164
+ // When the transport closes (client disconnect or store-initiated
165
+ // eviction/timeout), tear down the server and remove the session.
166
+ transport.onclose = () => {
167
+ keepAliveController.abort();
168
+ void server
169
+ .close()
170
+ .catch((error) => {
171
+ this.logger.error({
172
+ id: LogId.streamableHttpTransportCloseFailure,
173
+ context: "streamableHttpTransport",
174
+ message: `Error closing legacy session ${sessionId}: ${error instanceof Error ? error.message : String(error)}`,
175
+ });
176
+ })
177
+ .finally(() => {
178
+ // The store removes the session before it closes the
179
+ // transport, so on store-initiated closes (eviction, idle
180
+ // timeout) the session is already gone — nothing to do.
181
+ if (!this.sessions.hasSession(sessionId)) {
182
+ return;
183
+ }
184
+ this.sessions.closeSession({ sessionId, reason: "transport_closed" }).catch((error) => {
185
+ this.logger.error({
186
+ id: LogId.streamableHttpTransportCloseFailure,
187
+ context: "streamableHttpTransport",
188
+ message: `Error removing legacy session ${sessionId}: ${error instanceof Error ? error.message : String(error)}`,
189
+ });
190
+ });
191
+ });
192
+ };
193
+ try {
194
+ await server.connect(transport);
195
+ }
196
+ catch (error) {
197
+ // Don't leave a never-connected session registered; closing the
198
+ // transport triggers the onclose teardown above.
199
+ await this.sessions.closeSession({ sessionId, reason: "unknown" }).catch(() => undefined);
200
+ await server.close().catch(() => undefined);
201
+ throw error;
202
+ }
203
+ return transport;
204
+ }
205
+ /**
206
+ * Serves a 2025-era request. POSTs that carry no session begin a new session
207
+ * (the `initialize` handshake); POSTs/GETs/DELETEs that carry a session id
208
+ * are routed to the stored transport, preserving the client's negotiated
209
+ * state and providing the SSE return channel for server→client requests.
210
+ */
211
+ async handle(req, res) {
212
+ const sessionId = req.headers["mcp-session-id"];
213
+ if (req.method === "GET") {
214
+ // The SSE idle stream only exists in SSE response mode.
215
+ if (this.http.responseType === "json") {
216
+ res.status(405).set("Allow", ["POST", "DELETE"]).send("Method Not Allowed");
217
+ return;
218
+ }
219
+ await this.handleSessionRequest(req, res, sessionId);
220
+ return;
221
+ }
222
+ if (req.method === "DELETE") {
223
+ await this.handleSessionRequest(req, res, sessionId);
224
+ return;
225
+ }
226
+ // POST /mcp.
227
+ if (sessionId && typeof sessionId !== "string") {
228
+ this.reportSessionError(res, JSON_RPC_ERROR_CODE_SESSION_ID_INVALID);
229
+ return;
230
+ }
231
+ if (isInitializeRequest(req.body)) {
232
+ if (sessionId) {
233
+ // This shim has no externally managed sessions; a client-provided
234
+ // session id on initialize is rejected as on main.
235
+ this.logger.debug({
236
+ id: LogId.streamableHttpTransportDisallowedExternalSessionError,
237
+ context: "streamableHttpTransport",
238
+ message: `Client provided session ID ${sessionId} on initialize, but externally managed sessions are not supported`,
239
+ attributes: { ...requestIdAttr(req.headers) },
240
+ });
241
+ this.reportSessionError(res, JSON_RPC_ERROR_CODE_DISALLOWED_EXTERNAL_SESSION);
242
+ return;
243
+ }
244
+ let transport;
245
+ try {
246
+ transport = await this.createSession(req);
247
+ }
248
+ catch (error) {
249
+ if (error instanceof SessionLimitExceededError) {
250
+ this.logger.warning({
251
+ id: LogId.streamableHttpTransportRequestFailure,
252
+ context: "streamableHttpTransport",
253
+ message: `Rejecting legacy session startup: ${error.message}`,
254
+ attributes: { ...requestIdAttr(req.headers) },
255
+ });
256
+ this.reportSessionError(res, JSON_RPC_ERROR_CODE_SESSION_LIMIT_EXCEEDED);
257
+ return;
258
+ }
259
+ throw error;
260
+ }
261
+ await transport.handleRequest(req, res, req.body);
262
+ return;
263
+ }
264
+ if (!sessionId) {
265
+ this.reportSessionError(res, JSON_RPC_ERROR_CODE_INVALID_REQUEST);
266
+ return;
267
+ }
268
+ await this.handleSessionRequest(req, res, sessionId);
269
+ }
270
+ /** Routes a session-carrying request to its live transport; reports a session error when missing. */
271
+ async handleSessionRequest(req, res, sessionId) {
272
+ if (!sessionId) {
273
+ this.reportSessionError(res, JSON_RPC_ERROR_CODE_SESSION_ID_REQUIRED);
274
+ return;
275
+ }
276
+ if (typeof sessionId !== "string") {
277
+ this.reportSessionError(res, JSON_RPC_ERROR_CODE_SESSION_ID_INVALID);
278
+ return;
279
+ }
280
+ const transport = await this.sessions.getSession(sessionId, req.headers);
281
+ if (!transport) {
282
+ this.logger.debug({
283
+ id: LogId.streamableHttpTransportSessionNotFound,
284
+ context: "streamableHttpTransport",
285
+ message: `Session with ID ${sessionId} not found`,
286
+ attributes: { ...requestIdAttr(req.headers) },
287
+ });
288
+ this.reportSessionError(res, JSON_RPC_ERROR_CODE_SESSION_NOT_FOUND);
289
+ return;
290
+ }
291
+ await transport.handleRequest(req, res, req.body);
292
+ }
293
+ }
294
+ //# sourceMappingURL=legacyMcpHttpHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"legacyMcpHttpHandler.js","sourceRoot":"","sources":["../src/legacyMcpHttpHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,EAAE,iCAAiC,EAAE,MAAM,4BAA4B,CAAC;AAS/E,OAAO,EAAE,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAC3E,OAAO,EAAE,yBAAyB,EAAsB,MAAM,sBAAsB,CAAC;AACrF,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC;;;;GAIG;AACH,MAAM,uCAAuC,GAAG,CAAC,KAAK,CAAC;AACvD,MAAM,sCAAsC,GAAG,CAAC,KAAK,CAAC;AACtD,MAAM,qCAAqC,GAAG,CAAC,KAAK,CAAC;AACrD,MAAM,mCAAmC,GAAG,CAAC,KAAK,CAAC;AACnD,MAAM,+CAA+C,GAAG,CAAC,KAAK,CAAC;AAC/D,MAAM,0CAA0C,GAAG,CAAC,KAAK,CAAC;AA4C1D;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,OAAO,oBAAoB;IAM7B,YAAY,EAAE,YAAY,EAAE,MAAM,EAAE,IAAI,EAAE,YAAY,EAA+B;QACjF,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,QAAQ,GA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