codex-tools: introduce named tool definitions (#15953)

## Why

This continues the `codex-tools` migration by moving one more piece of
generic tool-definition bookkeeping out of `codex-core`.

The earlier extraction steps moved shared schema parsing into
`codex-tools`, but `core/src/tools/spec.rs` still had to supply tool
names separately and perform ad hoc rewrites for deferred MCP aliases.
That meant the crate boundary was still awkward: the parsed shape coming
back from `codex-tools` was missing part of the definition that
`codex-core` ultimately needs to assemble a `ResponsesApiTool`.

This change introduces a named `ToolDefinition` in `codex-tools` so both
MCP tools and dynamic tools cross the crate boundary in the same
reusable model. `codex-core` still owns the final `ResponsesApiTool`
assembly, but less of the generic tool-definition shaping logic stays
behind in `core`.

## What changed

- replaced `ParsedToolDefinition` with a named `ToolDefinition` in
`codex-rs/tools/src/tool_definition.rs`
- added `codex-rs/tools/src/tool_definition_tests.rs` for `renamed()`
and `into_deferred()`
- updated `parse_dynamic_tool()` and `parse_mcp_tool()` to return
`ToolDefinition`
- simplified `codex-rs/core/src/tools/spec.rs` so it adapts
`ToolDefinition` into `ResponsesApiTool` instead of rewriting names and
deferred fields inline
- updated parser tests and `codex-rs/tools/README.md` to reflect the
named tool-definition model

## Test plan

- `cargo test -p codex-tools`
- `cargo test -p codex-core --lib tools::spec::`
This commit is contained in:
Michael Bolin
2026-03-27 12:02:55 -07:00
committed by GitHub
parent 2616c7cf12
commit caee620a53
10 changed files with 109 additions and 54 deletions

View File

@@ -46,7 +46,7 @@ use codex_protocol::openai_models::WebSearchToolType;
use codex_protocol::protocol::SandboxPolicy;
use codex_protocol::protocol::SessionSource;
use codex_protocol::protocol::SubAgentSource;
use codex_tools::ParsedToolDefinition;
use codex_tools::ToolDefinition;
use codex_tools::parse_dynamic_tool;
use codex_tools::parse_mcp_tool;
use codex_utils_absolute_path::AbsolutePathBuf;
@@ -2386,9 +2386,8 @@ pub(crate) fn mcp_tool_to_openai_tool(
fully_qualified_name: String,
tool: rmcp::model::Tool,
) -> Result<ResponsesApiTool, serde_json::Error> {
Ok(parsed_tool_to_openai_tool(
fully_qualified_name,
parse_mcp_tool(&tool)?,
Ok(tool_definition_to_openai_tool(
parse_mcp_tool(&tool)?.renamed(fully_qualified_name),
))
}
@@ -2396,35 +2395,25 @@ pub(crate) fn mcp_tool_to_deferred_openai_tool(
name: String,
tool: rmcp::model::Tool,
) -> Result<ResponsesApiTool, serde_json::Error> {
let parsed_tool = parse_mcp_tool(&tool)?;
Ok(parsed_tool_to_openai_tool(
name,
ParsedToolDefinition {
output_schema: None,
defer_loading: true,
..parsed_tool
},
Ok(tool_definition_to_openai_tool(
parse_mcp_tool(&tool)?.renamed(name).into_deferred(),
))
}
fn dynamic_tool_to_openai_tool(
tool: &DynamicToolSpec,
) -> Result<ResponsesApiTool, serde_json::Error> {
Ok(parsed_tool_to_openai_tool(
tool.name.clone(),
parse_dynamic_tool(tool)?,
))
Ok(tool_definition_to_openai_tool(parse_dynamic_tool(tool)?))
}
fn parsed_tool_to_openai_tool(name: String, parsed_tool: ParsedToolDefinition) -> ResponsesApiTool {
fn tool_definition_to_openai_tool(tool_definition: ToolDefinition) -> ResponsesApiTool {
ResponsesApiTool {
name,
description: parsed_tool.description,
name: tool_definition.name,
description: tool_definition.description,
strict: false,
defer_loading: parsed_tool.defer_loading.then_some(true),
parameters: parsed_tool.input_schema,
output_schema: parsed_tool.output_schema,
defer_loading: tool_definition.defer_loading.then_some(true),
parameters: tool_definition.input_schema,
output_schema: tool_definition.output_schema,
}
}