@tmcp/transport-http 0.3.0 → 0.4.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/CHANGELOG.md +12 -0
- package/package.json +2 -1
- package/src/index.d.ts +0 -9
- package/src/index.js +44 -9
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @tmcp/transport-http
|
|
2
2
|
|
|
3
|
+
## 0.4.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 77d3257: fix: respond immediately to allow for requests from the server
|
|
8
|
+
|
|
9
|
+
## 0.4.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- 8f09cd2: fix: send requests back on the same stream if available
|
|
14
|
+
|
|
3
15
|
## 0.3.0
|
|
4
16
|
|
|
5
17
|
### Minor Changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tmcp/transport-http",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
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
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,41 @@ 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
|
-
|
|
166
|
-
|
|
167
|
-
|
|
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(
|
|
172
|
-
controller
|
|
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 handle = async () => {
|
|
193
|
+
const response = await this.#controller_storage.run(
|
|
194
|
+
controller,
|
|
195
|
+
() => this.#server.receive(body, session_id),
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
controller?.enqueue(
|
|
199
|
+
'data: ' + JSON.stringify(response) + '\n\n',
|
|
200
|
+
);
|
|
201
|
+
controller?.close();
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
handle();
|
|
205
|
+
|
|
206
|
+
const messages = Array.isArray(body) ? body : [body];
|
|
207
|
+
|
|
208
|
+
// Determine status code based on response type
|
|
209
|
+
// 202 Accepted for notifications/responses, 200 OK for standard requests
|
|
210
|
+
const status = !messages.some((message) => message.id != null)
|
|
211
|
+
? 202
|
|
212
|
+
: 200;
|
|
213
|
+
|
|
179
214
|
return new Response(stream, {
|
|
180
215
|
headers: {
|
|
181
216
|
'Content-Type': 'text/event-stream',
|