Skip to content

Commit 4019e11

Browse files
committed
fix: multi client
1 parent 5151aff commit 4019e11

File tree

3 files changed

+485
-137
lines changed

3 files changed

+485
-137
lines changed

src/core/MCPServer.ts

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,27 @@ export class MCPServer {
137137
};
138138
logger.debug(`Creating HttpStreamTransport. response mode: ${httpConfig.responseMode}`);
139139
transport = new HttpStreamTransport(httpConfig);
140+
141+
(transport as HttpStreamTransport).setServerConfig(
142+
{ name: this.serverName, version: this.serverVersion },
143+
async (mcpServer) => {
144+
for (const [toolName, tool] of this.toolsMap.entries()) {
145+
(mcpServer as any).tool(
146+
toolName,
147+
tool.inputSchema.properties || {},
148+
async (params: any) => {
149+
const result = await tool.toolCall({
150+
params: {
151+
name: toolName,
152+
arguments: params,
153+
},
154+
});
155+
return result;
156+
}
157+
);
158+
}
159+
}
160+
);
140161
break;
141162
}
142163
case 'stdio':
@@ -164,6 +185,16 @@ export class MCPServer {
164185
return transport;
165186
}
166187

188+
private createServerInstance() {
189+
const server = new Server(
190+
{ name: this.serverName, version: this.serverVersion },
191+
{ capabilities: this.capabilities }
192+
);
193+
194+
this.setupHandlers(server);
195+
return server;
196+
}
197+
167198
private readPackageJson(): any {
168199
try {
169200
const projectRoot = process.cwd();
@@ -201,9 +232,11 @@ export class MCPServer {
201232
return '0.0.0';
202233
}
203234

204-
private setupHandlers() {
235+
private setupHandlers(server?: Server) {
236+
const targetServer = server || this.server;
237+
205238
// TODO: Replace 'any' with the specific inferred request type from the SDK schema if available
206-
this.server.setRequestHandler(ListToolsRequestSchema, async (request: any) => {
239+
targetServer.setRequestHandler(ListToolsRequestSchema, async (request: any) => {
207240
logger.debug(`Received ListTools request: ${JSON.stringify(request)}`);
208241

209242
const tools = Array.from(this.toolsMap.values()).map((tool) => tool.toolDefinition);
@@ -221,7 +254,7 @@ export class MCPServer {
221254
});
222255

223256
// TODO: Replace 'any' with the specific inferred request type from the SDK schema if available
224-
this.server.setRequestHandler(CallToolRequestSchema, async (request: any) => {
257+
targetServer.setRequestHandler(CallToolRequestSchema, async (request: any) => {
225258
logger.debug(`Tool call request received for: ${request.params.name}`);
226259
logger.debug(`Tool call arguments: ${JSON.stringify(request.params.arguments)}`);
227260

@@ -251,14 +284,14 @@ export class MCPServer {
251284
});
252285

253286
if (this.capabilities.prompts) {
254-
this.server.setRequestHandler(ListPromptsRequestSchema, async () => {
287+
targetServer.setRequestHandler(ListPromptsRequestSchema, async () => {
255288
return {
256289
prompts: Array.from(this.promptsMap.values()).map((prompt) => prompt.promptDefinition),
257290
};
258291
});
259292

260293
// TODO: Replace 'any' with the specific inferred request type from the SDK schema if available
261-
this.server.setRequestHandler(GetPromptRequestSchema, async (request: any) => {
294+
targetServer.setRequestHandler(GetPromptRequestSchema, async (request: any) => {
262295
const prompt = this.promptsMap.get(request.params.name);
263296
if (!prompt) {
264297
throw new Error(
@@ -275,7 +308,7 @@ export class MCPServer {
275308
}
276309

277310
if (this.capabilities.resources) {
278-
this.server.setRequestHandler(ListResourcesRequestSchema, async () => {
311+
targetServer.setRequestHandler(ListResourcesRequestSchema, async () => {
279312
return {
280313
resources: Array.from(this.resourcesMap.values()).map(
281314
(resource) => resource.resourceDefinition
@@ -284,7 +317,7 @@ export class MCPServer {
284317
});
285318

286319
// TODO: Replace 'any' with the specific inferred request type from the SDK schema if available
287-
this.server.setRequestHandler(ReadResourceRequestSchema, async (request: any) => {
320+
targetServer.setRequestHandler(ReadResourceRequestSchema, async (request: any) => {
288321
const resource = this.resourcesMap.get(request.params.uri);
289322
if (!resource) {
290323
throw new Error(
@@ -299,7 +332,7 @@ export class MCPServer {
299332
};
300333
});
301334

302-
this.server.setRequestHandler(ListResourceTemplatesRequestSchema, async () => {
335+
targetServer.setRequestHandler(ListResourceTemplatesRequestSchema, async () => {
303336
logger.debug(`Received ListResourceTemplates request`);
304337
const response = {
305338
resourceTemplates: [],
@@ -310,7 +343,7 @@ export class MCPServer {
310343
});
311344

312345
// TODO: Replace 'any' with the specific inferred request type from the SDK schema if available
313-
this.server.setRequestHandler(SubscribeRequestSchema, async (request: any) => {
346+
targetServer.setRequestHandler(SubscribeRequestSchema, async (request: any) => {
314347
const resource = this.resourcesMap.get(request.params.uri);
315348
if (!resource) {
316349
throw new Error(`Unknown resource: ${request.params.uri}`);
@@ -325,7 +358,7 @@ export class MCPServer {
325358
});
326359

327360
// TODO: Replace 'any' with the specific inferred request type from the SDK schema if available
328-
this.server.setRequestHandler(UnsubscribeRequestSchema, async (request: any) => {
361+
targetServer.setRequestHandler(UnsubscribeRequestSchema, async (request: any) => {
329362
const resource = this.resourcesMap.get(request.params.uri);
330363
if (!resource) {
331364
throw new Error(`Unknown resource: ${request.params.uri}`);

0 commit comments

Comments
 (0)