Skip to content

Commit 16d4ea9

Browse files
authored
codex-tools: extract responses API tool models (#16031)
## Why The previous extraction steps moved shared tool-schema parsing into `codex-tools`, but `codex-core` still owned the generic Responses API tool models and the last adapter layer that turned parsed tool definitions into `ResponsesApiTool` values. That left `core/src/tools/spec.rs` and `core/src/client_common.rs` holding a chunk of tool-shaping code that does not need session state, runtime plumbing, or any other `codex-core`-specific dependency. As a result, `codex-tools` owned the parsed tool definition, but `codex-core` still owned the generic wire model that those definitions are converted into. This change moves that boundary one step further. `codex-tools` now owns the reusable Responses/tool wire structs and the shared conversion helpers for dynamic tools, MCP tools, and deferred MCP aliases. `codex-core` continues to own `ToolSpec` orchestration and the remaining web-search-specific request shapes. ## What changed - added `tools/src/responses_api.rs` to own `ResponsesApiTool`, `FreeformTool`, `ToolSearchOutputTool`, namespace output types, and the shared `ToolDefinition -> ResponsesApiTool` adapter helpers - added `tools/src/responses_api_tests.rs` for deferred-loading behavior, adapter coverage, and namespace serialization coverage - rewired `core/src/tools/spec.rs` to use the extracted dynamic/MCP adapter helpers instead of defining those conversions locally - rewired `core/src/tools/handlers/tool_search.rs` to use the extracted deferred MCP adapter and namespace output types directly - slimmed `core/src/client_common.rs` so it now keeps `ToolSpec` and the web-search-specific wire types, while reusing the extracted tool models from `codex-tools` - moved the extracted seam tests out of `core` and updated `codex-rs/tools/README.md` plus `tools/src/lib.rs` to reflect the expanded `codex-tools` boundary ## Test plan - `cargo test -p codex-tools` - `cargo test -p codex-core --lib tools::spec::` - `cargo test -p codex-core --lib tools::handlers::tool_search::` - `just fix -p codex-tools -p codex-core` - `just argument-comment-lint` ## References - [#15923](#15923) `codex-tools: extract shared tool schema parsing` - [#15928](#15928) `codex-tools: extract MCP schema adapters` - [#15944](#15944) `codex-tools: extract dynamic tool adapters` - [#15953](#15953) `codex-tools: introduce named tool definitions`
1 parent 82e8031 commit 16d4ea9

File tree

10 files changed

+320
-217
lines changed

10 files changed

+320
-217
lines changed

codex-rs/core/src/client_common.rs

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,16 @@ fn strip_total_output_header(output: &str) -> Option<(&str, u32)> {
157157
}
158158

159159
pub(crate) mod tools {
160-
use crate::tools::spec::JsonSchema;
161160
use codex_protocol::config_types::WebSearchContextSize;
162161
use codex_protocol::config_types::WebSearchFilters as ConfigWebSearchFilters;
163162
use codex_protocol::config_types::WebSearchUserLocation as ConfigWebSearchUserLocation;
164163
use codex_protocol::config_types::WebSearchUserLocationType;
165-
use serde::Deserialize;
164+
pub(crate) use codex_tools::FreeformTool;
165+
pub(crate) use codex_tools::FreeformToolFormat;
166+
use codex_tools::JsonSchema;
167+
pub(crate) use codex_tools::ResponsesApiTool;
168+
pub(crate) use codex_tools::ToolSearchOutputTool;
166169
use serde::Serialize;
167-
use serde_json::Value;
168170

169171
/// When serialized as JSON, this produces a valid "Tool" in the OpenAI
170172
/// Responses API.
@@ -256,59 +258,6 @@ pub(crate) mod tools {
256258
}
257259
}
258260
}
259-
260-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
261-
pub struct FreeformTool {
262-
pub(crate) name: String,
263-
pub(crate) description: String,
264-
pub(crate) format: FreeformToolFormat,
265-
}
266-
267-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
268-
pub struct FreeformToolFormat {
269-
pub(crate) r#type: String,
270-
pub(crate) syntax: String,
271-
pub(crate) definition: String,
272-
}
273-
274-
#[derive(Debug, Clone, Serialize, PartialEq)]
275-
pub struct ResponsesApiTool {
276-
pub(crate) name: String,
277-
pub(crate) description: String,
278-
/// TODO: Validation. When strict is set to true, the JSON schema,
279-
/// `required` and `additional_properties` must be present. All fields in
280-
/// `properties` must be present in `required`.
281-
pub(crate) strict: bool,
282-
#[serde(skip_serializing_if = "Option::is_none")]
283-
pub(crate) defer_loading: Option<bool>,
284-
pub(crate) parameters: JsonSchema,
285-
#[serde(skip)]
286-
pub(crate) output_schema: Option<Value>,
287-
}
288-
289-
#[derive(Debug, Clone, Serialize, PartialEq)]
290-
#[serde(tag = "type")]
291-
pub(crate) enum ToolSearchOutputTool {
292-
#[allow(dead_code)]
293-
#[serde(rename = "function")]
294-
Function(ResponsesApiTool),
295-
#[serde(rename = "namespace")]
296-
Namespace(ResponsesApiNamespace),
297-
}
298-
299-
#[derive(Debug, Clone, Serialize, PartialEq)]
300-
pub(crate) struct ResponsesApiNamespace {
301-
pub(crate) name: String,
302-
pub(crate) description: String,
303-
pub(crate) tools: Vec<ResponsesApiNamespaceTool>,
304-
}
305-
306-
#[derive(Debug, Clone, Serialize, PartialEq)]
307-
#[serde(tag = "type")]
308-
pub(crate) enum ResponsesApiNamespaceTool {
309-
#[serde(rename = "function")]
310-
Function(ResponsesApiTool),
311-
}
312261
}
313262

