@tsed/platform-mcp 8.38.8 → 8.39.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/lib/esm/cli/services/mcpServerConnect.js +10 -7
- package/lib/esm/cli/services/mcpStreamableServer.js +19 -7
- package/lib/esm/common/index.js +4 -1
- package/lib/esm/common/utils/createMcpServer.js +41 -0
- package/lib/esm/common/utils/resolvePrompts.js +24 -0
- package/lib/esm/common/utils/resolveResources.js +24 -0
- package/lib/esm/common/utils/resolveTools.js +23 -0
- package/lib/esm/http/services/PlatformMcpModule.js +30 -25
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/types/cli/services/mcpServerConnect.d.ts +1 -1
- package/lib/types/cli/services/mcpStreamableServer.d.ts +1 -1
- package/lib/types/common/index.d.ts +4 -1
- package/lib/types/common/interfaces/PlatformMcpSettings.d.ts +1 -1
- package/lib/types/common/utils/createMcpServer.d.ts +12 -0
- package/lib/types/common/utils/resolvePrompts.d.ts +6 -0
- package/lib/types/common/utils/resolveResources.d.ts +9 -0
- package/lib/types/common/utils/resolveTools.d.ts +7 -0
- package/lib/types/http/services/PlatformMcpModule.d.ts +5 -10
- package/package.json +8 -8
- package/src/cli/services/mcpServerConnect.spec.ts +72 -11
- package/src/cli/services/mcpServerConnect.ts +14 -6
- package/src/cli/services/mcpStreamableServer.spec.ts +22 -8
- package/src/cli/services/mcpStreamableServer.ts +20 -7
- package/src/common/index.ts +4 -1
- package/src/common/interfaces/PlatformMcpSettings.ts +1 -1
- package/src/common/utils/createMcpServer.spec.ts +46 -0
- package/src/common/utils/createMcpServer.ts +57 -0
- package/src/common/utils/resolveMcpServerOptions.spec.ts +59 -0
- package/src/common/utils/resolvePrompts.ts +35 -0
- package/src/common/utils/resolveResources.ts +36 -0
- package/src/common/utils/resolveTools.ts +33 -0
- package/src/http/services/PlatformMcpModule.spec.ts +102 -16
- package/src/http/services/PlatformMcpModule.ts +39 -32
- package/lib/esm/common/services/McpServerFactory.js +0 -56
- package/lib/types/common/services/McpServerFactory.d.ts +0 -16
- package/src/common/services/McpServerFactory.spec.ts +0 -39
- package/src/common/services/McpServerFactory.ts +0 -78
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import {inject, injector} from "@tsed/di";
|
|
2
|
+
import {MCP_PROVIDER_TYPES} from "../constants/constants.js";
|
|
3
|
+
import type {ToolProps} from "../fn/defineTool.js";
|
|
4
|
+
import type {PlatformMcpSettings} from "../interfaces/PlatformMcpSettings.js";
|
|
5
|
+
import {isClass} from "@tsed/core";
|
|
6
|
+
|
|
7
|
+
export type ResolvedTool = ToolProps<any, any> & {name: string; handler: any};
|
|
8
|
+
|
|
9
|
+
export function resolveTools(settings: PlatformMcpSettings): ResolvedTool[] {
|
|
10
|
+
const classTokens = settings.tools?.filter((tool) => isClass(tool)) || [];
|
|
11
|
+
const handlerTools = (settings.tools?.filter((tool) => !isClass(tool)) || []).map((token) => {
|
|
12
|
+
return inject<ResolvedTool>(token);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
const classTools = injector()
|
|
16
|
+
.providers.getMany(MCP_PROVIDER_TYPES.TOOL)
|
|
17
|
+
.map((provider) => {
|
|
18
|
+
const definition = inject<ResolvedTool>(provider.token);
|
|
19
|
+
|
|
20
|
+
if ("token" in definition && "propertyKey" in definition) {
|
|
21
|
+
if (!classTokens.includes(definition.token)) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return definition;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return false;
|
|
29
|
+
})
|
|
30
|
+
.filter(Boolean) as ResolvedTool[];
|
|
31
|
+
|
|
32
|
+
return [...handlerTools, ...classTools];
|
|
33
|
+
}
|
|
@@ -5,8 +5,21 @@ import {PlatformMcpModule} from "./PlatformMcpModule.js";
|
|
|
5
5
|
import {PlatformTest} from "@tsed/platform-http/testing";
|
|
6
6
|
import {application} from "@tsed/platform-http";
|
|
7
7
|
|
|
8
|
+
const {createMcpServer, resolveMcpServerOptions} = vi.hoisted(() => ({
|
|
9
|
+
createMcpServer: vi.fn(),
|
|
10
|
+
resolveMcpServerOptions: vi.fn((settings) => ({...settings, tools: [], prompts: [], resources: []}))
|
|
11
|
+
}));
|
|
12
|
+
|
|
13
|
+
vi.mock("../../common/utils/createMcpServer.js", () => ({createMcpServer, resolveMcpServerOptions}));
|
|
14
|
+
|
|
15
|
+
type TestTransportInstance = {
|
|
16
|
+
close: () => unknown;
|
|
17
|
+
handleRequest: (...args: unknown[]) => Promise<unknown>;
|
|
18
|
+
options: Record<string, unknown>;
|
|
19
|
+
};
|
|
20
|
+
|
|
8
21
|
const {TestTransport, transportInstances} = vi.hoisted(() => {
|
|
9
|
-
const transportInstances:
|
|
22
|
+
const transportInstances: TestTransportInstance[] = [];
|
|
10
23
|
|
|
11
24
|
class TestTransport {
|
|
12
25
|
close = vi.fn().mockResolvedValue(undefined);
|
|
@@ -26,16 +39,27 @@ vi.mock("@modelcontextprotocol/node", () => {
|
|
|
26
39
|
};
|
|
27
40
|
});
|
|
28
41
|
|
|
29
|
-
function createModule() {
|
|
30
|
-
const module =
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
42
|
+
async function createModule() {
|
|
43
|
+
const module = await PlatformTest.invoke<PlatformMcpModule>(PlatformMcpModule);
|
|
44
|
+
const servers: Array<{connect: ReturnType<typeof vi.fn>; close: ReturnType<typeof vi.fn>}> = [];
|
|
45
|
+
|
|
46
|
+
createMcpServer.mockImplementation(() => {
|
|
47
|
+
let transport: TestTransportInstance | undefined;
|
|
48
|
+
const server = {
|
|
49
|
+
connect: vi.fn().mockImplementation((value) => {
|
|
50
|
+
transport = value;
|
|
51
|
+
}),
|
|
52
|
+
close: vi.fn().mockImplementation(() => transport?.close())
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
servers.push(server);
|
|
56
|
+
|
|
57
|
+
return server;
|
|
58
|
+
});
|
|
34
59
|
|
|
35
60
|
module["settings"] = {};
|
|
36
|
-
module["server"] = server as never;
|
|
37
61
|
|
|
38
|
-
return {module,
|
|
62
|
+
return {module, servers};
|
|
39
63
|
}
|
|
40
64
|
|
|
41
65
|
function createExpressContext() {
|
|
@@ -81,8 +105,8 @@ describe("PlatformMcpModule", () => {
|
|
|
81
105
|
});
|
|
82
106
|
|
|
83
107
|
describe("$onRoutesInit()", () => {
|
|
84
|
-
it("should register the MCP route only once", () => {
|
|
85
|
-
const {module} = createModule();
|
|
108
|
+
it("should register the MCP route only once", async () => {
|
|
109
|
+
const {module} = await createModule();
|
|
86
110
|
|
|
87
111
|
vi.spyOn(application(), "post").mockReturnValue(undefined as never);
|
|
88
112
|
|
|
@@ -91,6 +115,24 @@ describe("PlatformMcpModule", () => {
|
|
|
91
115
|
|
|
92
116
|
expect(application().post).toHaveBeenCalledTimes(1);
|
|
93
117
|
expect(application().post).toHaveBeenCalledWith("/mcp", expect.any(Function));
|
|
118
|
+
expect(resolveMcpServerOptions).toHaveBeenCalledOnce();
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("skips disabled settings without preventing enabled MCP servers from mounting", async () => {
|
|
122
|
+
const {module} = await createModule();
|
|
123
|
+
module["settings"] = [{path: "/first"}, {path: "/disabled", enabled: false}, {path: "/second"}];
|
|
124
|
+
vi.spyOn(application(), "post").mockReturnValue(undefined as never);
|
|
125
|
+
|
|
126
|
+
module.$onRoutesInit();
|
|
127
|
+
|
|
128
|
+
expect(application().post).toHaveBeenCalledTimes(2);
|
|
129
|
+
expect(application().post).toHaveBeenNthCalledWith(1, "/first", expect.any(Function));
|
|
130
|
+
expect(application().post).toHaveBeenNthCalledWith(2, "/second", expect.any(Function));
|
|
131
|
+
expect(resolveMcpServerOptions).toHaveBeenCalledTimes(2);
|
|
132
|
+
expect(module.$logRoutes([])).toEqual([
|
|
133
|
+
{method: "POST", name: "PlatformMcpModule.dispatch()", url: "/first"},
|
|
134
|
+
{method: "POST", name: "PlatformMcpModule.dispatch()", url: "/second"}
|
|
135
|
+
]);
|
|
94
136
|
});
|
|
95
137
|
});
|
|
96
138
|
|
|
@@ -99,31 +141,75 @@ describe("PlatformMcpModule", () => {
|
|
|
99
141
|
["fastify", createFastifyContext]
|
|
100
142
|
])("dispatch() with %s", (_, createContext) => {
|
|
101
143
|
it("should bind close on the low-level response and forward handleRequest", async () => {
|
|
102
|
-
const {module,
|
|
144
|
+
const {module, servers} = await createModule();
|
|
103
145
|
const {$ctx, res} = createContext();
|
|
104
146
|
|
|
105
|
-
await module["dispatch"]($ctx);
|
|
147
|
+
await module["dispatch"]({tools: [], prompts: [], resources: []}, $ctx);
|
|
106
148
|
|
|
107
149
|
const transport = transportInstances[0];
|
|
108
150
|
|
|
109
151
|
expect(transport).toBeDefined();
|
|
110
|
-
expect(
|
|
152
|
+
expect(servers[0].connect).toHaveBeenCalledWith(transport);
|
|
111
153
|
expect(transport.handleRequest).toHaveBeenCalledWith($ctx.request.getReq(), res, $ctx.request.body);
|
|
154
|
+
expect(createMcpServer).toHaveBeenCalledWith({tools: [], prompts: [], resources: []});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("forwards the transport settings resolved for the MCP endpoint", async () => {
|
|
158
|
+
const {module} = await createModule();
|
|
159
|
+
const {$ctx} = createContext();
|
|
160
|
+
|
|
161
|
+
await module["dispatch"]({tools: [], prompts: [], resources: [], transportOptions: {enableJsonResponse: false}}, $ctx);
|
|
162
|
+
|
|
163
|
+
expect(transportInstances[0].options).toEqual({
|
|
164
|
+
sessionIdGenerator: undefined,
|
|
165
|
+
enableJsonResponse: false
|
|
166
|
+
});
|
|
112
167
|
});
|
|
113
168
|
|
|
114
|
-
it("should close
|
|
115
|
-
const {module} = createModule();
|
|
169
|
+
it("should close the request server after a JSON response", async () => {
|
|
170
|
+
const {module, servers} = await createModule();
|
|
116
171
|
const {$ctx, res} = createContext();
|
|
117
172
|
|
|
118
|
-
await module["dispatch"]($ctx);
|
|
173
|
+
await module["dispatch"]({tools: [], prompts: [], resources: []}, $ctx);
|
|
119
174
|
|
|
120
175
|
const transport = transportInstances[0];
|
|
121
176
|
|
|
177
|
+
expect(servers[0].close).toHaveBeenCalledTimes(1);
|
|
122
178
|
expect(transport.close).toHaveBeenCalledTimes(1);
|
|
123
179
|
|
|
124
180
|
res.emit("close");
|
|
125
181
|
|
|
126
182
|
expect(transport.close).toHaveBeenCalledTimes(1);
|
|
127
183
|
});
|
|
184
|
+
|
|
185
|
+
it("should close the request server when an SSE response closes", async () => {
|
|
186
|
+
const {module, servers} = await createModule();
|
|
187
|
+
const {$ctx, res} = createContext();
|
|
188
|
+
|
|
189
|
+
await module["dispatch"]({tools: [], prompts: [], resources: [], transportOptions: {enableJsonResponse: false}}, $ctx);
|
|
190
|
+
|
|
191
|
+
expect(servers[0].close).not.toHaveBeenCalled();
|
|
192
|
+
|
|
193
|
+
res.emit("close");
|
|
194
|
+
|
|
195
|
+
expect(servers[0].close).toHaveBeenCalledOnce();
|
|
196
|
+
expect(transportInstances[0].close).toHaveBeenCalledOnce();
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("should resolve an isolated server for each request", async () => {
|
|
200
|
+
const {module, servers} = await createModule();
|
|
201
|
+
const first = createContext();
|
|
202
|
+
const second = createContext();
|
|
203
|
+
|
|
204
|
+
await Promise.all([
|
|
205
|
+
module["dispatch"]({tools: [], prompts: [], resources: []}, first.$ctx),
|
|
206
|
+
module["dispatch"]({tools: [], prompts: [], resources: []}, second.$ctx)
|
|
207
|
+
]);
|
|
208
|
+
|
|
209
|
+
expect(servers).toHaveLength(2);
|
|
210
|
+
expect(servers[0]).not.toBe(servers[1]);
|
|
211
|
+
expect(servers[0].connect).toHaveBeenCalledWith(transportInstances[0]);
|
|
212
|
+
expect(servers[1].connect).toHaveBeenCalledWith(transportInstances[1]);
|
|
213
|
+
});
|
|
128
214
|
});
|
|
129
215
|
});
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import {application, type OnRoutesInit, PlatformContext, type PlatformRouteDetails} from "@tsed/platform-http";
|
|
2
|
-
import {constant,
|
|
3
|
-
import {MCP_SERVER} from "../../common/services/McpServerFactory.js";
|
|
4
|
-
import {type McpServer} from "@modelcontextprotocol/server";
|
|
2
|
+
import {constant, injectable} from "@tsed/di";
|
|
5
3
|
import type {PlatformMcpSettings} from "../../common/index.js";
|
|
6
4
|
import {NodeStreamableHTTPServerTransport} from "@modelcontextprotocol/node";
|
|
7
5
|
import {useContextHandler} from "@tsed/platform-router";
|
|
6
|
+
import {createMcpServer, resolveMcpServerOptions, type CreateMcpServerOpts} from "../../common/utils/createMcpServer.js";
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
9
|
* Platform module that mounts the MCP HTTP endpoint and forwards requests to the configured server instance.
|
|
@@ -13,71 +12,79 @@ import {useContextHandler} from "@tsed/platform-router";
|
|
|
13
12
|
* @since 8.17.0
|
|
14
13
|
*/
|
|
15
14
|
export class PlatformMcpModule implements OnRoutesInit {
|
|
16
|
-
protected settings = constant<PlatformMcpSettings>("mcp", {});
|
|
15
|
+
protected settings = constant<PlatformMcpSettings | PlatformMcpSettings[]>("mcp", {});
|
|
17
16
|
protected app = application();
|
|
18
|
-
protected
|
|
17
|
+
protected mcps: PlatformRouteDetails[] = [];
|
|
19
18
|
private loaded = false;
|
|
20
19
|
|
|
21
20
|
$onRoutesInit() {
|
|
22
|
-
if (
|
|
21
|
+
if (this.loaded) {
|
|
23
22
|
return;
|
|
24
23
|
}
|
|
25
24
|
|
|
26
|
-
const
|
|
25
|
+
const allSettings = ([] as PlatformMcpSettings[]).concat(this.settings);
|
|
27
26
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
for (const opts of allSettings) {
|
|
28
|
+
if (opts.enabled === false) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const path = opts.path || "/mcp";
|
|
33
|
+
const resolvedSettings = resolveMcpServerOptions(opts);
|
|
34
|
+
|
|
35
|
+
this.app.post(
|
|
36
|
+
path,
|
|
37
|
+
useContextHandler(async ($ctx) => this.dispatch(resolvedSettings, $ctx as PlatformContext))
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
this.mcps.push({
|
|
41
|
+
method: "POST",
|
|
42
|
+
name: "PlatformMcpModule.dispatch()",
|
|
43
|
+
url: path
|
|
44
|
+
} as PlatformRouteDetails);
|
|
45
|
+
}
|
|
32
46
|
|
|
33
47
|
this.loaded = true;
|
|
34
48
|
}
|
|
35
49
|
|
|
36
50
|
$logRoutes(routes: PlatformRouteDetails[]) {
|
|
37
|
-
return [
|
|
38
|
-
...routes,
|
|
39
|
-
this.isEnabled() && {
|
|
40
|
-
method: "POST",
|
|
41
|
-
name: "PlatformMcpModule.dispatch()",
|
|
42
|
-
url: this.settings?.path || "/mcp"
|
|
43
|
-
}
|
|
44
|
-
].filter(Boolean);
|
|
51
|
+
return [...routes, ...this.mcps].filter(Boolean);
|
|
45
52
|
}
|
|
46
53
|
|
|
47
|
-
protected async dispatch($ctx: PlatformContext) {
|
|
54
|
+
protected async dispatch(settings: CreateMcpServerOpts, $ctx: PlatformContext) {
|
|
55
|
+
const server = createMcpServer(settings);
|
|
56
|
+
|
|
48
57
|
const transport = new NodeStreamableHTTPServerTransport({
|
|
49
58
|
sessionIdGenerator: undefined,
|
|
50
59
|
enableJsonResponse: true,
|
|
51
|
-
...
|
|
60
|
+
...settings.transportOptions
|
|
52
61
|
});
|
|
53
62
|
|
|
54
63
|
const {request, response} = $ctx;
|
|
55
|
-
const res = response.getRes();
|
|
64
|
+
const res = response.getRes() as any;
|
|
56
65
|
let closed = false;
|
|
57
66
|
|
|
58
|
-
const
|
|
67
|
+
const closeServer = async () => {
|
|
59
68
|
if (closed) {
|
|
60
69
|
return;
|
|
61
70
|
}
|
|
62
71
|
|
|
63
72
|
closed = true;
|
|
64
|
-
await
|
|
73
|
+
await server.close();
|
|
65
74
|
};
|
|
66
75
|
|
|
67
|
-
res?.once("close",
|
|
76
|
+
res?.once("close", closeServer);
|
|
68
77
|
|
|
69
78
|
try {
|
|
70
|
-
await
|
|
79
|
+
await server.connect(transport as any);
|
|
71
80
|
await transport.handleRequest(request.getReq(), res, request.body);
|
|
72
81
|
} finally {
|
|
73
|
-
|
|
74
|
-
|
|
82
|
+
if (settings.transportOptions?.enableJsonResponse !== false) {
|
|
83
|
+
res?.off?.("close", closeServer);
|
|
84
|
+
await closeServer();
|
|
85
|
+
}
|
|
75
86
|
}
|
|
76
87
|
}
|
|
77
|
-
|
|
78
|
-
private isEnabled() {
|
|
79
|
-
return this.settings?.enabled !== false;
|
|
80
|
-
}
|
|
81
88
|
}
|
|
82
89
|
|
|
83
90
|
injectable(PlatformMcpModule);
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { constant, inject, injectable, injector } from "@tsed/di";
|
|
2
|
-
import { MCP_PROVIDER_TYPES } from "../index.js";
|
|
3
|
-
import { McpServer } from "@modelcontextprotocol/server";
|
|
4
|
-
function collectTokens(type, configured = []) {
|
|
5
|
-
const tokens = new Set(configured);
|
|
6
|
-
injector()
|
|
7
|
-
.providers.getMany(type)
|
|
8
|
-
.forEach((provider) => tokens.add(provider.token));
|
|
9
|
-
return [...tokens];
|
|
10
|
-
}
|
|
11
|
-
export function createMcpServer() {
|
|
12
|
-
const settings = constant("mcp", {}) || {};
|
|
13
|
-
const name = settings.name || constant("name") || "tsed-mcp";
|
|
14
|
-
const version = settings.version || constant("version") || "0.0.0";
|
|
15
|
-
const { websiteUrl, description, title, icons } = settings;
|
|
16
|
-
const server = new McpServer({
|
|
17
|
-
websiteUrl,
|
|
18
|
-
description,
|
|
19
|
-
icons,
|
|
20
|
-
title,
|
|
21
|
-
name,
|
|
22
|
-
version
|
|
23
|
-
}, settings?.serverOptions);
|
|
24
|
-
const toolTokens = collectTokens(MCP_PROVIDER_TYPES.TOOL, settings.tools);
|
|
25
|
-
toolTokens.forEach((token) => {
|
|
26
|
-
const definition = inject(token);
|
|
27
|
-
const { name, handler, ...opts } = definition;
|
|
28
|
-
server.registerTool(name, opts, handler);
|
|
29
|
-
});
|
|
30
|
-
const resourceTokens = collectTokens(MCP_PROVIDER_TYPES.RESOURCE, settings.resources);
|
|
31
|
-
resourceTokens.forEach((token) => {
|
|
32
|
-
const definition = inject(token);
|
|
33
|
-
const { name, handler, uri, template, ...opts } = definition;
|
|
34
|
-
const resourceName = name || String(token);
|
|
35
|
-
if (uri) {
|
|
36
|
-
server.registerResource(resourceName, uri, opts, handler);
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
server.registerResource(resourceName, template, opts, handler);
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
const promptTokens = collectTokens(MCP_PROVIDER_TYPES.PROMPT, settings.prompts);
|
|
43
|
-
promptTokens.forEach((token) => {
|
|
44
|
-
const definition = inject(token);
|
|
45
|
-
const { name, handler, ...opts } = definition;
|
|
46
|
-
server.registerPrompt(name || String(token), opts, handler);
|
|
47
|
-
});
|
|
48
|
-
return server;
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Injectable MCP server instance configured with registered tools, resources, and prompts.
|
|
52
|
-
*
|
|
53
|
-
* @module platform/mcp
|
|
54
|
-
* @since 8.17.0
|
|
55
|
-
*/
|
|
56
|
-
export const MCP_SERVER = injectable(McpServer).factory(createMcpServer).token();
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { McpServer } from "@modelcontextprotocol/server";
|
|
2
|
-
export declare function createMcpServer(): McpServer;
|
|
3
|
-
/**
|
|
4
|
-
* Injectable MCP server instance configured with registered tools, resources, and prompts.
|
|
5
|
-
*
|
|
6
|
-
* @module platform/mcp
|
|
7
|
-
* @since 8.17.0
|
|
8
|
-
*/
|
|
9
|
-
export declare const MCP_SERVER: import("@tsed/di").FactoryTokenProvider<McpServer>;
|
|
10
|
-
/**
|
|
11
|
-
* Type alias referencing the MCP server provider token.
|
|
12
|
-
*
|
|
13
|
-
* @module platform/mcp
|
|
14
|
-
* @since 8.17.0
|
|
15
|
-
*/
|
|
16
|
-
export type MCP_SERVER = typeof MCP_SERVER;
|
|
@@ -1,39 +0,0 @@
|
|
|
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,78 +0,0 @@
|
|
|
1
|
-
import {constant, inject, injectable, injector, type TokenProvider} from "@tsed/di";
|
|
2
|
-
import type {PlatformMcpSettings, PromptsSettings, ResourceSettings, ToolProps} from "../index.js";
|
|
3
|
-
import {MCP_PROVIDER_TYPES} from "../index.js";
|
|
4
|
-
import {McpServer, type ResourceTemplate} from "@modelcontextprotocol/server";
|
|
5
|
-
|
|
6
|
-
function collectTokens(type: string, configured: TokenProvider[] = []): TokenProvider[] {
|
|
7
|
-
const tokens = new Set<TokenProvider>(configured);
|
|
8
|
-
|
|
9
|
-
injector()
|
|
10
|
-
.providers.getMany(type)
|
|
11
|
-
.forEach((provider) => tokens.add(provider.token));
|
|
12
|
-
|
|
13
|
-
return [...tokens];
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function createMcpServer() {
|
|
17
|
-
const settings = constant<PlatformMcpSettings>("mcp", {}) || {};
|
|
18
|
-
const name = settings.name || constant<string>("name") || "tsed-mcp";
|
|
19
|
-
const version = settings.version || constant<string>("version") || "0.0.0";
|
|
20
|
-
const {websiteUrl, description, title, icons} = settings;
|
|
21
|
-
|
|
22
|
-
const server = new McpServer(
|
|
23
|
-
{
|
|
24
|
-
websiteUrl,
|
|
25
|
-
description,
|
|
26
|
-
icons,
|
|
27
|
-
title,
|
|
28
|
-
name,
|
|
29
|
-
version
|
|
30
|
-
},
|
|
31
|
-
settings?.serverOptions
|
|
32
|
-
);
|
|
33
|
-
|
|
34
|
-
const toolTokens = collectTokens(MCP_PROVIDER_TYPES.TOOL, settings.tools);
|
|
35
|
-
toolTokens.forEach((token) => {
|
|
36
|
-
const definition = inject<ToolProps<any, any> & {handler: any}>(token);
|
|
37
|
-
const {name, handler, ...opts} = definition;
|
|
38
|
-
server.registerTool(name!, opts as any, handler as any);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
const resourceTokens = collectTokens(MCP_PROVIDER_TYPES.RESOURCE, settings.resources);
|
|
42
|
-
resourceTokens.forEach((token) => {
|
|
43
|
-
const definition = inject<ResourceSettings & {uri?: string; template?: ResourceTemplate}>(token);
|
|
44
|
-
const {name, handler, uri, template, ...opts} = definition;
|
|
45
|
-
const resourceName = name || String(token);
|
|
46
|
-
|
|
47
|
-
if (uri) {
|
|
48
|
-
server.registerResource(resourceName, uri, opts, handler as any);
|
|
49
|
-
} else {
|
|
50
|
-
server.registerResource(resourceName, template as ResourceTemplate, opts, handler as any);
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
const promptTokens = collectTokens(MCP_PROVIDER_TYPES.PROMPT, settings.prompts);
|
|
55
|
-
promptTokens.forEach((token) => {
|
|
56
|
-
const definition = inject<PromptsSettings>(token);
|
|
57
|
-
const {name, handler, ...opts} = definition;
|
|
58
|
-
server.registerPrompt(name || String(token), opts as any, handler as any);
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
return server;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Injectable MCP server instance configured with registered tools, resources, and prompts.
|
|
66
|
-
*
|
|
67
|
-
* @module platform/mcp
|
|
68
|
-
* @since 8.17.0
|
|
69
|
-
*/
|
|
70
|
-
export const MCP_SERVER = injectable(McpServer).factory(createMcpServer).token();
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Type alias referencing the MCP server provider token.
|
|
74
|
-
*
|
|
75
|
-
* @module platform/mcp
|
|
76
|
-
* @since 8.17.0
|
|
77
|
-
*/
|
|
78
|
-
export type MCP_SERVER = typeof MCP_SERVER;
|