@tsed/platform-mcp 8.44.12 → 8.44.14
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/common/services/PlatformMcpGatewayService.js +63 -12
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/types/common/services/PlatformMcpGatewayService.d.ts +15 -1
- package/package.json +9 -9
- package/src/common/services/PlatformMcpGatewayService.spec.ts +58 -0
- package/src/common/services/PlatformMcpGatewayService.ts +77 -13
|
@@ -53,7 +53,21 @@ export declare class PlatformMcpGatewayService implements OnDestroy {
|
|
|
53
53
|
getConnection(upstream: PlatformMcpUpstreamSettings, resolved?: PlatformMcpUpstreamSettings): Promise<PlatformMcpUpstreamConnection>;
|
|
54
54
|
$onDestroy(): Promise<void>;
|
|
55
55
|
protected connect(upstream: PlatformMcpUpstreamSettings, entry: PoolEntry): Promise<PlatformMcpUpstreamConnection>;
|
|
56
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Lists every catalog the upstream advertises.
|
|
58
|
+
*
|
|
59
|
+
* A listing that fails is logged and left empty so the others stay available: an upstream may advertise
|
|
60
|
+
* a capability it cannot serve (e.g. prompts without any prompt configured). The catalog is then flagged
|
|
61
|
+
* incomplete so the next request lists it again. It rejects only when every advertised listing fails.
|
|
62
|
+
*/
|
|
63
|
+
protected loadCatalog(client: Client, label: string): Promise<{
|
|
64
|
+
catalog: PlatformMcpUpstreamCatalog;
|
|
65
|
+
complete: boolean;
|
|
66
|
+
}>;
|
|
67
|
+
/**
|
|
68
|
+
* Names the upstream in the logs without the credentials its URL may carry (userinfo, query, hash).
|
|
69
|
+
*/
|
|
70
|
+
protected getLabel(upstream: PlatformMcpUpstreamSettings): string;
|
|
57
71
|
protected getPool(upstream: PlatformMcpUpstreamSettings): Map<string, PoolEntry>;
|
|
58
72
|
protected getKey(upstream: PlatformMcpUpstreamSettings): string;
|
|
59
73
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/platform-mcp",
|
|
3
|
-
"version": "8.44.
|
|
3
|
+
"version": "8.44.14",
|
|
4
4
|
"description": "Model Context Protocol integration for Ts.ED HTTP adapters.",
|
|
5
5
|
"source": "./src/http/index.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -51,14 +51,14 @@
|
|
|
51
51
|
"uuid": ">=11.0.2"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
|
-
"@tsed/barrels": "8.44.
|
|
55
|
-
"@tsed/core": "8.44.
|
|
56
|
-
"@tsed/di": "8.44.
|
|
57
|
-
"@tsed/exceptions": "8.44.
|
|
58
|
-
"@tsed/json-mapper": "8.44.
|
|
59
|
-
"@tsed/platform-http": "8.44.
|
|
60
|
-
"@tsed/schema": "8.44.
|
|
61
|
-
"@tsed/typescript": "8.44.
|
|
54
|
+
"@tsed/barrels": "8.44.14",
|
|
55
|
+
"@tsed/core": "8.44.14",
|
|
56
|
+
"@tsed/di": "8.44.14",
|
|
57
|
+
"@tsed/exceptions": "8.44.14",
|
|
58
|
+
"@tsed/json-mapper": "8.44.14",
|
|
59
|
+
"@tsed/platform-http": "8.44.14",
|
|
60
|
+
"@tsed/schema": "8.44.14",
|
|
61
|
+
"@tsed/typescript": "8.44.14",
|
|
62
62
|
"jose": "^6.1.3",
|
|
63
63
|
"typescript": "6.0.2",
|
|
64
64
|
"vitest": "4.1.4"
|
|
@@ -121,6 +121,64 @@ describe("PlatformMcpGatewayService", () => {
|
|
|
121
121
|
expect(listTools).toHaveBeenCalledTimes(2);
|
|
122
122
|
});
|
|
123
123
|
|
|
124
|
+
it("keeps the other listings when an advertised listing fails, and lists it again on the next request", async () => {
|
|
125
|
+
createUpstreamTransport.mockImplementation(async () => {
|
|
126
|
+
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
|
|
127
|
+
const server = new McpServer({name: "upstream", version: "1.0.0"});
|
|
128
|
+
|
|
129
|
+
server.registerTool("first", {description: "First tool"}, () => ({content: []}));
|
|
130
|
+
server.registerPrompt("greet", {description: "Greet"}, () => ({messages: []}));
|
|
131
|
+
|
|
132
|
+
await server.connect(serverTransport);
|
|
133
|
+
|
|
134
|
+
return clientTransport;
|
|
135
|
+
});
|
|
136
|
+
vi.spyOn(logger(), "warn").mockReturnValue(undefined as never);
|
|
137
|
+
|
|
138
|
+
const connection = await inject(PlatformMcpGatewayService).getConnection(createUpstream());
|
|
139
|
+
const error = new Error("A prompts collection must be set in settings");
|
|
140
|
+
const listPrompts = vi.spyOn(connection.client, "listPrompts").mockRejectedValueOnce(error);
|
|
141
|
+
|
|
142
|
+
await expect(connection.getCatalog()).resolves.toMatchObject({tools: [{name: "first"}], prompts: []});
|
|
143
|
+
expect(logger().warn).toHaveBeenCalledWith({
|
|
144
|
+
event: "MCP_GATEWAY_CATALOG_ERROR",
|
|
145
|
+
message: 'MCP upstream "http://localhost/mcp": unable to list prompts, they are skipped.',
|
|
146
|
+
error
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
await expect(connection.getCatalog()).resolves.toMatchObject({tools: [{name: "first"}], prompts: [{name: "greet"}]});
|
|
150
|
+
expect(listPrompts).toHaveBeenCalledTimes(2);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("rejects the catalog when every advertised listing fails", async () => {
|
|
154
|
+
createUpstreamTransport.mockImplementation(async () => {
|
|
155
|
+
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
|
|
156
|
+
const server = new McpServer({name: "upstream", version: "1.0.0"});
|
|
157
|
+
|
|
158
|
+
server.registerTool("first", {description: "First tool"}, () => ({content: []}));
|
|
159
|
+
server.registerPrompt("greet", {description: "Greet"}, () => ({messages: []}));
|
|
160
|
+
|
|
161
|
+
await server.connect(serverTransport);
|
|
162
|
+
|
|
163
|
+
return clientTransport;
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
vi.spyOn(logger(), "warn").mockReturnValue(undefined as never);
|
|
167
|
+
|
|
168
|
+
const upstream = createUpstream({url: "https://user:secret@localhost/mcp?token=secret#hash"});
|
|
169
|
+
const connection = await inject(PlatformMcpGatewayService).getConnection(upstream);
|
|
170
|
+
|
|
171
|
+
vi.spyOn(connection.client, "listTools").mockRejectedValueOnce(new Error("tools failed"));
|
|
172
|
+
vi.spyOn(connection.client, "listPrompts").mockRejectedValueOnce(new Error("prompts failed"));
|
|
173
|
+
|
|
174
|
+
await expect(connection.getCatalog()).rejects.toThrow("tools failed");
|
|
175
|
+
// every failure is logged, and the label carries no credential of the URL
|
|
176
|
+
expect(vi.mocked(logger().warn).mock.calls.map(([log]) => (log as any).message)).toEqual([
|
|
177
|
+
'MCP upstream "https://localhost/mcp": unable to list tools, they are skipped.',
|
|
178
|
+
'MCP upstream "https://localhost/mcp": unable to list prompts, they are skipped.'
|
|
179
|
+
]);
|
|
180
|
+
});
|
|
181
|
+
|
|
124
182
|
it("refreshes the catalog when the upstream notifies a list change", async () => {
|
|
125
183
|
const servers = mockUpstreamServers();
|
|
126
184
|
const connection = await inject(PlatformMcpGatewayService).getConnection(createUpstream());
|
|
@@ -108,6 +108,7 @@ export class PlatformMcpGatewayService implements OnDestroy {
|
|
|
108
108
|
protected async connect(upstream: PlatformMcpUpstreamSettings, entry: PoolEntry): Promise<PlatformMcpUpstreamConnection> {
|
|
109
109
|
const {Client} = await import("@modelcontextprotocol/client");
|
|
110
110
|
const transport = await createUpstreamTransport(upstream);
|
|
111
|
+
const label = this.getLabel(upstream);
|
|
111
112
|
let catalog: Promise<PlatformMcpUpstreamCatalog> | undefined;
|
|
112
113
|
const invalidate = () => {
|
|
113
114
|
catalog = undefined;
|
|
@@ -148,11 +149,27 @@ export class PlatformMcpGatewayService implements OnDestroy {
|
|
|
148
149
|
return {
|
|
149
150
|
client,
|
|
150
151
|
getCatalog: () => {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
152
|
+
if (!catalog) {
|
|
153
|
+
const pending: Promise<PlatformMcpUpstreamCatalog> = this.loadCatalog(client, label).then(
|
|
154
|
+
({catalog: loaded, complete}) => {
|
|
155
|
+
// an incomplete catalog is served once and listed again on the next request
|
|
156
|
+
if (!complete && catalog === pending) {
|
|
157
|
+
catalog = undefined;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return loaded;
|
|
161
|
+
},
|
|
162
|
+
(error) => {
|
|
163
|
+
if (catalog === pending) {
|
|
164
|
+
catalog = undefined;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
throw error;
|
|
168
|
+
}
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
catalog = pending;
|
|
172
|
+
}
|
|
156
173
|
|
|
157
174
|
return catalog;
|
|
158
175
|
},
|
|
@@ -169,17 +186,64 @@ export class PlatformMcpGatewayService implements OnDestroy {
|
|
|
169
186
|
};
|
|
170
187
|
}
|
|
171
188
|
|
|
172
|
-
|
|
189
|
+
/**
|
|
190
|
+
* Lists every catalog the upstream advertises.
|
|
191
|
+
*
|
|
192
|
+
* A listing that fails is logged and left empty so the others stay available: an upstream may advertise
|
|
193
|
+
* a capability it cannot serve (e.g. prompts without any prompt configured). The catalog is then flagged
|
|
194
|
+
* incomplete so the next request lists it again. It rejects only when every advertised listing fails.
|
|
195
|
+
*/
|
|
196
|
+
protected async loadCatalog(client: Client, label: string): Promise<{catalog: PlatformMcpUpstreamCatalog; complete: boolean}> {
|
|
173
197
|
const capabilities = client.getServerCapabilities() || {};
|
|
198
|
+
const listings = [
|
|
199
|
+
["tools", capabilities.tools, (params: {cursor?: string}) => client.listTools(params)],
|
|
200
|
+
["resources", capabilities.resources, (params: {cursor?: string}) => client.listResources(params)],
|
|
201
|
+
["resourceTemplates", capabilities.resources, (params: {cursor?: string}) => client.listResourceTemplates(params)],
|
|
202
|
+
["prompts", capabilities.prompts, (params: {cursor?: string}) => client.listPrompts(params)]
|
|
203
|
+
] as const;
|
|
204
|
+
|
|
205
|
+
const results = await Promise.allSettled(listings.map(([key, enabled, list]) => listAll(enabled, list, key)));
|
|
206
|
+
const failures = results.flatMap((result, index) =>
|
|
207
|
+
result.status === "rejected" ? [{key: listings[index][0], error: result.reason}] : []
|
|
208
|
+
);
|
|
209
|
+
const advertised = listings.filter(([, enabled]) => enabled).length;
|
|
210
|
+
|
|
211
|
+
failures.forEach(({key, error}) => {
|
|
212
|
+
logger().warn({
|
|
213
|
+
event: "MCP_GATEWAY_CATALOG_ERROR",
|
|
214
|
+
message: `MCP upstream "${label}": unable to list ${key}, they are skipped.`,
|
|
215
|
+
error
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
if (failures.length && failures.length === advertised) {
|
|
220
|
+
throw failures[0].error;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const [tools, resources, resourceTemplates, prompts] = results.map((result) => (result.status === "fulfilled" ? result.value : []));
|
|
224
|
+
|
|
225
|
+
return {
|
|
226
|
+
catalog: {
|
|
227
|
+
tools: tools as Tool[],
|
|
228
|
+
resources: resources as Resource[],
|
|
229
|
+
resourceTemplates: resourceTemplates as ResourceTemplateType[],
|
|
230
|
+
prompts: prompts as Prompt[]
|
|
231
|
+
},
|
|
232
|
+
complete: !failures.length
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Names the upstream in the logs without the credentials its URL may carry (userinfo, query, hash).
|
|
238
|
+
*/
|
|
239
|
+
protected getLabel(upstream: PlatformMcpUpstreamSettings) {
|
|
240
|
+
if (upstream.type === "stdio") {
|
|
241
|
+
return upstream.command;
|
|
242
|
+
}
|
|
174
243
|
|
|
175
|
-
const
|
|
176
|
-
listAll<Tool>(capabilities.tools, (params) => client.listTools(params), "tools"),
|
|
177
|
-
listAll<Resource>(capabilities.resources, (params) => client.listResources(params), "resources"),
|
|
178
|
-
listAll<ResourceTemplateType>(capabilities.resources, (params) => client.listResourceTemplates(params), "resourceTemplates"),
|
|
179
|
-
listAll<Prompt>(capabilities.prompts, (params) => client.listPrompts(params), "prompts")
|
|
180
|
-
]);
|
|
244
|
+
const {origin, pathname} = new URL(upstream.url);
|
|
181
245
|
|
|
182
|
-
return {
|
|
246
|
+
return `${origin}${pathname}`;
|
|
183
247
|
}
|
|
184
248
|
|
|
185
249
|
protected getPool(upstream: PlatformMcpUpstreamSettings) {
|