@@ -1,92 +0,0 @@
1-import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
2-import { join } from "node:path";
3-import type { ToolDef } from "../llm-api/types.ts";
4-import { getConfigDir } from "../session/db.ts";
5-import {
6- type McpClient,
7- type McpServerConfig,
8- connectMcpServer,
9-} from "./client.ts";
10-
11-// ─── Config persistence ───────────────────────────────────────────────────────
12-
13-function getMcpConfigPath(): string {
14- return join(getConfigDir(), "mcp.json");
15-}
16-
17-export function loadMcpConfig(): McpServerConfig[] {
18- const path = getMcpConfigPath();
19- if (!existsSync(path)) return [];
20- try {
21- const raw = readFileSync(path, "utf-8");
22- return JSON.parse(raw) as McpServerConfig[];
23- } catch {
24- return [];
25- }
26-}
27-
28-export function saveMcpConfig(servers: McpServerConfig[]): void {
29- const dir = getConfigDir();
30- if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
31- writeFileSync(getMcpConfigPath(), JSON.stringify(servers, null, 2));
32-}
33-
34-export function addMcpConfig(server: McpServerConfig): void {
35- const servers = loadMcpConfig();
36- const existing = servers.findIndex((s) => s.name === server.name);
37- if (existing !== -1) servers[existing] = server;
38- else servers.push(server);
39- saveMcpConfig(servers);
40-}
41-
42-export function removeMcpConfig(name: string): void {
43- const servers = loadMcpConfig().filter((s) => s.name !== name);
44- saveMcpConfig(servers);
45-}
46-
47-// ─── Active connections ───────────────────────────────────────────────────────
48-
49-const _activeClients: Map<string, McpClient> = new Map();
50-
51-/**
52- * Connect to all configured MCP servers, return their combined tools.
53- */
54-export async function connectAllMcp(): Promise<ToolDef[]> {
55- const configs = loadMcpConfig();
56- const allTools: ToolDef[] = [];
57-
58- for (const config of configs) {
59- try {
60- if (!_activeClients.has(config.name)) {
61- const client = await connectMcpServer(config);
62- _activeClients.set(config.name, client);
63- }
64- const client = _activeClients.get(config.name);
65- if (!client) {
66- throw new Error(`MCP client missing for ${config.name}`);
67- }
68- allTools.push(...client.tools);
69- } catch (err) {
70- console.error(
71- `[mcp] Failed to connect to "${config.name}":`,
72- err instanceof Error ? err.message : err,
73- );
74- }
75- }
76-
77- return allTools;
78-}
79-
80-/**
81- * Close all active MCP connections.
82- */
83-export async function closeAllMcp(): Promise<void> {
84- for (const client of _activeClients.values()) {
85- try {
86- await client.close();
87- } catch {
88- /* ignore */
89- }
90- }
91- _activeClients.clear();
92-}