Route a tool call to orchestrator handler or MCP. Checks orchestrator tools first (local, fast). Falls back to MCP for sandbox tools.
(
tool_name: str,
tool_args: dict[str, Any],
orchestrator_tools: list[OrchestratorTool],
mcp_client: Any,
)
| 429 | |
| 430 | |
| 431 | def dispatch_tool_call( |
| 432 | tool_name: str, |
| 433 | tool_args: dict[str, Any], |
| 434 | orchestrator_tools: list[OrchestratorTool], |
| 435 | mcp_client: Any, |
| 436 | ) -> str: |
| 437 | """Route a tool call to orchestrator handler or MCP. |
| 438 | |
| 439 | Checks orchestrator tools first (local, fast). Falls back to MCP |
| 440 | for sandbox tools. |
| 441 | """ |
| 442 | for tool in orchestrator_tools: |
| 443 | if tool.name == tool_name: |
| 444 | return tool.handler(tool_args) |
| 445 | # Not an orchestrator tool -> dispatch to MCP |
| 446 | result = mcp_client.invoke(tool_name, tool_args) |
| 447 | return ( |
| 448 | json.dumps(result, default=str) |
| 449 | if isinstance(result, (dict, list)) |
| 450 | else str(result) |
| 451 | ) |