Support structured dynamic tool results in Code Mode (#63766)

## Why

Dynamic tool clients could return JSON as text in `contentItems`, so Code Mode scripts had to parse that text to access fields in the result.

## What changed

App server dynamic tool requests advertise `supportedResultFormat: "structuredContent"`. Clients can reply with `structuredContent` and a `success` flag, report a failure with `errorMessage` and `success: false`, or continue using `contentItems`.

Code Mode exposes structured replies as MCP-style tool results, with the original JSON under `structuredContent` and failure status in `isError`. Completed tool items include a text representation of the reply. Matching post-tool-use hooks can receive the structured result for successful calls.

## Testing

Add protocol tests for the wire formats and rejection of ambiguous replies, plus an app server integration test that exercises structured results, content items, and errors through Code Mode.

GitOrigin-RevId: d63152633b43bd5bd236f6e0dcb41ee7e98e7947
This commit is contained in:
Brandon Zhang
2026-11-24 23:38:37 +00:00
committed by copyberry
parent 0adee0df26
commit ff146c344d
37 changed files with 1104 additions and 169 deletions
@@ -2523,6 +2523,7 @@ async fn root_switch_preserves_idle_root_with_running_subagent() -> Result<()> {
AppServerEvent::ServerRequest(Box::new(ServerRequest::DynamicToolCall {
request_id: RequestId::Integer(99),
params: DynamicToolCallParams {
supported_result_format: None,
thread_id: idle_child_id.to_string(),
turn_id: "turn".into(),
call_id: "call".into(),
+1
View File
@@ -6982,6 +6982,7 @@ async fn side_defers_subagent_approval_overlay_until_side_exits() -> Result<()>
ServerRequest::DynamicToolCall {
request_id: AppServerRequestId::Integer(99),
params: codex_app_server_protocol::DynamicToolCallParams {
supported_result_format: None,
thread_id: quiet_thread_id.to_string(),
turn_id: "turn-quiet".to_string(),
call_id: "call-quiet".to_string(),
@@ -3889,6 +3889,7 @@ async fn embedded_server_dispatches_only_its_own_dynamic_tools() -> Result<()> {
ServerRequest::DynamicToolCall {
request_id: AppServerRequestId::Integer(id),
params: codex_app_server_protocol::DynamicToolCallParams {
supported_result_format: None,
thread_id: "thread-1".to_string(),
turn_id: "turn-1".to_string(),
call_id: "call-1".to_string(),
@@ -4032,6 +4033,7 @@ async fn check_dynamic_tool_requests(
ServerRequest::DynamicToolCall {
request_id: AppServerRequestId::Integer(100),
params: codex_app_server_protocol::DynamicToolCallParams {
supported_result_format: None,
thread_id: thread_id.clone(),
turn_id: "turn-1".to_string(),
call_id: "call-1".to_string(),
@@ -4052,6 +4054,7 @@ async fn check_dynamic_tool_requests(
ServerRequest::DynamicToolCall {
request_id: AppServerRequestId::Integer(101),
params: codex_app_server_protocol::DynamicToolCallParams {
supported_result_format: None,
thread_id: thread_id.clone(),
turn_id: "turn-1".to_string(),
call_id: "call-2".to_string(),
@@ -4111,6 +4114,7 @@ async fn check_dynamic_tool_requests(
ServerRequest::DynamicToolCall {
request_id: AppServerRequestId::Integer(102),
params: codex_app_server_protocol::DynamicToolCallParams {
supported_result_format: None,
thread_id: thread_id.clone(),
turn_id: "turn-1".to_string(),
call_id: "call-3".to_string(),
@@ -4145,6 +4149,7 @@ async fn check_dynamic_tool_requests(
ServerRequest::DynamicToolCall {
request_id: AppServerRequestId::String(format!("rejected-{index}")),
params: codex_app_server_protocol::DynamicToolCallParams {
supported_result_format: None,
thread_id: thread_id.clone(),
turn_id: "turn-1".to_string(),
call_id: format!("rejected-{index}"),
@@ -4224,6 +4229,7 @@ async fn check_dynamic_tool_requests(
&app_server,
AppServerRequestId::Integer(103),
codex_app_server_protocol::DynamicToolCallParams {
supported_result_format: None,
thread_id: creation_source.to_string(),
turn_id: "turn-1".to_string(),
call_id: "call-4".to_string(),
@@ -4348,6 +4354,7 @@ async fn check_dynamic_tool_requests(
&app_server,
AppServerRequestId::Integer(104),
codex_app_server_protocol::DynamicToolCallParams {
supported_result_format: None,
thread_id: thread_id.clone(),
turn_id: "turn-1".to_string(),
call_id: "call-5".to_string(),
@@ -4894,6 +4901,7 @@ async fn remote_legacy_history_start_negotiates_once_for_resume_and_fork() -> Re
let response = crate::dynamic_tools::execute(
app_server.request_handle(),
codex_app_server_protocol::DynamicToolCallParams {
supported_result_format: None,
thread_id: started.session.thread_id.to_string(),
turn_id: "source-turn".to_string(),
call_id: "legacy-wait".to_string(),
+1
View File
@@ -299,6 +299,7 @@ impl ServerHandler for DynamicToolMcpHandler {
.and_then(Value::as_str)
.map_or_else(|| format!("mcp-call-{}", Uuid::new_v4()), str::to_string);
let params = DynamicToolCallParams {
supported_result_format: None,
thread_id: thread_id.to_string(),
turn_id,
call_id,
+1
View File
@@ -130,6 +130,7 @@ async fn call_tool(
execute(
server.request_handle(),
DynamicToolCallParams {
supported_result_format: None,
thread_id: source.to_string(),
turn_id: "persisted-turn".to_string(),
call_id: "call-1".to_string(),