314263
pub struct ResponseStream {

codex-rs/core/src/client_common_tests.rs

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -197,49 +197,3 @@ fn reserializes_shell_outputs_for_function_and_custom_tool_calls() {
197197
]
198198
);
199199
}
200-
201-
#[test]
202-
fn tool_search_output_namespace_serializes_with_deferred_child_tools() {
203-
let namespace = tools::ToolSearchOutputTool::Namespace(tools::ResponsesApiNamespace {
204-
name: "mcp__codex_apps__calendar".to_string(),
205-
description: "Plan events".to_string(),
206-
tools: vec![tools::ResponsesApiNamespaceTool::Function(
207-
tools::ResponsesApiTool {
208-
name: "create_event".to_string(),
209-
description: "Create a calendar event.".to_string(),
210-
strict: false,
211-
defer_loading: Some(true),
212-
parameters: crate::tools::spec::JsonSchema::Object {
213-
properties: Default::default(),
214-
required: None,
215-
additional_properties: None,
216-
},
217-
output_schema: None,
218-
},
219-
)],
220-
});
221-
222-
let value = serde_json::to_value(namespace).expect("serialize namespace");
223-
224-
assert_eq!(
225-
value,
226-
serde_json::json!({
227-
"type": "namespace",
228-
"name": "mcp__codex_apps__calendar",
229-
"description": "Plan events",
230-
"tools": [
231-
{
232-
"type": "function",
233-
"name": "create_event",
234-
"description": "Create a calendar event.",
235-
"strict": false,
236-
"defer_loading": true,
237-
"parameters": {
238-
"type": "object",
239-
"properties": {}
240-
}
241-
}
242-
]
243-
})
244-
);
245-
}

codex-rs/core/src/tools/handlers/tool_search.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1-
use crate::client_common::tools::ResponsesApiNamespace;
2-
use crate::client_common::tools::ResponsesApiNamespaceTool;
3-
use crate::client_common::tools::ToolSearchOutputTool;
41
use crate::function_tool::FunctionCallError;
52
use crate::mcp_connection_manager::ToolInfo;
63
use crate::tools::context::ToolInvocation;
74
use crate::tools::context::ToolPayload;
85
use crate::tools::context::ToolSearchOutput;
96
use crate::tools::registry::ToolHandler;
107
use crate::tools::registry::ToolKind;
11-
use crate::tools::spec::mcp_tool_to_deferred_openai_tool;
128
use async_trait::async_trait;
139
use bm25::Document;
1410
use bm25::Language;
1511
use bm25::SearchEngineBuilder;
12+
use codex_tools::ResponsesApiNamespace;
13+
use codex_tools::ResponsesApiNamespaceTool;
14+
use codex_tools::ToolSearchOutputTool;
15+
use codex_tools::mcp_tool_to_deferred_responses_api_tool;
1616
use std::collections::BTreeMap;
1717
use std::collections::HashMap;
1818

