@tsed/platform-mcp 8.31.0 → 8.31.2
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.
|
@@ -11,6 +11,7 @@ export declare class PlatformMcpModule implements OnRoutesInit {
|
|
|
11
11
|
protected settings: PlatformMcpSettings;
|
|
12
12
|
protected app: import("@tsed/platform-http").PlatformApplication<TsED.Application>;
|
|
13
13
|
protected server: McpServer;
|
|
14
|
+
private loaded;
|
|
14
15
|
$onRoutesInit(): void;
|
|
15
16
|
$logRoutes(routes: PlatformRouteDetails[]): (false | PlatformRouteDetails | {
|
|
16
17
|
method: string;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@tsed/platform-mcp",
|
|
3
3
|
"description": "Model Context Protocol integration for Ts.ED HTTP adapters.",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "8.31.
|
|
5
|
+
"version": "8.31.2",
|
|
6
6
|
"source": "./src/index.ts",
|
|
7
7
|
"main": "./lib/esm/index.js",
|
|
8
8
|
"module": "./lib/esm/index.js",
|
|
@@ -32,12 +32,12 @@
|
|
|
32
32
|
"zod": ">=4.0.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@tsed/barrels": "8.31.
|
|
36
|
-
"@tsed/core": "8.31.
|
|
37
|
-
"@tsed/di": "8.31.
|
|
38
|
-
"@tsed/platform-http": "8.31.
|
|
39
|
-
"@tsed/schema": "8.31.
|
|
40
|
-
"@tsed/typescript": "8.31.
|
|
35
|
+
"@tsed/barrels": "8.31.2",
|
|
36
|
+
"@tsed/core": "8.31.2",
|
|
37
|
+
"@tsed/di": "8.31.2",
|
|
38
|
+
"@tsed/platform-http": "8.31.2",
|
|
39
|
+
"@tsed/schema": "8.31.2",
|
|
40
|
+
"@tsed/typescript": "8.31.2",
|
|
41
41
|
"eslint": "9.12.0",
|
|
42
42
|
"typescript": "6.0.2",
|
|
43
43
|
"vitest": "4.1.4"
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import {EventEmitter} from "node:events";
|
|
2
|
+
|
|
3
|
+
import {PlatformFastifyResponse} from "@tsed/platform-fastify";
|
|
4
|
+
import {application} from "@tsed/platform-http";
|
|
5
|
+
import {PlatformTest} from "@tsed/platform-http/testing";
|
|
6
|
+
import {afterEach, beforeEach, describe, expect, it, vi} from "vitest";
|
|
7
|
+
|
|
8
|
+
import {PlatformMcpModule} from "./PlatformMcpModule.js";
|
|
9
|
+
|
|
10
|
+
const {TestTransport, transportInstances} = vi.hoisted(() => {
|
|
11
|
+
const transportInstances: TestTransport[] = [];
|
|
12
|
+
|
|
13
|
+
class TestTransport {
|
|
14
|
+
close = vi.fn().mockResolvedValue(undefined);
|
|
15
|
+
handleRequest = vi.fn().mockResolvedValue(undefined);
|
|
16
|
+
|
|
17
|
+
constructor(readonly options: Record<string, unknown>) {
|
|
18
|
+
transportInstances.push(this);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return {TestTransport, transportInstances};
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
vi.mock("@modelcontextprotocol/sdk/server/streamableHttp.js", () => {
|
|
26
|
+
return {
|
|
27
|
+
StreamableHTTPServerTransport: TestTransport
|
|
28
|
+
};
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
function createModule() {
|
|
32
|
+
const module = new PlatformMcpModule();
|
|
33
|
+
const server = {
|
|
34
|
+
connect: vi.fn().mockResolvedValue(undefined)
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
module["settings"] = {};
|
|
38
|
+
module["server"] = server as never;
|
|
39
|
+
|
|
40
|
+
return {module, server};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function createExpressContext() {
|
|
44
|
+
const $ctx = PlatformTest.createRequestContext();
|
|
45
|
+
const res = new EventEmitter() as EventEmitter & {locals: Record<string, unknown>};
|
|
46
|
+
|
|
47
|
+
res.locals = {};
|
|
48
|
+
$ctx.response.getRes = vi.fn().mockReturnValue(res as never);
|
|
49
|
+
|
|
50
|
+
return {$ctx, res};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function createFastifyContext() {
|
|
54
|
+
const raw = new EventEmitter();
|
|
55
|
+
const reply = Object.assign(PlatformTest.createResponse(), {
|
|
56
|
+
raw,
|
|
57
|
+
locals: {},
|
|
58
|
+
code: vi.fn(),
|
|
59
|
+
type: vi.fn(),
|
|
60
|
+
header: vi.fn(),
|
|
61
|
+
getHeader: vi.fn()
|
|
62
|
+
});
|
|
63
|
+
const $ctx = PlatformTest.createRequestContext({
|
|
64
|
+
event: {
|
|
65
|
+
response: reply
|
|
66
|
+
},
|
|
67
|
+
ResponseKlass: PlatformFastifyResponse
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
return {$ctx, res: raw};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
describe("PlatformMcpModule", () => {
|
|
74
|
+
beforeEach(() => {
|
|
75
|
+
PlatformTest.create();
|
|
76
|
+
transportInstances.length = 0;
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
afterEach(() => {
|
|
80
|
+
PlatformTest.reset();
|
|
81
|
+
vi.clearAllMocks();
|
|
82
|
+
transportInstances.length = 0;
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
describe("$onRoutesInit()", () => {
|
|
86
|
+
it("should register the MCP route only once", () => {
|
|
87
|
+
const {module} = createModule();
|
|
88
|
+
|
|
89
|
+
vi.spyOn(application(), "post").mockReturnValue(undefined as never);
|
|
90
|
+
|
|
91
|
+
module.$onRoutesInit();
|
|
92
|
+
module.$onRoutesInit();
|
|
93
|
+
|
|
94
|
+
expect(application().post).toHaveBeenCalledTimes(1);
|
|
95
|
+
expect(application().post).toHaveBeenCalledWith("/mcp", expect.any(Function));
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe.each([
|
|
100
|
+
["express", createExpressContext],
|
|
101
|
+
["fastify", createFastifyContext]
|
|
102
|
+
])("dispatch() with %s", (_, createContext) => {
|
|
103
|
+
it("should bind close on the low-level response and forward handleRequest", async () => {
|
|
104
|
+
const {module, server} = createModule();
|
|
105
|
+
const {$ctx, res} = createContext();
|
|
106
|
+
|
|
107
|
+
await module["dispatch"]($ctx);
|
|
108
|
+
|
|
109
|
+
const transport = transportInstances[0];
|
|
110
|
+
|
|
111
|
+
expect(transport).toBeDefined();
|
|
112
|
+
expect(server.connect).toHaveBeenCalledWith(transport);
|
|
113
|
+
expect(transport.handleRequest).toHaveBeenCalledWith($ctx.request.getReq(), res, $ctx.request.body);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("should close transport when the low-level response closes", async () => {
|
|
117
|
+
const {module} = createModule();
|
|
118
|
+
const {$ctx, res} = createContext();
|
|
119
|
+
|
|
120
|
+
await module["dispatch"]($ctx);
|
|
121
|
+
|
|
122
|
+
const transport = transportInstances[0];
|
|
123
|
+
|
|
124
|
+
expect(transport.close).toHaveBeenCalledTimes(1);
|
|
125
|
+
|
|
126
|
+
res.emit("close");
|
|
127
|
+
|
|
128
|
+
expect(transport.close).toHaveBeenCalledTimes(1);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
});
|
|
@@ -17,16 +17,21 @@ export class PlatformMcpModule implements OnRoutesInit {
|
|
|
17
17
|
protected settings = constant<PlatformMcpSettings>("mcp", {});
|
|
18
18
|
protected app = application();
|
|
19
19
|
protected server = inject<McpServer>(MCP_SERVER);
|
|
20
|
+
private loaded = false;
|
|
20
21
|
|
|
21
22
|
$onRoutesInit() {
|
|
22
|
-
if (this.isEnabled()) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
this.app.post(
|
|
26
|
-
path,
|
|
27
|
-
useContextHandler(async ($ctx) => this.dispatch($ctx as PlatformContext))
|
|
28
|
-
);
|
|
23
|
+
if (!this.isEnabled() || this.loaded) {
|
|
24
|
+
return;
|
|
29
25
|
}
|
|
26
|
+
|
|
27
|
+
const path = this.settings?.path || "/mcp";
|
|
28
|
+
|
|
29
|
+
this.app.post(
|
|
30
|
+
path,
|
|
31
|
+
useContextHandler(async ($ctx) => this.dispatch($ctx as PlatformContext))
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
this.loaded = true;
|
|
30
35
|
}
|
|
31
36
|
|
|
32
37
|
$logRoutes(routes: PlatformRouteDetails[]) {
|
|
@@ -48,14 +53,26 @@ export class PlatformMcpModule implements OnRoutesInit {
|
|
|
48
53
|
});
|
|
49
54
|
|
|
50
55
|
const {request, response} = $ctx;
|
|
56
|
+
const res = response.getRes();
|
|
57
|
+
let closed = false;
|
|
51
58
|
|
|
52
|
-
|
|
59
|
+
const closeTransport = async () => {
|
|
60
|
+
if (closed) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
closed = true;
|
|
65
|
+
await transport.close();
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
res?.once("close", closeTransport);
|
|
53
69
|
|
|
54
70
|
try {
|
|
55
71
|
await this.server.connect(transport as any);
|
|
56
|
-
await transport.handleRequest(request.getReq(),
|
|
72
|
+
await transport.handleRequest(request.getReq(), res, request.body);
|
|
57
73
|
} finally {
|
|
58
|
-
|
|
74
|
+
res?.off?.("close", closeTransport);
|
|
75
|
+
await closeTransport();
|
|
59
76
|
}
|
|
60
77
|
}
|
|
61
78
|
|