@tsed/platform-mcp 8.38.0-beta.1 → 8.38.0-beta.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/.barrels.json +5 -0
- package/lib/esm/cli/exports.js +1 -0
- package/lib/esm/cli/index.js +7 -0
- package/lib/esm/cli/services/mcpServerConnect.js +15 -0
- package/lib/esm/cli/services/mcpStdioServer.js +7 -0
- package/lib/esm/cli/services/mcpStreamableServer.js +40 -0
- package/lib/esm/{index.js → common/index.js} +0 -1
- package/lib/esm/{services → common/services}/McpServerFactory.js +1 -1
- package/lib/esm/http/exports.js +1 -0
- package/lib/esm/http/index.js +5 -0
- package/lib/esm/{services → http/services}/PlatformMcpModule.js +1 -1
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/types/cli/exports.d.ts +1 -0
- package/lib/types/cli/index.d.ts +7 -0
- package/lib/types/cli/services/mcpServerConnect.d.ts +1 -0
- package/lib/types/cli/services/mcpStdioServer.d.ts +2 -0
- package/lib/types/cli/services/mcpStreamableServer.d.ts +2 -0
- package/lib/types/{index.d.ts → common/index.d.ts} +0 -1
- package/lib/types/http/exports.d.ts +1 -0
- package/lib/types/http/index.d.ts +5 -0
- package/lib/types/{services → http/services}/PlatformMcpModule.d.ts +1 -1
- package/package.json +34 -16
- package/src/cli/exports.ts +1 -0
- package/src/cli/index.ts +7 -0
- package/src/cli/services/mcpServerConnect.spec.ts +37 -0
- package/src/cli/services/mcpServerConnect.ts +19 -0
- package/src/cli/services/mcpStdioServer.spec.ts +68 -0
- package/src/cli/services/mcpStdioServer.ts +12 -0
- package/src/cli/services/mcpStreamableServer.spec.ts +87 -0
- package/src/cli/services/mcpStreamableServer.ts +48 -0
- package/src/{index.ts → common/index.ts} +0 -1
- package/src/common/services/McpServerFactory.spec.ts +39 -0
- package/src/{services → common/services}/McpServerFactory.ts +2 -5
- package/src/http/exports.ts +1 -0
- package/src/http/index.ts +5 -0
- package/src/{services → http/services}/PlatformMcpModule.ts +3 -3
- /package/lib/esm/{constants → common/constants}/constants.js +0 -0
- /package/lib/esm/{decorators → common/decorators}/prompt.js +0 -0
- /package/lib/esm/{decorators → common/decorators}/resource.js +0 -0
- /package/lib/esm/{decorators → common/decorators}/tool.js +0 -0
- /package/lib/esm/{fn → common/fn}/definePrompt.js +0 -0
- /package/lib/esm/{fn → common/fn}/defineResource.js +0 -0
- /package/lib/esm/{fn → common/fn}/defineTool.js +0 -0
- /package/lib/esm/{interfaces → common/interfaces}/PlatformMcpSettings.js +0 -0
- /package/lib/esm/{utils → common/utils}/asResourceResponse.js +0 -0
- /package/lib/esm/{utils → common/utils}/asToolResponse.js +0 -0
- /package/lib/esm/{utils → common/utils}/fromJsonSchema.js +0 -0
- /package/lib/types/{constants → common/constants}/constants.d.ts +0 -0
- /package/lib/types/{decorators → common/decorators}/prompt.d.ts +0 -0
- /package/lib/types/{decorators → common/decorators}/resource.d.ts +0 -0
- /package/lib/types/{decorators → common/decorators}/tool.d.ts +0 -0
- /package/lib/types/{fn → common/fn}/definePrompt.d.ts +0 -0
- /package/lib/types/{fn → common/fn}/defineResource.d.ts +0 -0
- /package/lib/types/{fn → common/fn}/defineTool.d.ts +0 -0
- /package/lib/types/{interfaces → common/interfaces}/PlatformMcpSettings.d.ts +0 -0
- /package/lib/types/{services → common/services}/McpServerFactory.d.ts +0 -0
- /package/lib/types/{utils → common/utils}/asResourceResponse.d.ts +0 -0
- /package/lib/types/{utils → common/utils}/asToolResponse.d.ts +0 -0
- /package/lib/types/{utils → common/utils}/fromJsonSchema.d.ts +0 -0
- /package/src/{constants → common/constants}/constants.ts +0 -0
- /package/src/{decorators → common/decorators}/prompt.spec.ts +0 -0
- /package/src/{decorators → common/decorators}/prompt.ts +0 -0
- /package/src/{decorators → common/decorators}/resource.spec.ts +0 -0
- /package/src/{decorators → common/decorators}/resource.ts +0 -0
- /package/src/{decorators → common/decorators}/tool.spec.ts +0 -0
- /package/src/{decorators → common/decorators}/tool.ts +0 -0
- /package/src/{fn → common/fn}/definePrompt.spec.ts +0 -0
- /package/src/{fn → common/fn}/definePrompt.ts +0 -0
- /package/src/{fn → common/fn}/defineResource.spec.ts +0 -0
- /package/src/{fn → common/fn}/defineResource.ts +0 -0
- /package/src/{fn → common/fn}/defineTool.spec.ts +0 -0
- /package/src/{fn → common/fn}/defineTool.ts +0 -0
- /package/src/{interfaces → common/interfaces}/PlatformMcpSettings.ts +0 -0
- /package/src/{utils → common/utils}/asResourceResponse.spec.ts +0 -0
- /package/src/{utils → common/utils}/asResourceResponse.ts +0 -0
- /package/src/{utils → common/utils}/asToolResponse.spec.ts +0 -0
- /package/src/{utils → common/utils}/asToolResponse.ts +0 -0
- /package/src/{utils → common/utils}/fromJsonSchema.spec.ts +0 -0
- /package/src/{utils → common/utils}/fromJsonSchema.ts +0 -0
- /package/src/{services → http/services}/PlatformMcpModule.spec.ts +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../common/index.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function mcpServerConnect(mode: "streamable-http" | "stdio"): Promise<import("@modelcontextprotocol/server").McpServer>;
|
|
@@ -10,7 +10,6 @@ export * from "./fn/defineResource.js";
|
|
|
10
10
|
export * from "./fn/defineTool.js";
|
|
11
11
|
export * from "./interfaces/PlatformMcpSettings.js";
|
|
12
12
|
export * from "./services/McpServerFactory.js";
|
|
13
|
-
export * from "./services/PlatformMcpModule.js";
|
|
14
13
|
export * from "./utils/asResourceResponse.js";
|
|
15
14
|
export * from "./utils/asToolResponse.js";
|
|
16
15
|
export * from "./utils/fromJsonSchema.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../common/index.js";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type OnRoutesInit, PlatformContext, type PlatformRouteDetails } from "@tsed/platform-http";
|
|
2
2
|
import { type McpServer } from "@modelcontextprotocol/server";
|
|
3
|
-
import type { PlatformMcpSettings } from "
|
|
3
|
+
import type { PlatformMcpSettings } from "../../common/index.js";
|
|
4
4
|
/**
|
|
5
5
|
* Platform module that mounts the MCP HTTP endpoint and forwards requests to the configured server instance.
|
|
6
6
|
*
|
package/package.json
CHANGED
|
@@ -1,18 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/platform-mcp",
|
|
3
|
-
"version": "8.38.0-beta.
|
|
3
|
+
"version": "8.38.0-beta.3",
|
|
4
4
|
"description": "Model Context Protocol integration for Ts.ED HTTP adapters.",
|
|
5
|
-
"source": "./src/index.ts",
|
|
5
|
+
"source": "./src/http/index.ts",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"main": "./lib/esm/index.js",
|
|
8
|
-
"module": "./lib/esm/index.js",
|
|
9
|
-
"typings": "./lib/types/index.d.ts",
|
|
7
|
+
"main": "./lib/esm/http/index.js",
|
|
8
|
+
"module": "./lib/esm/http/index.js",
|
|
9
|
+
"typings": "./lib/types/http/index.d.ts",
|
|
10
10
|
"exports": {
|
|
11
11
|
".": {
|
|
12
|
-
"tsed-source": "./src/index.ts",
|
|
13
|
-
"types": "./lib/types/index.d.ts",
|
|
14
|
-
"import": "./lib/esm/index.js",
|
|
15
|
-
"default": "./lib/esm/index.js"
|
|
12
|
+
"tsed-source": "./src/http/index.ts",
|
|
13
|
+
"types": "./lib/types/http/index.d.ts",
|
|
14
|
+
"import": "./lib/esm/http/index.js",
|
|
15
|
+
"default": "./lib/esm/http/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./http": {
|
|
18
|
+
"tsed-source": "./src/http/index.ts",
|
|
19
|
+
"types": "./lib/types/http/index.d.ts",
|
|
20
|
+
"import": "./lib/esm/http/index.js",
|
|
21
|
+
"default": "./lib/esm/http/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./cli": {
|
|
24
|
+
"tsed-source": "./src/cli/index.ts",
|
|
25
|
+
"types": "./lib/types/cli/index.d.ts",
|
|
26
|
+
"import": "./lib/esm/cli/index.js",
|
|
27
|
+
"default": "./lib/esm/cli/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./common": {
|
|
30
|
+
"tsed-source": "./src/common/index.ts",
|
|
31
|
+
"types": "./lib/types/common/index.d.ts",
|
|
32
|
+
"import": "./lib/esm/common/index.js",
|
|
33
|
+
"default": "./lib/esm/common/index.js"
|
|
16
34
|
}
|
|
17
35
|
},
|
|
18
36
|
"scripts": {
|
|
@@ -31,13 +49,13 @@
|
|
|
31
49
|
"uuid": ">=11.0.2"
|
|
32
50
|
},
|
|
33
51
|
"devDependencies": {
|
|
34
|
-
"@tsed/barrels": "8.38.0-beta.
|
|
35
|
-
"@tsed/core": "8.38.0-beta.
|
|
36
|
-
"@tsed/di": "8.38.0-beta.
|
|
37
|
-
"@tsed/json-mapper": "8.38.0-beta.
|
|
38
|
-
"@tsed/platform-http": "8.38.0-beta.
|
|
39
|
-
"@tsed/schema": "8.38.0-beta.
|
|
40
|
-
"@tsed/typescript": "8.38.0-beta.
|
|
52
|
+
"@tsed/barrels": "8.38.0-beta.3",
|
|
53
|
+
"@tsed/core": "8.38.0-beta.3",
|
|
54
|
+
"@tsed/di": "8.38.0-beta.3",
|
|
55
|
+
"@tsed/json-mapper": "8.38.0-beta.3",
|
|
56
|
+
"@tsed/platform-http": "8.38.0-beta.3",
|
|
57
|
+
"@tsed/schema": "8.38.0-beta.3",
|
|
58
|
+
"@tsed/typescript": "8.38.0-beta.3",
|
|
41
59
|
"typescript": "6.0.2",
|
|
42
60
|
"vitest": "4.1.4"
|
|
43
61
|
},
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../common/index.js";
|
package/src/cli/index.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import {DITest, injector} from "@tsed/di";
|
|
2
|
+
import {afterEach, beforeEach, describe, expect, it, vi} from "vitest";
|
|
3
|
+
|
|
4
|
+
const {MCP_SERVER} = vi.hoisted(() => ({MCP_SERVER: Symbol("MCP_SERVER")}));
|
|
5
|
+
|
|
6
|
+
vi.mock("../../common/index.js", () => ({MCP_SERVER}));
|
|
7
|
+
vi.mock("./mcpStdioServer.js", () => ({mcpStdioServer: vi.fn()}));
|
|
8
|
+
vi.mock("./mcpStreamableServer.js", () => ({mcpStreamableServer: vi.fn()}));
|
|
9
|
+
|
|
10
|
+
import {mcpServerConnect} from "./mcpServerConnect.js";
|
|
11
|
+
import {mcpStdioServer} from "./mcpStdioServer.js";
|
|
12
|
+
import {mcpStreamableServer} from "./mcpStreamableServer.js";
|
|
13
|
+
|
|
14
|
+
describe("mcpServerConnect", () => {
|
|
15
|
+
const server = {};
|
|
16
|
+
|
|
17
|
+
beforeEach(() => {
|
|
18
|
+
vi.clearAllMocks();
|
|
19
|
+
DITest.create();
|
|
20
|
+
injector().add(MCP_SERVER, {useValue: server});
|
|
21
|
+
});
|
|
22
|
+
afterEach(() => DITest.reset());
|
|
23
|
+
|
|
24
|
+
it("connects the stdio transport", async () => {
|
|
25
|
+
await mcpServerConnect("stdio");
|
|
26
|
+
|
|
27
|
+
expect(mcpStdioServer).toHaveBeenCalledWith(server);
|
|
28
|
+
expect(mcpStreamableServer).not.toHaveBeenCalled();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("connects the Streamable HTTP transport", async () => {
|
|
32
|
+
await mcpServerConnect("streamable-http");
|
|
33
|
+
|
|
34
|
+
expect(mcpStreamableServer).toHaveBeenCalledWith(server);
|
|
35
|
+
expect(mcpStdioServer).not.toHaveBeenCalled();
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {inject, logger} from "@tsed/di";
|
|
2
|
+
|
|
3
|
+
import {MCP_SERVER} from "../../common/index.js";
|
|
4
|
+
import {mcpStdioServer} from "./mcpStdioServer.js";
|
|
5
|
+
import {mcpStreamableServer} from "./mcpStreamableServer.js";
|
|
6
|
+
|
|
7
|
+
export async function mcpServerConnect(mode: "streamable-http" | "stdio") {
|
|
8
|
+
const server = inject(MCP_SERVER);
|
|
9
|
+
|
|
10
|
+
if (mode === "streamable-http") {
|
|
11
|
+
logger().info({event: "MCP_SERVER_CONNECT", mode});
|
|
12
|
+
|
|
13
|
+
await mcpStreamableServer(server);
|
|
14
|
+
} else {
|
|
15
|
+
await mcpStdioServer(server);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return server;
|
|
19
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import {McpServer} from "@modelcontextprotocol/server";
|
|
2
|
+
import {DITest} from "@tsed/di";
|
|
3
|
+
import {beforeEach, describe, expect, it, vi} from "vitest";
|
|
4
|
+
|
|
5
|
+
import {mcpStdioServer} from "./mcpStdioServer.js";
|
|
6
|
+
|
|
7
|
+
// Mock the SDK
|
|
8
|
+
vi.mock("@modelcontextprotocol/server/stdio", () => ({
|
|
9
|
+
StdioServerTransport: vi.fn(function StdioServerTransport() {
|
|
10
|
+
return {
|
|
11
|
+
connect: vi.fn(),
|
|
12
|
+
close: vi.fn()
|
|
13
|
+
};
|
|
14
|
+
})
|
|
15
|
+
}));
|
|
16
|
+
|
|
17
|
+
describe("mcpStdioServer", () => {
|
|
18
|
+
let mockServer: McpServer;
|
|
19
|
+
let mockConnect: any;
|
|
20
|
+
|
|
21
|
+
beforeEach(() => DITest.create());
|
|
22
|
+
beforeEach(async () => {
|
|
23
|
+
vi.clearAllMocks();
|
|
24
|
+
|
|
25
|
+
mockConnect = vi.fn().mockResolvedValue(undefined);
|
|
26
|
+
mockServer = {
|
|
27
|
+
connect: mockConnect
|
|
28
|
+
} as any;
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("should create a StdioServerTransport", async () => {
|
|
32
|
+
await mcpStdioServer(mockServer);
|
|
33
|
+
|
|
34
|
+
const {StdioServerTransport} = await import("@modelcontextprotocol/server/stdio");
|
|
35
|
+
|
|
36
|
+
expect(StdioServerTransport).toHaveBeenCalledTimes(1);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("should connect the server with the transport", async () => {
|
|
40
|
+
await mcpStdioServer(mockServer);
|
|
41
|
+
|
|
42
|
+
expect(mockConnect).toHaveBeenCalledTimes(1);
|
|
43
|
+
expect(mockConnect).toHaveBeenCalledWith(expect.any(Object));
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("should return the connection result", async () => {
|
|
47
|
+
mockConnect.mockResolvedValue("connection-result");
|
|
48
|
+
|
|
49
|
+
const result = await mcpStdioServer(mockServer);
|
|
50
|
+
|
|
51
|
+
expect(result).toBe("connection-result");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("should handle connection errors", async () => {
|
|
55
|
+
const error = new Error("Connection failed");
|
|
56
|
+
mockConnect.mockRejectedValue(error);
|
|
57
|
+
|
|
58
|
+
await expect(mcpStdioServer(mockServer)).rejects.toThrow("Connection failed");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("should dynamically import StdioServerTransport", async () => {
|
|
62
|
+
const importSpy = vi.spyOn(await import("@modelcontextprotocol/server/stdio"), "StdioServerTransport");
|
|
63
|
+
|
|
64
|
+
await mcpStdioServer(mockServer);
|
|
65
|
+
|
|
66
|
+
expect(importSpy).toHaveBeenCalled();
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type {McpServer} from "@modelcontextprotocol/server";
|
|
2
|
+
import {logger} from "@tsed/di";
|
|
3
|
+
|
|
4
|
+
export async function mcpStdioServer(server: McpServer) {
|
|
5
|
+
const {StdioServerTransport} = await import("@modelcontextprotocol/server/stdio");
|
|
6
|
+
|
|
7
|
+
const transport = new StdioServerTransport();
|
|
8
|
+
|
|
9
|
+
logger().stop();
|
|
10
|
+
|
|
11
|
+
return server.connect(transport);
|
|
12
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import {McpServer} from "@modelcontextprotocol/server";
|
|
2
|
+
import {afterEach, beforeEach, describe, expect, it, vi} from "vitest";
|
|
3
|
+
|
|
4
|
+
const {app, listenServer, NodeStreamableHTTPServerTransport, express, routeHandlers} = vi.hoisted(() => {
|
|
5
|
+
const routeHandlers = new Map<string, Function>();
|
|
6
|
+
const listeners = new Map<string, Function>();
|
|
7
|
+
const listenServer = {
|
|
8
|
+
on: vi.fn((event: string, handler: Function) => {
|
|
9
|
+
listeners.set(event, handler);
|
|
10
|
+
return listenServer;
|
|
11
|
+
})
|
|
12
|
+
};
|
|
13
|
+
const app = {
|
|
14
|
+
use: vi.fn(),
|
|
15
|
+
post: vi.fn((path: string, handler: Function) => routeHandlers.set(path, handler)),
|
|
16
|
+
listen: vi.fn((_: number, handler: Function) => {
|
|
17
|
+
handler();
|
|
18
|
+
return listenServer;
|
|
19
|
+
})
|
|
20
|
+
};
|
|
21
|
+
const express = Object.assign(
|
|
22
|
+
vi.fn(() => app),
|
|
23
|
+
{json: vi.fn(() => "json-middleware")}
|
|
24
|
+
);
|
|
25
|
+
const NodeStreamableHTTPServerTransport = vi.fn(function () {
|
|
26
|
+
return {close: vi.fn(), handleRequest: vi.fn().mockResolvedValue(undefined)};
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
return {app, listenServer, NodeStreamableHTTPServerTransport, express, routeHandlers, listeners};
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
vi.mock("express", () => ({default: express}));
|
|
33
|
+
vi.mock("@modelcontextprotocol/node", () => ({NodeStreamableHTTPServerTransport}));
|
|
34
|
+
|
|
35
|
+
import {mcpStreamableServer} from "./mcpStreamableServer.js";
|
|
36
|
+
|
|
37
|
+
describe("mcpStreamableServer", () => {
|
|
38
|
+
let server: McpServer;
|
|
39
|
+
let connect: ReturnType<typeof vi.fn>;
|
|
40
|
+
|
|
41
|
+
beforeEach(() => {
|
|
42
|
+
vi.clearAllMocks();
|
|
43
|
+
routeHandlers.clear();
|
|
44
|
+
server = {connect: (connect = vi.fn().mockResolvedValue(undefined))} as any;
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
afterEach(() => {
|
|
48
|
+
const close = listenServer.on.mock.calls.find(([event]) => event === "close")?.[1] as Function | undefined;
|
|
49
|
+
close?.();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("configures an Express MCP endpoint", async () => {
|
|
53
|
+
void mcpStreamableServer(server);
|
|
54
|
+
await vi.waitFor(() => expect(app.listen).toHaveBeenCalled());
|
|
55
|
+
|
|
56
|
+
expect(express.json).toHaveBeenCalledOnce();
|
|
57
|
+
expect(app.use).toHaveBeenCalledWith("json-middleware");
|
|
58
|
+
expect(app.post).toHaveBeenCalledWith("/mcp", expect.any(Function));
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("creates a transport and dispatches each MCP request", async () => {
|
|
62
|
+
void mcpStreamableServer(server);
|
|
63
|
+
await vi.waitFor(() => expect(routeHandlers.get("/mcp")).toBeDefined());
|
|
64
|
+
const on = vi.fn();
|
|
65
|
+
const req = {body: {jsonrpc: "2.0"}};
|
|
66
|
+
const res = {on};
|
|
67
|
+
|
|
68
|
+
await routeHandlers.get("/mcp")!(req, res);
|
|
69
|
+
|
|
70
|
+
const transport = NodeStreamableHTTPServerTransport.mock.results[0].value;
|
|
71
|
+
expect(NodeStreamableHTTPServerTransport).toHaveBeenCalledWith({sessionIdGenerator: undefined, enableJsonResponse: true});
|
|
72
|
+
expect(connect).toHaveBeenCalledWith(transport);
|
|
73
|
+
expect(transport.handleRequest).toHaveBeenCalledWith(req, res, req.body);
|
|
74
|
+
|
|
75
|
+
on.mock.calls.find(([event]) => event === "close")![1]();
|
|
76
|
+
expect(transport.close).toHaveBeenCalledOnce();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("rejects when Express cannot start", async () => {
|
|
80
|
+
const result = mcpStreamableServer(server);
|
|
81
|
+
await vi.waitFor(() => expect(listenServer.on).toHaveBeenCalledWith("error", expect.any(Function)));
|
|
82
|
+
const error = new Error("port unavailable");
|
|
83
|
+
listenServer.on.mock.calls.find(([event]) => event === "error");
|
|
84
|
+
|
|
85
|
+
await expect(result).rejects.toThrow(error);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type {McpServer} from "@modelcontextprotocol/server";
|
|
2
|
+
import {logger} from "@tsed/di";
|
|
3
|
+
|
|
4
|
+
export async function mcpStreamableServer(server: McpServer) {
|
|
5
|
+
const {NodeStreamableHTTPServerTransport} = await import("@modelcontextprotocol/node");
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
const {default: express} = await import("express");
|
|
8
|
+
|
|
9
|
+
const app = express();
|
|
10
|
+
app.use(express.json());
|
|
11
|
+
|
|
12
|
+
app.post("/mcp", async (req: any, res: any) => {
|
|
13
|
+
// Create a new transport for each request to prevent request ID collisions
|
|
14
|
+
const transport = new NodeStreamableHTTPServerTransport({
|
|
15
|
+
sessionIdGenerator: undefined,
|
|
16
|
+
enableJsonResponse: true
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
res.on("close", () => {
|
|
20
|
+
transport.close();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
await server.connect(transport);
|
|
24
|
+
await transport.handleRequest(req, res, req.body);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const port = parseInt(process.env.PORT || "3000");
|
|
28
|
+
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
app
|
|
31
|
+
.listen(port, () => {
|
|
32
|
+
logger().info({
|
|
33
|
+
event: "MCP_STREAMABLE_SERVER",
|
|
34
|
+
state: "OK",
|
|
35
|
+
message: `Running http://localhost:${port}/mcp`
|
|
36
|
+
});
|
|
37
|
+
})
|
|
38
|
+
.on("close", () => resolve(true))
|
|
39
|
+
.on("error", (error: any) => {
|
|
40
|
+
logger().error({
|
|
41
|
+
event: "MCP_STREAMABLE_SERVER",
|
|
42
|
+
state: "KO",
|
|
43
|
+
message: error.message
|
|
44
|
+
});
|
|
45
|
+
reject(error);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
}
|
|
@@ -10,7 +10,6 @@ export * from "./fn/defineResource.js";
|
|
|
10
10
|
export * from "./fn/defineTool.js";
|
|
11
11
|
export * from "./interfaces/PlatformMcpSettings.js";
|
|
12
12
|
export * from "./services/McpServerFactory.js";
|
|
13
|
-
export * from "./services/PlatformMcpModule.js";
|
|
14
13
|
export * from "./utils/asResourceResponse.js";
|
|
15
14
|
export * from "./utils/asToolResponse.js";
|
|
16
15
|
export * from "./utils/fromJsonSchema.js";
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {DITest, injector} from "@tsed/di";
|
|
2
|
+
import {afterEach, beforeEach, describe, expect, it, vi} from "vitest";
|
|
3
|
+
|
|
4
|
+
import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
|
|
5
|
+
import {MCP_SERVER} from "./McpServerFactory.js";
|
|
6
|
+
|
|
7
|
+
describe("McpServerFactory", () => {
|
|
8
|
+
beforeEach(() => DITest.create({name: "test-server", version: "1.0.0"}));
|
|
9
|
+
afterEach(() => DITest.reset());
|
|
10
|
+
|
|
11
|
+
it("registers providers configured under mcp", async () => {
|
|
12
|
+
const tool = Symbol("tool");
|
|
13
|
+
const resource = Symbol("resource");
|
|
14
|
+
const prompt = Symbol("prompt");
|
|
15
|
+
const toolHandler = vi.fn();
|
|
16
|
+
const resourceHandler = vi.fn();
|
|
17
|
+
const promptHandler = vi.fn();
|
|
18
|
+
|
|
19
|
+
injector().add(tool, {useFactory: () => ({name: "tool", handler: toolHandler})});
|
|
20
|
+
injector().add(resource, {useFactory: () => ({uri: "file:///resource", handler: resourceHandler})});
|
|
21
|
+
injector().add(prompt, {useFactory: () => ({name: "prompt", handler: promptHandler})});
|
|
22
|
+
injector().settings.set("mcp", {tools: [tool], resources: [resource], prompts: [prompt]});
|
|
23
|
+
|
|
24
|
+
const server = await DITest.invoke(MCP_SERVER);
|
|
25
|
+
|
|
26
|
+
expect((server as any)._registeredTools.tool).toBeDefined();
|
|
27
|
+
expect((server as any)._registeredResources["file:///resource"]).toBeDefined();
|
|
28
|
+
expect((server as any)._registeredPrompts.prompt).toBeDefined();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("registers providers discovered by MCP type", async () => {
|
|
32
|
+
const tool = Symbol("tool");
|
|
33
|
+
injector().add(tool, {type: MCP_PROVIDER_TYPES.TOOL, useFactory: () => ({name: "tool", handler: vi.fn()})});
|
|
34
|
+
|
|
35
|
+
const server = await DITest.invoke(MCP_SERVER);
|
|
36
|
+
|
|
37
|
+
expect((server as any)._registeredTools.tool).toBeDefined();
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import {constant, inject, injectable, injector, type TokenProvider} from "@tsed/di";
|
|
2
|
-
import {
|
|
2
|
+
import type {PlatformMcpSettings, PromptsSettings, ResourceSettings, ToolProps} from "../index.js";
|
|
3
|
+
import {MCP_PROVIDER_TYPES} from "../index.js";
|
|
3
4
|
import {McpServer, type ResourceTemplate} from "@modelcontextprotocol/server";
|
|
4
|
-
import type {PlatformMcpSettings} from "../interfaces/PlatformMcpSettings.js";
|
|
5
|
-
import type {PromptsSettings} from "../fn/definePrompt.js";
|
|
6
|
-
import type {ResourceSettings} from "../fn/defineResource.js";
|
|
7
|
-
import type {ToolProps} from "../fn/defineTool.js";
|
|
8
5
|
|
|
9
6
|
function collectTokens(type: string, configured: TokenProvider[] = []): TokenProvider[] {
|
|
10
7
|
const tokens = new Set<TokenProvider>(configured);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../common/index.js";
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {type OnRoutesInit, PlatformContext, type PlatformRouteDetails
|
|
1
|
+
import {application, type OnRoutesInit, PlatformContext, type PlatformRouteDetails} from "@tsed/platform-http";
|
|
2
2
|
import {constant, inject, injectable} from "@tsed/di";
|
|
3
|
-
import {MCP_SERVER} from "
|
|
3
|
+
import {MCP_SERVER} from "../../common/services/McpServerFactory.js";
|
|
4
4
|
import {type McpServer} from "@modelcontextprotocol/server";
|
|
5
|
-
import type {PlatformMcpSettings} from "
|
|
5
|
+
import type {PlatformMcpSettings} from "../../common/index.js";
|
|
6
6
|
import {NodeStreamableHTTPServerTransport} from "@modelcontextprotocol/node";
|
|
7
7
|
import {useContextHandler} from "@tsed/platform-router";
|
|
8
8
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|