hataraku
Version:
An autonomous coding agent for building AI-powered development tools. The name "Hataraku" (働く) means "to work" in Japanese.
70 lines • 2.52 kB
JavaScript
import { jsonSchema } from 'ai';
import { McpClient } from "./mcp-client";
import { isMcpConfig } from './config';
import { readFile } from 'node:fs/promises';
/**
* Creates a set of tools from MCP servers that can be passed to an Agent
*/
export async function getMcpTools(options) {
const client = new McpClient();
// If config provided, validate and use it directly
if (options?.config) {
if (!isMcpConfig(options.config)) {
throw new Error('Invalid config object provided');
}
await client.loadConfig(options.config);
}
// If configPath provided, load and validate from that path
else if (options?.configPath) {
const configContent = await readFile(options.configPath, 'utf-8');
let config;
try {
config = JSON.parse(configContent);
}
catch (error) {
throw new Error('Failed to parse config file');
}
if (!isMcpConfig(config)) {
throw new Error('Invalid config file format');
}
await client.loadConfigFromPath(options.configPath);
}
// Otherwise use default path
else {
await client.initializeServers();
}
const toolset = {};
const servers = client.getAvailableServers();
for (const serverName of servers) {
const { tools } = await client.getServerTools(serverName);
const serverConfig = options?.config?.mcpServers[serverName];
for (const tool of tools) {
// Skip if tool is in disabled list
if (serverConfig?.disabledTools?.includes(tool.name)) {
continue;
}
const qualifiedName = `${serverName}_${tool.name}`;
toolset[qualifiedName] = {
name: tool.name,
description: tool.description,
parameters: jsonSchema(tool.inputSchema),
execute: async (args) => {
const resultPromise = client.callTool(serverName, tool.name, args);
if (options?.onToolCall) {
options.onToolCall(serverName, tool.name, args, resultPromise);
}
return resultPromise;
},
};
}
}
return {
tools: toolset,
disconnect: async () => {
for (const serverName of servers) {
await client.disconnectServer(serverName);
}
}
};
}
//# sourceMappingURL=toolWrapper.js.map