chore: refactor tool handling (#4510)
# Tool System Refactor
- Centralizes tool definitions and execution in `core/src/tools/*`:
specs (`spec.rs`), handlers (`handlers/*`), router (`router.rs`),
registry/dispatch (`registry.rs`), and shared context (`context.rs`).
One registry now builds the model-visible tool list and binds handlers.
- Router converts model responses to tool calls; Registry dispatches
with consistent telemetry via `codex-rs/otel` and unified error
handling. Function, Local Shell, MCP, and experimental `unified_exec`
all flow through this path; legacy shell aliases still work.
- Rationale: reduce per‑tool boilerplate, keep spec/handler in sync, and
make adding tools predictable and testable.
Example: `read_file`
- Spec: `core/src/tools/spec.rs` (see `create_read_file_tool`,
registered by `build_specs`).
- Handler: `core/src/tools/handlers/read_file.rs` (absolute `file_path`,
1‑indexed `offset`, `limit`, `L#: ` prefixes, safe truncation).
- E2E test: `core/tests/suite/read_file.rs` validates the tool returns
the requested lines.
## Next steps:
- Decompose `handle_container_exec_with_params`
- Add parallel tool calls
2025-10-03 13:21:06 +01:00
|
|
|
use async_trait::async_trait;
|
|
|
|
|
|
|
|
|
|
use crate::function_tool::FunctionCallError;
|
|
|
|
|
use crate::mcp_tool_call::handle_mcp_tool_call;
|
|
|
|
|
use crate::tools::context::ToolInvocation;
|
|
|
|
|
use crate::tools::context::ToolOutput;
|
|
|
|
|
use crate::tools::context::ToolPayload;
|
|
|
|
|
use crate::tools::registry::ToolHandler;
|
|
|
|
|
use crate::tools::registry::ToolKind;
|
|
|
|
|
|
|
|
|
|
pub struct McpHandler;
|
|
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
|
impl ToolHandler for McpHandler {
|
|
|
|
|
fn kind(&self) -> ToolKind {
|
|
|
|
|
ToolKind::Mcp
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 17:10:49 +01:00
|
|
|
async fn handle(&self, invocation: ToolInvocation) -> Result<ToolOutput, FunctionCallError> {
|
chore: refactor tool handling (#4510)
# Tool System Refactor
- Centralizes tool definitions and execution in `core/src/tools/*`:
specs (`spec.rs`), handlers (`handlers/*`), router (`router.rs`),
registry/dispatch (`registry.rs`), and shared context (`context.rs`).
One registry now builds the model-visible tool list and binds handlers.
- Router converts model responses to tool calls; Registry dispatches
with consistent telemetry via `codex-rs/otel` and unified error
handling. Function, Local Shell, MCP, and experimental `unified_exec`
all flow through this path; legacy shell aliases still work.
- Rationale: reduce per‑tool boilerplate, keep spec/handler in sync, and
make adding tools predictable and testable.
Example: `read_file`
- Spec: `core/src/tools/spec.rs` (see `create_read_file_tool`,
registered by `build_specs`).
- Handler: `core/src/tools/handlers/read_file.rs` (absolute `file_path`,
1‑indexed `offset`, `limit`, `L#: ` prefixes, safe truncation).
- E2E test: `core/tests/suite/read_file.rs` validates the tool returns
the requested lines.
## Next steps:
- Decompose `handle_container_exec_with_params`
- Add parallel tool calls
2025-10-03 13:21:06 +01:00
|
|
|
let ToolInvocation {
|
|
|
|
|
session,
|
|
|
|
|
sub_id,
|
|
|
|
|
call_id,
|
|
|
|
|
payload,
|
|
|
|
|
..
|
|
|
|
|
} = invocation;
|
|
|
|
|
|
|
|
|
|
let payload = match payload {
|
|
|
|
|
ToolPayload::Mcp {
|
|
|
|
|
server,
|
|
|
|
|
tool,
|
|
|
|
|
raw_arguments,
|
|
|
|
|
} => (server, tool, raw_arguments),
|
|
|
|
|
_ => {
|
|
|
|
|
return Err(FunctionCallError::RespondToModel(
|
|
|
|
|
"mcp handler received unsupported payload".to_string(),
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let (server, tool, raw_arguments) = payload;
|
|
|
|
|
let arguments_str = raw_arguments;
|
|
|
|
|
|
|
|
|
|
let response = handle_mcp_tool_call(
|
2025-10-05 17:10:49 +01:00
|
|
|
session.as_ref(),
|
|
|
|
|
&sub_id,
|
chore: refactor tool handling (#4510)
# Tool System Refactor
- Centralizes tool definitions and execution in `core/src/tools/*`:
specs (`spec.rs`), handlers (`handlers/*`), router (`router.rs`),
registry/dispatch (`registry.rs`), and shared context (`context.rs`).
One registry now builds the model-visible tool list and binds handlers.
- Router converts model responses to tool calls; Registry dispatches
with consistent telemetry via `codex-rs/otel` and unified error
handling. Function, Local Shell, MCP, and experimental `unified_exec`
all flow through this path; legacy shell aliases still work.
- Rationale: reduce per‑tool boilerplate, keep spec/handler in sync, and
make adding tools predictable and testable.
Example: `read_file`
- Spec: `core/src/tools/spec.rs` (see `create_read_file_tool`,
registered by `build_specs`).
- Handler: `core/src/tools/handlers/read_file.rs` (absolute `file_path`,
1‑indexed `offset`, `limit`, `L#: ` prefixes, safe truncation).
- E2E test: `core/tests/suite/read_file.rs` validates the tool returns
the requested lines.
## Next steps:
- Decompose `handle_container_exec_with_params`
- Add parallel tool calls
2025-10-03 13:21:06 +01:00
|
|
|
call_id.clone(),
|
|
|
|
|
server,
|
|
|
|
|
tool,
|
|
|
|
|
arguments_str,
|
|
|
|
|
)
|
|
|
|
|
.await;
|
|
|
|
|
|
|
|
|
|
match response {
|
|
|
|
|
codex_protocol::models::ResponseInputItem::McpToolCallOutput { result, .. } => {
|
|
|
|
|
Ok(ToolOutput::Mcp { result })
|
|
|
|
|
}
|
|
|
|
|
codex_protocol::models::ResponseInputItem::FunctionCallOutput { output, .. } => {
|
|
|
|
|
let codex_protocol::models::FunctionCallOutputPayload { content, success } = output;
|
|
|
|
|
Ok(ToolOutput::Function { content, success })
|
|
|
|
|
}
|
|
|
|
|
_ => Err(FunctionCallError::RespondToModel(
|
|
|
|
|
"mcp handler received unexpected response variant".to_string(),
|
|
|
|
|
)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|