@nitrostack/core 1.0.11 → 1.0.12

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.
@@ -14,8 +14,16 @@
14
14
  * - Protocol version header support
15
15
  */
16
16
  import { Express, Request, Response } from 'express';
17
- import { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';
18
- import { JSONRPCMessage, Tool as McpTool } from '@modelcontextprotocol/sdk/types.js';
17
+ import type { Server as McpServer } from '@modelcontextprotocol/sdk/server/index.js';
18
+ import { Tool as McpTool } from '@modelcontextprotocol/sdk/types.js';
19
+ /**
20
+ * Factory that builds a fully-configured MCP server instance.
21
+ * Each Streamable HTTP session gets its own server, since an SDK server can
22
+ * only be connected to a single transport at a time.
23
+ */
24
+ export type McpServerFactory = () => McpServer;
25
+ /** Handles legacy HTTP+SSE clients that open GET without a Streamable HTTP session id (e.g. Cursor). */
26
+ export type LegacySseHandler = (req: Request, res: Response) => Promise<void>;
19
27
  export interface StreamableHttpTransportOptions {
20
28
  /**
21
29
  * Port to listen on (default: 3000)
@@ -35,8 +43,15 @@ export interface StreamableHttpTransportOptions {
35
43
  enableSessions?: boolean;
36
44
  /**
37
45
  * Session timeout in ms (default: 30 minutes)
46
+ * Idle sessions are torn down after this period of inactivity.
38
47
  */
39
48
  sessionTimeout?: number;
49
+ /**
50
+ * Maximum number of concurrent MCP sessions (default: 1000).
51
+ * Once reached, new `initialize` requests are rejected with HTTP 429 to
52
+ * bound memory usage against unauthenticated session-creation floods.
53
+ */
54
+ maxSessions?: number;
40
55
  /**
41
56
  * Custom Express app (optional)
42
57
  */
@@ -47,23 +62,41 @@ export interface StreamableHttpTransportOptions {
47
62
  enableCors?: boolean;
48
63
  }
49
64
  /**
50
- * Streamable HTTP Transport
51
- * Implements MCP Streamable HTTP specification
65
+ * Streamable HTTP host.
66
+ *
67
+ * This class owns the Express application (CORS, origin checks, OAuth discovery
68
+ * routes, health, documentation page, legacy-SSE lifecycle) but delegates the
69
+ * actual MCP protocol handling on `/mcp` to the official
70
+ * `@modelcontextprotocol/sdk` `StreamableHTTPServerTransport`. That transport
71
+ * returns the JSON-RPC result on the POST response (or an SSE stream on the same
72
+ * request), which is what modern MCP clients such as Cursor expect.
52
73
  */
53
- export declare class StreamableHttpTransport implements Transport {
74
+ export declare class StreamableHttpTransport {
54
75
  private app;
55
76
  private server;
56
- private sessions;
57
- private activeStreams;
58
- private messageHandler?;
59
- private closeHandler?;
60
- private errorHandler?;
61
77
  private options;
62
- private sessionCleanupInterval?;
63
78
  private getToolsCallback?;
64
79
  private serverConfig?;
65
80
  private logoBase64?;
81
+ private _routesRegistered;
82
+ private mcpServerFactory?;
83
+ private legacySseHandler?;
84
+ private mcpSessions;
85
+ private pendingSessions;
86
+ private sessionCleanupInterval?;
87
+ /** Grace period for a created session to finish `initialize` before it is reaped. */
88
+ private static readonly PENDING_SESSION_TIMEOUT_MS;
66
89
  constructor(options?: StreamableHttpTransportOptions);
90
+ /**
91
+ * Provide the factory used to build a configured MCP server per session.
92
+ * Must be called before `start()`.
93
+ */
94
+ setMcpServerFactory(factory: McpServerFactory): void;
95
+ /**
96
+ * Fallback for clients that speak legacy HTTP+SSE on GET /mcp (no mcp-session-id).
97
+ * Streamable HTTP clients always begin with POST initialize instead.
98
+ */
99
+ setLegacySseHandler(handler: LegacySseHandler): void;
67
100
  /**
68
101
  * Load logo image as base64 for embedding in documentation page
69
102
  */
@@ -77,41 +110,30 @@ export declare class StreamableHttpTransport implements Transport {
77
110
  */
78
111
  private setupRoutes;
79
112
  /**
80
- * Handle POST requests (client sending messages to server)
81
- */
82
- private handlePost;
83
- /**
84
- * Handle GET requests (client opening SSE stream)
85
- */
86
- private handleGet;
87
- /**
88
- * Handle DELETE requests (session termination)
113
+ * Delegate an `/mcp` request (POST/GET/DELETE) to the official SDK
114
+ * Streamable HTTP transport. A new session (and its own MCP server instance)
115
+ * is created on an `initialize` POST; subsequent requests are routed by the
116
+ * `mcp-session-id` header.
89
117
  */
90
- private handleDelete;
118
+ private handleMcpRequest;
91
119
  /**
92
- * Start SSE stream for a request
120
+ * Create a new MCP session: a fresh configured MCP server connected to a new
121
+ * official Streamable HTTP transport. The transport registers itself in the
122
+ * session map once it has negotiated a session id, and removes itself on close.
93
123
  */
94
- private startSSEStream;
124
+ private createSession;
95
125
  /**
96
- * Send message to client(s)
126
+ * Tear down a session, removing it from both the live and pending collections
127
+ * and closing its transport and server. Safe to call from the sweeper, request
128
+ * error paths, and shutdown.
97
129
  */
98
- send(message: JSONRPCMessage): Promise<void>;
130
+ private destroySession;
99
131
  /**
100
- * Send message to a specific stream
132
+ * Periodically tear down sessions that have been idle longer than
133
+ * `sessionTimeout`. Prevents unbounded memory growth from clients that
134
+ * initialize but never disconnect or send DELETE.
101
135
  */
102
- private sendToStream;
103
- /**
104
- * Send response to the stream that made the request
105
- */
106
- private sendToRequestStream;
107
- /**
108
- * Send message to a sessionless stream
109
- */
110
- private sendToStreamSessionless;
111
- /**
112
- * Replay messages for resumability
113
- */
114
- private replayMessages;
136
+ private startSessionCleanup;
115
137
  /**
116
138
  * Start the HTTP server
117
139
  */
@@ -120,33 +142,14 @@ export declare class StreamableHttpTransport implements Transport {
120
142
  * Register additional HTTP routes
121
143
  * Allows modules (like OAuthModule) to add custom endpoints
122
144
  */
123
- on(path: string, handler: (req: Request, res: Response) => void): void;
145
+ on(path: string, handler: (req: any, res: any) => void): void;
124
146
  /**
125
- * Close the transport
147
+ * Close the transport: tear down all live MCP sessions and the HTTP server.
126
148
  */
127
149
  close(): Promise<void>;
128
150
  /**
129
- * Set message handler
130
- */
131
- set onmessage(handler: (message: JSONRPCMessage) => Promise<void>);
132
- /**
133
- * Set close handler
134
- */
135
- set onclose(handler: () => void);
136
- /**
137
- * Set error handler
138
- */
139
- set onerror(handler: (error: Error) => void);
140
- /**
141
- * Start session cleanup interval
142
- */
143
- private startSessionCleanup;
144
- /**
145
- * Helper methods
151
+ * Whether a parsed JSON-RPC body is an `initialize` request.
146
152
  */
147
- private generateSessionId;
148
- private getMessageType;
149
- private isResponse;
150
153
  private isInitializeRequest;
151
154
  private isLocalhost;
152
155
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"streamable-http.d.ts","sourceRoot":"","sources":["../../../src/core/transports/streamable-http.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAgB,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAgB,MAAM,SAAS,CAAC;AAE5E,OAAO,EAAE,SAAS,EAAE,MAAM,+CAA+C,CAAC;AAC1E,OAAO,EAAE,cAAc,EAAwD,IAAI,IAAI,OAAO,EAAE,MAAM,oCAAoC,CAAC;AAM3I,MAAM,WAAW,8BAA8B;IAC7C;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;OAEG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;IAEzB;;OAEG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB;;OAEG;IACH,GAAG,CAAC,EAAE,OAAO,CAAC;IAEd;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAuBD;;;GAGG;AACH,qBAAa,uBAAwB,YAAW,SAAS;IACvD,OAAO,CAAC,GAAG,CAAU;IACrB,OAAO,CAAC,MAAM,CAA2B;IACzC,OAAO,CAAC,QAAQ,CAAyC;IACzD,OAAO,CAAC,aAAa,CAAqC;IAC1D,OAAO,CAAC,cAAc,CAAC,CAA6C;IACpE,OAAO,CAAC,YAAY,CAAC,CAAa;IAClC,OAAO,CAAC,YAAY,CAAC,CAAyB;IAC9C,OAAO,CAAC,OAAO,CAAwD;IACvE,OAAO,CAAC,sBAAsB,CAAC,CAAiB;IAChD,OAAO,CAAC,gBAAgB,CAAC,CAA2B;IACpD,OAAO,CAAC,YAAY,CAAC,CAA0D;IAC/E,O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