-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathmcp-client.ts
More file actions
647 lines (559 loc) · 18.1 KB
/
mcp-client.ts
File metadata and controls
647 lines (559 loc) · 18.1 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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
import {Client} from '@modelcontextprotocol/sdk/client/index.js';
import {StdioClientTransport} from '@modelcontextprotocol/sdk/client/stdio.js';
import {StreamableHTTPClientTransport} from '@modelcontextprotocol/sdk/client/streamableHttp.js';
import {WebSocketClientTransport} from '@modelcontextprotocol/sdk/client/websocket.js';
// Union type for all supported client transports
type ClientTransport =
| StdioClientTransport
| WebSocketClientTransport
| StreamableHTTPClientTransport;
import {dynamicTool} from 'ai';
import {getCurrentMode} from '@/context/mode-context';
import type {
AISDKCoreTool,
MCPInitResult,
MCPServer,
MCPTool,
Tool,
ToolParameterSchema,
} from '@/types/index';
import {jsonSchema} from '@/types/index';
import {
endMetrics,
formatMemoryUsage,
generateCorrelationId,
getLogger,
startMetrics,
withNewCorrelationContext,
} from '@/utils/logging';
import {TransportFactory} from './transport-factory.js';
export class MCPClient {
private clients: Map<string, Client> = new Map();
private transports: Map<string, ClientTransport> = new Map();
private serverTools: Map<string, MCPTool[]> = new Map();
private serverConfigs: Map<string, MCPServer> = new Map();
private isConnected: boolean = false;
private logger = getLogger();
private isToolAutoApproved(toolName: string, serverName: string): boolean {
const serverConfig = this.serverConfigs.get(serverName);
if (!serverConfig?.alwaysAllow) {
return false;
}
return serverConfig.alwaysAllow.includes(toolName);
}
constructor() {
this.logger.debug('MCP client initialized');
}
/**
* Ensures backward compatibility for old MCP server configurations
* by adding default transport type for existing configurations
*/
private normalizeServerConfig(server: MCPServer): MCPServer {
// If no transport is specified, default to 'stdio' for backward compatibility
if (!server.transport) {
return {
...server,
transport: 'stdio',
};
}
return server;
}
async connectToServer(server: MCPServer): Promise<void> {
const correlationId = generateCorrelationId();
const metrics = startMetrics();
return await withNewCorrelationContext(async () => {
// Normalize server configuration for backward compatibility
const normalizedServer = this.normalizeServerConfig(server);
this.logger.info('Connecting to MCP server', {
serverName: normalizedServer.name,
transport: normalizedServer.transport,
hasUrl: !!normalizedServer.url,
hasCommand: !!normalizedServer.command,
correlationId,
});
// Validate server configuration
const validation =
TransportFactory.validateServerConfig(normalizedServer);
if (!validation.valid) {
const finalMetrics = endMetrics(metrics);
this.logger.error('MCP server configuration validation failed', {
serverName: normalizedServer.name,
errors: validation.errors,
duration: `${finalMetrics.duration.toFixed(2)}ms`,
correlationId,
});
throw new Error(
`Invalid MCP server configuration for "${
normalizedServer.name
}": ${validation.errors.join(', ')}`,
);
}
try {
// Create transport using the factory
const transport = TransportFactory.createTransport(normalizedServer);
this.logger.debug('MCP transport created', {
serverName: normalizedServer.name,
transportType: normalizedServer.transport,
});
// Create and connect client
const client = new Client({
name: 'nanocoder-mcp-client',
version: '1.0.0',
});
this.logger.debug('MCP client created, attempting connection', {
serverName: normalizedServer.name,
});
await client.connect(transport);
this.logger.info('MCP server connected successfully', {
serverName: normalizedServer.name,
transport: normalizedServer.transport,
});
// Store client, transport, and server config
this.clients.set(normalizedServer.name, client);
this.transports.set(normalizedServer.name, transport);
this.serverConfigs.set(normalizedServer.name, normalizedServer);
// List available tools from this server
const toolsResult = await client.listTools();
const tools: MCPTool[] = toolsResult.tools.map(tool => ({
name: tool.name,
description: tool.description || undefined,
inputSchema: tool.inputSchema,
serverName: normalizedServer.name,
}));
this.serverTools.set(normalizedServer.name, tools);
const finalMetrics = endMetrics(metrics);
this.logger.info('MCP server connection completed', {
serverName: normalizedServer.name,
toolCount: tools.length,
duration: `${finalMetrics.duration.toFixed(2)}ms`,
memoryDelta: formatMemoryUsage(
finalMetrics.memoryUsage || process.memoryUsage(),
),
correlationId,
});
} catch (error) {
const finalMetrics = endMetrics(metrics);
this.logger.error('Failed to connect to MCP server', {
serverName: normalizedServer.name,
transport: normalizedServer.transport,
error: error instanceof Error ? error.message : error,
errorName: error instanceof Error ? error.name : 'Unknown',
duration: `${finalMetrics.duration.toFixed(2)}ms`,
memoryDelta: formatMemoryUsage(
finalMetrics.memoryUsage || process.memoryUsage(),
),
correlationId,
});
throw error;
}
}, correlationId);
}
async connectToServers(
servers: MCPServer[],
onProgress?: (result: MCPInitResult) => void,
): Promise<MCPInitResult[]> {
const results: MCPInitResult[] = [];
const correlationId = generateCorrelationId();
const metrics = startMetrics();
this.logger.info('Starting batch MCP server connections', {
serverCount: servers.length,
serverNames: servers.map(s => this.normalizeServerConfig(s).name),
correlationId,
});
return await withNewCorrelationContext(async () => {
// Connect to servers in parallel for better performance
const connectionPromises = servers.map(async server => {
try {
// Normalize server configuration for backward compatibility
const normalizedServer = this.normalizeServerConfig(server);
await this.connectToServer(normalizedServer);
const tools = this.serverTools.get(normalizedServer.name) || [];
const result: MCPInitResult = {
serverName: normalizedServer.name,
success: true,
toolCount: tools.length,
};
results.push(result);
this.logger.debug('MCP server connection successful in batch', {
serverName: normalizedServer.name,
toolCount: tools.length,
correlationId,
});
onProgress?.(result);
return result;
} catch (error) {
const normalizedServer = this.normalizeServerConfig(server);
const result: MCPInitResult = {
serverName: normalizedServer.name,
success: false,
error: error instanceof Error ? error.message : String(error),
};
this.logger.error('MCP server connection failed in batch', {
serverName: normalizedServer.name,
error: result.error,
errorName: error instanceof Error ? error.name : 'Unknown',
correlationId,
});
results.push(result);
onProgress?.(result);
return result;
}
});
// Wait for all connections to complete
await Promise.all(connectionPromises);
const finalMetrics = endMetrics(metrics);
const successfulConnections = results.filter(r => r.success).length;
const failedConnections = results.length - successfulConnections;
this.logger.info('Batch MCP server connections completed', {
totalServers: servers.length,
successfulConnections,
failedConnections,
duration: `${finalMetrics.duration.toFixed(2)}ms`,
correlationId,
});
this.isConnected = true;
return results;
}, correlationId);
}
getAllTools(): Tool[] {
const tools: Tool[] = [];
this.logger.debug('Building all tools registry from MCP servers', {
serverCount: this.serverTools.size,
totalToolsAvailable: Array.from(this.serverTools.values()).reduce(
(sum, tools) => sum + tools.length,
0,
),
});
for (const [serverName, serverTools] of this.serverTools.entries()) {
this.logger.debug('Processing tools from MCP server', {
serverName,
toolCount: serverTools.length,
});
for (const mcpTool of serverTools) {
// Convert MCP tool to nanocoder Tool format
// Use the original tool name for better model compatibility
const schema = mcpTool.inputSchema as
| {
type?: string;
properties?: Record<string, unknown>;
required?: string[];
}
| undefined;
const tool: Tool = {
type: 'function',
function: {
name: mcpTool.name,
description: mcpTool.description
? `[MCP:${serverName}] ${mcpTool.description}`
: `MCP tool from ${serverName}`,
parameters: {
type: 'object',
properties: (schema?.properties || {}) as Record<
string,
ToolParameterSchema
>,
required: schema?.required || [],
},
},
};
tools.push(tool);
}
}
return tools;
}
/**
* Get all MCP tools as AI SDK native CoreTool format
* Converts MCP tool schemas to AI SDK's tool() format
*/
getNativeToolsRegistry(): Record<string, AISDKCoreTool> {
const nativeTools: Record<string, AISDKCoreTool> = {};
for (const [serverName, serverTools] of this.serverTools.entries()) {
for (const mcpTool of serverTools) {
// dynamicTool is more explicit about unknown types compared to tool()
// MCP schemas come from external servers and are not known at compile time
const toolName = mcpTool.name;
const isAutoApproved = this.isToolAutoApproved(toolName, serverName);
const coreTool = dynamicTool({
description: mcpTool.description
? `[MCP:${serverName}] ${mcpTool.description}`
: `MCP tool from ${serverName}`,
inputSchema: jsonSchema<Record<string, unknown>>(
(mcpTool.inputSchema as unknown) || {type: 'object'},
),
// Medium risk: MCP tools require approval unless explicitly configured in the server's alwaysAllow list or in auto-accept mode
needsApproval: () => {
if (isAutoApproved) {
return false;
}
const mode = getCurrentMode();
return mode !== 'auto-accept'; // true in normal/plan, false in auto-accept
},
execute: async (input, _options) => {
// dynamicTool passes 'input' as unknown, validate at runtime
return await this.callTool(
toolName,
input as Record<string, unknown>,
);
},
});
nativeTools[mcpTool.name] = coreTool;
}
}
return nativeTools;
}
getToolMapping(): Map<string, {serverName: string; originalName: string}> {
const mapping = new Map<
string,
{serverName: string; originalName: string}
>();
for (const [serverName, serverTools] of this.serverTools.entries()) {
for (const mcpTool of serverTools) {
mapping.set(mcpTool.name, {
serverName,
originalName: mcpTool.name,
});
}
}
return mapping;
}
/**
* Get all MCP tools as entries with handlers for easy registration
* Each entry contains the native AI SDK tool and its handler function
*
* the AI SDK tool definition and the corresponding handler function.
* This enables cleaner integration with ToolManager.
*
* @returns Array of tool entries with name, AI SDK tool, and handler function
*/
getToolEntries(): Array<{
name: string;
tool: AISDKCoreTool;
handler: (args: Record<string, unknown>) => Promise<string>;
}> {
const entries: Array<{
name: string;
tool: AISDKCoreTool;
handler: (args: Record<string, unknown>) => Promise<string>;
}> = [];
// Get native tools once to avoid redundant calls
const nativeTools = this.getNativeToolsRegistry();
for (const [, serverTools] of this.serverTools.entries()) {
for (const mcpTool of serverTools) {
const toolName = mcpTool.name;
// Get the AI SDK native tool
const coreTool = nativeTools[toolName];
if (coreTool) {
// Create handler that calls this tool
const handler = async (args: Record<string, unknown>) => {
return this.callTool(toolName, args);
};
entries.push({
name: toolName,
tool: coreTool,
handler,
});
}
}
}
return entries;
}
async callTool(
toolName: string,
args: Record<string, unknown>,
): Promise<string> {
// First, try to find which server has this tool
const toolMapping = this.getToolMapping();
const mapping = toolMapping.get(toolName);
if (!mapping) {
// Fallback: try parsing as prefixed name (mcp_serverName_toolName) for backward compatibility
const parts = toolName.split('_');
if (parts.length >= 3 && parts[0] === 'mcp' && parts[1]) {
const serverName = parts[1];
const originalToolName = parts.slice(2).join('_');
const client = this.clients.get(serverName);
if (client) {
return this.executeToolCall(client, originalToolName, args);
}
}
throw new Error(`MCP tool not found: ${toolName}`);
}
const client = this.clients.get(mapping.serverName);
if (!client) {
throw new Error(
`No MCP client connected for server: ${mapping.serverName}`,
);
}
return this.executeToolCall(client, mapping.originalName, args);
}
private async executeToolCall(
client: Client,
toolName: string,
args: Record<string, unknown>,
): Promise<string> {
const correlationId = generateCorrelationId();
const metrics = startMetrics();
return await withNewCorrelationContext(async () => {
this.logger.info('Executing MCP tool', {
toolName,
argumentCount: Object.keys(args).length,
hasArguments: Object.keys(args).length > 0,
correlationId,
});
try {
const result = await client.callTool({
name: toolName,
arguments: args,
});
this.logger.debug('MCP tool executed successfully', {
toolName,
hasContent: !!result.content,
contentLength: Array.isArray(result.content)
? result.content.length
: 0,
correlationId,
});
// Convert result content to string
if (
result.content &&
Array.isArray(result.content) &&
result.content.length > 0
) {
const content = result.content[0] as
| {type: 'text'; text?: string}
| Record<string, unknown>;
if ('type' in content && content.type === 'text') {
const textContent = content as {type: 'text'; text?: string};
const responseText = textContent.text || '';
const finalMetrics = endMetrics(metrics);
this.logger.info('MCP tool execution completed', {
toolName,
responseLength: responseText.length,
duration: `${finalMetrics.duration.toFixed(2)}ms`,
correlationId,
});
return responseText;
}
const jsonResponse = JSON.stringify(content);
const finalMetrics = endMetrics(metrics);
this.logger.info('MCP tool execution completed (JSON)', {
toolName,
responseLength: jsonResponse.length,
duration: `${finalMetrics.duration.toFixed(2)}ms`,
correlationId,
});
return jsonResponse;
}
const finalMetrics = endMetrics(metrics);
this.logger.info('MCP tool execution completed (no output)', {
toolName,
duration: `${finalMetrics.duration.toFixed(2)}ms`,
correlationId,
});
return 'Tool executed successfully (no output)';
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : 'Unknown error';
const errorName = error instanceof Error ? error.name : 'Unknown';
const finalMetrics = endMetrics(metrics);
this.logger.error('MCP tool execution failed', {
toolName,
error: errorMessage,
errorName,
duration: `${finalMetrics.duration.toFixed(2)}ms`,
correlationId,
});
throw new Error(`MCP tool execution failed: ${errorMessage}`);
}
}, correlationId);
}
async disconnect(): Promise<void> {
const correlationId = generateCorrelationId();
const serverNames = Array.from(this.clients.keys());
if (serverNames.length === 0) {
this.logger.debug('No MCP servers to disconnect from');
return;
}
this.logger.info('Disconnecting from MCP servers', {
serverCount: serverNames.length,
serverNames,
correlationId,
});
return await withNewCorrelationContext(async () => {
let successfulDisconnections = 0;
let failedDisconnections = 0;
for (const [serverName, client] of this.clients.entries()) {
try {
await client.close();
successfulDisconnections++;
this.logger.info('Disconnected from MCP server successfully', {
serverName,
correlationId,
});
} catch (error) {
failedDisconnections++;
const errorMessage =
error instanceof Error ? error.message : 'Unknown error';
const errorName = error instanceof Error ? error.name : 'Unknown';
this.logger.error('Error disconnecting from MCP server', {
serverName,
error: errorMessage,
errorName,
correlationId,
});
}
}
this.clients.clear();
this.transports.clear();
this.serverTools.clear();
this.serverConfigs.clear();
this.isConnected = false;
this.logger.info('MCP client disconnection completed', {
totalServers: serverNames.length,
successfulDisconnections,
failedDisconnections,
correlationId,
});
}, correlationId);
}
getConnectedServers(): string[] {
return Array.from(this.clients.keys());
}
isServerConnected(serverName: string): boolean {
return this.clients.has(serverName);
}
getServerTools(serverName: string): MCPTool[] {
return this.serverTools.get(serverName) || [];
}
/**
* Gets server information including transport type and URL for remote servers
*/
getServerInfo(serverName: string):
| {
name: string;
transport: string;
url?: string;
toolCount: number;
connected: boolean;
description?: string;
tags?: string[];
autoApprovedCommands?: string[];
}
| undefined {
const client = this.clients.get(serverName);
const serverConfig = this.serverConfigs.get(serverName);
const tools = this.serverTools.get(serverName) || [];
if (!client || !serverConfig) {
return undefined;
}
return {
name: serverName,
transport: serverConfig.transport,
url: serverConfig.url,
toolCount: tools.length,
connected: true,
description: serverConfig.description,
tags: serverConfig.tags,
autoApprovedCommands: serverConfig.alwaysAllow,
};
}
}