@tmcp/transport-http 0.5.1 → 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.
Files changed (3) hide show
  1. package/README.md +36 -53
  2. package/package.json +5 -5
  3. package/src/index.js +13 -3
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 { createServer } from 'http';
59
-
60
- const httpServer = createServer(async (req, res) => {
61
- try {
62
- let body = '';
63
- req.on('data', (chunk) => (body += chunk));
64
- req.on('end', async () => {
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.1",
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.9.0"
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
- "@tmcp/adapter-valibot": "^0.1.3",
39
- "@tmcp/auth": "^0.3.1",
40
- "tmcp": "^1.9.0"
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"
package/src/index.js CHANGED
@@ -47,6 +47,8 @@ export class HttpTransport {
47
47
  */
48
48
  #oauth;
49
49
 
50
+ #text_encoder = new TextEncoder();
51
+
50
52
  /**
51
53
  *
52
54
  * @param {McpServer<any>} server
@@ -74,13 +76,19 @@ export class HttpTransport {
74
76
  const controller = this.#controller_storage.getStore();
75
77
  if (!controller) return;
76
78
 
77
- controller.enqueue('data: ' + JSON.stringify(request) + '\n\n');
79
+ controller.enqueue(
80
+ this.#text_encoder.encode(
81
+ 'data: ' + JSON.stringify(request) + '\n\n',
82
+ ),
83
+ );
78
84
  return;
79
85
  }
80
86
  for (let [session_id, controller] of this.#session.entries()) {
81
87
  if (sessions === undefined || sessions.includes(session_id)) {
82
88
  controller.enqueue(
83
- 'data: ' + JSON.stringify(request) + '\n\n',
89
+ this.#text_encoder.encode(
90
+ 'data: ' + JSON.stringify(request) + '\n\n',
91
+ ),
84
92
  );
85
93
  }
86
94
  }
@@ -214,7 +222,9 @@ export class HttpTransport {
214
222
  );
215
223
 
216
224
  controller?.enqueue(
217
- 'data: ' + JSON.stringify(response) + '\n\n',
225
+ this.#text_encoder.encode(
226
+ 'data: ' + JSON.stringify(response) + '\n\n',
227
+ ),
218
228
  );
219
229
  controller?.close();
220
230
  };