@tmcp/transport-http 0.2.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # @tmcp/transport-http
2
2
 
3
+ ## 0.4.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 8f09cd2: fix: send requests back on the same stream if available
8
+
9
+ ## 0.3.0
10
+
11
+ ### Minor Changes
12
+
13
+ - 3847654: fix: answer with conflict for second SSE connection
14
+
15
+ ### Patch Changes
16
+
17
+ - Updated dependencies [1b50780]
18
+ - tmcp@1.7.1
19
+
3
20
  ## 0.2.1
4
21
 
5
22
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmcp/transport-http",
3
- "version": "0.2.1",
3
+ "version": "0.4.0",
4
4
  "description": "Transport for TMCP using HTTP",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -13,7 +13,7 @@
13
13
  "./package.json": "./package.json"
14
14
  },
15
15
  "peerDependencies": {
16
- "tmcp": "^1.7.0"
16
+ "tmcp": "^1.7.1"
17
17
  },
18
18
  "keywords": [
19
19
  "tmcp",
@@ -26,8 +26,9 @@
26
26
  "directory": "packages/transport-http"
27
27
  },
28
28
  "devDependencies": {
29
+ "@types/node": "^24.0.15",
29
30
  "publint": "^0.3.12",
30
- "tmcp": "^1.7.0"
31
+ "tmcp": "^1.7.1"
31
32
  },
32
33
  "publishConfig": {
33
34
  "access": "public"
package/src/index.d.ts CHANGED
@@ -1,12 +1,3 @@
1
- /**
2
- * @import { McpServer, ClientCapabilities } from "tmcp";
3
- */
4
- /**
5
- * @typedef {{
6
- * getSessionId?: () => string
7
- * path?: string
8
- * }} HttpTransportOptions
9
- */
10
1
  export class HttpTransport {
11
2
  /**
12
3
  *
package/src/index.js CHANGED
@@ -9,6 +9,8 @@
9
9
  * }} HttpTransportOptions
10
10
  */
11
11
 
12
+ import { AsyncLocalStorage } from 'node:async_hooks';
13
+
12
14
  export class HttpTransport {
13
15
  /**
14
16
  * @type {McpServer<any>}
@@ -34,6 +36,11 @@ export class HttpTransport {
34
36
  */
35
37
  #streams = new Map();
36
38
 
39
+ /**
40
+ * @type {AsyncLocalStorage<ReadableStreamDefaultController | undefined>}
41
+ */
42
+ #controller_storage = new AsyncLocalStorage();
43
+
37
44
  /**
38
45
  *
39
46
  * @param {McpServer<any>} server
@@ -48,6 +55,15 @@ export class HttpTransport {
48
55
  this.#options = { getSessionId, path };
49
56
  this.#path = path;
50
57
  this.#server.on('send', ({ request, context: { sessions } }) => {
58
+ // use the current controller if the request has an id (it means it's a request and not a notification)
59
+ if (request.id != null) {
60
+ const controller = this.#controller_storage.getStore();
61
+ if (!controller)
62
+ throw new Error('Controller not found in storage');
63
+
64
+ controller.enqueue('data: ' + JSON.stringify(request) + '\n\n');
65
+ return;
66
+ }
51
67
  for (let [session_id, controller] of this.#session.entries()) {
52
68
  if (sessions === undefined || sessions.includes(session_id)) {
53
69
  controller.enqueue(
@@ -87,15 +103,24 @@ export class HttpTransport {
87
103
  // If session already exists, return existing stream
88
104
  const existing_stream = this.#streams.get(session_id);
89
105
  if (existing_stream) {
90
- return new Response(existing_stream, {
91
- headers: {
92
- 'Content-Type': 'text/event-stream',
93
- 'Cache-Control': 'no-cache',
94
- Connection: 'keep-alive',
95
- 'mcp-session-id': session_id,
106
+ return new Response(
107
+ JSON.stringify({
108
+ jsonrpc: '2.0',
109
+ error: {
110
+ code: -32000,
111
+ message:
112
+ 'Conflict: Only one SSE stream is allowed per session',
113
+ },
114
+ id: null,
115
+ }),
116
+ {
117
+ headers: {
118
+ 'Content-Type': 'application/json',
119
+ 'mcp-session-id': session_id,
120
+ },
121
+ status: 409,
96
122
  },
97
- status: 200,
98
- });
123
+ );
99
124
  }
100
125
 
101
126
  // Create new long-lived stream for notifications
@@ -151,22 +176,31 @@ export class HttpTransport {
151
176
 
152
177
  try {
153
178
  const body = await request.clone().json();
154
- const response = await this.#server.receive(body, session_id);
155
179
 
156
- // Determine status code based on response type
157
- // 202 Accepted for notifications/responses, 200 OK for standard requests
158
- const status = response == null || response.id == null ? 202 : 200;
180
+ /**
181
+ * @type {ReadableStreamDefaultController | undefined}
182
+ */
183
+ let controller;
159
184
 
160
185
  // Create a short-lived stream that closes after sending the response
161
186
  const stream = new ReadableStream({
162
- start(controller) {
163
- controller.enqueue(
164
- 'data: ' + JSON.stringify(response) + '\n\n',
165
- );
166
- controller.close();
187
+ start(_controller) {
188
+ controller = _controller;
167
189
  },
168
190
  });
169
191
 
192
+ const response = await this.#controller_storage.run(
193
+ controller,
194
+ () => this.#server.receive(body, session_id),
195
+ );
196
+
197
+ controller?.enqueue('data: ' + JSON.stringify(response) + '\n\n');
198
+ controller?.close();
199
+
200
+ // Determine status code based on response type
201
+ // 202 Accepted for notifications/responses, 200 OK for standard requests
202
+ const status = response == null || response.id == null ? 202 : 200;
203
+
170
204
  return new Response(stream, {
171
205
  headers: {
172
206
  'Content-Type': 'text/event-stream',