-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcpClient.js
More file actions
468 lines (406 loc) · 14.3 KB
/
mcpClient.js
File metadata and controls
468 lines (406 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/**
* guIDE MCP Client — Model Context Protocol client for external tool servers.
* Connects to MCP servers via stdio transport, discovers tools, and routes execution.
* Copyright (c) 2025-2026 Brendan Gray (GitHub: FileShot)
* All Rights Reserved. See LICENSE for terms.
*
* Config format (mcp_config.json in project root):
* {
* "mcpServers": {
* "server-name": {
* "command": "npx",
* "args": ["-y", "@modelcontextprotocol/server-filesystem", "/path"],
* "env": {}
* }
* }
* }
*/
const { spawn } = require('child_process');
const path = require('path');
const fs = require('fs');
class MCPClient {
constructor(options = {}) {
this.projectPath = options.projectPath || process.cwd();
this._servers = new Map(); // serverName → { process, tools, status, config }
this._toolServerMap = new Map(); // toolName → serverName
this._discoveredTools = []; // merged tool definitions from all servers
this._onLog = options.onLog || (() => {});
this._onToolsChanged = options.onToolsChanged || null;
this._shuttingDown = false;
}
// ─── Config Loading ──────────────────────────────────────────────────────
/**
* Load MCP server config from mcp_config.json in the project root.
* Returns the parsed config or null if not found.
*/
loadConfig() {
const configPaths = [
path.join(this.projectPath, 'mcp_config.json'),
path.join(this.projectPath, '.guide', 'mcp_config.json'),
];
for (const configPath of configPaths) {
if (fs.existsSync(configPath)) {
try {
const raw = fs.readFileSync(configPath, 'utf8');
const config = JSON.parse(raw);
this._onLog(`[MCPClient] Loaded config from ${configPath}`);
return config;
} catch (e) {
this._onLog(`[MCPClient] Failed to parse ${configPath}: ${e.message}`);
return null;
}
}
}
return null;
}
/**
* Initialize all MCP servers from config. Returns array of server names started.
*/
async initFromConfig(config) {
if (!config || !config.mcpServers || typeof config.mcpServers !== 'object') {
this._onLog('[MCPClient] No mcpServers in config');
return [];
}
const started = [];
for (const [name, serverConfig] of Object.entries(config.mcpServers)) {
try {
await this.startServer(name, serverConfig);
started.push(name);
} catch (e) {
this._onLog(`[MCPClient] Failed to start server "${name}": ${e.message}`);
}
}
return started;
}
// ─── Server Lifecycle ────────────────────────────────────────────────────
/**
* Start an MCP server via stdio transport.
* Spawns the process, waits for initialization, then discovers tools.
*/
async startServer(name, config) {
if (this._servers.has(name)) {
await this.stopServer(name);
}
const command = config.command;
const args = config.args || [];
const env = { ...process.env, ...(config.env || {}) };
if (!command) {
throw new Error(`Server "${name}" has no command specified`);
}
this._onLog(`[MCPClient] Starting server "${name}": ${command} ${args.join(' ')}`);
const child = spawn(command, args, {
stdio: ['pipe', 'pipe', 'pipe'],
env,
cwd: this.projectPath,
shell: process.platform === 'win32',
});
const serverEntry = {
name,
process: child,
config,
tools: [],
status: 'starting',
requestId: 0,
pendingRequests: new Map(),
buffer: '',
};
this._servers.set(name, serverEntry);
// Handle stdout — JSON-RPC messages are line-delimited
child.stdout.on('data', (data) => {
serverEntry.buffer += data.toString();
this._processBuffer(name);
});
// Handle stderr — log it
child.stderr.on('data', (data) => {
const msg = data.toString().trim();
if (msg) this._onLog(`[MCPClient:${name}:stderr] ${msg.substring(0, 500)}`);
});
// Handle exit — auto-restart with exponential backoff on unexpected exit
child.on('exit', (code) => {
this._onLog(`[MCPClient] Server "${name}" exited with code ${code}`);
const entry = this._servers.get(name);
if (entry) {
entry.status = 'stopped';
// Reject all pending requests
for (const [, reject] of entry.pendingRequests.values()) {
reject(new Error(`Server "${name}" exited`));
}
entry.pendingRequests.clear();
// Auto-restart if not shutting down and exit was unexpected
if (!this._shuttingDown && code !== 0 && entry._restartCount < 3) {
const delay = [5000, 15000, 45000][entry._restartCount] || 45000;
entry._restartCount = (entry._restartCount || 0) + 1;
this._onLog(`[MCPClient] Auto-restarting "${name}" in ${delay}ms (attempt ${entry._restartCount}/3)`);
setTimeout(() => {
if (!this._shuttingDown && this._servers.has(name)) {
this.startServer(name, config).catch(err => {
this._onLog(`[MCPClient] Auto-restart failed for "${name}": ${err.message}`);
});
}
}, delay);
}
// Notify tools changed (tools from this server are no longer available)
if (this._onToolsChanged) this._onToolsChanged();
}
});
child.on('error', (err) => {
this._onLog(`[MCPClient] Server "${name}" error: ${err.message}`);
serverEntry.status = 'error';
});
// Initialize the server (MCP handshake)
try {
await this._sendRequest(name, 'initialize', {
protocolVersion: '2024-11-05',
capabilities: {},
clientInfo: { name: 'guIDE', version: '3.0' },
});
// Send initialized notification
this._sendNotification(name, 'notifications/initialized', {});
serverEntry.status = 'initialized';
this._onLog(`[MCPClient] Server "${name}" initialized`);
// Discover tools
await this._discoverTools(name);
return serverEntry;
} catch (e) {
serverEntry.status = 'error';
this._onLog(`[MCPClient] Server "${name}" init failed: ${e.message}`);
throw e;
}
}
/**
* Stop an MCP server gracefully.
*/
async stopServer(name) {
const entry = this._servers.get(name);
if (!entry) return;
entry.status = 'stopping';
try {
// Send shutdown request
await this._sendRequest(name, 'shutdown', {}, 5000);
} catch {
// Ignore shutdown errors
}
try {
entry.process.kill('SIGTERM');
} catch {}
// Remove tool mappings
for (const tool of entry.tools) {
this._toolServerMap.delete(tool.name);
}
this._servers.delete(name);
this._rebuildDiscoveredTools();
}
/**
* Stop all servers.
*/
async stopAll() {
this._shuttingDown = true;
const names = [...this._servers.keys()];
for (const name of names) {
try {
await this.stopServer(name);
} catch {}
}
}
// ─── Tool Discovery ─────────────────────────────────────────────────────
async _discoverTools(name) {
const entry = this._servers.get(name);
if (!entry) return;
try {
const result = await this._sendRequest(name, 'tools/list', {});
if (result && result.tools && Array.isArray(result.tools)) {
entry.tools = result.tools.map(t => ({
name: t.name,
description: t.description || '',
parameters: this._convertInputSchema(t.inputSchema),
_mcpServer: name, // track which server owns this tool
}));
this._onLog(`[MCPClient] Server "${name}" has ${entry.tools.length} tools: ${entry.tools.map(t => t.name).join(', ')}`);
// Update tool→server mapping
for (const tool of entry.tools) {
this._toolServerMap.set(tool.name, name);
}
this._rebuildDiscoveredTools();
}
} catch (e) {
this._onLog(`[MCPClient] Failed to discover tools from "${name}": ${e.message}`);
}
}
/**
* Convert JSON Schema inputSchema to the flat parameter format used by guIDE tool defs.
*/
_convertInputSchema(schema) {
if (!schema || !schema.properties) return {};
const params = {};
const required = new Set(schema.required || []);
for (const [key, prop] of Object.entries(schema.properties)) {
params[key] = {
type: prop.type || 'string',
description: prop.description || '',
required: required.has(key),
};
if (prop.enum) params[key].enum = prop.enum;
if (prop.default !== undefined) params[key].default = prop.default;
}
return params;
}
/**
* Rebuild the merged discovered tools list from all servers.
*/
_rebuildDiscoveredTools() {
this._discoveredTools = [];
for (const [, entry] of this._servers) {
if (entry.tools && entry.tools.length > 0) {
this._discoveredTools.push(...entry.tools);
}
}
if (this._onToolsChanged) {
this._onToolsChanged(this._discoveredTools);
}
}
/**
* Get all discovered tool definitions from MCP servers.
*/
getDiscoveredTools() {
return this._discoveredTools;
}
// ─── Tool Execution ──────────────────────────────────────────────────────
/**
* Execute a tool call on the appropriate MCP server.
* Returns the tool result or null if the tool is not from an MCP server.
*/
async executeTool(toolName, params) {
const serverName = this._toolServerMap.get(toolName);
if (!serverName) return null; // Not an MCP tool
const entry = this._servers.get(serverName);
if (!entry || entry.status !== 'initialized') {
return { success: false, error: `MCP server "${serverName}" is not running` };
}
try {
const result = await this._sendRequest(serverName, 'tools/call', {
name: toolName,
arguments: params,
});
// MCP tool results have content array
if (result && result.content) {
const textParts = result.content
.filter(c => c.type === 'text')
.map(c => c.text);
const imageParts = result.content
.filter(c => c.type === 'image')
.map(c => ({ type: 'image', mimeType: c.mimeType, data: c.data }));
const output = textParts.join('\n');
const isError = result.isError === true;
return {
success: !isError,
output,
images: imageParts.length > 0 ? imageParts : undefined,
error: isError ? output : undefined,
_mcpServer: serverName,
};
}
return { success: true, output: JSON.stringify(result), _mcpServer: serverName };
} catch (e) {
return { success: false, error: `MCP tool "${toolName}" failed: ${e.message}`, _mcpServer: serverName };
}
}
/**
* Check if a tool name belongs to an MCP server.
*/
isMCPTool(toolName) {
return this._toolServerMap.has(toolName);
}
/**
* Get list of connected server names and their status.
*/
getServerStatus() {
const status = {};
for (const [name, entry] of this._servers) {
status[name] = {
status: entry.status,
toolCount: entry.tools.length,
tools: entry.tools.map(t => t.name),
};
}
return status;
}
// ─── JSON-RPC Transport ──────────────────────────────────────────────────
_processBuffer(name) {
const entry = this._servers.get(name);
if (!entry) return;
const lines = entry.buffer.split('\n');
// Keep the last incomplete line in the buffer
entry.buffer = lines.pop() || '';
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed) continue;
try {
const message = JSON.parse(trimmed);
this._handleMessage(name, message);
} catch {
// Not valid JSON — skip
}
}
}
_handleMessage(name, message) {
const entry = this._servers.get(name);
if (!entry) return;
if (message.id && entry.pendingRequests.has(message.id)) {
const { resolve, reject } = entry.pendingRequests.get(message.id);
entry.pendingRequests.delete(message.id);
if (message.error) {
reject(new Error(message.error.message || JSON.stringify(message.error)));
} else {
resolve(message.result);
}
} else if (message.method) {
// Notification from server — log it
this._onLog(`[MCPClient:${name}:notify] ${message.method}`);
}
}
_sendRequest(serverName, method, params, timeoutMs = 30000) {
const entry = this._servers.get(serverName);
if (!entry) return Promise.reject(new Error(`Server "${serverName}" not found`));
const id = ++entry.requestId;
const request = {
jsonrpc: '2.0',
id,
method,
params: params || {},
};
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
entry.pendingRequests.delete(id);
reject(new Error(`Request "${method}" to "${serverName}" timed out (${timeoutMs}ms)`));
}, timeoutMs);
entry.pendingRequests.set(id, {
resolve: (result) => { clearTimeout(timer); resolve(result); },
reject: (err) => { clearTimeout(timer); reject(err); },
});
try {
const payload = JSON.stringify(request) + '\n';
entry.process.stdin.write(payload);
} catch (e) {
entry.pendingRequests.delete(id);
clearTimeout(timer);
reject(new Error(`Failed to send request to "${serverName}": ${e.message}`));
}
});
}
_sendNotification(serverName, method, params) {
const entry = this._servers.get(serverName);
if (!entry) return;
const notification = {
jsonrpc: '2.0',
method,
params: params || {},
};
try {
const payload = JSON.stringify(notification) + '\n';
entry.process.stdin.write(payload);
} catch (e) {
this._onLog(`[MCPClient] Failed to send notification to "${serverName}": ${e.message}`);
}
}
}
module.exports = { MCPClient };