@tmcp/transport-http 0.5.2 → 0.5.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/README.md +36 -53
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -5,11 +5,7 @@ An HTTP transport implementation for TMCP (TypeScript Model Context Protocol) se
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
npm install @tmcp/transport-http tmcp
|
|
9
|
-
# or
|
|
10
8
|
pnpm add @tmcp/transport-http tmcp
|
|
11
|
-
# or
|
|
12
|
-
yarn add @tmcp/transport-http tmcp
|
|
13
9
|
```
|
|
14
10
|
|
|
15
11
|
## Usage
|
|
@@ -54,56 +50,18 @@ server.tool(
|
|
|
54
50
|
const transport = new HttpTransport(server);
|
|
55
51
|
|
|
56
52
|
// Use with your preferred HTTP server
|
|
57
|
-
// Example with Node.js built-in server
|
|
58
|
-
import
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const request = new Request(`http://localhost${req.url}`, {
|
|
66
|
-
method: req.method,
|
|
67
|
-
headers: req.headers,
|
|
68
|
-
body: req.method === 'POST' ? body : undefined,
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
const response = await transport.respond(request);
|
|
72
|
-
|
|
73
|
-
// If response is null, the request wasn't for MCP
|
|
74
|
-
if (response === null) {
|
|
75
|
-
res.statusCode = 404;
|
|
76
|
-
res.end('Not Found');
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// Copy response to Node.js response
|
|
81
|
-
response.headers.forEach((value, key) => {
|
|
82
|
-
res.setHeader(key, value);
|
|
83
|
-
});
|
|
84
|
-
res.statusCode = response.status;
|
|
85
|
-
|
|
86
|
-
if (response.body) {
|
|
87
|
-
const reader = response.body.getReader();
|
|
88
|
-
const pump = async () => {
|
|
89
|
-
const { done, value } = await reader.read();
|
|
90
|
-
if (done) {
|
|
91
|
-
res.end();
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
res.write(value);
|
|
95
|
-
pump();
|
|
96
|
-
};
|
|
97
|
-
pump();
|
|
98
|
-
} else {
|
|
99
|
-
res.end();
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
} catch (error) {
|
|
103
|
-
res.statusCode = 500;
|
|
104
|
-
res.end('Internal Server Error');
|
|
53
|
+
// Example with Node.js built-in server + @remix-run/
|
|
54
|
+
import * as http from 'node:http';
|
|
55
|
+
import { createRequestListener } from '@remix-run/node-fetch-server';
|
|
56
|
+
|
|
57
|
+
let httpServer = http.createServer(createRequestListener((request)=>{
|
|
58
|
+
const response = await transport.respond(request);
|
|
59
|
+
if(response){
|
|
60
|
+
return response;
|
|
105
61
|
}
|
|
106
|
-
});
|
|
62
|
+
return new Response(null, { status: 404 });
|
|
63
|
+
}));
|
|
64
|
+
|
|
107
65
|
|
|
108
66
|
httpServer.listen(3000, () => {
|
|
109
67
|
console.log('MCP HTTP server listening on port 3000');
|
|
@@ -157,6 +115,7 @@ Creates a new HTTP transport instance.
|
|
|
157
115
|
interface HttpTransportOptions {
|
|
158
116
|
getSessionId: () => string; // Custom session ID generator
|
|
159
117
|
path?: string; // MCP endpoint path (default: '/mcp')
|
|
118
|
+
oauth?: OAuth; // an oauth provider generated from @tmcp/auth
|
|
160
119
|
}
|
|
161
120
|
```
|
|
162
121
|
|
|
@@ -187,6 +146,7 @@ Processes an HTTP request and returns a Response with Server-Sent Events, or nul
|
|
|
187
146
|
The transport supports three HTTP methods:
|
|
188
147
|
|
|
189
148
|
#### POST - Message Processing
|
|
149
|
+
|
|
190
150
|
Clients send JSON-RPC messages via HTTP POST requests:
|
|
191
151
|
|
|
192
152
|
```http
|
|
@@ -215,6 +175,7 @@ data: {"jsonrpc":"2.0","id":1,"result":{"tools":[...]}}
|
|
|
215
175
|
```
|
|
216
176
|
|
|
217
177
|
#### GET - Notification Stream
|
|
178
|
+
|
|
218
179
|
Establishes long-lived connections for server notifications:
|
|
219
180
|
|
|
220
181
|
```http
|
|
@@ -236,6 +197,7 @@ data: {"jsonrpc":"2.0","method":"notifications/initialized","params":{}}
|
|
|
236
197
|
```
|
|
237
198
|
|
|
238
199
|
#### DELETE - Session Disconnect
|
|
200
|
+
|
|
239
201
|
Disconnects a session and cleans up resources:
|
|
240
202
|
|
|
241
203
|
```http
|
|
@@ -298,6 +260,27 @@ Deno.serve({ port: 3000 }, async (req) => {
|
|
|
298
260
|
});
|
|
299
261
|
```
|
|
300
262
|
|
|
263
|
+
### `srvx`
|
|
264
|
+
|
|
265
|
+
If you want to have the same experience throughout Deno, Bun or Node you can also use `srvx`
|
|
266
|
+
|
|
267
|
+
```js
|
|
268
|
+
import { McpServer } from 'tmcp';
|
|
269
|
+
import { HttpTransport } from '@tmcp/transport-http';
|
|
270
|
+
import { serve } from 'srvx';
|
|
271
|
+
|
|
272
|
+
const server = new McpServer(/* ... */);
|
|
273
|
+
const transport = new HttpTransport(server);
|
|
274
|
+
|
|
275
|
+
serve(async (req) => {
|
|
276
|
+
const response = await transport.respond(req);
|
|
277
|
+
if (response === null) {
|
|
278
|
+
return new Response('Not Found', { status: 404 });
|
|
279
|
+
}
|
|
280
|
+
return response;
|
|
281
|
+
});
|
|
282
|
+
```
|
|
283
|
+
|
|
301
284
|
## Error Handling
|
|
302
285
|
|
|
303
286
|
The transport includes comprehensive error handling:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tmcp/transport-http",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.3",
|
|
4
4
|
"description": "Transport for TMCP using HTTP",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"src/**/*"
|
|
17
17
|
],
|
|
18
18
|
"peerDependencies": {
|
|
19
|
-
"tmcp": "^1.
|
|
19
|
+
"tmcp": "^1.10.2"
|
|
20
20
|
},
|
|
21
21
|
"keywords": [
|
|
22
22
|
"tmcp",
|
|
@@ -35,9 +35,9 @@
|
|
|
35
35
|
"publint": "^0.3.12",
|
|
36
36
|
"valibot": "^1.1.0",
|
|
37
37
|
"vitest": "^3.2.4",
|
|
38
|
-
"
|
|
39
|
-
"@tmcp/
|
|
40
|
-
"tmcp": "^
|
|
38
|
+
"tmcp": "^1.10.2",
|
|
39
|
+
"@tmcp/adapter-valibot": "^0.1.4",
|
|
40
|
+
"@tmcp/auth": "^0.3.3"
|
|
41
41
|
},
|
|
42
42
|
"publishConfig": {
|
|
43
43
|
"access": "public"
|