mcp-use
Version:
A utility library for integrating Model Context Protocol (MCP) with LangChain, Zod, and related tools. Provides helpers for schema conversion, event streaming, and SDK usage.
41 lines (40 loc) • 1.98 kB
JavaScript
import { StructuredTool } from 'langchain/tools';
import { z } from 'zod';
import { logger } from '../../logging.js';
export class AddMCPServerFromConfigTool extends StructuredTool {
name = 'add_mcp_server_from_config';
description = 'Adds a new MCP server to the client from a configuration object and connects to it, making its tools available.';
schema = z.object({
serverName: z.string().describe('The name for the new MCP server.'),
serverConfig: z
.any()
.describe('The configuration object for the server. This should not include the top-level "mcpServers" key.'),
});
manager;
constructor(manager) {
super();
this.manager = manager;
}
async _call({ serverName, serverConfig, }) {
try {
this.manager.client.addServer(serverName, serverConfig);
let result = `Server '${serverName}' added to the client.`;
logger.debug(`Connecting to new server '${serverName}' and discovering tools.`);
const session = await this.manager.client.createSession(serverName);
const connector = session.connector;
const tools = await this.manager.adapter.createToolsFromConnectors([connector]);
this.manager.serverTools[serverName] = tools;
this.manager.initializedServers[serverName] = true;
this.manager.activeServer = serverName; // Set as active server
const numTools = tools.length;
result += ` Session created and connected. '${serverName}' is now the active server with ${numTools} tools available.`;
result += `\n\n${tools.map(t => t.name).join('\n')}`;
logger.info(result);
return result;
}
catch (e) {
logger.error(`Failed to add or connect to server '${serverName}': ${e.message}`);
return `Failed to add or connect to server '${serverName}': ${e.message}`;
}
}
}