-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmcp-client.ts
More file actions
389 lines (340 loc) · 10.6 KB
/
mcp-client.ts
File metadata and controls
389 lines (340 loc) · 10.6 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
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
import {
CallToolResultSchema,
ListToolsResultSchema,
} from "@modelcontextprotocol/sdk/types.js";
import { logger } from "@runtimed/agent-core";
import { join } from "@std/path";
interface MCPServerConfig {
command?: string;
args?: string[];
env?: Record<string, string>;
url?: string;
name: string;
description?: string;
}
interface MCPConfig {
mcpServers: Record<string, MCPServerConfig>;
}
function assertIsMCPConfig(val: unknown): asserts val is MCPConfig {
if (
typeof val !== "object" ||
val === null ||
!("mcpServers" in val)
) {
throw new Error("Invalid config: missing 'mcpServers'");
}
const servers = val.mcpServers;
if (
typeof servers !== "object" ||
servers === null ||
Array.isArray(servers)
) {
throw new Error("Invalid 'mcpServers': must be an object");
}
for (const [key, server] of Object.entries(servers)) {
if (
typeof server !== "object" ||
server === null ||
"name" in server &&
typeof server.name !== "string"
) {
throw new Error(
`Invalid MCPServerConfig at key '${key}': must have a string 'name'`,
);
}
if ("command" in server && typeof server.command !== "string") {
throw new Error(`Invalid 'command' in '${key}': must be a string`);
}
if ("args" in server && !Array.isArray(server.args)) {
throw new Error(
`Invalid 'args' in '${key}': must be an array of strings`,
);
} else if (
"args" in server &&
Array.isArray(server.args) &&
!server.args.every((arg: unknown) => typeof arg === "string")
) {
throw new Error(`Invalid 'args' in '${key}': all items must be strings`);
}
if ("env" in server && typeof server.env !== "object") {
throw new Error(`Invalid 'env' in '${key}': must be an object`);
} else if (
"env" in server &&
typeof server.env === "object" &&
server.env !== null
) {
for (const [envKey, envVal] of Object.entries(server.env)) {
if (typeof envVal !== "string") {
throw new Error(
`Invalid 'env' in '${key}': key '${envKey}' must map to a string`,
);
}
}
}
if ("url" in server && typeof server.url !== "string") {
throw new Error(`Invalid 'url' in '${key}': must be a string`);
}
if (
"description" in server &&
typeof server.description !== "string"
) {
throw new Error(`Invalid 'description' in '${key}': must be a string`);
}
}
}
interface MCPTool {
name: string;
description: string;
parameters: {
type: string;
properties: Record<string, unknown>;
required?: string[];
};
serverName: string;
}
const fileExists = (path: string): Promise<boolean> =>
Deno.stat(path).then(() => true).catch(() => false);
export class MCPClient {
private clients = new Map<string, Client>();
private tools: MCPTool[] = [];
private isInitialized = false;
async initialize(): Promise<void> {
if (this.isInitialized) {
return;
}
const config = await this.loadConfig();
await this.connectToServers(config);
await this.discoverTools();
this.isInitialized = true;
logger.info(
`MCP client initialized with ${this.tools.length} tools from ${this.clients.size} servers`,
);
}
private async loadConfig(): Promise<MCPConfig> {
const homeDir = Deno.env.get("HOME") || Deno.env.get("USERPROFILE") || "";
const configPath = join(homeDir, ".runt", "mcp.json");
// Check if file exists, if not then return empty config
if (!(await fileExists(configPath))) {
logger.info(`MCP config file not found at ${configPath}`);
return { mcpServers: {} };
}
const config: MCPConfig = await Deno.readTextFile(configPath).then(
(txt) => {
const config = JSON.parse(txt);
assertIsMCPConfig(config);
return config;
},
).catch((error) => {
logger.error(error);
return { mcpServers: {} };
});
logger.info(`Loaded MCP config from ${configPath}`, {
serverCount: Object.keys(config.mcpServers).length,
servers: Object.keys(config.mcpServers),
});
return config;
}
private async connectToServers(config: MCPConfig): Promise<void> {
const connectionPromises = Object.entries(config.mcpServers).map(
async ([serverName, serverConfig]) => {
try {
await this.connectToServer(serverName, serverConfig);
} catch (error) {
logger.error(`Failed to connect to MCP server ${serverName}`, error);
}
},
);
await Promise.allSettled(connectionPromises);
}
private async connectToServer(
serverName: string,
config: MCPServerConfig,
): Promise<void> {
logger.info(`Connecting to MCP server: ${serverName}`, {
hasCommand: !!config.command,
hasUrl: !!config.url,
});
let transport;
let client;
try {
if (config.command) {
// Stdio transport for command-based servers
// Parse the command string into command and arguments
const commandParts = config.command.split(" ");
const command = commandParts[0];
if (!command) {
throw new Error(`Invalid command specified for server ${serverName}`);
}
const commandArgs = commandParts.slice(1);
transport = new StdioClientTransport({
command: command,
args: commandArgs.concat(config.args || []),
env: { ...Deno.env.toObject(), ...(config.env || {}) },
});
} else if (config.url) {
// SSE transport for URL-based servers
transport = new SSEClientTransport(new URL(config.url));
} else {
throw new Error(
`Server ${serverName} must specify either 'command' or 'url'`,
);
}
client = new Client(
{
name: "runt-ai",
version: "1.0.0",
},
{
capabilities: {
tools: {},
},
},
);
await client.connect(transport);
this.clients.set(serverName, client);
logger.info(`Successfully connected to MCP server: ${serverName}`);
} catch (error) {
logger.error(`Failed to connect to MCP server ${serverName}`, error);
if (transport) {
try {
await transport.close();
} catch (closeError) {
logger.debug(`Error closing transport for ${serverName}`, {
error: String(closeError),
});
}
}
throw error;
}
}
private async discoverTools(): Promise<void> {
this.tools = [];
const discoveryPromises = Array.from(this.clients.entries()).map(
async ([serverName, client]) => {
try {
const response = await client.listTools();
const tools = ListToolsResultSchema.parse(response).tools;
for (const tool of tools) {
this.tools.push({
name: `mcp__${serverName}__${tool.name}`,
description: tool.description || `Tool from ${serverName}`,
parameters: tool.inputSchema as {
type: string;
properties: Record<string, unknown>;
required?: string[];
},
serverName,
});
}
logger.info(
`Discovered ${tools.length} tools from server ${serverName}`,
{
tools: tools.map((t) => t.name),
},
);
} catch (error) {
logger.error(
`Failed to discover tools from server ${serverName}`,
error,
);
}
},
);
await Promise.allSettled(discoveryPromises);
}
getTools(): MCPTool[] {
return [...this.tools];
}
async callTool(
toolName: string,
args: Record<string, unknown>,
): Promise<string> {
if (!this.isInitialized) {
throw new Error("MCP client not initialized");
}
// Parse server name and tool name
const colonIndex = toolName.indexOf(":");
if (colonIndex === -1) {
throw new Error(
`Invalid MCP tool name format: ${toolName}. Expected format: serverName:toolName`,
);
}
const serverName = toolName.substring(0, colonIndex);
const actualToolName = toolName.substring(colonIndex + 1);
const client = this.clients.get(serverName);
if (!client) {
throw new Error(`MCP server not found: ${serverName}`);
}
try {
logger.info(
`Calling MCP tool ${actualToolName} on server ${serverName}`,
{ args },
);
const response = await client.callTool({
name: actualToolName,
arguments: args,
});
const result = CallToolResultSchema.parse(response);
// Process the tool result content
if (result.content && result.content.length > 0) {
const content = result.content[0];
if (!content) {
return `Tool ${toolName} executed successfully`;
}
if (content.type === "text") {
return (content as { text: string }).text;
} else if (content.type === "image") {
return `[Image result from ${toolName}]`;
} else if (content.type === "resource") {
const resource =
(content as { resource?: { uri?: string } }).resource;
return `[Resource result from ${toolName}: ${
resource?.uri || "unknown"
}]`;
}
}
return `Tool ${toolName} executed successfully`;
} catch (error) {
logger.error(`Error calling MCP tool ${toolName}`, error);
throw new Error(
`MCP tool call failed: ${
error instanceof Error ? error.message : String(error)
}`,
);
}
}
async close(): Promise<void> {
const closePromises = Array.from(this.clients.values()).map(
async (client) => {
try {
await client.close();
} catch (error) {
logger.debug("Error closing MCP client", { error: String(error) });
}
},
);
await Promise.allSettled(closePromises);
this.clients.clear();
this.tools = [];
this.isInitialized = false;
logger.info("MCP client closed");
}
}
// Global MCP client instance
let mcpClient: MCPClient | null = null;
export async function getMCPClient(): Promise<MCPClient> {
if (!mcpClient) {
mcpClient = new MCPClient();
await mcpClient.initialize();
}
return mcpClient;
}
export async function closeMCPClient(): Promise<void> {
if (mcpClient) {
await mcpClient.close();
mcpClient = null;
}
}