Demonstrate tool execution in interactive mode.
()
| 88 | |
| 89 | |
| 90 | async def demo_tool_execution(): |
| 91 | """Demonstrate tool execution in interactive mode.""" |
| 92 | |
| 93 | # Initialize |
| 94 | output.rule("Tool Execution Demo - Interactive Mode", style="bold cyan") |
| 95 | output.info("Demonstrating the new 'execute' command for running MCP tools") |
| 96 | |
| 97 | # Register all commands |
| 98 | register_all_commands() |
| 99 | |
| 100 | # Initialize context with mock tool manager |
| 101 | initialize_context() |
| 102 | context = get_context() |
| 103 | |
| 104 | # Create mock tool manager with tools |
| 105 | tool_manager = Mock(spec=ToolManager) |
| 106 | tool_manager.tools = create_mock_tools() |
| 107 | |
| 108 | # Mock the execute_tool method |
| 109 | async def mock_execute(tool_name, arguments, server_index=0): |
| 110 | if tool_name == "echo": |
| 111 | return {"echoed": arguments.get("message", "")} |
| 112 | elif tool_name == "calculate": |
| 113 | op = arguments.get("operation") |
| 114 | a = arguments.get("a", 0) |
| 115 | b = arguments.get("b", 0) |
| 116 | if op == "add": |
| 117 | return {"result": a + b} |
| 118 | elif op == "subtract": |
| 119 | return {"result": a - b} |
| 120 | elif op == "multiply": |
| 121 | return {"result": a * b} |
| 122 | elif op == "divide": |
| 123 | return {"result": a / b if b != 0 else "Error: Division by zero"} |
| 124 | elif tool_name == "query_database": |
| 125 | return { |
| 126 | "rows": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}], |
| 127 | "count": 2, |
| 128 | } |
| 129 | elif tool_name == "list_files": |
| 130 | return {"files": ["file1.txt", "file2.py", "data.json"]} |
| 131 | return {"error": "Unknown tool"} |
| 132 | |
| 133 | tool_manager.execute_tool = mock_execute |
| 134 | |
| 135 | # Update context with our mock tool manager |
| 136 | context.tool_manager = tool_manager |
| 137 | |
| 138 | # Demo 1: List available tools |
| 139 | output.rule("Demo 1: List Available Tools") |
| 140 | output.info("Command: execute") |
| 141 | await InteractiveCommandAdapter.handle_command("execute") |
| 142 | |
| 143 | await asyncio.sleep(1) |
| 144 | |
| 145 | # Demo 2: Show tool details |
| 146 | output.rule("Demo 2: Show Tool Details") |
| 147 | output.info("Command: execute echo") |
no test coverage detected