@l4t/mcp-ai
Version:
A set of tools for making integration and aggregation of MCP servers extremely easy.
45 lines • 1.82 kB
JavaScript
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { asyncMap } from 'modern-async';
import { McpClientConfigs, } from '../common/types.js';
import { createTransport } from '../common/libs.js';
const DEFAULT_MAX_PARALLEL_CALLS = 10;
const create = (config) => {
// eslint-disable-next-line functional/no-let
let clients = {};
// eslint-disable-next-line functional/no-let
let toolToClient = {};
const createClient = async (connection) => {
const transport = createTransport(connection);
const client = new Client(McpClientConfigs.integrator);
await client.connect(transport);
return client;
};
return {
connect: async () => {
clients = await Promise.all(config.mcps.map(async (mcp) => {
const client = await createClient(mcp.connection);
return [mcp.id, client];
})).then(Object.fromEntries);
},
getTools: async () => {
toolToClient = {};
const allTools = await asyncMap(Object.values(clients), async (client) => {
const tools = await client.listTools().then(x => x.tools);
tools.forEach(tool => {
// eslint-disable-next-line functional/immutable-data
toolToClient[tool.name] = client;
});
return tools;
}, config.maxParallelCalls || DEFAULT_MAX_PARALLEL_CALLS);
return allTools.flat();
},
executeTool: async (toolName, params) => {
const client = toolToClient[toolName];
return client
.callTool({ name: toolName, arguments: params })
.catch(() => []);
},
};
};
export { create };
//# sourceMappingURL=services.js.map