@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.
111 lines (110 loc) • 5.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServerManager = void 0;
const lodash_es_1 = require("lodash-es");
const logging_js_1 = require("../logging.js");
const acquire_active_mcp_server_js_1 = require("./tools/acquire_active_mcp_server.js");
const add_server_from_config_js_1 = require("./tools/add_server_from_config.js");
const connect_mcp_server_js_1 = require("./tools/connect_mcp_server.js");
const list_mcp_servers_js_1 = require("./tools/list_mcp_servers.js");
const release_mcp_server_connection_js_1 = require("./tools/release_mcp_server_connection.js");
class ServerManager {
initializedServers = {};
serverTools = {};
client;
adapter;
activeServer = null;
overrideManagementTools;
constructor(client, adapter, managementTools) {
this.client = client;
this.adapter = adapter;
this.overrideManagementTools = managementTools;
}
setManagementTools(tools) {
this.overrideManagementTools = tools;
logging_js_1.logger.info(`Overriding default management tools with a new set of ${tools.length} tools.`);
}
logState(context) {
const allServerNames = this.client.getServerNames();
const activeSessionNames = Object.keys(this.client.getAllActiveSessions());
if (allServerNames.length === 0) {
logging_js_1.logger.info('Server Manager State: No servers configured.');
return;
}
const tableData = allServerNames.map(name => ({
'Server Name': name,
'Connected': activeSessionNames.includes(name) ? '✅' : '❌',
'Initialized': this.initializedServers[name] ? '✅' : '❌',
'Tool Count': this.serverTools[name]?.length ?? 0,
'Active': this.activeServer === name ? '✅' : '❌',
}));
logging_js_1.logger.info(`Server Manager State: [${context}]`);
console.table(tableData); // eslint-disable-line no-console
}
initialize() {
const serverNames = this.client.getServerNames?.();
if (serverNames.length === 0) {
logging_js_1.logger.warn('No MCP servers defined in client configuration');
}
}
async prefetchServerTools() {
const servers = this.client.getServerNames();
for (const serverName of servers) {
try {
let session = null;
session = this.client.getSession(serverName);
logging_js_1.logger.debug(`Using existing session for server '${serverName}' to prefetch tools.`);
if (!session) {
session = await this.client.createSession(serverName).catch((createSessionError) => {
logging_js_1.logger.warn(`Could not create session for '${serverName}' during prefetch: ${createSessionError}`);
return null;
});
logging_js_1.logger.debug(`Temporarily created session for '${serverName}' to prefetch tools.`);
}
if (session) {
const connector = session.connector;
let tools = [];
try {
tools = await this.adapter.createToolsFromConnectors([connector]);
}
catch (toolFetchError) {
logging_js_1.logger.error(`Failed to create tools from connector for server '${serverName}': ${toolFetchError}`);
continue;
}
const cachedTools = this.serverTools[serverName];
const toolsChanged = !cachedTools || !(0, lodash_es_1.isEqual)(cachedTools, tools);
if (toolsChanged) {
this.serverTools[serverName] = tools;
this.initializedServers[serverName] = true;
logging_js_1.logger.debug(`Prefetched ${tools.length} tools for server '${serverName}'.`);
}
else {
logging_js_1.logger.debug(`Tools for server '${serverName}' unchanged, using cached version.`);
}
}
}
catch (outerError) {
logging_js_1.logger.error(`Error prefetching tools for server '${serverName}': ${outerError}`);
}
}
}
get tools() {
if (logging_js_1.logger.level === 'debug') {
this.logState('Providing tools to agent');
}
const managementTools = this.overrideManagementTools ?? [
new add_server_from_config_js_1.AddMCPServerFromConfigTool(this),
new list_mcp_servers_js_1.ListMCPServersTool(this),
new connect_mcp_server_js_1.ConnectMCPServerTool(this),
new acquire_active_mcp_server_js_1.AcquireActiveMCPServerTool(this),
new release_mcp_server_connection_js_1.ReleaseMCPServerConnectionTool(this),
];
if (this.activeServer && this.serverTools[this.activeServer]) {
const activeTools = this.serverTools[this.activeServer];
logging_js_1.logger.debug(`Adding ${activeTools.length} tools from active server '${this.activeServer}'`);
return [...managementTools, ...activeTools];
}
return managementTools;
}
}
exports.ServerManager = ServerManager;