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
+26
View File
@@ -62,6 +62,32 @@ pub struct DynamicToolResponse {
pub success: bool,
}
/// The result delivered to a pending dynamic tool call in Core.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, JsonSchema, TS)]
#[serde(untagged)]
pub enum DynamicToolCallResult {
ContentItems(DynamicToolResponse),
StructuredContent {
#[serde(rename = "structuredContent")]
structured_content: JsonValue,
success: bool,
},
Error {
#[serde(rename = "errorMessage")]
error_message: String,
},
}
impl DynamicToolCallResult {
pub fn success(&self) -> bool {
match self {
Self::ContentItems(response) => response.success,
Self::StructuredContent { success, .. } => *success,
Self::Error { .. } => false,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, JsonSchema, TS)]
#[serde(tag = "type", rename_all = "camelCase")]
#[ts(tag = "type")]
+10 -1
View File
@@ -31,6 +31,7 @@ use crate::config_types::ReasoningSummary as ReasoningSummaryConfig;
use crate::config_types::WindowsSandboxLevel;
use crate::dynamic_tools::DynamicToolCallOutputContentItem;
use crate::dynamic_tools::DynamicToolCallRequest;
use crate::dynamic_tools::DynamicToolCallResult;
use crate::dynamic_tools::DynamicToolResponse;
use crate::dynamic_tools::DynamicToolSpec;
use crate::error::Result as CodexResult;
@@ -921,6 +922,12 @@ pub enum Op {
/// Tool output payload.
response: DynamicToolResponse,
},
/// Reply from a client that supports structured dynamic tool results.
#[debug("DynamicToolResult {{ id: {id:?}, success: {} }}", response.success())]
DynamicToolResult {
id: String,
response: DynamicToolCallResult,
},
/// Request MCP servers to reinitialize and refresh cached tool lists.
RefreshMcpServers,
@@ -1172,7 +1179,9 @@ impl Op {
Self::ResolveElicitation { .. } => "resolve_elicitation",
Self::UserInputAnswer { .. } => "user_input_answer",
Self::RequestPermissionsResponse { .. } => "request_permissions_response",
Self::DynamicToolResponse { .. } => "dynamic_tool_response",
Self::DynamicToolResponse { .. } | Self::DynamicToolResult { .. } => {
"dynamic_tool_response"
}
Self::RefreshMcpServers => "refresh_mcp_servers",
Self::ReloadUserConfig => "reload_user_config",
Self::Compact => "compact",