@agentpaid/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.
51 lines (50 loc) • 2.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConnectMCPServerTool = void 0;
const zod_1 = require("zod");
const logging_js_1 = require("../../logging.js");
const base_js_1 = require("./base.js");
const ConnectMCPServerSchema = zod_1.z.object({
serverName: zod_1.z.string().describe('The name of the MCP server.'),
});
class ConnectMCPServerTool extends base_js_1.MCPServerTool {
name = 'connect_to_mcp_server';
description = 'Connect to a specific MCP (Model Context Protocol) server to use its tools. Use this tool to connect to a specific server and use its tools.';
schema = ConnectMCPServerSchema;
constructor(manager) {
super(manager);
}
async _call({ serverName }) {
const serverNames = this.manager.client.getServerNames();
if (!serverNames.includes(serverName)) {
const available = serverNames.length > 0 ? serverNames.join(', ') : 'none';
return `Server '${serverName}' not found. Available servers: ${available}`;
}
if (this.manager.activeServer === serverName) {
return `Already connected to MCP server '${serverName}'`;
}
try {
let session = this.manager.client.getSession(serverName);
logging_js_1.logger.debug(`Using existing session for server '${serverName}'`);
if (!session) {
logging_js_1.logger.debug(`Creating new session for server '${serverName}'`);
session = await this.manager.client.createSession(serverName);
}
this.manager.activeServer = serverName;
if (this.manager.serverTools[serverName]) {
const connector = session.connector;
const tools = await this.manager.adapter.createToolsFromConnectors([connector]);
this.manager.serverTools[serverName] = tools;
this.manager.initializedServers[serverName] = true;
}
const serverTools = this.manager.serverTools[serverName] || [];
const numTools = serverTools.length;
return `Connected to MCP server '${serverName}'. ${numTools} tools are now available.`;
}
catch (error) {
logging_js_1.logger.error(`Error connecting to server '${serverName}': ${String(error)}`);
return `Failed to connect to server '${serverName}': ${String(error)}`;
}
}
}
exports.ConnectMCPServerTool = ConnectMCPServerTool;