import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
async function runTool() {
const toolName = process.argv[2];
const toolArgsRaw = process.argv[3];
if (!toolName) {
console.error('Usage: npx tsx tests/run_tool.ts <tool_name> [json_args]');
process.exit(1);
}
const toolArgs = toolArgsRaw ? JSON.parse(toolArgsRaw) : {};
const serverPath = path.resolve(__dirname, '../dist/index.js');
const transport = new StdioClientTransport({
command: 'node',
args: [serverPath],
});
const client = new Client(
{ name: 'mcp-tester', version: '0.0.1' },
{ capabilities: {} }
);
await client.connect(transport);
try {
const result = await client.callTool({
name: toolName,
arguments: toolArgs,
});
if (result.content && Array.isArray(result.content)) {
result.content.forEach((item: any) => {
if (item.type === 'text') {
console.log(item.text);
} else {
console.log(JSON.stringify(item));
}
});
} else {
console.log(JSON.stringify(result));
}
} catch (error: any) {
console.error(`Error: ${error.message}`);
}
await client.close();
}
runTool().catch(console.error);