@tmcp/transport-http 0.3.0 → 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,11 @@
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
+
3
9
  ## 0.3.0
4
10
 
5
11
  ### Minor Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmcp/transport-http",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Transport for TMCP using HTTP",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -26,6 +26,7 @@
26
26
  "directory": "packages/transport-http"
27
27
  },
28
28
  "devDependencies": {
29
+ "@types/node": "^24.0.15",
29
30
  "publint": "^0.3.12",
30
31
  "tmcp": "^1.7.1"
31
32
  },
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(
@@ -160,22 +176,31 @@ export class HttpTransport {
160
176
 
161
177
  try {
162
178
  const body = await request.clone().json();
163
- const response = await this.#server.receive(body, session_id);
164
179
 
165
- // Determine status code based on response type
166
- // 202 Accepted for notifications/responses, 200 OK for standard requests
167
- const status = response == null || response.id == null ? 202 : 200;
180
+ /**
181
+ * @type {ReadableStreamDefaultController | undefined}
182
+ */
183
+ let controller;
168
184
 
169
185
  // Create a short-lived stream that closes after sending the response
170
186
  const stream = new ReadableStream({
171
- start(controller) {
172
- controller.enqueue(
173
- 'data: ' + JSON.stringify(response) + '\n\n',
174
- );
175
- controller.close();
187
+ start(_controller) {
188
+ controller = _controller;
176
189
  },
177
190
  });
178
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
+
179
204
  return new Response(stream, {
180
205
  headers: {
181
206
  'Content-Type': 'text/event-stream',