UNPKG

yszl-mcp

Version:

Scenic Area Tourist Flow Query Server with official MCP SDK

114 lines (99 loc) 3.09 kB
#!/usr/bin/env node /** * 测试脚本,用于向stdio模式的MCP服务发送JSON-RPC 2.0格式请求 */ import { spawn } from 'child_process'; import path from 'path'; import { fileURLToPath } from 'url'; // Get current directory const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); // 启动MCP服务 const mcp = spawn('node', [path.join(__dirname, 'bin/index.js')], { stdio: ['pipe', 'pipe', process.stderr] }); let serverInitialized = false; // 处理MCP服务的输出 mcp.stdout.on('data', (data) => { try { const responses = data.toString().trim().split('\n'); for (const response of responses) { if (!response) continue; const parsed = JSON.parse(response); console.log('收到响应:', JSON.stringify(parsed, null, 2)); // 检测服务器初始化消息 if (parsed.method === 'mcp.initialize' || (parsed.result && parsed.result.capabilities)) { serverInitialized = true; console.log('服务器初始化完成,可用工具:'); const tools = parsed.params ? parsed.params.capabilities.tools : parsed.result.capabilities.tools; tools.forEach(tool => { console.log(` - ${tool.name}: ${tool.description}`); }); // 收到初始化后发送ping setTimeout(() => { console.log('发送ping请求...'); mcp.stdin.write(JSON.stringify({ jsonrpc: "2.0", method: "mcp.ping", id: "ping-1" }) + '\n'); }, 500); } // 收到ping响应后测试工具调用 if (parsed.id === 'ping-1' && parsed.result) { setTimeout(() => { testToolCalls(); }, 500); } } } catch (error) { console.error('解析响应出错:', error.message); console.error('原始响应:', data.toString()); } }); // 测试工具调用 function testToolCalls() { console.log('测试工具调用...'); // 测试实时客流工具 console.log('调用 TouristFlow 工具...'); mcp.stdin.write(JSON.stringify({ jsonrpc: "2.0", method: "mcp.tool_call", id: "call-1", params: { name: "TouristFlow", args: { scenicId: "F51018268" } } }) + '\n'); // 延迟测试累计客流工具 setTimeout(() => { console.log('调用 CumulativeTouristFlow 工具...'); mcp.stdin.write(JSON.stringify({ jsonrpc: "2.0", method: "mcp.tool_call", id: "call-2", params: { name: "CumulativeTouristFlow", args: { scenicId: "F51018268" } } }) + '\n'); }, 1000); // 延迟测试日客流工具 setTimeout(() => { console.log('调用 DailyTouristFlow 工具...'); mcp.stdin.write(JSON.stringify({ jsonrpc: "2.0", method: "mcp.tool_call", id: "call-3", params: { name: "DailyTouristFlow", args: { scenicId: "F51018268", date: "2025-04-01" } } }) + '\n'); }, 2000); } // 处理退出 process.on('SIGINT', () => { mcp.kill(); process.exit(); });