* 调用工具
(client: MCPClient, sessionId: string)
| 533 | * 调用工具 |
| 534 | */ |
| 535 | async function callTool(client: MCPClient, sessionId: string): Promise<void> { |
| 536 | try { |
| 537 | const tools = await client.listTools(sessionId); |
| 538 | |
| 539 | if (tools.length === 0) { |
| 540 | console.log(chalk.yellow('🔧 没有可用的工具')); |
| 541 | return; |
| 542 | } |
| 543 | |
| 544 | const { selectedTool } = await inquirer.prompt([ |
| 545 | { |
| 546 | type: 'list', |
| 547 | name: 'selectedTool', |
| 548 | message: '选择要调用的工具:', |
| 549 | choices: tools.map(tool => ({ |
| 550 | name: `${tool.name} - ${tool.description}`, |
| 551 | value: tool.name, |
| 552 | })), |
| 553 | }, |
| 554 | ]); |
| 555 | |
| 556 | const tool = tools.find(t => t.name === selectedTool)!; |
| 557 | const toolArgs: Record<string, any> = {}; |
| 558 | |
| 559 | // 收集工具参数 |
| 560 | const properties = tool.inputSchema.properties; |
| 561 | if (properties && Object.keys(properties).length > 0) { |
| 562 | console.log(chalk.blue('📝 请输入工具参数:')); |
| 563 | |
| 564 | for (const [key] of Object.entries(properties)) { |
| 565 | const isRequired = tool.inputSchema.required?.includes(key); |
| 566 | const { value } = await inquirer.prompt([ |
| 567 | { |
| 568 | type: 'input', |
| 569 | name: 'value', |
| 570 | message: `${key}${isRequired ? ' (必需)' : ''}:`, |
| 571 | validate: input => { |
| 572 | if (isRequired && !input.trim()) { |
| 573 | return `${key} 是必需参数`; |
| 574 | } |
| 575 | return true; |
| 576 | }, |
| 577 | }, |
| 578 | ]); |
| 579 | |
| 580 | if (value.trim()) { |
| 581 | toolArgs[key] = value; |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | console.log(chalk.blue('⚡ 调用工具...')); |
| 587 | const result = await client.callTool(sessionId, { |
| 588 | name: selectedTool, |
| 589 | arguments: toolArgs, |
| 590 | }); |
| 591 | |
| 592 | console.log(chalk.green('✅ 工具调用成功:')); |
no test coverage detected