19-
#[cfg(test)]
20-
use crate::client_common::tools::ResponsesApiTool;
21-
2219
pub struct ToolSearchHandler {
2320
tools: HashMap<String, ToolInfo>,
2421
}
@@ -129,7 +126,7 @@ fn serialize_tool_search_output_tools(
129126
let tools = tools
130127
.iter()
131128
.map(|tool| {
132-
mcp_tool_to_deferred_openai_tool(tool.tool_name.clone(), tool.tool.clone())
129+
mcp_tool_to_deferred_responses_api_tool(tool.tool_name.clone(), &tool.tool)
133130
.map(ResponsesApiNamespaceTool::Function)
134131
})
135132
.collect::<Result<Vec<_>, _>>()?;

codex-rs/core/src/tools/handlers/tool_search_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::*;
22
use crate::mcp::CODEX_APPS_MCP_SERVER_NAME;
3+
use codex_tools::ResponsesApiTool;
34
use pretty_assertions::assert_eq;
45
use rmcp::model::JsonObject;
56
use rmcp::model::Tool;

codex-rs/core/src/tools/spec.rs

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
use crate::client_common::tools::FreeformTool;
2-
use crate::client_common::tools::FreeformToolFormat;
3-
use crate::client_common::tools::ResponsesApiTool;
41
use crate::client_common::tools::ToolSpec;
52
use crate::config::AgentRoleConfig;
63
use crate::mcp::CODEX_APPS_MCP_SERVER_NAME;
@@ -46,9 +43,11 @@ use codex_protocol::openai_models::WebSearchToolType;
4643
use codex_protocol::protocol::SandboxPolicy;
4744
use codex_protocol::protocol::SessionSource;
4845
use codex_protocol::protocol::SubAgentSource;
49-
use codex_tools::ToolDefinition;
50-
use codex_tools::parse_dynamic_tool;
51-
use codex_tools::parse_mcp_tool;
46+
use codex_tools::FreeformTool;
47+
use codex_tools::FreeformToolFormat;
48+
use codex_tools::ResponsesApiTool;
49+
use codex_tools::dynamic_tool_to_responses_api_tool;
50+
use codex_tools::mcp_tool_to_responses_api_tool;
5251
use codex_utils_absolute_path::AbsolutePathBuf;
5352
use codex_utils_template::Template;
5453
use serde::Deserialize;
@@ -2382,41 +2381,6 @@ fn push_tool_spec(
23822381
}
23832382
}
23842383

