|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Neo.mjs MCP Client CLI |
| 5 | + * |
| 6 | + * This script provides a command-line interface to interact with MCP servers |
| 7 | + * using the Neo.ai.mcp.client.Client. |
| 8 | + * |
| 9 | + * Usage: |
| 10 | + * node ai/mcp/client/mcp-cli.mjs --server <serverName> --list-tools |
| 11 | + * node ai/mcp/client/mcp-cli.mjs --server <serverName> --call-tool <toolName> --args '{"key": "value"}' |
| 12 | + */ |
| 13 | + |
| 14 | +import { Command } from 'commander'; |
| 15 | +import Neo from '../../../src/Neo.mjs'; |
| 16 | +import * as core from '../../../src/core/_export.mjs'; // For Neo.core.Base setup |
| 17 | +import InstanceManager from '../../../src/manager/Instance.mjs'; // For Neo.core.Base setup |
| 18 | +import Client from './Client.mjs'; |
| 19 | +import ClientConfig from './config.mjs'; |
| 20 | +import path from 'path'; |
| 21 | +import { fileURLToPath } from 'url'; |
| 22 | +import dotenv from 'dotenv'; |
| 23 | + |
| 24 | +// Load environment variables from project root |
| 25 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 26 | +dotenv.config({ path: path.resolve(__dirname, '../../../.env') }); |
| 27 | + |
| 28 | +const program = new Command(); |
| 29 | + |
| 30 | +program |
| 31 | + .name('mcp-cli') |
| 32 | + .description('CLI for interacting with MCP servers') |
| 33 | + .requiredOption('-s, --server <name>', 'Logical name of the MCP server to connect to (e.g., github-workflow)') |
| 34 | + .option('-l, --list-tools', 'List available tools on the server') |
| 35 | + .option('-c, --call-tool <toolName>', 'Name of the tool to call') |
| 36 | + .option('-a, --args <json>', 'JSON string of arguments for --call-tool', '{}') |
| 37 | + .option('-d, --debug', 'Enable debug logging'); |
| 38 | + |
| 39 | +program.parse(process.argv); |
| 40 | + |
| 41 | +const options = program.opts(); |
| 42 | + |
| 43 | +async function run() { |
| 44 | + console.log(`🤖 MCP Client CLI starting for server: ${options.server}`); |
| 45 | + console.log('GH_TOKEN present:', !!process.env.GH_TOKEN); // Debug info |
| 46 | + |
| 47 | + const mcpClient = Neo.create(Client, { |
| 48 | + clientName: 'Neo.ai.MCP.CLI', |
| 49 | + serverName: options.server, |
| 50 | + env: process.env // Pass environment variables |
| 51 | + }); |
| 52 | + |
| 53 | + try { |
| 54 | + await mcpClient.connect(); |
| 55 | + console.log(`✅ Connected to ${options.server} MCP server.`); |
| 56 | + |
| 57 | + if (options.listTools) { |
| 58 | + console.log('\n🛠️ Listing tools...'); |
| 59 | + const tools = await mcpClient.listTools(); |
| 60 | + tools.forEach(tool => { |
| 61 | + console.log(` - ${tool.name}: ${tool.description || 'No description.'}`); |
| 62 | + }); |
| 63 | + console.log(`\nFound ${tools.length} tools.`); |
| 64 | + } else if (options.callTool) { |
| 65 | + console.log(`\nCalling tool: ${options.callTool} with args: ${options.args}`); |
| 66 | + const args = JSON.parse(options.args); |
| 67 | + const result = await mcpClient.callTool(options.callTool, args); |
| 68 | + |
| 69 | + if (result.isError) { |
| 70 | + console.error(`\n❌ Tool call failed:`); |
| 71 | + result.content.forEach(c => console.error(c.text)); |
| 72 | + } else { |
| 73 | + console.log(`\n✅ Tool call successful:`); |
| 74 | + result.content.forEach(c => console.log(c.text)); |
| 75 | + } |
| 76 | + } else { |
| 77 | + console.log('\n🤷 No action specified. Use --list-tools or --call-tool.'); |
| 78 | + } |
| 79 | + |
| 80 | + } catch (error) { |
| 81 | + console.error('\n💥 MCP CLI Error:', error.message); |
| 82 | + process.exit(1); |
| 83 | + } finally { |
| 84 | + await mcpClient.close(); |
| 85 | + console.log('\n🔌 Connection closed.'); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +run(); |
0 commit comments