@tmcp/transport-http 0.5.5 → 0.6.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmcp/transport-http",
3
- "version": "0.5.5",
3
+ "version": "0.6.0",
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.10.2"
19
+ "tmcp": "^1.11.0"
20
20
  },
21
21
  "keywords": [
22
22
  "tmcp",
@@ -29,7 +29,7 @@
29
29
  "directory": "packages/transport-http"
30
30
  },
31
31
  "dependencies": {
32
- "@tmcp/session-manager": "^0.1.0"
32
+ "@tmcp/session-manager": "^0.1.1"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@modelcontextprotocol/sdk": "^1.17.1",
@@ -40,8 +40,8 @@
40
40
  "valibot": "^1.1.0",
41
41
  "vitest": "^3.2.4",
42
42
  "@tmcp/adapter-valibot": "^0.1.4",
43
- "tmcp": "^1.10.2",
44
- "@tmcp/auth": "^0.3.3"
43
+ "@tmcp/auth": "^0.3.3",
44
+ "tmcp": "^1.11.0"
45
45
  },
46
46
  "publishConfig": {
47
47
  "access": "public"
package/src/index.js CHANGED
@@ -27,9 +27,13 @@
27
27
 
28
28
  import { AsyncLocalStorage } from 'node:async_hooks';
29
29
  import { InMemorySessionManager } from '@tmcp/session-manager';
30
+
31
+ /**
32
+ * @template {Record<string, unknown> | undefined} [TCustom=undefined]
33
+ */
30
34
  export class HttpTransport {
31
35
  /**
32
- * @type {McpServer<any>}
36
+ * @type {McpServer<any, TCustom>}
33
37
  */
34
38
  #server;
35
39
 
@@ -57,7 +61,7 @@ export class HttpTransport {
57
61
 
58
62
  /**
59
63
  *
60
- * @param {McpServer<any>} server
64
+ * @param {McpServer<any, TCustom>} server
61
65
  * @param {HttpTransportOptions} [options]
62
66
  */
63
67
  constructor(server, options) {
@@ -78,7 +82,7 @@ export class HttpTransport {
78
82
 
79
83
  this.#options = { getSessionId, path, cors, sessionManager };
80
84
  this.#path = path;
81
- this.#server.on('send', ({ request, context: { sessions } }) => {
85
+ this.#server.on('send', async ({ request, context: { sessions } }) => {
82
86
  // use the current controller if the request has an id (it means it's a request and not a notification)
83
87
  if (request.id != null) {
84
88
  const controller = this.#controller_storage.getStore();
@@ -91,7 +95,7 @@ export class HttpTransport {
91
95
  );
92
96
  return;
93
97
  }
94
- this.#options.sessionManager.send(
98
+ await this.#options.sessionManager.send(
95
99
  sessions,
96
100
  'data: ' + JSON.stringify(request) + '\n\n',
97
101
  );
@@ -184,8 +188,8 @@ export class HttpTransport {
184
188
  /**
185
189
  * @param {string} session_id
186
190
  */
187
- #handle_delete(session_id) {
188
- this.#options.sessionManager.delete(session_id);
191
+ async #handle_delete(session_id) {
192
+ await this.#options.sessionManager.delete(session_id);
189
193
  return new Response(null, {
190
194
  status: 204,
191
195
  headers: {
@@ -226,11 +230,11 @@ export class HttpTransport {
226
230
 
227
231
  // Create new long-lived stream for notifications
228
232
  const stream = new ReadableStream({
229
- start(controller) {
230
- sessions.create(session_id, controller);
233
+ async start(controller) {
234
+ await sessions.create(session_id, controller);
231
235
  },
232
- cancel() {
233
- sessions.delete(session_id);
236
+ async cancel() {
237
+ await sessions.delete(session_id);
234
238
  },
235
239
  });
236
240
 
@@ -250,8 +254,9 @@ export class HttpTransport {
250
254
  * @param {string} session_id
251
255
  * @param {Request} request
252
256
  * @param {AuthInfo | null} auth_info
257
+ * @param {TCustom} [ctx]
253
258
  */
254
- async #handle_post(session_id, request, auth_info) {
259
+ async #handle_post(session_id, request, auth_info, ctx) {
255
260
  // Check Content-Type header
256
261
  const content_type = request.headers.get('content-type');
257
262
  if (!content_type || !content_type.includes('application/json')) {
@@ -296,6 +301,7 @@ export class HttpTransport {
296
301
  this.#server.receive(body, {
297
302
  sessionId: session_id,
298
303
  auth: auth_info ?? undefined,
304
+ custom: ctx,
299
305
  }),
300
306
  );
301
307
 
@@ -375,9 +381,10 @@ export class HttpTransport {
375
381
  /**
376
382
  *
377
383
  * @param {Request} request
384
+ * @param {TCustom} [ctx]
378
385
  * @returns {Promise<Response | null>}
379
386
  */
380
- async respond(request) {
387
+ async respond(request, ctx) {
381
388
  const url = new URL(request.url);
382
389
 
383
390
  /**
@@ -433,7 +440,7 @@ export class HttpTransport {
433
440
  }
434
441
  // Handle DELETE request - disconnect session
435
442
  else if (method === 'DELETE') {
436
- response = this.#handle_delete(session_id);
443
+ response = await this.#handle_delete(session_id);
437
444
  }
438
445
  // Handle GET request - establish long-lived connection for notifications
439
446
  else if (method === 'GET') {
@@ -441,7 +448,12 @@ export class HttpTransport {
441
448
  }
442
449
  // Handle POST request - process message and respond through event stream
443
450
  else if (method === 'POST') {
444
- response = await this.#handle_post(session_id, request, auth_info);
451
+ response = await this.#handle_post(
452
+ session_id,
453
+ request,
454
+ auth_info,
455
+ ctx,
456
+ );
445
457
  }
446
458
  // Method not supported
447
459
  else {
@@ -2,11 +2,11 @@ declare module '@tmcp/transport-http' {
2
2
  import type { McpServer } from 'tmcp';
3
3
  import type { OAuth } from '@tmcp/auth';
4
4
  import type { SessionManager } from '@tmcp/session-manager';
5
- export class HttpTransport {
5
+ export class HttpTransport<TCustom extends Record<string, unknown> | undefined = undefined> {
6
6
 
7
- constructor(server: McpServer<any>, options?: HttpTransportOptions);
7
+ constructor(server: McpServer<any, TCustom>, options?: HttpTransportOptions);
8
8
 
9
- respond(request: Request): Promise<Response | null>;
9
+ respond(request: Request, ctx?: TCustom): Promise<Response | null>;
10
10
  #private;
11
11
  }
12
12
  export type CorsConfig = {
@@ -10,6 +10,6 @@
10
10
  "sourcesContent": [
11
11
  null
12
12
  ],
13
- "mappings": ";;;;cA6BaA,aAAaA",
13
+ "mappings": ";;;;cAiCaA,aAAaA",
14
14
  "ignoreList": []
15
15
  }