Skip to content

Commit 43c72fe

Browse files
committed
Merge remote-tracking branch 'origin/main' into feature/sampling-agent
2 parents 187c8f6 + ae5ca65 commit 43c72fe

File tree

4 files changed

+41
-275
lines changed

4 files changed

+41
-275
lines changed

packages/core/deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mcpc/core",
3-
"version": "0.1.1-beta.1",
3+
"version": "0.1.1-beta.2",
44
"tasks": {
55
"server:compile": "echo \"no need to compile\"",
66
"test": "deno test --allow-env --allow-read tests/",

packages/core/src/service/tools.ts

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
import { experimental_createMCPClient } from "ai";
2-
import { readFileSync } from "node:fs";
31
import { z } from "zod";
4-
import { isProdEnv } from "../../mod.ts";
5-
6-
export enum ServerName {
7-
DIAGRAM = "diagram-thinker",
8-
OAPI = "oapi-invoker",
9-
CODE_RUNNER = "code-runner",
10-
}
112

123
const AutoApproveSchema = z.array(z.string()).default([]);
134

@@ -51,45 +42,5 @@ export const McpSettingsSchema: z.ZodObject<{
5142
mcpServers: z.record(ServerConfigSchema),
5243
});
5344

54-
const configStr = readFileSync(
55-
new URL(
56-
`../../../../${isProdEnv() ? "mcp.json" : "mcp.local.json"}`,
57-
import.meta.url,
58-
),
59-
"utf-8",
60-
);
61-
6245
export type McpServerConfig = z.infer<typeof ServerConfigSchema>;
6346
export type MCPSetting = z.infer<typeof McpSettingsSchema>;
64-
65-
const mcpSettings = McpSettingsSchema.parse(JSON.parse(configStr));
66-
const _mcpEnabledConfigs = Object.entries(mcpSettings.mcpServers).filter(
67-
([_name, config]) => {
68-
if (config.disabled) {
69-
return false;
70-
}
71-
return true;
72-
},
73-
);
74-
75-
export async function getMcpClient(
76-
serverConfig: [string, z.infer<typeof ServerConfigSchema>],
77-
) {
78-
const [_mcpName, mcpConfig] = serverConfig;
79-
const transport: any = mcpConfig.transportType === "sse"
80-
? {
81-
type: "sse",
82-
url: mcpConfig.url,
83-
headers: {},
84-
}
85-
: {
86-
type: "stdio",
87-
command: mcpConfig.command!,
88-
args: mcpConfig.args || [],
89-
env: mcpConfig.env || {},
90-
};
91-
const client = await experimental_createMCPClient({
92-
transport,
93-
});
94-
return client;
95-
}

packages/core/src/utils/common/ai.ts

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,22 +217,57 @@ export async function composeMcpDepTools(
217217
) {
218218
return;
219219
}
220-
const execute = (args: any) =>
221-
client.callTool({ name: internalToolName, arguments: args });
222-
tool.execute = execute;
223-
allTools[toolId] = tool;
220+
// Store client reference in a way that can be cleaned up
221+
const clientRef = { current: client as Client | null };
222+
223+
const execute = (args: Record<string, unknown>) => {
224+
if (!clientRef.current) {
225+
throw new Error(`Client for tool ${toolId} has been disposed`);
226+
}
227+
return clientRef.current.callTool({
228+
name: internalToolName,
229+
arguments: args,
230+
});
231+
};
232+
233+
// Create a new tool object to avoid modifying the original
234+
const toolWithExecute = {
235+
...tool,
236+
execute,
237+
// Store cleanup function for this specific tool
238+
_cleanup: () => {
239+
clientRef.current = null;
240+
},
241+
};
242+
243+
allTools[toolId] = toolWithExecute;
224244
});
225245
} catch (error) {
226246
console.error(`Error creating MCP client for ${name}:`, error);
227247
}
228248
}
229249

230250
const cleanupClients = async () => {
251+
// Cleanup individual tool references
252+
Object.values(allTools).forEach((tool: { _cleanup?: () => void }) => {
253+
if (tool._cleanup && typeof tool._cleanup === "function") {
254+
tool._cleanup();
255+
}
256+
});
257+
231258
await Promise.all(
232259
Object.values(allClients).map(async (client) => {
233-
await client.close();
260+
try {
261+
await client.close();
262+
} catch (error) {
263+
console.error("Error closing MCP client:", error);
264+
}
234265
}),
235266
);
267+
268+
// Clear references to help GC
269+
Object.keys(allTools).forEach((key) => delete allTools[key]);
270+
Object.keys(allClients).forEach((key) => delete allClients[key]);
236271
};
237272

238273
return { tools: allTools, clients: allClients, cleanupClients };

scripts/generate-mcp-types.ts

Lines changed: 0 additions & 220 deletions
This file was deleted.

0 commit comments

Comments
 (0)