Execute a tool and show the results.
(tm, tools)
| 135 | |
| 136 | |
| 137 | async def execute_tool_example(tm, tools): |
| 138 | """Execute a tool and show the results.""" |
| 139 | output.rule("[bold green]Executing Tool[/bold green]") |
| 140 | |
| 141 | if not tm or not tools: |
| 142 | output.error("No tools available to execute") |
| 143 | return |
| 144 | |
| 145 | # Find a suitable tool to execute |
| 146 | target_tool = None |
| 147 | |
| 148 | # Look for echo_message or similar simple tool |
| 149 | for tool in tools: |
| 150 | if "echo" in tool.name.lower() and "message" in tool.name.lower(): |
| 151 | target_tool = tool |
| 152 | break |
| 153 | |
| 154 | # Fallback to first available tool |
| 155 | if not target_tool: |
| 156 | target_tool = tools[0] |
| 157 | |
| 158 | output.info(f"Executing tool: {target_tool.name}") |
| 159 | |
| 160 | try: |
| 161 | # Prepare arguments based on tool name |
| 162 | arguments = {} |
| 163 | |
| 164 | # Add appropriate arguments based on tool name |
| 165 | if "echo" in target_tool.name.lower(): |
| 166 | arguments["message"] = "Hello from MCP CLI runtime server!" |
| 167 | output.info(" Sending message: 'Hello from MCP CLI runtime server!'") |
| 168 | elif "get" in target_tool.name.lower() or "list" in target_tool.name.lower(): |
| 169 | # For getter/lister tools, usually no args needed |
| 170 | output.info(" Executing with no arguments (getter/lister tool)") |
| 171 | else: |
| 172 | # For other tools, try common argument names |
| 173 | arguments["input"] = "test" |
| 174 | output.info(" Sending test input") |
| 175 | |
| 176 | # Execute the tool with correct signature |
| 177 | output.info(" Executing...") |
| 178 | result = await tm.execute_tool(target_tool.name, arguments) |
| 179 | |
| 180 | if result: |
| 181 | output.success("✅ Tool executed successfully!") |
| 182 | |
| 183 | # Format and display result |
| 184 | if isinstance(result, dict): |
| 185 | result_str = json.dumps(result, indent=2) |
| 186 | else: |
| 187 | result_str = str(result) |
| 188 | |
| 189 | output.print("\n[bold]Result:[/bold]") |
| 190 | if len(result_str) > 500: |
| 191 | output.print(result_str[:500] + "...") |
| 192 | else: |
| 193 | output.print(result_str) |
| 194 | else: |
no test coverage detected