@tmcp/transport-http 0.5.6 → 0.6.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/README.md +46 -1
- package/package.json +4 -4
- package/src/index.js +17 -5
- package/src/types/index.d.ts +3 -3
- package/src/types/index.d.ts.map +1 -1
package/README.md
CHANGED
|
@@ -81,6 +81,50 @@ const transport = new HttpTransport(server, {
|
|
|
81
81
|
});
|
|
82
82
|
```
|
|
83
83
|
|
|
84
|
+
### With Custom Context
|
|
85
|
+
|
|
86
|
+
You can pass custom context data to your MCP server for each request. This is useful for authentication, user information, database connections, etc.
|
|
87
|
+
|
|
88
|
+
```javascript
|
|
89
|
+
// Define your custom context type
|
|
90
|
+
interface MyContext {
|
|
91
|
+
userId: string;
|
|
92
|
+
permissions: string[];
|
|
93
|
+
database: DatabaseConnection;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Create server with custom context
|
|
97
|
+
const server = new McpServer(serverInfo, options).withContext<MyContext>();
|
|
98
|
+
|
|
99
|
+
server.tool(
|
|
100
|
+
{
|
|
101
|
+
name: 'get-user-profile',
|
|
102
|
+
description: 'Get the current user profile',
|
|
103
|
+
},
|
|
104
|
+
async () => {
|
|
105
|
+
// Access custom context in your handler
|
|
106
|
+
const { userId, database } = server.ctx.custom!;
|
|
107
|
+
const profile = await database.users.findById(userId);
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
content: [
|
|
111
|
+
{ type: 'text', text: `User profile: ${JSON.stringify(profile)}` }
|
|
112
|
+
],
|
|
113
|
+
};
|
|
114
|
+
},
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
// Create transport (it will be typed to accept your custom context)
|
|
118
|
+
const transport = new HttpTransport(server);
|
|
119
|
+
|
|
120
|
+
// then in the handler
|
|
121
|
+
const response = await transport.respond(req, {
|
|
122
|
+
userId,
|
|
123
|
+
permissions,
|
|
124
|
+
database: req.locals.db,
|
|
125
|
+
});
|
|
126
|
+
```
|
|
127
|
+
|
|
84
128
|
### Session Management
|
|
85
129
|
|
|
86
130
|
The HTTP transport supports custom session managers for different deployment scenarios:
|
|
@@ -154,13 +198,14 @@ interface HttpTransportOptions {
|
|
|
154
198
|
|
|
155
199
|
#### Methods
|
|
156
200
|
|
|
157
|
-
##### `respond(request: Request): Promise<Response | null>`
|
|
201
|
+
##### `respond(request: Request, customContext?: T): Promise<Response | null>`
|
|
158
202
|
|
|
159
203
|
Processes an HTTP request and returns a Response with Server-Sent Events, or null if the request path doesn't match the configured MCP path.
|
|
160
204
|
|
|
161
205
|
**Parameters:**
|
|
162
206
|
|
|
163
207
|
- `request` - A Web API Request object containing the JSON-RPC message
|
|
208
|
+
- `customContext` - Optional custom context data to pass to the MCP server for this request
|
|
164
209
|
|
|
165
210
|
**Returns:**
|
|
166
211
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tmcp/transport-http",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
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.12.2"
|
|
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.
|
|
32
|
+
"@tmcp/session-manager": "^0.1.2"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@modelcontextprotocol/sdk": "^1.17.1",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"vitest": "^3.2.4",
|
|
42
42
|
"@tmcp/adapter-valibot": "^0.1.4",
|
|
43
43
|
"@tmcp/auth": "^0.3.3",
|
|
44
|
-
"tmcp": "^1.
|
|
44
|
+
"tmcp": "^1.12.2"
|
|
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) {
|
|
@@ -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
|
/**
|
|
@@ -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(
|
|
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 {
|
package/src/types/index.d.ts
CHANGED
|
@@ -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 = {
|