2385-
pub(crate) fn mcp_tool_to_openai_tool(
2386-
fully_qualified_name: String,
2387-
tool: rmcp::model::Tool,
2388-
) -> Result<ResponsesApiTool, serde_json::Error> {
2389-
Ok(tool_definition_to_openai_tool(
2390-
parse_mcp_tool(&tool)?.renamed(fully_qualified_name),
2391-
))
2392-
}
2393-
2394-
pub(crate) fn mcp_tool_to_deferred_openai_tool(
2395-
name: String,
2396-
tool: rmcp::model::Tool,
2397-
) -> Result<ResponsesApiTool, serde_json::Error> {
2398-
Ok(tool_definition_to_openai_tool(
2399-
parse_mcp_tool(&tool)?.renamed(name).into_deferred(),
2400-
))
2401-
}
2402-
2403-
fn dynamic_tool_to_openai_tool(
2404-
tool: &DynamicToolSpec,
2405-
) -> Result<ResponsesApiTool, serde_json::Error> {
2406-
Ok(tool_definition_to_openai_tool(parse_dynamic_tool(tool)?))
2407-
}
2408-
2409-
fn tool_definition_to_openai_tool(tool_definition: ToolDefinition) -> ResponsesApiTool {
2410-
ResponsesApiTool {
2411-
name: tool_definition.name,
2412-
description: tool_definition.description,
2413-
strict: false,
2414-
defer_loading: tool_definition.defer_loading.then_some(true),
2415-
parameters: tool_definition.input_schema,
2416-
output_schema: tool_definition.output_schema,
2417-
}
2418-
}
2419-
24202384
/// Builds the tool registry builder while collecting tool specs for later serialization.
24212385
#[cfg(test)]
24222386
pub(crate) fn build_specs(
@@ -2911,7 +2875,7 @@ pub(crate) fn build_specs_with_discoverable_tools(
29112875
entries.sort_by(|a, b| a.0.cmp(&b.0));
29122876

29132877
for (name, tool) in entries.into_iter() {
2914-
match mcp_tool_to_openai_tool(name.clone(), tool.clone()) {
2878+
match mcp_tool_to_responses_api_tool(name.clone(), &tool) {
29152879
Ok(converted_tool) => {
29162880
push_tool_spec(
29172881
&mut builder,
@@ -2930,7 +2894,7 @@ pub(crate) fn build_specs_with_discoverable_tools(
29302894

29312895
if !dynamic_tools.is_empty() {
29322896
for tool in dynamic_tools {
2933-
match dynamic_tool_to_openai_tool(tool) {
2897+
match dynamic_tool_to_responses_api_tool(tool) {
29342898
Ok(converted_tool) => {
29352899
push_tool_spec(
29362900
&mut builder,

codex-rs/core/src/tools/spec_tests.rs

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ use crate::tools::ToolRouter;
88
use crate::tools::registry::ConfiguredToolSpec;
99
use crate::tools::router::ToolRouterParams;
1010
use codex_app_server_protocol::AppInfo;
11-
use codex_protocol::dynamic_tools::DynamicToolSpec;
1211
use codex_protocol::openai_models::InputModality;
1312
use codex_protocol::openai_models::ModelInfo;
1413
use codex_protocol::openai_models::ModelsResponse;
1514
use codex_tools::AdditionalProperties;
15+
use codex_tools::mcp_tool_to_deferred_responses_api_tool;
1616
use codex_utils_absolute_path::AbsolutePathBuf;
1717
use pretty_assertions::assert_eq;
1818
use std::path::PathBuf;
@@ -64,28 +64,6 @@ fn search_capable_model_info() -> ModelInfo {
6464
model_info
6565
}
6666

67-
#[test]
68-
fn search_tool_deferred_tools_always_set_defer_loading_true() {
69-
let tool = mcp_tool(
70-
"lookup_order",
71-
"Look up an order",
72-
serde_json::json!({
73-
"type": "object",
74-
"properties": {
75-
"order_id": {"type": "string"}
76-
},
77-
"required": ["order_id"],
78-
"additionalProperties": false,
79-
}),
80-
);
81-
82-
let openai_tool =
83-
mcp_tool_to_deferred_openai_tool("mcp__codex_apps__lookup_order".to_string(), tool)
84-
.expect("convert deferred tool");
85-
86-
assert_eq!(openai_tool.defer_loading, Some(true));
87-
}
88-
8967
#[test]
9068
fn deferred_responses_api_tool_serializes_with_defer_loading() {
9169
let tool = mcp_tool(
@@ -102,7 +80,7 @@ fn deferred_responses_api_tool_serializes_with_defer_loading() {
10280
);
10381

10482
let serialized = serde_json::to_value(ToolSpec::Function(
105-
mcp_tool_to_deferred_openai_tool("mcp__codex_apps__lookup_order".to_string(), tool)
83+
mcp_tool_to_deferred_responses_api_tool("mcp__codex_apps__lookup_order".to_string(), &tool)
10684
.expect("convert deferred tool"),
10785
))
10886
.expect("serialize deferred tool");
@@ -127,44 +105,6 @@ fn deferred_responses_api_tool_serializes_with_defer_loading() {
127105
);
128106
}
129107

130-
#[test]
131-
fn dynamic_tool_preserves_defer_loading() {
132-
let tool = DynamicToolSpec {
133-
name: "lookup_order".to_string(),
134-
description: "Look up an order".to_string(),
135-
input_schema: serde_json::json!({
136-
"type": "object",
137-
"properties": {
138-
"order_id": {"type": "string"}
139-
},
140-
"required": ["order_id"],
141-
"additionalProperties": false,
142-
}),
143-
defer_loading: true,
144-
};
145-
146-
let openai_tool = dynamic_tool_to_openai_tool(&tool).expect("convert dynamic tool");
147-
148-
assert_eq!(
149-
openai_tool,
150-
ResponsesApiTool {
151-
name: "lookup_order".to_string(),
152-
description: "Look up an order".to_string(),
153-
strict: false,
154-
defer_loading: Some(true),
155-
parameters: JsonSchema::Object {
156-
properties: BTreeMap::from([(
157-
"order_id".to_string(),
158-
JsonSchema::String { description: None },
159-
)]),
160-
required: Some(vec!["order_id".to_string()]),
161-
additional_properties: Some(false.into()),
162-
},
163-
output_schema: None,
164-
}
165-
);
166-
}
167-
168108
fn tool_name(tool: &ToolSpec) -> &str {
169109
match tool {
170110
ToolSpec::Function(ResponsesApiTool { name, .. }) => name,

codex-rs/tools/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,26 @@ shared across multiple crates and does not need to stay coupled to
55
`codex-core`.
66

77
Today this crate is intentionally small. It currently owns the shared tool
8-
schema primitives that no longer need to live in `core/src/tools/spec.rs`:
8+
schema and Responses API tool primitives that no longer need to live in
9+
`core/src/tools/spec.rs` or `core/src/client_common.rs`:
910

1011
- `JsonSchema`
1112
- `AdditionalProperties`
1213
- `ToolDefinition`
14+
- `ResponsesApiTool`
15+
- `FreeformTool`
16+
- `FreeformToolFormat`
17+
- `ToolSearchOutputTool`
18+
- `ResponsesApiNamespace`
19+
- `ResponsesApiNamespaceTool`
1320
- `parse_tool_input_schema()`
1421
- `parse_dynamic_tool()`
1522
- `parse_mcp_tool()`
1623
- `mcp_call_tool_result_output_schema()`
24+
- `tool_definition_to_responses_api_tool()`
25+
- `dynamic_tool_to_responses_api_tool()`
26+
- `mcp_tool_to_responses_api_tool()`
27+
- `mcp_tool_to_deferred_responses_api_tool()`
1728

1829
That extraction is the first step in a longer migration. The goal is not to
1930
move all of `core/src/tools` into this crate in one shot. Instead, the plan is

0 commit comments

Comments
 (0)