diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000000..618207f301 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,18 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Cargo launch", + "cargo": { + "cwd": "${workspaceFolder}/codex-rs", + "args": [ + "build", + "--bin=codex-tui" + ] + }, + "args": [] + } + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000000..f66a12583c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,10 @@ +{ + "rust-analyzer.checkOnSave": true, + "rust-analyzer.check.command": "clippy", + "rust-analyzer.check.extraArgs": ["--all-features", "--tests"], + "rust-analyzer.rustfmt.extraArgs": ["--config", "imports_granularity=Item"], + "[rust]": { + "editor.defaultFormatter": "rust-lang.rust-analyzer", + "editor.formatOnSave": true, + } +} diff --git a/AGENTS.md b/AGENTS.md index 1348e57824..27af48ae60 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -3,3 +3,7 @@ In the codex-rs folder where the rust code lives: - Never add or modify any code related to `CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR`. You operate in a sandbox where `CODEX_SANDBOX_NETWORK_DISABLED=1` will be set whenever you use the `shell` tool. Any existing code that uses `CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR` was authored with this fact in mind. It is often used to early exit out of tests that the author knew you would not be able to run given your sandbox limitations. + +Before creating a pull request with changes to `codex-rs`, run `just fmt` (in `codex-rs` directory) to format the code and `just fix` (in `codex-rs` directory) to fix any linter issues in the code, ensure the test suite passes by running `cargo test --all-features` in the `codex-rs` directory. + +When making individual changes prefer running tests on individual files or projects first. diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index e59dbfa255..6a8e76dd8a 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -399,6 +399,15 @@ version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6099cdc01846bc367c4e7dd630dc5966dccf36b652fae7a74e17b640411a91b2" +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + [[package]] name = "bstr" version = "1.12.0" @@ -671,6 +680,7 @@ dependencies = [ "seccompiler", "serde", "serde_json", + "sha1", "strum_macros 0.27.1", "tempfile", "thiserror 2.0.12", @@ -683,6 +693,7 @@ dependencies = [ "tree-sitter", "tree-sitter-bash", "uuid", + "walkdir", "wildmatch", "wiremock", ] @@ -781,6 +792,7 @@ name = "codex-mcp-server" version = "0.0.0" dependencies = [ "anyhow", + "assert_cmd", "codex-core", "codex-linux-sandbox", "mcp-types", @@ -788,10 +800,14 @@ dependencies = [ "schemars 0.8.22", "serde", "serde_json", + "shlex", + "tempfile", "tokio", + "tokio-test", "toml 0.9.1", "tracing", "tracing-subscriber", + "wiremock", ] [[package]] @@ -932,6 +948,15 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + [[package]] name = "crc32fast" version = "1.4.2" @@ -1006,6 +1031,16 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + [[package]] name = "ctor" version = "0.1.26" @@ -1156,6 +1191,16 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + [[package]] name = "dirs" version = "6.0.0" @@ -1645,6 +1690,16 @@ dependencies = [ "byteorder", ] +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "getopts" version = "0.2.23" @@ -3944,6 +3999,17 @@ dependencies = [ "syn 2.0.104", ] +[[package]] +name = "sha1" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "sharded-slab" version = "0.1.7" @@ -4851,6 +4917,12 @@ dependencies = [ "unicode-width 0.2.0", ] +[[package]] +name = "typenum" +version = "1.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f" + [[package]] name = "unicase" version = "2.8.1" diff --git a/codex-rs/common/src/config_override.rs b/codex-rs/common/src/config_override.rs index 610195d6d1..c9b18edc7c 100644 --- a/codex-rs/common/src/config_override.rs +++ b/codex-rs/common/src/config_override.rs @@ -64,7 +64,11 @@ impl CliConfigOverrides { // `-c model=o3` without the quotes. let value: Value = match parse_toml_value(value_str) { Ok(v) => v, - Err(_) => Value::String(value_str.to_string()), + Err(_) => { + // Strip leading/trailing quotes if present + let trimmed = value_str.trim().trim_matches(|c| c == '"' || c == '\''); + Value::String(trimmed.to_string()) + } }; Ok((key.to_string(), value)) diff --git a/codex-rs/config.md b/codex-rs/config.md index 438b7e767d..3d38ded1a5 100644 --- a/codex-rs/config.md +++ b/codex-rs/config.md @@ -92,6 +92,32 @@ http_headers = { "X-Example-Header" = "example-value" } env_http_headers = { "X-Example-Features": "EXAMPLE_FEATURES" } ``` +### Per-provider network tuning + +The following optional settings control retry behaviour and streaming idle timeouts **per model provider**. They must be specified inside the corresponding `[model_providers.]` block in `config.toml`. (Older releases accepted top‑level keys; those are now ignored.) + +Example: + +```toml +[model_providers.openai] +name = "OpenAI" +base_url = "https://api.openai.com/v1" +env_key = "OPENAI_API_KEY" +# network tuning overrides (all optional; falls back to built‑in defaults) +request_max_retries = 4 # retry failed HTTP requests +stream_max_retries = 10 # retry dropped SSE streams +stream_idle_timeout_ms = 300000 # 5m idle timeout +``` + +#### request_max_retries +How many times Codex will retry a failed HTTP request to the model provider. Defaults to `4`. + +#### stream_max_retries +Number of times Codex will attempt to reconnect when a streaming response is interrupted. Defaults to `10`. + +#### stream_idle_timeout_ms +How long Codex will wait for activity on a streaming response before treating the connection as lost. Defaults to `300_000` (5 minutes). + ## model_provider Identifies which provider to use from the `model_providers` map. Defaults to `"openai"`. You can override the `base_url` for the built-in `openai` provider via the `OPENAI_BASE_URL` environment variable. @@ -444,7 +470,7 @@ Currently, `"vscode"` is the default, though Codex does not verify VS Code is in ## hide_agent_reasoning -Codex intermittently emits "reasoning" events that show the model’s internal "thinking" before it produces a final answer. Some users may find these events distracting, especially in CI logs or minimal terminal output. +Codex intermittently emits "reasoning" events that show the model's internal "thinking" before it produces a final answer. Some users may find these events distracting, especially in CI logs or minimal terminal output. Setting `hide_agent_reasoning` to `true` suppresses these events in **both** the TUI as well as the headless `exec` sub-command: diff --git a/codex-rs/core/Cargo.toml b/codex-rs/core/Cargo.toml index c55d7d395d..e192a71f39 100644 --- a/codex-rs/core/Cargo.toml +++ b/codex-rs/core/Cargo.toml @@ -28,6 +28,7 @@ rand = "0.9" reqwest = { version = "0.12", features = ["json", "stream"] } serde = { version = "1", features = ["derive"] } serde_json = "1" +sha1 = "0.10.6" strum_macros = "0.27.1" thiserror = "2.0.12" time = { version = "0.3", features = ["formatting", "local-offset", "macros"] } @@ -65,4 +66,5 @@ predicates = "3" pretty_assertions = "1.4.1" tempfile = "3" tokio-test = "0.4" +walkdir = "2.5.0" wiremock = "0.6" diff --git a/codex-rs/core/src/chat_completions.rs b/codex-rs/core/src/chat_completions.rs index ad7b55952a..35045c8e1b 100644 --- a/codex-rs/core/src/chat_completions.rs +++ b/codex-rs/core/src/chat_completions.rs @@ -21,8 +21,6 @@ use crate::client_common::ResponseEvent; use crate::client_common::ResponseStream; use crate::error::CodexErr; use crate::error::Result; -use crate::flags::OPENAI_REQUEST_MAX_RETRIES; -use crate::flags::OPENAI_STREAM_IDLE_TIMEOUT_MS; use crate::models::ContentItem; use crate::models::ResponseItem; use crate::openai_tools::create_tools_json_for_chat_completions_api; @@ -121,6 +119,7 @@ pub(crate) async fn stream_chat_completions( ); let mut attempt = 0; + let max_retries = provider.request_max_retries(); loop { attempt += 1; @@ -136,7 +135,11 @@ pub(crate) async fn stream_chat_completions( Ok(resp) if resp.status().is_success() => { let (tx_event, rx_event) = mpsc::channel::>(1600); let stream = resp.bytes_stream().map_err(CodexErr::Reqwest); - tokio::spawn(process_chat_sse(stream, tx_event)); + tokio::spawn(process_chat_sse( + stream, + tx_event, + provider.stream_idle_timeout(), + )); return Ok(ResponseStream { rx_event }); } Ok(res) => { @@ -146,7 +149,7 @@ pub(crate) async fn stream_chat_completions( return Err(CodexErr::UnexpectedStatus(status, body)); } - if attempt > *OPENAI_REQUEST_MAX_RETRIES { + if attempt > max_retries { return Err(CodexErr::RetryLimit(status)); } @@ -162,7 +165,7 @@ pub(crate) async fn stream_chat_completions( tokio::time::sleep(delay).await; } Err(e) => { - if attempt > *OPENAI_REQUEST_MAX_RETRIES { + if attempt > max_retries { return Err(e.into()); } let delay = backoff(attempt); @@ -175,14 +178,15 @@ pub(crate) async fn stream_chat_completions( /// Lightweight SSE processor for the Chat Completions streaming format. The /// output is mapped onto Codex's internal [`ResponseEvent`] so that the rest /// of the pipeline can stay agnostic of the underlying wire format. -async fn process_chat_sse(stream: S, tx_event: mpsc::Sender>) -where +async fn process_chat_sse( + stream: S, + tx_event: mpsc::Sender>, + idle_timeout: Duration, +) where S: Stream> + Unpin, { let mut stream = stream.eventsource(); - let idle_timeout = *OPENAI_STREAM_IDLE_TIMEOUT_MS; - // State to accumulate a function call across streaming chunks. // OpenAI may split the `arguments` string over multiple `delta` events // until the chunk whose `finish_reason` is `tool_calls` is emitted. We diff --git a/codex-rs/core/src/client.rs b/codex-rs/core/src/client.rs index 8ec68d02e8..62fcabe05b 100644 --- a/codex-rs/core/src/client.rs +++ b/codex-rs/core/src/client.rs @@ -15,6 +15,7 @@ use tokio_util::io::ReaderStream; use tracing::debug; use tracing::trace; use tracing::warn; +use uuid::Uuid; use crate::chat_completions::AggregateStreamExt; use crate::chat_completions::stream_chat_completions; @@ -29,8 +30,6 @@ use crate::config_types::ReasoningSummary as ReasoningSummaryConfig; use crate::error::CodexErr; use crate::error::Result; use crate::flags::CODEX_RS_SSE_FIXTURE; -use crate::flags::OPENAI_REQUEST_MAX_RETRIES; -use crate::flags::OPENAI_STREAM_IDLE_TIMEOUT_MS; use crate::model_provider_info::ModelProviderInfo; use crate::model_provider_info::WireApi; use crate::models::ResponseItem; @@ -44,6 +43,7 @@ pub struct ModelClient { config: Arc, client: reqwest::Client, provider: ModelProviderInfo, + session_id: Uuid, effort: ReasoningEffortConfig, summary: ReasoningSummaryConfig, } @@ -54,11 +54,13 @@ impl ModelClient { provider: ModelProviderInfo, effort: ReasoningEffortConfig, summary: ReasoningSummaryConfig, + session_id: Uuid, ) -> Self { Self { config, client: reqwest::Client::new(), provider, + session_id, effort, summary, } @@ -109,7 +111,7 @@ impl ModelClient { if let Some(path) = &*CODEX_RS_SSE_FIXTURE { // short circuit for tests warn!(path, "Streaming from fixture"); - return stream_from_fixture(path).await; + return stream_from_fixture(path, self.provider.clone()).await; } let full_instructions = prompt.get_full_instructions(&self.config.model); @@ -136,6 +138,7 @@ impl ModelClient { ); let mut attempt = 0; + let max_retries = self.provider.request_max_retries(); loop { attempt += 1; @@ -143,6 +146,7 @@ impl ModelClient { .provider .create_request_builder(&self.client)? .header("OpenAI-Beta", "responses=experimental") + .header("session_id", self.session_id.to_string()) .header(reqwest::header::ACCEPT, "text/event-stream") .json(&payload); @@ -153,7 +157,11 @@ impl ModelClient { // spawn task to process SSE let stream = resp.bytes_stream().map_err(CodexErr::Reqwest); - tokio::spawn(process_sse(stream, tx_event)); + tokio::spawn(process_sse( + stream, + tx_event, + self.provider.stream_idle_timeout(), + )); return Ok(ResponseStream { rx_event }); } @@ -172,7 +180,7 @@ impl ModelClient { return Err(CodexErr::UnexpectedStatus(status, body)); } - if attempt > *OPENAI_REQUEST_MAX_RETRIES { + if attempt > max_retries { return Err(CodexErr::RetryLimit(status)); } @@ -189,7 +197,7 @@ impl ModelClient { tokio::time::sleep(delay).await; } Err(e) => { - if attempt > *OPENAI_REQUEST_MAX_RETRIES { + if attempt > max_retries { return Err(e.into()); } let delay = backoff(attempt); @@ -198,6 +206,10 @@ impl ModelClient { } } } + + pub fn get_provider(&self) -> ModelProviderInfo { + self.provider.clone() + } } #[derive(Debug, Deserialize, Serialize)] @@ -249,14 +261,16 @@ struct ResponseCompletedOutputTokensDetails { reasoning_tokens: u64, } -async fn process_sse(stream: S, tx_event: mpsc::Sender>) -where +async fn process_sse( + stream: S, + tx_event: mpsc::Sender>, + idle_timeout: Duration, +) where S: Stream> + Unpin, { let mut stream = stream.eventsource(); // If the stream stays completely silent for an extended period treat it as disconnected. - let idle_timeout = *OPENAI_STREAM_IDLE_TIMEOUT_MS; // The response id returned from the "complete" message. let mut response_completed: Option = None; @@ -317,7 +331,7 @@ where // duplicated `output` array embedded in the `response.completed` // payload. That produced two concrete issues: // 1. No real‑time streaming – the user only saw output after the - // entire turn had finished, which broke the “typing” UX and + // entire turn had finished, which broke the "typing" UX and // made long‑running turns look stalled. // 2. Duplicate `function_call_output` items – both the // individual *and* the completed array were forwarded, which @@ -390,7 +404,10 @@ where } /// used in tests to stream from a text SSE file -async fn stream_from_fixture(path: impl AsRef) -> Result { +async fn stream_from_fixture( + path: impl AsRef, + provider: ModelProviderInfo, +) -> Result { let (tx_event, rx_event) = mpsc::channel::>(1600); let f = std::fs::File::open(path.as_ref())?; let lines = std::io::BufReader::new(f).lines(); @@ -404,7 +421,11 @@ async fn stream_from_fixture(path: impl AsRef) -> Result { let rdr = std::io::Cursor::new(content); let stream = ReaderStream::new(rdr).map_err(CodexErr::Io); - tokio::spawn(process_sse(stream, tx_event)); + tokio::spawn(process_sse( + stream, + tx_event, + provider.stream_idle_timeout(), + )); Ok(ResponseStream { rx_event }) } @@ -424,7 +445,10 @@ mod tests { /// Runs the SSE parser on pre-chunked byte slices and returns every event /// (including any final `Err` from a stream-closure check). - async fn collect_events(chunks: &[&[u8]]) -> Vec> { + async fn collect_events( + chunks: &[&[u8]], + provider: ModelProviderInfo, + ) -> Vec> { let mut builder = IoBuilder::new(); for chunk in chunks { builder.read(chunk); @@ -433,7 +457,7 @@ mod tests { let reader = builder.build(); let stream = ReaderStream::new(reader).map_err(CodexErr::Io); let (tx, mut rx) = mpsc::channel::>(16); - tokio::spawn(process_sse(stream, tx)); + tokio::spawn(process_sse(stream, tx, provider.stream_idle_timeout())); let mut events = Vec::new(); while let Some(ev) = rx.recv().await { @@ -444,7 +468,10 @@ mod tests { /// Builds an in-memory SSE stream from JSON fixtures and returns only the /// successfully parsed events (panics on internal channel errors). - async fn run_sse(events: Vec) -> Vec { + async fn run_sse( + events: Vec, + provider: ModelProviderInfo, + ) -> Vec { let mut body = String::new(); for e in events { let kind = e @@ -460,7 +487,7 @@ mod tests { let (tx, mut rx) = mpsc::channel::>(8); let stream = ReaderStream::new(std::io::Cursor::new(body)).map_err(CodexErr::Io); - tokio::spawn(process_sse(stream, tx)); + tokio::spawn(process_sse(stream, tx, provider.stream_idle_timeout())); let mut out = Vec::new(); while let Some(ev) = rx.recv().await { @@ -505,7 +532,25 @@ mod tests { let sse2 = format!("event: response.output_item.done\ndata: {item2}\n\n"); let sse3 = format!("event: response.completed\ndata: {completed}\n\n"); - let events = collect_events(&[sse1.as_bytes(), sse2.as_bytes(), sse3.as_bytes()]).await; + let provider = ModelProviderInfo { + name: "test".to_string(), + base_url: "https://test.com".to_string(), + env_key: Some("TEST_API_KEY".to_string()), + env_key_instructions: None, + wire_api: WireApi::Responses, + query_params: None, + http_headers: None, + env_http_headers: None, + request_max_retries: Some(0), + stream_max_retries: Some(0), + stream_idle_timeout_ms: Some(1000), + }; + + let events = collect_events( + &[sse1.as_bytes(), sse2.as_bytes(), sse3.as_bytes()], + provider, + ) + .await; assert_eq!(events.len(), 3); @@ -546,8 +591,21 @@ mod tests { .to_string(); let sse1 = format!("event: response.output_item.done\ndata: {item1}\n\n"); + let provider = ModelProviderInfo { + name: "test".to_string(), + base_url: "https://test.com".to_string(), + env_key: Some("TEST_API_KEY".to_string()), + env_key_instructions: None, + wire_api: WireApi::Responses, + query_params: None, + http_headers: None, + env_http_headers: None, + request_max_retries: Some(0), + stream_max_retries: Some(0), + stream_idle_timeout_ms: Some(1000), + }; - let events = collect_events(&[sse1.as_bytes()]).await; + let events = collect_events(&[sse1.as_bytes()], provider).await; assert_eq!(events.len(), 2); @@ -635,7 +693,21 @@ mod tests { let mut evs = vec![case.event]; evs.push(completed.clone()); - let out = run_sse(evs).await; + let provider = ModelProviderInfo { + name: "test".to_string(), + base_url: "https://test.com".to_string(), + env_key: Some("TEST_API_KEY".to_string()), + env_key_instructions: None, + wire_api: WireApi::Responses, + query_params: None, + http_headers: None, + env_http_headers: None, + request_max_retries: Some(0), + stream_max_retries: Some(0), + stream_idle_timeout_ms: Some(1000), + }; + + let out = run_sse(evs, provider).await; assert_eq!(out.len(), case.expected_len, "case {}", case.name); assert!( (case.expect_first)(&out[0]), diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index 74ba36b039..47e252307e 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -49,9 +49,7 @@ use crate::exec::ExecToolCallOutput; use crate::exec::SandboxType; use crate::exec::process_exec_tool_call; use crate::exec_env::create_env; -use crate::flags::OPENAI_STREAM_MAX_RETRIES; use crate::mcp_connection_manager::McpConnectionManager; -use crate::mcp_connection_manager::try_parse_fully_qualified_tool_name; use crate::mcp_tool_call::handle_mcp_tool_call; use crate::models::ContentItem; use crate::models::FunctionCallOutputPayload; @@ -104,6 +102,9 @@ impl Codex { /// of `Codex` and the ID of the `SessionInitialized` event that was /// submitted to start the session. pub async fn spawn(config: Config, ctrl_c: Arc) -> CodexResult<(Codex, String)> { + // experimental resume path (undocumented) + let resume_path = config.experimental_resume.clone(); + info!("resume_path: {resume_path:?}"); let (tx_sub, rx_sub) = async_channel::bounded(64); let (tx_event, rx_event) = async_channel::bounded(1600); @@ -119,6 +120,7 @@ impl Codex { disable_response_storage: config.disable_response_storage, notify: config.notify.clone(), cwd: config.cwd.clone(), + resume_path: resume_path.clone(), }; let config = Arc::new(config); @@ -308,24 +310,30 @@ impl Session { /// transcript, if enabled. async fn record_conversation_items(&self, items: &[ResponseItem]) { debug!("Recording items for conversation: {items:?}"); - self.record_rollout_items(items).await; + self.record_state_snapshot(items).await; if let Some(transcript) = self.state.lock().unwrap().zdr_transcript.as_mut() { transcript.record_items(items); } } - /// Append the given items to the session's rollout transcript (if enabled) - /// and persist them to disk. - async fn record_rollout_items(&self, items: &[ResponseItem]) { - // Clone the recorder outside of the mutex so we don't hold the lock - // across an await point (MutexGuard is not Send). + async fn record_state_snapshot(&self, items: &[ResponseItem]) { + let snapshot = { + let state = self.state.lock().unwrap(); + crate::rollout::SessionStateSnapshot { + previous_response_id: state.previous_response_id.clone(), + } + }; + let recorder = { let guard = self.rollout.lock().unwrap(); guard.as_ref().cloned() }; if let Some(rec) = recorder { + if let Err(e) = rec.record_state(snapshot).await { + error!("failed to record rollout state: {e:#}"); + } if let Err(e) = rec.record_items(items).await { error!("failed to record rollout items: {e:#}"); } @@ -519,7 +527,7 @@ async fn submission_loop( ctrl_c: Arc, ) { // Generate a unique ID for the lifetime of this Codex session. - let session_id = Uuid::new_v4(); + let mut session_id = Uuid::new_v4(); let mut sess: Option> = None; // shorthand - send an event when there is no active session @@ -572,8 +580,11 @@ async fn submission_loop( disable_response_storage, notify, cwd, + resume_path, } => { - info!("Configuring session: model={model}; provider={provider:?}"); + info!( + "Configuring session: model={model}; provider={provider:?}; resume={resume_path:?}" + ); if !cwd.is_absolute() { let message = format!("cwd is not absolute: {cwd:?}"); error!(message); @@ -586,12 +597,48 @@ async fn submission_loop( } return; } + // Optionally resume an existing rollout. + let mut restored_items: Option> = None; + let mut restored_prev_id: Option = None; + let rollout_recorder: Option = + if let Some(path) = resume_path.as_ref() { + match RolloutRecorder::resume(path).await { + Ok((rec, saved)) => { + session_id = saved.session_id; + restored_prev_id = saved.state.previous_response_id; + if !saved.items.is_empty() { + restored_items = Some(saved.items); + } + Some(rec) + } + Err(e) => { + warn!("failed to resume rollout from {path:?}: {e}"); + None + } + } + } else { + None + }; + + let rollout_recorder = match rollout_recorder { + Some(rec) => Some(rec), + None => match RolloutRecorder::new(&config, session_id, instructions.clone()) + .await + { + Ok(r) => Some(r), + Err(e) => { + warn!("failed to initialise rollout recorder: {e}"); + None + } + }, + }; let client = ModelClient::new( config.clone(), provider.clone(), model_reasoning_effort, model_reasoning_summary, + session_id, ); // abort any current running session and clone its state @@ -645,21 +692,6 @@ async fn submission_loop( }); } } - - // Attempt to create a RolloutRecorder *before* moving the - // `instructions` value into the Session struct. - // TODO: if ConfigureSession is sent twice, we will create an - // overlapping rollout file. Consider passing RolloutRecorder - // from above. - let rollout_recorder = - match RolloutRecorder::new(&config, session_id, instructions.clone()).await { - Ok(r) => Some(r), - Err(e) => { - warn!("failed to initialise rollout recorder: {e}"); - None - } - }; - sess = Some(Arc::new(Session { client, tx_event: tx_event.clone(), @@ -677,6 +709,19 @@ async fn submission_loop( codex_linux_sandbox_exe: config.codex_linux_sandbox_exe.clone(), })); + // Patch restored state into the newly created session. + if let Some(sess_arc) = &sess { + if restored_prev_id.is_some() || restored_items.is_some() { + let mut st = sess_arc.state.lock().unwrap(); + st.previous_response_id = restored_prev_id; + if let (Some(hist), Some(items)) = + (st.zdr_transcript.as_mut(), restored_items.as_ref()) + { + hist.record_items(items.iter()); + } + } + } + // Gather history metadata for SessionConfiguredEvent. let (history_log_id, history_entry_count) = crate::message_history::history_metadata(&config).await; @@ -745,6 +790,8 @@ async fn submission_loop( } } Op::AddToHistory { text } => { + // TODO: What should we do if we got AddToHistory before ConfigureSession? + // currently, if ConfigureSession has resume path, this history will be ignored let id = session_id; let config = config.clone(); tokio::spawn(async move { @@ -950,15 +997,17 @@ async fn run_task(sess: Arc, sub_id: String, input: Vec) { ) => { items_to_record_in_conversation_history.push(item); let (content, success): (String, Option) = match result { - Ok(CallToolResult { content, is_error }) => { - match serde_json::to_string(content) { - Ok(content) => (content, *is_error), - Err(e) => { - warn!("Failed to serialize MCP tool call output: {e}"); - (e.to_string(), Some(true)) - } + Ok(CallToolResult { + content, + is_error, + structured_content: _, + }) => match serde_json::to_string(content) { + Ok(content) => (content, *is_error), + Err(e) => { + warn!("Failed to serialize MCP tool call output: {e}"); + (e.to_string(), Some(true)) } - } + }, Err(e) => (e.clone(), Some(true)), }; items_to_record_in_conversation_history.push( @@ -1057,12 +1106,13 @@ async fn run_turn( Err(CodexErr::Interrupted) => return Err(CodexErr::Interrupted), Err(CodexErr::EnvVar(var)) => return Err(CodexErr::EnvVar(var)), Err(e) => { - if retries < *OPENAI_STREAM_MAX_RETRIES { + // Use the configured provider-specific stream retry budget. + let max_retries = sess.client.get_provider().stream_max_retries(); + if retries < max_retries { retries += 1; let delay = backoff(retries); warn!( - "stream disconnected - retrying turn ({retries}/{} in {delay:?})...", - *OPENAI_STREAM_MAX_RETRIES + "stream disconnected - retrying turn ({retries}/{max_retries} in {delay:?})...", ); // Surface retry information to any UI/front‑end so the @@ -1071,8 +1121,7 @@ async fn run_turn( sess.notify_background_event( &sub_id, format!( - "stream error: {e}; retrying {retries}/{} in {:?}…", - *OPENAI_STREAM_MAX_RETRIES, delay + "stream error: {e}; retrying {retries}/{max_retries} in {delay:?}…" ), ) .await; @@ -1154,7 +1203,28 @@ async fn try_run_turn( let mut stream = sess.client.clone().stream(&prompt).await?; let mut output = Vec::new(); - while let Some(Ok(event)) = stream.next().await { + loop { + // Poll the next item from the model stream. We must inspect *both* Ok and Err + // cases so that transient stream failures (e.g., dropped SSE connection before + // `response.completed`) bubble up and trigger the caller's retry logic. + let event = stream.next().await; + let Some(event) = event else { + // Channel closed without yielding a final Completed event or explicit error. + // Treat as a disconnected stream so the caller can retry. + return Err(CodexErr::Stream( + "stream closed before response.completed".into(), + )); + }; + + let event = match event { + Ok(ev) => ev, + Err(e) => { + // Propagate the underlying stream error to the caller (run_turn), which + // will apply the configured `stream_max_retries` policy. + return Err(e); + } + }; + match event { ResponseEvent::Created => { let mut state = sess.state.lock().unwrap(); @@ -1195,7 +1265,7 @@ async fn try_run_turn( let mut state = sess.state.lock().unwrap(); state.previous_response_id = Some(response_id); - break; + return Ok(output); } ResponseEvent::OutputTextDelta(delta) => { let event = Event { @@ -1213,7 +1283,6 @@ async fn try_run_turn( } } } - Ok(output) } async fn handle_response_item( @@ -1316,13 +1385,13 @@ async fn handle_function_call( let params = match parse_container_exec_arguments(arguments, sess, &call_id) { Ok(params) => params, Err(output) => { - return output; + return *output; } }; handle_container_exec_with_params(params, sess, sub_id, call_id).await } _ => { - match try_parse_fully_qualified_tool_name(&name) { + match sess.mcp_connection_manager.parse_tool_name(&name) { Some((server, tool_name)) => { // TODO(mbolin): Determine appropriate timeout for tool call. let timeout = None; @@ -1359,7 +1428,7 @@ fn parse_container_exec_arguments( arguments: String, sess: &Session, call_id: &str, -) -> Result { +) -> Result> { // parse command match serde_json::from_str::(&arguments) { Ok(shell_tool_call_params) => Ok(to_exec_params(shell_tool_call_params, sess)), @@ -1372,7 +1441,7 @@ fn parse_container_exec_arguments( success: None, }, }; - Err(output) + Err(Box::new(output)) } } } diff --git a/codex-rs/core/src/config.rs b/codex-rs/core/src/config.rs index d67e692fc8..f1d0dd9d60 100644 --- a/codex-rs/core/src/config.rs +++ b/codex-rs/core/src/config.rs @@ -137,6 +137,9 @@ pub struct Config { /// Base URL for requests to ChatGPT (as opposed to the OpenAI API). pub chatgpt_base_url: String, + + /// Experimental rollout resume path (absolute path to .jsonl; undocumented). + pub experimental_resume: Option, } impl Config { @@ -321,6 +324,9 @@ pub struct ConfigToml { /// Base URL for requests to ChatGPT (as opposed to the OpenAI API). pub chatgpt_base_url: Option, + + /// Experimental rollout resume path (absolute path to .jsonl; undocumented). + pub experimental_resume: Option, } impl ConfigToml { @@ -448,6 +454,9 @@ impl Config { .as_ref() .map(|info| info.max_output_tokens) }); + + let experimental_resume = cfg.experimental_resume; + let config = Self { model, model_context_window, @@ -494,6 +503,8 @@ impl Config { .chatgpt_base_url .or(cfg.chatgpt_base_url) .unwrap_or("https://chatgpt.com/backend-api/".to_string()), + + experimental_resume, }; Ok(config) } @@ -682,6 +693,9 @@ name = "OpenAI using Chat Completions" base_url = "https://api.openai.com/v1" env_key = "OPENAI_API_KEY" wire_api = "chat" +request_max_retries = 4 # retry failed HTTP requests +stream_max_retries = 10 # retry dropped SSE streams +stream_idle_timeout_ms = 300000 # 5m idle timeout [profiles.o3] model = "o3" @@ -722,6 +736,9 @@ disable_response_storage = true query_params: None, http_headers: None, env_http_headers: None, + request_max_retries: Some(4), + stream_max_retries: Some(10), + stream_idle_timeout_ms: Some(300_000), }; let model_provider_map = { let mut model_provider_map = built_in_model_providers(); @@ -800,6 +817,7 @@ disable_response_storage = true model_reasoning_summary: ReasoningSummary::Detailed, model_supports_reasoning_summaries: false, chatgpt_base_url: "https://chatgpt.com/backend-api/".to_string(), + experimental_resume: None, }, o3_profile_config ); @@ -846,6 +864,7 @@ disable_response_storage = true model_reasoning_summary: ReasoningSummary::default(), model_supports_reasoning_summaries: false, chatgpt_base_url: "https://chatgpt.com/backend-api/".to_string(), + experimental_resume: None, }; assert_eq!(expected_gpt3_profile_config, gpt3_profile_config); @@ -907,6 +926,7 @@ disable_response_storage = true model_reasoning_summary: ReasoningSummary::default(), model_supports_reasoning_summaries: false, chatgpt_base_url: "https://chatgpt.com/backend-api/".to_string(), + experimental_resume: None, }; assert_eq!(expected_zdr_profile_config, zdr_profile_config); diff --git a/codex-rs/core/src/flags.rs b/codex-rs/core/src/flags.rs index c21ef67026..c150405491 100644 --- a/codex-rs/core/src/flags.rs +++ b/codex-rs/core/src/flags.rs @@ -11,14 +11,6 @@ env_flags! { pub OPENAI_TIMEOUT_MS: Duration = Duration::from_millis(300_000), |value| { value.parse().map(Duration::from_millis) }; - pub OPENAI_REQUEST_MAX_RETRIES: u64 = 4; - pub OPENAI_STREAM_MAX_RETRIES: u64 = 10; - - // We generally don't want to disconnect; this updates the timeout to be five minutes - // which matches the upstream typescript codex impl. - pub OPENAI_STREAM_IDLE_TIMEOUT_MS: Duration = Duration::from_millis(300_000), |value| { - value.parse().map(Duration::from_millis) - }; /// Fixture path for offline tests (see client.rs). pub CODEX_RS_SSE_FIXTURE: Option<&str> = None; diff --git a/codex-rs/core/src/mcp_connection_manager.rs b/codex-rs/core/src/mcp_connection_manager.rs index 7cf6762752..886e4f8bf7 100644 --- a/codex-rs/core/src/mcp_connection_manager.rs +++ b/codex-rs/core/src/mcp_connection_manager.rs @@ -7,6 +7,7 @@ //! `""` as the key. use std::collections::HashMap; +use std::collections::HashSet; use std::time::Duration; use anyhow::Context; @@ -16,8 +17,13 @@ use codex_mcp_client::McpClient; use mcp_types::ClientCapabilities; use mcp_types::Implementation; use mcp_types::Tool; + +use serde_json::json; +use sha1::Digest; +use sha1::Sha1; use tokio::task::JoinSet; use tracing::info; +use tracing::warn; use crate::config_types::McpServerConfig; @@ -26,7 +32,8 @@ use crate::config_types::McpServerConfig; /// /// OpenAI requires tool names to conform to `^[a-zA-Z0-9_-]+$`, so we must /// choose a delimiter from this character set. -const MCP_TOOL_NAME_DELIMITER: &str = "__OAI_CODEX_MCP__"; +const MCP_TOOL_NAME_DELIMITER: &str = "__"; +const MAX_TOOL_NAME_LENGTH: usize = 64; /// Timeout for the `tools/list` request. const LIST_TOOLS_TIMEOUT: Duration = Duration::from_secs(10); @@ -35,16 +42,42 @@ const LIST_TOOLS_TIMEOUT: Duration = Duration::from_secs(10); /// spawned successfully. pub type ClientStartErrors = HashMap; -fn fully_qualified_tool_name(server: &str, tool: &str) -> String { - format!("{server}{MCP_TOOL_NAME_DELIMITER}{tool}") +fn qualify_tools(tools: Vec) -> HashMap { + let mut used_names = HashSet::new(); + let mut qualified_tools = HashMap::new(); + for tool in tools { + let mut qualified_name = format!( + "{}{}{}", + tool.server_name, MCP_TOOL_NAME_DELIMITER, tool.tool_name + ); + if qualified_name.len() > MAX_TOOL_NAME_LENGTH { + let mut hasher = Sha1::new(); + hasher.update(qualified_name.as_bytes()); + let sha1 = hasher.finalize(); + let sha1_str = format!("{sha1:x}"); + + // Truncate to make room for the hash suffix + let prefix_len = MAX_TOOL_NAME_LENGTH - sha1_str.len(); + + qualified_name = format!("{}{}", &qualified_name[..prefix_len], sha1_str); + } + + if used_names.contains(&qualified_name) { + warn!("skipping duplicated tool {}", qualified_name); + continue; + } + + used_names.insert(qualified_name.clone()); + qualified_tools.insert(qualified_name, tool); + } + + qualified_tools } -pub(crate) fn try_parse_fully_qualified_tool_name(fq_name: &str) -> Option<(String, String)> { - let (server, tool) = fq_name.split_once(MCP_TOOL_NAME_DELIMITER)?; - if server.is_empty() || tool.is_empty() { - return None; - } - Some((server.to_string(), tool.to_string())) +struct ToolInfo { + server_name: String, + tool_name: String, + tool: Tool, } /// A thin wrapper around a set of running [`McpClient`] instances. @@ -57,7 +90,7 @@ pub(crate) struct McpConnectionManager { clients: HashMap>, /// Fully qualified tool name -> tool instance. - tools: HashMap, + tools: HashMap, } impl McpConnectionManager { @@ -103,10 +136,14 @@ impl McpConnectionManager { experimental: None, roots: None, sampling: None, + // https://modelcontextprotocol.io/specification/2025-06-18/client/elicitation#capabilities + // indicates this should be an empty object. + elicitation: Some(json!({})), }, client_info: Implementation { name: "codex-mcp-client".to_owned(), version: env!("CARGO_PKG_VERSION").to_owned(), + title: Some("Codex".into()), }, protocol_version: mcp_types::MCP_SCHEMA_VERSION.to_owned(), }; @@ -141,7 +178,9 @@ impl McpConnectionManager { } } - let tools = list_all_tools(&clients).await?; + let all_tools = list_all_tools(&clients).await?; + + let tools = qualify_tools(all_tools); Ok((Self { clients, tools }, errors)) } @@ -149,7 +188,10 @@ impl McpConnectionManager { /// Returns a single map that contains **all** tools. Each key is the /// fully-qualified name for the tool. pub fn list_all_tools(&self) -> HashMap { - self.tools.clone() + self.tools + .iter() + .map(|(name, tool)| (name.clone(), tool.tool.clone())) + .collect() } /// Invoke the tool indicated by the (server, tool) pair. @@ -171,13 +213,19 @@ impl McpConnectionManager { .await .with_context(|| format!("tool call failed for `{server}/{tool}`")) } + + pub fn parse_tool_name(&self, tool_name: &str) -> Option<(String, String)> { + self.tools + .get(tool_name) + .map(|tool| (tool.server_name.clone(), tool.tool_name.clone())) + } } /// Query every server for its available tools and return a single map that /// contains **all** tools. Each key is the fully-qualified name for the tool. -pub async fn list_all_tools( +async fn list_all_tools( clients: &HashMap>, -) -> Result> { +) -> Result> { let mut join_set = JoinSet::new(); // Spawn one task per server so we can query them concurrently. This @@ -194,18 +242,19 @@ pub async fn list_all_tools( }); } - let mut aggregated: HashMap = HashMap::with_capacity(join_set.len()); + let mut aggregated: Vec = Vec::with_capacity(join_set.len()); while let Some(join_res) = join_set.join_next().await { let (server_name, list_result) = join_res?; let list_result = list_result?; for tool in list_result.tools { - // TODO(mbolin): escape tool names that contain invalid characters. - let fq_name = fully_qualified_tool_name(&server_name, &tool.name); - if aggregated.insert(fq_name.clone(), tool).is_some() { - panic!("tool name collision for '{fq_name}': suspicious"); - } + let tool_info = ToolInfo { + server_name: server_name.clone(), + tool_name: tool.name.clone(), + tool, + }; + aggregated.push(tool_info); } } @@ -224,3 +273,92 @@ fn is_valid_mcp_server_name(server_name: &str) -> bool { .chars() .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-') } + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod tests { + use super::*; + use mcp_types::ToolInputSchema; + + fn create_test_tool(server_name: &str, tool_name: &str) -> ToolInfo { + ToolInfo { + server_name: server_name.to_string(), + tool_name: tool_name.to_string(), + tool: Tool { + annotations: None, + description: Some(format!("Test tool: {tool_name}")), + input_schema: ToolInputSchema { + properties: None, + required: None, + r#type: "object".to_string(), + }, + name: tool_name.to_string(), + output_schema: None, + title: None, + }, + } + } + + #[test] + fn test_qualify_tools_short_non_duplicated_names() { + let tools = vec![ + create_test_tool("server1", "tool1"), + create_test_tool("server1", "tool2"), + ]; + + let qualified_tools = qualify_tools(tools); + + assert_eq!(qualified_tools.len(), 2); + assert!(qualified_tools.contains_key("server1__tool1")); + assert!(qualified_tools.contains_key("server1__tool2")); + } + + #[test] + fn test_qualify_tools_duplicated_names_skipped() { + let tools = vec![ + create_test_tool("server1", "duplicate_tool"), + create_test_tool("server1", "duplicate_tool"), + ]; + + let qualified_tools = qualify_tools(tools); + + // Only the first tool should remain, the second is skipped + assert_eq!(qualified_tools.len(), 1); + assert!(qualified_tools.contains_key("server1__duplicate_tool")); + } + + #[test] + fn test_qualify_tools_long_names_same_server() { + let server_name = "my_server"; + + let tools = vec![ + create_test_tool( + server_name, + "extremely_lengthy_function_name_that_absolutely_surpasses_all_reasonable_limits", + ), + create_test_tool( + server_name, + "yet_another_extremely_lengthy_function_name_that_absolutely_surpasses_all_reasonable_limits", + ), + ]; + + let qualified_tools = qualify_tools(tools); + + assert_eq!(qualified_tools.len(), 2); + + let mut keys: Vec<_> = qualified_tools.keys().cloned().collect(); + keys.sort(); + + assert_eq!(keys[0].len(), 64); + assert_eq!( + keys[0], + "my_server__extremely_lena02e507efc5a9de88637e436690364fd4219e4ef" + ); + + assert_eq!(keys[1].len(), 64); + assert_eq!( + keys[1], + "my_server__yet_another_e1c3987bd9c50b826cbe1687966f79f0c602d19ca" + ); + } +} diff --git a/codex-rs/core/src/model_provider_info.rs b/codex-rs/core/src/model_provider_info.rs index b38c912d34..72ef58c60a 100644 --- a/codex-rs/core/src/model_provider_info.rs +++ b/codex-rs/core/src/model_provider_info.rs @@ -9,6 +9,7 @@ use serde::Deserialize; use serde::Serialize; use std::collections::HashMap; use std::env::VarError; +use std::time::Duration; use crate::error::EnvVarError; use crate::openai_api_key::get_openai_api_key; @@ -16,6 +17,9 @@ use crate::openai_api_key::get_openai_api_key; /// Value for the `OpenAI-Originator` header that is sent with requests to /// OpenAI. const OPENAI_ORIGINATOR_HEADER: &str = "codex_cli_rs"; +const DEFAULT_STREAM_IDLE_TIMEOUT_MS: u64 = 300_000; +const DEFAULT_STREAM_MAX_RETRIES: u64 = 10; +const DEFAULT_REQUEST_MAX_RETRIES: u64 = 4; /// Wire protocol that the provider speaks. Most third-party services only /// implement the classic OpenAI Chat Completions JSON schema, whereas OpenAI @@ -26,7 +30,7 @@ const OPENAI_ORIGINATOR_HEADER: &str = "codex_cli_rs"; #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] pub enum WireApi { - /// The experimental “Responses” API exposed by OpenAI at `/v1/responses`. + /// The experimental "Responses" API exposed by OpenAI at `/v1/responses`. Responses, /// Regular Chat Completions compatible with `/v1/chat/completions`. @@ -64,6 +68,16 @@ pub struct ModelProviderInfo { /// value should be used. If the environment variable is not set, or the /// value is empty, the header will not be included in the request. pub env_http_headers: Option>, + + /// Maximum number of times to retry a failed HTTP request to this provider. + pub request_max_retries: Option, + + /// Number of times to retry reconnecting a dropped streaming response before failing. + pub stream_max_retries: Option, + + /// Idle timeout (in milliseconds) to wait for activity on a streaming response before treating + /// the connection as lost. + pub stream_idle_timeout_ms: Option, } impl ModelProviderInfo { @@ -161,6 +175,25 @@ impl ModelProviderInfo { None => Ok(None), } } + + /// Effective maximum number of request retries for this provider. + pub fn request_max_retries(&self) -> u64 { + self.request_max_retries + .unwrap_or(DEFAULT_REQUEST_MAX_RETRIES) + } + + /// Effective maximum number of stream reconnection attempts for this provider. + pub fn stream_max_retries(&self) -> u64 { + self.stream_max_retries + .unwrap_or(DEFAULT_STREAM_MAX_RETRIES) + } + + /// Effective idle timeout for streaming responses. + pub fn stream_idle_timeout(&self) -> Duration { + self.stream_idle_timeout_ms + .map(Duration::from_millis) + .unwrap_or(Duration::from_millis(DEFAULT_STREAM_IDLE_TIMEOUT_MS)) + } } /// Built-in default provider list. @@ -205,6 +238,10 @@ pub fn built_in_model_providers() -> HashMap { .into_iter() .collect(), ), + // Use global defaults for retry/timeout unless overridden in config.toml. + request_max_retries: None, + stream_max_retries: None, + stream_idle_timeout_ms: None, }, ), ] @@ -234,6 +271,9 @@ base_url = "http://localhost:11434/v1" query_params: None, http_headers: None, env_http_headers: None, + request_max_retries: None, + stream_max_retries: None, + stream_idle_timeout_ms: None, }; let provider: ModelProviderInfo = toml::from_str(azure_provider_toml).unwrap(); @@ -259,6 +299,9 @@ query_params = { api-version = "2025-04-01-preview" } }), http_headers: None, env_http_headers: None, + request_max_retries: None, + stream_max_retries: None, + stream_idle_timeout_ms: None, }; let provider: ModelProviderInfo = toml::from_str(azure_provider_toml).unwrap(); @@ -287,6 +330,9 @@ env_http_headers = { "X-Example-Env-Header" = "EXAMPLE_ENV_VAR" } env_http_headers: Some(maplit::hashmap! { "X-Example-Env-Header".to_string() => "EXAMPLE_ENV_VAR".to_string(), }), + request_max_retries: None, + stream_max_retries: None, + stream_idle_timeout_ms: None, }; let provider: ModelProviderInfo = toml::from_str(azure_provider_toml).unwrap(); diff --git a/codex-rs/core/src/protocol.rs b/codex-rs/core/src/protocol.rs index cbdb23b3a3..aa6728bb05 100644 --- a/codex-rs/core/src/protocol.rs +++ b/codex-rs/core/src/protocol.rs @@ -69,6 +69,10 @@ pub enum Op { /// `ConfigureSession` operation so that the business-logic layer can /// operate deterministically. cwd: std::path::PathBuf, + + /// Path to a rollout file to resume from. + #[serde(skip_serializing_if = "Option::is_none")] + resume_path: Option, }, /// Abort current task. diff --git a/codex-rs/core/src/rollout.rs b/codex-rs/core/src/rollout.rs index c18a58df06..bb2abe45cd 100644 --- a/codex-rs/core/src/rollout.rs +++ b/codex-rs/core/src/rollout.rs @@ -1,33 +1,47 @@ -//! Functionality to persist a Codex conversation *rollout* – a linear list of -//! [`ResponseItem`] objects exchanged during a session – to disk so that -//! sessions can be replayed or inspected later (mirrors the behaviour of the -//! upstream TypeScript implementation). +//! Persist Codex session rollouts (.jsonl) so sessions can be replayed or inspected later. use std::fs::File; use std::fs::{self}; use std::io::Error as IoError; +use std::path::Path; +use serde::Deserialize; use serde::Serialize; +use serde_json::Value; use time::OffsetDateTime; use time::format_description::FormatItem; use time::macros::format_description; use tokio::io::AsyncWriteExt; use tokio::sync::mpsc::Sender; use tokio::sync::mpsc::{self}; +use tracing::info; use uuid::Uuid; use crate::config::Config; use crate::models::ResponseItem; -/// Folder inside `~/.codex` that holds saved rollouts. const SESSIONS_SUBDIR: &str = "sessions"; -#[derive(Serialize)] -struct SessionMeta { - id: String, - timestamp: String, - #[serde(skip_serializing_if = "Option::is_none")] - instructions: Option, +#[derive(Serialize, Deserialize, Clone, Default)] +pub struct SessionMeta { + pub id: Uuid, + pub timestamp: String, + pub instructions: Option, +} + +#[derive(Serialize, Deserialize, Default, Clone)] +pub struct SessionStateSnapshot { + pub previous_response_id: Option, +} + +#[derive(Serialize, Deserialize, Default, Clone)] +pub struct SavedSession { + pub session: SessionMeta, + #[serde(default)] + pub items: Vec, + #[serde(default)] + pub state: SessionStateSnapshot, + pub session_id: Uuid, } /// Records all [`ResponseItem`]s for a session and flushes them to disk after @@ -41,7 +55,13 @@ struct SessionMeta { /// ``` #[derive(Clone)] pub(crate) struct RolloutRecorder { - tx: Sender, + tx: Sender, +} + +#[derive(Clone)] +enum RolloutCmd { + AddItems(Vec), + UpdateState(SessionStateSnapshot), } impl RolloutRecorder { @@ -59,7 +79,6 @@ impl RolloutRecorder { timestamp, } = create_log_file(config, uuid)?; - // Build the static session metadata JSON first. let timestamp_format: &[FormatItem] = format_description!( "[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:3]Z" ); @@ -69,46 +88,29 @@ impl RolloutRecorder { let meta = SessionMeta { timestamp, - id: session_id.to_string(), + id: session_id, instructions, }; // A reasonably-sized bounded channel. If the buffer fills up the send // future will yield, which is fine – we only need to ensure we do not // perform *blocking* I/O on the caller’s thread. - let (tx, mut rx) = mpsc::channel::(256); + let (tx, rx) = mpsc::channel::(256); // Spawn a Tokio task that owns the file handle and performs async // writes. Using `tokio::fs::File` keeps everything on the async I/O // driver instead of blocking the runtime. - tokio::task::spawn(async move { - let mut file = tokio::fs::File::from_std(file); + tokio::task::spawn(rollout_writer( + tokio::fs::File::from_std(file), + rx, + Some(meta), + )); - while let Some(line) = rx.recv().await { - // Write line + newline, then flush to disk. - if let Err(e) = file.write_all(line.as_bytes()).await { - tracing::warn!("rollout writer: failed to write line: {e}"); - break; - } - if let Err(e) = file.write_all(b"\n").await { - tracing::warn!("rollout writer: failed to write newline: {e}"); - break; - } - if let Err(e) = file.flush().await { - tracing::warn!("rollout writer: failed to flush: {e}"); - break; - } - } - }); - - let recorder = Self { tx }; - // Ensure SessionMeta is the first item in the file. - recorder.record_item(&meta).await?; - Ok(recorder) + Ok(Self { tx }) } - /// Append `items` to the rollout file. pub(crate) async fn record_items(&self, items: &[ResponseItem]) -> std::io::Result<()> { + let mut filtered = Vec::new(); for item in items { match item { // Note that function calls may look a bit strange if they are @@ -117,27 +119,86 @@ impl RolloutRecorder { ResponseItem::Message { .. } | ResponseItem::LocalShellCall { .. } | ResponseItem::FunctionCall { .. } - | ResponseItem::FunctionCallOutput { .. } => {} + | ResponseItem::FunctionCallOutput { .. } => filtered.push(item.clone()), ResponseItem::Reasoning { .. } | ResponseItem::Other => { // These should never be serialized. continue; } } - self.record_item(item).await?; } - Ok(()) + if filtered.is_empty() { + return Ok(()); + } + self.tx + .send(RolloutCmd::AddItems(filtered)) + .await + .map_err(|e| IoError::other(format!("failed to queue rollout items: {e}"))) } - async fn record_item(&self, item: &impl Serialize) -> std::io::Result<()> { - // Serialize the item to JSON first so that the writer thread only has - // to perform the actual write. - let json = serde_json::to_string(item) - .map_err(|e| IoError::other(format!("failed to serialize response items: {e}")))?; - + pub(crate) async fn record_state(&self, state: SessionStateSnapshot) -> std::io::Result<()> { self.tx - .send(json) + .send(RolloutCmd::UpdateState(state)) .await - .map_err(|e| IoError::other(format!("failed to queue rollout item: {e}"))) + .map_err(|e| IoError::other(format!("failed to queue rollout state: {e}"))) + } + + pub async fn resume(path: &Path) -> std::io::Result<(Self, SavedSession)> { + info!("Resuming rollout from {path:?}"); + let text = tokio::fs::read_to_string(path).await?; + let mut lines = text.lines(); + let meta_line = lines + .next() + .ok_or_else(|| IoError::other("empty session file"))?; + let session: SessionMeta = serde_json::from_str(meta_line) + .map_err(|e| IoError::other(format!("failed to parse session meta: {e}")))?; + let mut items = Vec::new(); + let mut state = SessionStateSnapshot::default(); + + for line in lines { + if line.trim().is_empty() { + continue; + } + let v: Value = match serde_json::from_str(line) { + Ok(v) => v, + Err(_) => continue, + }; + if v.get("record_type") + .and_then(|rt| rt.as_str()) + .map(|s| s == "state") + .unwrap_or(false) + { + if let Ok(s) = serde_json::from_value::(v.clone()) { + state = s + } + continue; + } + if let Ok(item) = serde_json::from_value::(v.clone()) { + match item { + ResponseItem::Message { .. } + | ResponseItem::LocalShellCall { .. } + | ResponseItem::FunctionCall { .. } + | ResponseItem::FunctionCallOutput { .. } => items.push(item), + ResponseItem::Reasoning { .. } | ResponseItem::Other => {} + } + } + } + + let saved = SavedSession { + session: session.clone(), + items: items.clone(), + state: state.clone(), + session_id: session.id, + }; + + let file = std::fs::OpenOptions::new() + .append(true) + .read(true) + .open(path)?; + + let (tx, rx) = mpsc::channel::(256); + tokio::task::spawn(rollout_writer(tokio::fs::File::from_std(file), rx, None)); + info!("Resumed rollout successfully from {path:?}"); + Ok((Self { tx }, saved)) } } @@ -153,13 +214,15 @@ struct LogFileInfo { } fn create_log_file(config: &Config, session_id: Uuid) -> std::io::Result { - // Resolve ~/.codex/sessions and create it if missing. - let mut dir = config.codex_home.clone(); - dir.push(SESSIONS_SUBDIR); - fs::create_dir_all(&dir)?; - + // Resolve ~/.codex/sessions/YYYY/MM/DD and create it if missing. let timestamp = OffsetDateTime::now_local() .map_err(|e| IoError::other(format!("failed to get local time: {e}")))?; + let mut dir = config.codex_home.clone(); + dir.push(SESSIONS_SUBDIR); + dir.push(timestamp.year().to_string()); + dir.push(format!("{:02}", u8::from(timestamp.month()))); + dir.push(format!("{:02}", timestamp.day())); + fs::create_dir_all(&dir)?; // Custom format for YYYY-MM-DDThh-mm-ss. Use `-` instead of `:` for // compatibility with filesystems that do not allow colons in filenames. @@ -183,3 +246,54 @@ fn create_log_file(config: &Config, session_id: Uuid) -> std::io::Result, + meta: Option, +) { + if let Some(meta) = meta { + if let Ok(json) = serde_json::to_string(&meta) { + let _ = file.write_all(json.as_bytes()).await; + let _ = file.write_all(b"\n").await; + let _ = file.flush().await; + } + } + while let Some(cmd) = rx.recv().await { + match cmd { + RolloutCmd::AddItems(items) => { + for item in items { + match item { + ResponseItem::Message { .. } + | ResponseItem::LocalShellCall { .. } + | ResponseItem::FunctionCall { .. } + | ResponseItem::FunctionCallOutput { .. } => { + if let Ok(json) = serde_json::to_string(&item) { + let _ = file.write_all(json.as_bytes()).await; + let _ = file.write_all(b"\n").await; + } + } + ResponseItem::Reasoning { .. } | ResponseItem::Other => {} + } + } + let _ = file.flush().await; + } + RolloutCmd::UpdateState(state) => { + #[derive(Serialize)] + struct StateLine<'a> { + record_type: &'static str, + #[serde(flatten)] + state: &'a SessionStateSnapshot, + } + if let Ok(json) = serde_json::to_string(&StateLine { + record_type: "state", + state: &state, + }) { + let _ = file.write_all(json.as_bytes()).await; + let _ = file.write_all(b"\n").await; + let _ = file.flush().await; + } + } + } + } +} diff --git a/codex-rs/core/tests/cli_stream.rs b/codex-rs/core/tests/cli_stream.rs index 9ef042eb1a..567279ebd0 100644 --- a/codex-rs/core/tests/cli_stream.rs +++ b/codex-rs/core/tests/cli_stream.rs @@ -2,7 +2,11 @@ use assert_cmd::Command as AssertCommand; use codex_core::exec::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR; +use std::time::Duration; +use std::time::Instant; use tempfile::TempDir; +use uuid::Uuid; +use walkdir::WalkDir; use wiremock::Mock; use wiremock::MockServer; use wiremock::ResponseTemplate; @@ -117,3 +121,241 @@ async fn responses_api_stream_cli() { let stdout = String::from_utf8_lossy(&output.stdout); assert!(stdout.contains("fixture hello")); } + +/// End-to-end: create a session (writes rollout), verify the file, then resume and confirm append. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn integration_creates_and_checks_session_file() { + // Honor sandbox network restrictions for CI parity with the other tests. + if std::env::var(CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR).is_ok() { + println!( + "Skipping test because it cannot execute when network is disabled in a Codex sandbox." + ); + return; + } + + // 1. Temp home so we read/write isolated session files. + let home = TempDir::new().unwrap(); + + // 2. Unique marker we'll look for in the session log. + let marker = format!("integration-test-{}", Uuid::new_v4()); + let prompt = format!("echo {marker}"); + + // 3. Use the same offline SSE fixture as responses_api_stream_cli so the test is hermetic. + let fixture = + std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/cli_responses_fixture.sse"); + + // 4. Run the codex CLI through cargo (ensures the right bin is built) and invoke `exec`, + // which is what records a session. + let mut cmd = AssertCommand::new("cargo"); + cmd.arg("run") + .arg("-p") + .arg("codex-cli") + .arg("--quiet") + .arg("--") + .arg("exec") + .arg("--skip-git-repo-check") + .arg("-C") + .arg(env!("CARGO_MANIFEST_DIR")) + .arg(&prompt); + cmd.env("CODEX_HOME", home.path()) + .env("OPENAI_API_KEY", "dummy") + .env("CODEX_RS_SSE_FIXTURE", &fixture) + // Required for CLI arg parsing even though fixture short-circuits network usage. + .env("OPENAI_BASE_URL", "http://unused.local"); + + let output = cmd.output().unwrap(); + assert!( + output.status.success(), + "codex-cli exec failed: {}", + String::from_utf8_lossy(&output.stderr) + ); + + // Wait for sessions dir to appear. + let sessions_dir = home.path().join("sessions"); + let dir_deadline = Instant::now() + Duration::from_secs(5); + while !sessions_dir.exists() && Instant::now() < dir_deadline { + std::thread::sleep(Duration::from_millis(50)); + } + assert!(sessions_dir.exists(), "sessions directory never appeared"); + + // Find the session file that contains `marker`. + let deadline = Instant::now() + Duration::from_secs(10); + let mut matching_path: Option = None; + while Instant::now() < deadline && matching_path.is_none() { + for entry in WalkDir::new(&sessions_dir) { + let entry = match entry { + Ok(e) => e, + Err(_) => continue, + }; + if !entry.file_type().is_file() { + continue; + } + if !entry.file_name().to_string_lossy().ends_with(".jsonl") { + continue; + } + let path = entry.path(); + let Ok(content) = std::fs::read_to_string(path) else { + continue; + }; + let mut lines = content.lines(); + if lines.next().is_none() { + continue; + } + for line in lines { + if line.trim().is_empty() { + continue; + } + let item: serde_json::Value = match serde_json::from_str(line) { + Ok(v) => v, + Err(_) => continue, + }; + if item.get("type").and_then(|t| t.as_str()) == Some("message") { + if let Some(c) = item.get("content") { + if c.to_string().contains(&marker) { + matching_path = Some(path.to_path_buf()); + break; + } + } + } + } + } + if matching_path.is_none() { + std::thread::sleep(Duration::from_millis(50)); + } + } + + let path = match matching_path { + Some(p) => p, + None => panic!("No session file containing the marker was found"), + }; + + // Basic sanity checks on location and metadata. + let rel = match path.strip_prefix(&sessions_dir) { + Ok(r) => r, + Err(_) => panic!("session file should live under sessions/"), + }; + let comps: Vec = rel + .components() + .map(|c| c.as_os_str().to_string_lossy().into_owned()) + .collect(); + assert_eq!( + comps.len(), + 4, + "Expected sessions/YYYY/MM/DD/, got {rel:?}" + ); + let year = &comps[0]; + let month = &comps[1]; + let day = &comps[2]; + assert!( + year.len() == 4 && year.chars().all(|c| c.is_ascii_digit()), + "Year dir not 4-digit numeric: {year}" + ); + assert!( + month.len() == 2 && month.chars().all(|c| c.is_ascii_digit()), + "Month dir not zero-padded 2-digit numeric: {month}" + ); + assert!( + day.len() == 2 && day.chars().all(|c| c.is_ascii_digit()), + "Day dir not zero-padded 2-digit numeric: {day}" + ); + if let Ok(m) = month.parse::() { + assert!((1..=12).contains(&m), "Month out of range: {m}"); + } + if let Ok(d) = day.parse::() { + assert!((1..=31).contains(&d), "Day out of range: {d}"); + } + + let content = + std::fs::read_to_string(&path).unwrap_or_else(|_| panic!("Failed to read session file")); + let mut lines = content.lines(); + let meta_line = lines + .next() + .ok_or("missing session meta line") + .unwrap_or_else(|_| panic!("missing session meta line")); + let meta: serde_json::Value = serde_json::from_str(meta_line) + .unwrap_or_else(|_| panic!("Failed to parse session meta line as JSON")); + assert!(meta.get("id").is_some(), "SessionMeta missing id"); + assert!( + meta.get("timestamp").is_some(), + "SessionMeta missing timestamp" + ); + + let mut found_message = false; + for line in lines { + if line.trim().is_empty() { + continue; + } + let Ok(item) = serde_json::from_str::(line) else { + continue; + }; + if item.get("type").and_then(|t| t.as_str()) == Some("message") { + if let Some(c) = item.get("content") { + if c.to_string().contains(&marker) { + found_message = true; + break; + } + } + } + } + assert!( + found_message, + "No message found in session file containing the marker" + ); + + // Second run: resume and append. + let orig_len = content.lines().count(); + let marker2 = format!("integration-resume-{}", Uuid::new_v4()); + let prompt2 = format!("echo {marker2}"); + // Cross‑platform safe resume override. On Windows, backslashes in a TOML string must be escaped + // or the parse will fail and the raw literal (including quotes) may be preserved all the way down + // to Config, which in turn breaks resume because the path is invalid. Normalize to forward slashes + // to sidestep the issue. + let resume_path_str = path.to_string_lossy().replace('\\', "/"); + let resume_override = format!("experimental_resume=\"{resume_path_str}\""); + let mut cmd2 = AssertCommand::new("cargo"); + cmd2.arg("run") + .arg("-p") + .arg("codex-cli") + .arg("--quiet") + .arg("--") + .arg("exec") + .arg("--skip-git-repo-check") + .arg("-c") + .arg(&resume_override) + .arg("-C") + .arg(env!("CARGO_MANIFEST_DIR")) + .arg(&prompt2); + cmd2.env("CODEX_HOME", home.path()) + .env("OPENAI_API_KEY", "dummy") + .env("CODEX_RS_SSE_FIXTURE", &fixture) + .env("OPENAI_BASE_URL", "http://unused.local"); + let output2 = cmd2.output().unwrap(); + assert!(output2.status.success(), "resume codex-cli run failed"); + + // The rollout writer runs on a background async task; give it a moment to flush. + let mut new_len = orig_len; + let deadline = Instant::now() + Duration::from_secs(5); + let mut content2 = String::new(); + while Instant::now() < deadline { + if let Ok(c) = std::fs::read_to_string(&path) { + let count = c.lines().count(); + if count > orig_len { + content2 = c; + new_len = count; + break; + } + } + std::thread::sleep(Duration::from_millis(50)); + } + if content2.is_empty() { + // last attempt + content2 = std::fs::read_to_string(&path).unwrap(); + new_len = content2.lines().count(); + } + assert!(new_len > orig_len, "rollout file did not grow after resume"); + assert!(content2.contains(&marker), "rollout lost original marker"); + assert!( + content2.contains(&marker2), + "rollout missing resumed marker" + ); +} diff --git a/codex-rs/core/tests/client.rs b/codex-rs/core/tests/client.rs new file mode 100644 index 0000000000..964710b83f --- /dev/null +++ b/codex-rs/core/tests/client.rs @@ -0,0 +1,113 @@ +use std::time::Duration; + +use codex_core::Codex; +use codex_core::ModelProviderInfo; +use codex_core::exec::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR; +use codex_core::protocol::EventMsg; +use codex_core::protocol::InputItem; +use codex_core::protocol::Op; +use codex_core::protocol::SessionConfiguredEvent; +mod test_support; +use tempfile::TempDir; +use test_support::load_default_config_for_test; +use test_support::load_sse_fixture_with_id; +use tokio::time::timeout; +use wiremock::Mock; +use wiremock::MockServer; +use wiremock::ResponseTemplate; +use wiremock::matchers::method; +use wiremock::matchers::path; + +/// Build minimal SSE stream with completed marker using the JSON fixture. +fn sse_completed(id: &str) -> String { + load_sse_fixture_with_id("tests/fixtures/completed_template.json", id) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn includes_session_id_and_model_headers_in_request() { + #![allow(clippy::unwrap_used)] + + if std::env::var(CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR).is_ok() { + println!( + "Skipping test because it cannot execute when network is disabled in a Codex sandbox." + ); + return; + } + + // Mock server + let server = MockServer::start().await; + + // First request – must NOT include `previous_response_id`. + let first = ResponseTemplate::new(200) + .insert_header("content-type", "text/event-stream") + .set_body_raw(sse_completed("resp1"), "text/event-stream"); + + Mock::given(method("POST")) + .and(path("/v1/responses")) + .respond_with(first) + .expect(1) + .mount(&server) + .await; + + let model_provider = ModelProviderInfo { + name: "openai".into(), + base_url: format!("{}/v1", server.uri()), + // Environment variable that should exist in the test environment. + // ModelClient will return an error if the environment variable for the + // provider is not set. + env_key: Some("PATH".into()), + env_key_instructions: None, + wire_api: codex_core::WireApi::Responses, + query_params: None, + http_headers: Some( + [("originator".to_string(), "codex_cli_rs".to_string())] + .into_iter() + .collect(), + ), + env_http_headers: None, + request_max_retries: Some(0), + stream_max_retries: Some(0), + stream_idle_timeout_ms: None, + }; + + // Init session + let codex_home = TempDir::new().unwrap(); + let mut config = load_default_config_for_test(&codex_home); + config.model_provider = model_provider; + let ctrl_c = std::sync::Arc::new(tokio::sync::Notify::new()); + let (codex, _init_id) = Codex::spawn(config, ctrl_c.clone()).await.unwrap(); + + codex + .submit(Op::UserInput { + items: vec![InputItem::Text { + text: "hello".into(), + }], + }) + .await + .unwrap(); + + let mut current_session_id = None; + // Wait for TaskComplete + loop { + let ev = timeout(Duration::from_secs(1), codex.next_event()) + .await + .unwrap() + .unwrap(); + + if let EventMsg::SessionConfigured(SessionConfiguredEvent { session_id, .. }) = ev.msg { + current_session_id = Some(session_id.to_string()); + } + if matches!(ev.msg, EventMsg::TaskComplete(_)) { + break; + } + } + + // get request from the server + let request = &server.received_requests().await.unwrap()[0]; + let request_body = request.headers.get("session_id").unwrap(); + let originator = request.headers.get("originator").unwrap(); + + assert!(current_session_id.is_some()); + assert_eq!(request_body.to_str().unwrap(), ¤t_session_id.unwrap()); + assert_eq!(originator.to_str().unwrap(), "codex_cli_rs"); +} diff --git a/codex-rs/core/tests/live_agent.rs b/codex-rs/core/tests/live_agent.rs index c21f9d0032..26a5539dd7 100644 --- a/codex-rs/core/tests/live_agent.rs +++ b/codex-rs/core/tests/live_agent.rs @@ -45,22 +45,10 @@ async fn spawn_codex() -> Result { "OPENAI_API_KEY must be set for live tests" ); - // Environment tweaks to keep the tests snappy and inexpensive while still - // exercising retry/robustness logic. - // - // NOTE: Starting with the 2024 edition `std::env::set_var` is `unsafe` - // because changing the process environment races with any other threads - // that might be performing environment look-ups at the same time. - // Restrict the unsafety to this tiny block that happens at the very - // beginning of the test, before we spawn any background tasks that could - // observe the environment. - unsafe { - std::env::set_var("OPENAI_REQUEST_MAX_RETRIES", "2"); - std::env::set_var("OPENAI_STREAM_MAX_RETRIES", "2"); - } - let codex_home = TempDir::new().unwrap(); - let config = load_default_config_for_test(&codex_home); + let mut config = load_default_config_for_test(&codex_home); + config.model_provider.request_max_retries = Some(2); + config.model_provider.stream_max_retries = Some(2); let (agent, _init_id) = Codex::spawn(config, std::sync::Arc::new(Notify::new())).await?; Ok(agent) @@ -79,7 +67,7 @@ async fn live_streaming_and_prev_id_reset() { let codex = spawn_codex().await.unwrap(); - // ---------- Task 1 ---------- + // ---------- Task 1 ---------- codex .submit(Op::UserInput { items: vec![InputItem::Text { @@ -113,7 +101,7 @@ async fn live_streaming_and_prev_id_reset() { "Agent did not stream any AgentMessage before TaskComplete" ); - // ---------- Task 2 (same session) ---------- + // ---------- Task 2 (same session) ---------- codex .submit(Op::UserInput { items: vec![InputItem::Text { diff --git a/codex-rs/core/tests/previous_response_id.rs b/codex-rs/core/tests/previous_response_id.rs index e64271a0ff..9630cc1028 100644 --- a/codex-rs/core/tests/previous_response_id.rs +++ b/codex-rs/core/tests/previous_response_id.rs @@ -88,13 +88,8 @@ async fn keeps_previous_response_id_between_tasks() { .mount(&server) .await; - // Environment - // Update environment – `set_var` is `unsafe` starting with the 2024 - // edition so we group the calls into a single `unsafe { … }` block. - unsafe { - std::env::set_var("OPENAI_REQUEST_MAX_RETRIES", "0"); - std::env::set_var("OPENAI_STREAM_MAX_RETRIES", "0"); - } + // Configure retry behavior explicitly to avoid mutating process-wide + // environment variables. let model_provider = ModelProviderInfo { name: "openai".into(), base_url: format!("{}/v1", server.uri()), @@ -107,6 +102,10 @@ async fn keeps_previous_response_id_between_tasks() { query_params: None, http_headers: None, env_http_headers: None, + // disable retries so we don't get duplicate calls in this test + request_max_retries: Some(0), + stream_max_retries: Some(0), + stream_idle_timeout_ms: None, }; // Init session diff --git a/codex-rs/core/tests/stream_no_completed.rs b/codex-rs/core/tests/stream_no_completed.rs index 8883eff373..f2de5de188 100644 --- a/codex-rs/core/tests/stream_no_completed.rs +++ b/codex-rs/core/tests/stream_no_completed.rs @@ -32,8 +32,6 @@ fn sse_completed(id: &str) -> String { } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -// this test is flaky (has race conditions), so we ignore it for now -#[ignore] async fn retries_on_early_close() { #![allow(clippy::unwrap_used)] @@ -72,19 +70,8 @@ async fn retries_on_early_close() { .mount(&server) .await; - // Environment - // - // As of Rust 2024 `std::env::set_var` has been made `unsafe` because - // mutating the process environment is inherently racy when other threads - // are running. We therefore have to wrap every call in an explicit - // `unsafe` block. These are limited to the test-setup section so the - // scope is very small and clearly delineated. - - unsafe { - std::env::set_var("OPENAI_REQUEST_MAX_RETRIES", "0"); - std::env::set_var("OPENAI_STREAM_MAX_RETRIES", "1"); - std::env::set_var("OPENAI_STREAM_IDLE_TIMEOUT_MS", "2000"); - } + // Configure retry behavior explicitly to avoid mutating process-wide + // environment variables. let model_provider = ModelProviderInfo { name: "openai".into(), @@ -98,6 +85,10 @@ async fn retries_on_early_close() { query_params: None, http_headers: None, env_http_headers: None, + // exercise retry path: first attempt yields incomplete stream, so allow 1 retry + request_max_retries: Some(0), + stream_max_retries: Some(1), + stream_idle_timeout_ms: Some(2000), }; let ctrl_c = std::sync::Arc::new(tokio::sync::Notify::new()); diff --git a/codex-rs/exec/src/cli.rs b/codex-rs/exec/src/cli.rs index 613fedf0a1..53af25c7e9 100644 --- a/codex-rs/exec/src/cli.rs +++ b/codex-rs/exec/src/cli.rs @@ -51,6 +51,10 @@ pub struct Cli { #[arg(long = "color", value_enum, default_value_t = Color::Auto)] pub color: Color, + /// Print events to stdout as JSONL. + #[arg(long = "json", default_value_t = false)] + pub json: bool, + /// Specifies file where the last message from the agent should be written. #[arg(long = "output-last-message")] pub last_message_file: Option, diff --git a/codex-rs/exec/src/event_processor.rs b/codex-rs/exec/src/event_processor.rs index 5ab09994b1..56db651a83 100644 --- a/codex-rs/exec/src/event_processor.rs +++ b/codex-rs/exec/src/event_processor.rs @@ -1,539 +1,37 @@ -use codex_common::elapsed::format_elapsed; use codex_common::summarize_sandbox_policy; use codex_core::WireApi; use codex_core::config::Config; use codex_core::model_supports_reasoning_summaries; -use codex_core::protocol::AgentMessageDeltaEvent; -use codex_core::protocol::AgentMessageEvent; -use codex_core::protocol::AgentReasoningDeltaEvent; -use codex_core::protocol::BackgroundEventEvent; -use codex_core::protocol::ErrorEvent; use codex_core::protocol::Event; -use codex_core::protocol::EventMsg; -use codex_core::protocol::ExecCommandBeginEvent; -use codex_core::protocol::ExecCommandEndEvent; -use codex_core::protocol::FileChange; -use codex_core::protocol::McpToolCallBeginEvent; -use codex_core::protocol::McpToolCallEndEvent; -use codex_core::protocol::PatchApplyBeginEvent; -use codex_core::protocol::PatchApplyEndEvent; -use codex_core::protocol::SessionConfiguredEvent; -use codex_core::protocol::TokenUsage; -use owo_colors::OwoColorize; -use owo_colors::Style; -use shlex::try_join; -use std::collections::HashMap; -use std::io::Write; -use std::time::Instant; -/// This should be configurable. When used in CI, users may not want to impose -/// a limit so they can see the full transcript. -const MAX_OUTPUT_LINES_FOR_EXEC_TOOL_CALL: usize = 20; +pub(crate) trait EventProcessor { + /// Print summary of effective configuration and user prompt. + fn print_config_summary(&mut self, config: &Config, prompt: &str); -pub(crate) struct EventProcessor { - call_id_to_command: HashMap, - call_id_to_patch: HashMap, - - /// Tracks in-flight MCP tool calls so we can calculate duration and print - /// a concise summary when the corresponding `McpToolCallEnd` event is - /// received. - call_id_to_tool_call: HashMap, - - // To ensure that --color=never is respected, ANSI escapes _must_ be added - // using .style() with one of these fields. If you need a new style, add a - // new field here. - bold: Style, - italic: Style, - dimmed: Style, - - magenta: Style, - red: Style, - green: Style, - cyan: Style, - - /// Whether to include `AgentReasoning` events in the output. - show_agent_reasoning: bool, - answer_started: bool, - reasoning_started: bool, + /// Handle a single event emitted by the agent. + fn process_event(&mut self, event: Event); } -impl EventProcessor { - pub(crate) fn create_with_ansi(with_ansi: bool, config: &Config) -> Self { - let call_id_to_command = HashMap::new(); - let call_id_to_patch = HashMap::new(); - let call_id_to_tool_call = HashMap::new(); - - if with_ansi { - Self { - call_id_to_command, - call_id_to_patch, - bold: Style::new().bold(), - italic: Style::new().italic(), - dimmed: Style::new().dimmed(), - magenta: Style::new().magenta(), - red: Style::new().red(), - green: Style::new().green(), - cyan: Style::new().cyan(), - call_id_to_tool_call, - show_agent_reasoning: !config.hide_agent_reasoning, - answer_started: false, - reasoning_started: false, - } - } else { - Self { - call_id_to_command, - call_id_to_patch, - bold: Style::new(), - italic: Style::new(), - dimmed: Style::new(), - magenta: Style::new(), - red: Style::new(), - green: Style::new(), - cyan: Style::new(), - call_id_to_tool_call, - show_agent_reasoning: !config.hide_agent_reasoning, - answer_started: false, - reasoning_started: false, - } - } - } -} - -struct ExecCommandBegin { - command: Vec, - start_time: Instant, -} - -/// Metadata captured when an `McpToolCallBegin` event is received. -struct McpToolCallBegin { - /// Formatted invocation string, e.g. `server.tool({"city":"sf"})`. - invocation: String, - /// Timestamp when the call started so we can compute duration later. - start_time: Instant, -} - -struct PatchApplyBegin { - start_time: Instant, - auto_approved: bool, -} - -// Timestamped println helper. The timestamp is styled with self.dimmed. -#[macro_export] -macro_rules! ts_println { - ($self:ident, $($arg:tt)*) => {{ - let now = chrono::Utc::now(); - let formatted = now.format("[%Y-%m-%dT%H:%M:%S]"); - print!("{} ", formatted.style($self.dimmed)); - println!($($arg)*); - }}; -} - -impl EventProcessor { - /// Print a concise summary of the effective configuration that will be used - /// for the session. This mirrors the information shown in the TUI welcome - /// screen. - pub(crate) fn print_config_summary(&mut self, config: &Config, prompt: &str) { - const VERSION: &str = env!("CARGO_PKG_VERSION"); - ts_println!( - self, - "OpenAI Codex v{} (research preview)\n--------", - VERSION - ); - - let mut entries = vec![ - ("workdir", config.cwd.display().to_string()), - ("model", config.model.clone()), - ("provider", config.model_provider_id.clone()), - ("approval", format!("{:?}", config.approval_policy)), - ("sandbox", summarize_sandbox_policy(&config.sandbox_policy)), - ]; - if config.model_provider.wire_api == WireApi::Responses - && model_supports_reasoning_summaries(config) - { - entries.push(( - "reasoning effort", - config.model_reasoning_effort.to_string(), - )); - entries.push(( - "reasoning summaries", - config.model_reasoning_summary.to_string(), - )); - } - - for (key, value) in entries { - println!("{} {}", format!("{key}:").style(self.bold), value); - } - - println!("--------"); - - // Echo the prompt that will be sent to the agent so it is visible in the - // transcript/logs before any events come in. Note the prompt may have been - // read from stdin, so it may not be visible in the terminal otherwise. - ts_println!( - self, - "{}\n{}", - "User instructions:".style(self.bold).style(self.cyan), - prompt - ); +pub(crate) fn create_config_summary_entries(config: &Config) -> Vec<(&'static str, String)> { + let mut entries = vec![ + ("workdir", config.cwd.display().to_string()), + ("model", config.model.clone()), + ("provider", config.model_provider_id.clone()), + ("approval", format!("{:?}", config.approval_policy)), + ("sandbox", summarize_sandbox_policy(&config.sandbox_policy)), + ]; + if config.model_provider.wire_api == WireApi::Responses + && model_supports_reasoning_summaries(config) + { + entries.push(( + "reasoning effort", + config.model_reasoning_effort.to_string(), + )); + entries.push(( + "reasoning summaries", + config.model_reasoning_summary.to_string(), + )); } - pub(crate) fn process_event(&mut self, event: Event) { - let Event { id: _, msg } = event; - match msg { - EventMsg::Error(ErrorEvent { message }) => { - let prefix = "ERROR:".style(self.red); - ts_println!(self, "{prefix} {message}"); - } - EventMsg::BackgroundEvent(BackgroundEventEvent { message }) => { - ts_println!(self, "{}", message.style(self.dimmed)); - } - EventMsg::TaskStarted | EventMsg::TaskComplete(_) => { - // Ignore. - } - EventMsg::TokenCount(TokenUsage { total_tokens, .. }) => { - ts_println!(self, "tokens used: {total_tokens}"); - } - EventMsg::AgentMessageDelta(AgentMessageDeltaEvent { delta }) => { - if !self.answer_started { - ts_println!(self, "{}\n", "codex".style(self.italic).style(self.magenta)); - self.answer_started = true; - } - print!("{delta}"); - #[allow(clippy::expect_used)] - std::io::stdout().flush().expect("could not flush stdout"); - } - EventMsg::AgentReasoningDelta(AgentReasoningDeltaEvent { delta }) => { - if !self.show_agent_reasoning { - return; - } - if !self.reasoning_started { - ts_println!( - self, - "{}\n", - "thinking".style(self.italic).style(self.magenta), - ); - self.reasoning_started = true; - } - print!("{delta}"); - #[allow(clippy::expect_used)] - std::io::stdout().flush().expect("could not flush stdout"); - } - EventMsg::AgentMessage(AgentMessageEvent { message }) => { - // if answer_started is false, this means we haven't received any - // delta. Thus, we need to print the message as a new answer. - if !self.answer_started { - ts_println!( - self, - "{}\n{}", - "codex".style(self.italic).style(self.magenta), - message, - ); - } else { - println!(); - self.answer_started = false; - } - } - EventMsg::ExecCommandBegin(ExecCommandBeginEvent { - call_id, - command, - cwd, - }) => { - self.call_id_to_command.insert( - call_id.clone(), - ExecCommandBegin { - command: command.clone(), - start_time: Instant::now(), - }, - ); - ts_println!( - self, - "{} {} in {}", - "exec".style(self.magenta), - escape_command(&command).style(self.bold), - cwd.to_string_lossy(), - ); - } - EventMsg::ExecCommandEnd(ExecCommandEndEvent { - call_id, - stdout, - stderr, - exit_code, - }) => { - let exec_command = self.call_id_to_command.remove(&call_id); - let (duration, call) = if let Some(ExecCommandBegin { - command, - start_time, - }) = exec_command - { - ( - format!(" in {}", format_elapsed(start_time)), - format!("{}", escape_command(&command).style(self.bold)), - ) - } else { - ("".to_string(), format!("exec('{call_id}')")) - }; - - let output = if exit_code == 0 { stdout } else { stderr }; - let truncated_output = output - .lines() - .take(MAX_OUTPUT_LINES_FOR_EXEC_TOOL_CALL) - .collect::>() - .join("\n"); - match exit_code { - 0 => { - let title = format!("{call} succeeded{duration}:"); - ts_println!(self, "{}", title.style(self.green)); - } - _ => { - let title = format!("{call} exited {exit_code}{duration}:"); - ts_println!(self, "{}", title.style(self.red)); - } - } - println!("{}", truncated_output.style(self.dimmed)); - } - EventMsg::McpToolCallBegin(McpToolCallBeginEvent { - call_id, - server, - tool, - arguments, - }) => { - // Build fully-qualified tool name: server.tool - let fq_tool_name = format!("{server}.{tool}"); - - // Format arguments as compact JSON so they fit on one line. - let args_str = arguments - .as_ref() - .map(|v: &serde_json::Value| { - serde_json::to_string(v).unwrap_or_else(|_| v.to_string()) - }) - .unwrap_or_default(); - - let invocation = if args_str.is_empty() { - format!("{fq_tool_name}()") - } else { - format!("{fq_tool_name}({args_str})") - }; - - self.call_id_to_tool_call.insert( - call_id.clone(), - McpToolCallBegin { - invocation: invocation.clone(), - start_time: Instant::now(), - }, - ); - - ts_println!( - self, - "{} {}", - "tool".style(self.magenta), - invocation.style(self.bold), - ); - } - EventMsg::McpToolCallEnd(tool_call_end_event) => { - let is_success = tool_call_end_event.is_success(); - let McpToolCallEndEvent { call_id, result } = tool_call_end_event; - // Retrieve start time and invocation for duration calculation and labeling. - let info = self.call_id_to_tool_call.remove(&call_id); - - let (duration, invocation) = if let Some(McpToolCallBegin { - invocation, - start_time, - .. - }) = info - { - (format!(" in {}", format_elapsed(start_time)), invocation) - } else { - (String::new(), format!("tool('{call_id}')")) - }; - - let status_str = if is_success { "success" } else { "failed" }; - let title_style = if is_success { self.green } else { self.red }; - let title = format!("{invocation} {status_str}{duration}:"); - - ts_println!(self, "{}", title.style(title_style)); - - if let Ok(res) = result { - let val: serde_json::Value = res.into(); - let pretty = - serde_json::to_string_pretty(&val).unwrap_or_else(|_| val.to_string()); - - for line in pretty.lines().take(MAX_OUTPUT_LINES_FOR_EXEC_TOOL_CALL) { - println!("{}", line.style(self.dimmed)); - } - } - } - EventMsg::PatchApplyBegin(PatchApplyBeginEvent { - call_id, - auto_approved, - changes, - }) => { - // Store metadata so we can calculate duration later when we - // receive the corresponding PatchApplyEnd event. - self.call_id_to_patch.insert( - call_id.clone(), - PatchApplyBegin { - start_time: Instant::now(), - auto_approved, - }, - ); - - ts_println!( - self, - "{} auto_approved={}:", - "apply_patch".style(self.magenta), - auto_approved, - ); - - // Pretty-print the patch summary with colored diff markers so - // it's easy to scan in the terminal output. - for (path, change) in changes.iter() { - match change { - FileChange::Add { content } => { - let header = format!( - "{} {}", - format_file_change(change), - path.to_string_lossy() - ); - println!("{}", header.style(self.magenta)); - for line in content.lines() { - println!("{}", line.style(self.green)); - } - } - FileChange::Delete => { - let header = format!( - "{} {}", - format_file_change(change), - path.to_string_lossy() - ); - println!("{}", header.style(self.magenta)); - } - FileChange::Update { - unified_diff, - move_path, - } => { - let header = if let Some(dest) = move_path { - format!( - "{} {} -> {}", - format_file_change(change), - path.to_string_lossy(), - dest.to_string_lossy() - ) - } else { - format!("{} {}", format_file_change(change), path.to_string_lossy()) - }; - println!("{}", header.style(self.magenta)); - - // Colorize diff lines. We keep file header lines - // (--- / +++) without extra coloring so they are - // still readable. - for diff_line in unified_diff.lines() { - if diff_line.starts_with('+') && !diff_line.starts_with("+++") { - println!("{}", diff_line.style(self.green)); - } else if diff_line.starts_with('-') - && !diff_line.starts_with("---") - { - println!("{}", diff_line.style(self.red)); - } else { - println!("{diff_line}"); - } - } - } - } - } - } - EventMsg::PatchApplyEnd(PatchApplyEndEvent { - call_id, - stdout, - stderr, - success, - }) => { - let patch_begin = self.call_id_to_patch.remove(&call_id); - - // Compute duration and summary label similar to exec commands. - let (duration, label) = if let Some(PatchApplyBegin { - start_time, - auto_approved, - }) = patch_begin - { - ( - format!(" in {}", format_elapsed(start_time)), - format!("apply_patch(auto_approved={auto_approved})"), - ) - } else { - (String::new(), format!("apply_patch('{call_id}')")) - }; - - let (exit_code, output, title_style) = if success { - (0, stdout, self.green) - } else { - (1, stderr, self.red) - }; - - let title = format!("{label} exited {exit_code}{duration}:"); - ts_println!(self, "{}", title.style(title_style)); - for line in output.lines() { - println!("{}", line.style(self.dimmed)); - } - } - EventMsg::ExecApprovalRequest(_) => { - // Should we exit? - } - EventMsg::ApplyPatchApprovalRequest(_) => { - // Should we exit? - } - EventMsg::AgentReasoning(agent_reasoning_event) => { - if self.show_agent_reasoning { - if !self.reasoning_started { - ts_println!( - self, - "{}\n{}", - "codex".style(self.italic).style(self.magenta), - agent_reasoning_event.text, - ); - } else { - println!(); - self.reasoning_started = false; - } - } - } - EventMsg::SessionConfigured(session_configured_event) => { - let SessionConfiguredEvent { - session_id, - model, - history_log_id: _, - history_entry_count: _, - } = session_configured_event; - - ts_println!( - self, - "{} {}", - "codex session".style(self.magenta).style(self.bold), - session_id.to_string().style(self.dimmed) - ); - - ts_println!(self, "model: {}", model); - println!(); - } - EventMsg::GetHistoryEntryResponse(_) => { - // Currently ignored in exec output. - } - } - } -} - -fn escape_command(command: &[String]) -> String { - try_join(command.iter().map(|s| s.as_str())).unwrap_or_else(|_| command.join(" ")) -} - -fn format_file_change(change: &FileChange) -> &'static str { - match change { - FileChange::Add { .. } => "A", - FileChange::Delete => "D", - FileChange::Update { - move_path: Some(_), .. - } => "R", - FileChange::Update { - move_path: None, .. - } => "M", - } + entries } diff --git a/codex-rs/exec/src/event_processor_with_human_output.rs b/codex-rs/exec/src/event_processor_with_human_output.rs new file mode 100644 index 0000000000..7b39071116 --- /dev/null +++ b/codex-rs/exec/src/event_processor_with_human_output.rs @@ -0,0 +1,520 @@ +use codex_common::elapsed::format_elapsed; +use codex_core::config::Config; +use codex_core::protocol::AgentMessageDeltaEvent; +use codex_core::protocol::AgentMessageEvent; +use codex_core::protocol::AgentReasoningDeltaEvent; +use codex_core::protocol::BackgroundEventEvent; +use codex_core::protocol::ErrorEvent; +use codex_core::protocol::Event; +use codex_core::protocol::EventMsg; +use codex_core::protocol::ExecCommandBeginEvent; +use codex_core::protocol::ExecCommandEndEvent; +use codex_core::protocol::FileChange; +use codex_core::protocol::McpToolCallBeginEvent; +use codex_core::protocol::McpToolCallEndEvent; +use codex_core::protocol::PatchApplyBeginEvent; +use codex_core::protocol::PatchApplyEndEvent; +use codex_core::protocol::SessionConfiguredEvent; +use codex_core::protocol::TokenUsage; +use owo_colors::OwoColorize; +use owo_colors::Style; +use shlex::try_join; +use std::collections::HashMap; +use std::io::Write; +use std::time::Instant; + +use crate::event_processor::EventProcessor; +use crate::event_processor::create_config_summary_entries; + +/// This should be configurable. When used in CI, users may not want to impose +/// a limit so they can see the full transcript. +const MAX_OUTPUT_LINES_FOR_EXEC_TOOL_CALL: usize = 20; +pub(crate) struct EventProcessorWithHumanOutput { + call_id_to_command: HashMap, + call_id_to_patch: HashMap, + + /// Tracks in-flight MCP tool calls so we can calculate duration and print + /// a concise summary when the corresponding `McpToolCallEnd` event is + /// received. + call_id_to_tool_call: HashMap, + + // To ensure that --color=never is respected, ANSI escapes _must_ be added + // using .style() with one of these fields. If you need a new style, add a + // new field here. + bold: Style, + italic: Style, + dimmed: Style, + + magenta: Style, + red: Style, + green: Style, + cyan: Style, + + /// Whether to include `AgentReasoning` events in the output. + show_agent_reasoning: bool, + answer_started: bool, + reasoning_started: bool, +} + +impl EventProcessorWithHumanOutput { + pub(crate) fn create_with_ansi(with_ansi: bool, config: &Config) -> Self { + let call_id_to_command = HashMap::new(); + let call_id_to_patch = HashMap::new(); + let call_id_to_tool_call = HashMap::new(); + + if with_ansi { + Self { + call_id_to_command, + call_id_to_patch, + bold: Style::new().bold(), + italic: Style::new().italic(), + dimmed: Style::new().dimmed(), + magenta: Style::new().magenta(), + red: Style::new().red(), + green: Style::new().green(), + cyan: Style::new().cyan(), + call_id_to_tool_call, + show_agent_reasoning: !config.hide_agent_reasoning, + answer_started: false, + reasoning_started: false, + } + } else { + Self { + call_id_to_command, + call_id_to_patch, + bold: Style::new(), + italic: Style::new(), + dimmed: Style::new(), + magenta: Style::new(), + red: Style::new(), + green: Style::new(), + cyan: Style::new(), + call_id_to_tool_call, + show_agent_reasoning: !config.hide_agent_reasoning, + answer_started: false, + reasoning_started: false, + } + } + } +} + +struct ExecCommandBegin { + command: Vec, + start_time: Instant, +} + +/// Metadata captured when an `McpToolCallBegin` event is received. +struct McpToolCallBegin { + /// Formatted invocation string, e.g. `server.tool({"city":"sf"})`. + invocation: String, + /// Timestamp when the call started so we can compute duration later. + start_time: Instant, +} + +struct PatchApplyBegin { + start_time: Instant, + auto_approved: bool, +} + +// Timestamped println helper. The timestamp is styled with self.dimmed. +#[macro_export] +macro_rules! ts_println { + ($self:ident, $($arg:tt)*) => {{ + let now = chrono::Utc::now(); + let formatted = now.format("[%Y-%m-%dT%H:%M:%S]"); + print!("{} ", formatted.style($self.dimmed)); + println!($($arg)*); + }}; +} + +impl EventProcessor for EventProcessorWithHumanOutput { + /// Print a concise summary of the effective configuration that will be used + /// for the session. This mirrors the information shown in the TUI welcome + /// screen. + fn print_config_summary(&mut self, config: &Config, prompt: &str) { + const VERSION: &str = env!("CARGO_PKG_VERSION"); + ts_println!( + self, + "OpenAI Codex v{} (research preview)\n--------", + VERSION + ); + + let entries = create_config_summary_entries(config); + + for (key, value) in entries { + println!("{} {}", format!("{key}:").style(self.bold), value); + } + + println!("--------"); + + // Echo the prompt that will be sent to the agent so it is visible in the + // transcript/logs before any events come in. Note the prompt may have been + // read from stdin, so it may not be visible in the terminal otherwise. + ts_println!( + self, + "{}\n{}", + "User instructions:".style(self.bold).style(self.cyan), + prompt + ); + } + + fn process_event(&mut self, event: Event) { + let Event { id: _, msg } = event; + match msg { + EventMsg::Error(ErrorEvent { message }) => { + let prefix = "ERROR:".style(self.red); + ts_println!(self, "{prefix} {message}"); + } + EventMsg::BackgroundEvent(BackgroundEventEvent { message }) => { + ts_println!(self, "{}", message.style(self.dimmed)); + } + EventMsg::TaskStarted | EventMsg::TaskComplete(_) => { + // Ignore. + } + EventMsg::TokenCount(TokenUsage { total_tokens, .. }) => { + ts_println!(self, "tokens used: {total_tokens}"); + } + EventMsg::AgentMessageDelta(AgentMessageDeltaEvent { delta }) => { + if !self.answer_started { + ts_println!(self, "{}\n", "codex".style(self.italic).style(self.magenta)); + self.answer_started = true; + } + print!("{delta}"); + #[allow(clippy::expect_used)] + std::io::stdout().flush().expect("could not flush stdout"); + } + EventMsg::AgentReasoningDelta(AgentReasoningDeltaEvent { delta }) => { + if !self.show_agent_reasoning { + return; + } + if !self.reasoning_started { + ts_println!( + self, + "{}\n", + "thinking".style(self.italic).style(self.magenta), + ); + self.reasoning_started = true; + } + print!("{delta}"); + #[allow(clippy::expect_used)] + std::io::stdout().flush().expect("could not flush stdout"); + } + EventMsg::AgentMessage(AgentMessageEvent { message }) => { + // if answer_started is false, this means we haven't received any + // delta. Thus, we need to print the message as a new answer. + if !self.answer_started { + ts_println!( + self, + "{}\n{}", + "codex".style(self.italic).style(self.magenta), + message, + ); + } else { + println!(); + self.answer_started = false; + } + } + EventMsg::ExecCommandBegin(ExecCommandBeginEvent { + call_id, + command, + cwd, + }) => { + self.call_id_to_command.insert( + call_id.clone(), + ExecCommandBegin { + command: command.clone(), + start_time: Instant::now(), + }, + ); + ts_println!( + self, + "{} {} in {}", + "exec".style(self.magenta), + escape_command(&command).style(self.bold), + cwd.to_string_lossy(), + ); + } + EventMsg::ExecCommandEnd(ExecCommandEndEvent { + call_id, + stdout, + stderr, + exit_code, + }) => { + let exec_command = self.call_id_to_command.remove(&call_id); + let (duration, call) = if let Some(ExecCommandBegin { + command, + start_time, + }) = exec_command + { + ( + format!(" in {}", format_elapsed(start_time)), + format!("{}", escape_command(&command).style(self.bold)), + ) + } else { + ("".to_string(), format!("exec('{call_id}')")) + }; + + let output = if exit_code == 0 { stdout } else { stderr }; + let truncated_output = output + .lines() + .take(MAX_OUTPUT_LINES_FOR_EXEC_TOOL_CALL) + .collect::>() + .join("\n"); + match exit_code { + 0 => { + let title = format!("{call} succeeded{duration}:"); + ts_println!(self, "{}", title.style(self.green)); + } + _ => { + let title = format!("{call} exited {exit_code}{duration}:"); + ts_println!(self, "{}", title.style(self.red)); + } + } + println!("{}", truncated_output.style(self.dimmed)); + } + EventMsg::McpToolCallBegin(McpToolCallBeginEvent { + call_id, + server, + tool, + arguments, + }) => { + // Build fully-qualified tool name: server.tool + let fq_tool_name = format!("{server}.{tool}"); + + // Format arguments as compact JSON so they fit on one line. + let args_str = arguments + .as_ref() + .map(|v: &serde_json::Value| { + serde_json::to_string(v).unwrap_or_else(|_| v.to_string()) + }) + .unwrap_or_default(); + + let invocation = if args_str.is_empty() { + format!("{fq_tool_name}()") + } else { + format!("{fq_tool_name}({args_str})") + }; + + self.call_id_to_tool_call.insert( + call_id.clone(), + McpToolCallBegin { + invocation: invocation.clone(), + start_time: Instant::now(), + }, + ); + + ts_println!( + self, + "{} {}", + "tool".style(self.magenta), + invocation.style(self.bold), + ); + } + EventMsg::McpToolCallEnd(tool_call_end_event) => { + let is_success = tool_call_end_event.is_success(); + let McpToolCallEndEvent { call_id, result } = tool_call_end_event; + // Retrieve start time and invocation for duration calculation and labeling. + let info = self.call_id_to_tool_call.remove(&call_id); + + let (duration, invocation) = if let Some(McpToolCallBegin { + invocation, + start_time, + .. + }) = info + { + (format!(" in {}", format_elapsed(start_time)), invocation) + } else { + (String::new(), format!("tool('{call_id}')")) + }; + + let status_str = if is_success { "success" } else { "failed" }; + let title_style = if is_success { self.green } else { self.red }; + let title = format!("{invocation} {status_str}{duration}:"); + + ts_println!(self, "{}", title.style(title_style)); + + if let Ok(res) = result { + let val: serde_json::Value = res.into(); + let pretty = + serde_json::to_string_pretty(&val).unwrap_or_else(|_| val.to_string()); + + for line in pretty.lines().take(MAX_OUTPUT_LINES_FOR_EXEC_TOOL_CALL) { + println!("{}", line.style(self.dimmed)); + } + } + } + EventMsg::PatchApplyBegin(PatchApplyBeginEvent { + call_id, + auto_approved, + changes, + }) => { + // Store metadata so we can calculate duration later when we + // receive the corresponding PatchApplyEnd event. + self.call_id_to_patch.insert( + call_id.clone(), + PatchApplyBegin { + start_time: Instant::now(), + auto_approved, + }, + ); + + ts_println!( + self, + "{} auto_approved={}:", + "apply_patch".style(self.magenta), + auto_approved, + ); + + // Pretty-print the patch summary with colored diff markers so + // it's easy to scan in the terminal output. + for (path, change) in changes.iter() { + match change { + FileChange::Add { content } => { + let header = format!( + "{} {}", + format_file_change(change), + path.to_string_lossy() + ); + println!("{}", header.style(self.magenta)); + for line in content.lines() { + println!("{}", line.style(self.green)); + } + } + FileChange::Delete => { + let header = format!( + "{} {}", + format_file_change(change), + path.to_string_lossy() + ); + println!("{}", header.style(self.magenta)); + } + FileChange::Update { + unified_diff, + move_path, + } => { + let header = if let Some(dest) = move_path { + format!( + "{} {} -> {}", + format_file_change(change), + path.to_string_lossy(), + dest.to_string_lossy() + ) + } else { + format!("{} {}", format_file_change(change), path.to_string_lossy()) + }; + println!("{}", header.style(self.magenta)); + + // Colorize diff lines. We keep file header lines + // (--- / +++) without extra coloring so they are + // still readable. + for diff_line in unified_diff.lines() { + if diff_line.starts_with('+') && !diff_line.starts_with("+++") { + println!("{}", diff_line.style(self.green)); + } else if diff_line.starts_with('-') + && !diff_line.starts_with("---") + { + println!("{}", diff_line.style(self.red)); + } else { + println!("{diff_line}"); + } + } + } + } + } + } + EventMsg::PatchApplyEnd(PatchApplyEndEvent { + call_id, + stdout, + stderr, + success, + }) => { + let patch_begin = self.call_id_to_patch.remove(&call_id); + + // Compute duration and summary label similar to exec commands. + let (duration, label) = if let Some(PatchApplyBegin { + start_time, + auto_approved, + }) = patch_begin + { + ( + format!(" in {}", format_elapsed(start_time)), + format!("apply_patch(auto_approved={auto_approved})"), + ) + } else { + (String::new(), format!("apply_patch('{call_id}')")) + }; + + let (exit_code, output, title_style) = if success { + (0, stdout, self.green) + } else { + (1, stderr, self.red) + }; + + let title = format!("{label} exited {exit_code}{duration}:"); + ts_println!(self, "{}", title.style(title_style)); + for line in output.lines() { + println!("{}", line.style(self.dimmed)); + } + } + EventMsg::ExecApprovalRequest(_) => { + // Should we exit? + } + EventMsg::ApplyPatchApprovalRequest(_) => { + // Should we exit? + } + EventMsg::AgentReasoning(agent_reasoning_event) => { + if self.show_agent_reasoning { + if !self.reasoning_started { + ts_println!( + self, + "{}\n{}", + "codex".style(self.italic).style(self.magenta), + agent_reasoning_event.text, + ); + } else { + println!(); + self.reasoning_started = false; + } + } + } + EventMsg::SessionConfigured(session_configured_event) => { + let SessionConfiguredEvent { + session_id, + model, + history_log_id: _, + history_entry_count: _, + } = session_configured_event; + + ts_println!( + self, + "{} {}", + "codex session".style(self.magenta).style(self.bold), + session_id.to_string().style(self.dimmed) + ); + + ts_println!(self, "model: {}", model); + println!(); + } + EventMsg::GetHistoryEntryResponse(_) => { + // Currently ignored in exec output. + } + } + } +} + +fn escape_command(command: &[String]) -> String { + try_join(command.iter().map(|s| s.as_str())).unwrap_or_else(|_| command.join(" ")) +} + +fn format_file_change(change: &FileChange) -> &'static str { + match change { + FileChange::Add { .. } => "A", + FileChange::Delete => "D", + FileChange::Update { + move_path: Some(_), .. + } => "R", + FileChange::Update { + move_path: None, .. + } => "M", + } +} diff --git a/codex-rs/exec/src/event_processor_with_json_output.rs b/codex-rs/exec/src/event_processor_with_json_output.rs new file mode 100644 index 0000000000..699460bbed --- /dev/null +++ b/codex-rs/exec/src/event_processor_with_json_output.rs @@ -0,0 +1,48 @@ +use std::collections::HashMap; + +use codex_core::config::Config; +use codex_core::protocol::Event; +use codex_core::protocol::EventMsg; +use serde_json::json; + +use crate::event_processor::EventProcessor; +use crate::event_processor::create_config_summary_entries; + +pub(crate) struct EventProcessorWithJsonOutput; + +impl EventProcessorWithJsonOutput { + pub fn new() -> Self { + Self {} + } +} + +impl EventProcessor for EventProcessorWithJsonOutput { + fn print_config_summary(&mut self, config: &Config, prompt: &str) { + let entries = create_config_summary_entries(config) + .into_iter() + .map(|(key, value)| (key.to_string(), value)) + .collect::>(); + #[allow(clippy::expect_used)] + let config_json = + serde_json::to_string(&entries).expect("Failed to serialize config summary to JSON"); + println!("{config_json}"); + + let prompt_json = json!({ + "prompt": prompt, + }); + println!("{prompt_json}"); + } + + fn process_event(&mut self, event: Event) { + match event.msg { + EventMsg::AgentMessageDelta(_) | EventMsg::AgentReasoningDelta(_) => { + // Suppress streaming events in JSON mode. + } + _ => { + if let Ok(line) = serde_json::to_string(&event) { + println!("{line}"); + } + } + } + } +} diff --git a/codex-rs/exec/src/lib.rs b/codex-rs/exec/src/lib.rs index afefed1a93..b557c89397 100644 --- a/codex-rs/exec/src/lib.rs +++ b/codex-rs/exec/src/lib.rs @@ -1,5 +1,7 @@ mod cli; mod event_processor; +mod event_processor_with_human_output; +mod event_processor_with_json_output; use std::io::IsTerminal; use std::io::Read; @@ -19,12 +21,15 @@ use codex_core::protocol::InputItem; use codex_core::protocol::Op; use codex_core::protocol::TaskCompleteEvent; use codex_core::util::is_inside_git_repo; -use event_processor::EventProcessor; +use event_processor_with_human_output::EventProcessorWithHumanOutput; +use event_processor_with_json_output::EventProcessorWithJsonOutput; use tracing::debug; use tracing::error; use tracing::info; use tracing_subscriber::EnvFilter; +use crate::event_processor::EventProcessor; + pub async fn run_main(cli: Cli, codex_linux_sandbox_exe: Option) -> anyhow::Result<()> { let Cli { images, @@ -36,6 +41,7 @@ pub async fn run_main(cli: Cli, codex_linux_sandbox_exe: Option) -> any skip_git_repo_check, color, last_message_file, + json: json_mode, sandbox_mode: sandbox_mode_cli_arg, prompt, config_overrides, @@ -115,7 +121,15 @@ pub async fn run_main(cli: Cli, codex_linux_sandbox_exe: Option) -> any }; let config = Config::load_with_cli_overrides(cli_kv_overrides, overrides)?; - let mut event_processor = EventProcessor::create_with_ansi(stdout_with_ansi, &config); + let mut event_processor: Box = if json_mode { + Box::new(EventProcessorWithJsonOutput::new()) + } else { + Box::new(EventProcessorWithHumanOutput::create_with_ansi( + stdout_with_ansi, + &config, + )) + }; + // Print the effective configuration and prompt so users can see what Codex // is using. event_processor.print_config_summary(&config, &prompt); diff --git a/codex-rs/justfile b/codex-rs/justfile index 83a390ec56..3e1336be43 100644 --- a/codex-rs/justfile +++ b/codex-rs/justfile @@ -23,3 +23,10 @@ file-search *args: # format code fmt: cargo fmt -- --config imports_granularity=Item + +fix: + cargo clippy --fix --all-features --tests --allow-dirty + +install: + rustup show active-toolchain + cargo fetch diff --git a/codex-rs/mcp-client/src/main.rs b/codex-rs/mcp-client/src/main.rs index 518383d1ea..8d671b830f 100644 --- a/codex-rs/mcp-client/src/main.rs +++ b/codex-rs/mcp-client/src/main.rs @@ -57,10 +57,12 @@ async fn main() -> Result<()> { experimental: None, roots: None, sampling: None, + elicitation: None, }, client_info: Implementation { name: "codex-mcp-client".to_owned(), version: env!("CARGO_PKG_VERSION").to_owned(), + title: Some("Codex".to_string()), }, protocol_version: MCP_SCHEMA_VERSION.to_owned(), }; diff --git a/codex-rs/mcp-server/Cargo.toml b/codex-rs/mcp-server/Cargo.toml index f91a3dc8f8..f43b101bd9 100644 --- a/codex-rs/mcp-server/Cargo.toml +++ b/codex-rs/mcp-server/Cargo.toml @@ -22,6 +22,7 @@ mcp-types = { path = "../mcp-types" } schemars = "0.8.22" serde = { version = "1", features = ["derive"] } serde_json = "1" +shlex = "1.3.0" toml = "0.9" tracing = { version = "0.1.41", features = ["log"] } tracing-subscriber = { version = "0.3", features = ["fmt", "env-filter"] } @@ -34,4 +35,8 @@ tokio = { version = "1", features = [ ] } [dev-dependencies] +assert_cmd = "2" pretty_assertions = "1.4.1" +tempfile = "3" +tokio-test = "0.4" +wiremock = "0.6" diff --git a/codex-rs/mcp-server/src/codex_tool_config.rs b/codex-rs/mcp-server/src/codex_tool_config.rs index 8555524942..9a31dbcccc 100644 --- a/codex-rs/mcp-server/src/codex_tool_config.rs +++ b/codex-rs/mcp-server/src/codex_tool_config.rs @@ -7,15 +7,16 @@ use mcp_types::ToolInputSchema; use schemars::JsonSchema; use schemars::r#gen::SchemaSettings; use serde::Deserialize; +use serde::Serialize; use std::collections::HashMap; use std::path::PathBuf; use crate::json_to_toml::json_to_toml; /// Client-supplied configuration for a `codex` tool-call. -#[derive(Debug, Clone, Deserialize, JsonSchema)] +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] #[serde(rename_all = "kebab-case")] -pub(crate) struct CodexToolCallParam { +pub struct CodexToolCallParam { /// The *initial user prompt* to start the Codex conversation. pub prompt: String, @@ -49,9 +50,9 @@ pub(crate) struct CodexToolCallParam { /// Custom enum mirroring [`AskForApproval`], but has an extra dependency on /// [`JsonSchema`]. -#[derive(Debug, Clone, Deserialize, JsonSchema)] +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] #[serde(rename_all = "kebab-case")] -pub(crate) enum CodexToolCallApprovalPolicy { +pub enum CodexToolCallApprovalPolicy { Untrusted, OnFailure, Never, @@ -69,9 +70,9 @@ impl From for AskForApproval { /// Custom enum mirroring [`SandboxMode`] from config_types.rs, but with /// `JsonSchema` support. -#[derive(Debug, Clone, Deserialize, JsonSchema)] +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] #[serde(rename_all = "kebab-case")] -pub(crate) enum CodexToolCallSandboxMode { +pub enum CodexToolCallSandboxMode { ReadOnly, WorkspaceWrite, DangerFullAccess, @@ -108,7 +109,10 @@ pub(crate) fn create_tool_for_codex_tool_call_param() -> Tool { Tool { name: "codex".to_string(), + title: Some("Codex".to_string()), input_schema: tool_input_schema, + // TODO(mbolin): This should be defined. + output_schema: None, description: Some( "Run a Codex session. Accepts configuration parameters matching the Codex Config struct.".to_string(), ), @@ -179,6 +183,7 @@ mod tests { let tool_json = serde_json::to_value(&tool).expect("tool serializes"); let expected_tool_json = serde_json::json!({ "name": "codex", + "title": "Codex", "description": "Run a Codex session. Accepts configuration parameters matching the Codex Config struct.", "inputSchema": { "type": "object", diff --git a/codex-rs/mcp-server/src/codex_tool_runner.rs b/codex-rs/mcp-server/src/codex_tool_runner.rs index 88dcf649dc..163055de5c 100644 --- a/codex-rs/mcp-server/src/codex_tool_runner.rs +++ b/codex-rs/mcp-server/src/codex_tool_runner.rs @@ -2,33 +2,36 @@ //! Tokio task. Separated from `message_processor.rs` to keep that file small //! and to make future feature-growth easier to manage. +use std::path::PathBuf; +use std::sync::Arc; + +use codex_core::Codex; use codex_core::codex_wrapper::init_codex; use codex_core::config::Config as CodexConfig; use codex_core::protocol::AgentMessageEvent; -use codex_core::protocol::Event; use codex_core::protocol::EventMsg; +use codex_core::protocol::ExecApprovalRequestEvent; use codex_core::protocol::InputItem; use codex_core::protocol::Op; +use codex_core::protocol::ReviewDecision; use codex_core::protocol::Submission; use codex_core::protocol::TaskCompleteEvent; use mcp_types::CallToolResult; -use mcp_types::CallToolResultContent; -use mcp_types::JSONRPC_VERSION; -use mcp_types::JSONRPCMessage; -use mcp_types::JSONRPCResponse; +use mcp_types::ContentBlock; +use mcp_types::ElicitRequest; +use mcp_types::ElicitRequestParamsRequestedSchema; +use mcp_types::JSONRPCErrorError; +use mcp_types::ModelContextProtocolRequest; use mcp_types::RequestId; use mcp_types::TextContent; -use tokio::sync::mpsc::Sender; +use serde::Deserialize; +use serde::Serialize; +use serde_json::json; +use tracing::error; -/// Convert a Codex [`Event`] to an MCP notification. -fn codex_event_to_notification(event: &Event) -> JSONRPCMessage { - #[expect(clippy::expect_used)] - JSONRPCMessage::Notification(mcp_types::JSONRPCNotification { - jsonrpc: JSONRPC_VERSION.into(), - method: "codex/event".into(), - params: Some(serde_json::to_value(event).expect("Event must serialize")), - }) -} +use crate::outgoing_message::OutgoingMessageSender; + +const INVALID_PARAMS_ERROR_CODE: i64 = -32602; /// Run a complete Codex session and stream events back to the client. /// @@ -38,34 +41,28 @@ pub async fn run_codex_tool_session( id: RequestId, initial_prompt: String, config: CodexConfig, - outgoing: Sender, + outgoing: Arc, ) { let (codex, first_event, _ctrl_c) = match init_codex(config).await { Ok(res) => res, Err(e) => { let result = CallToolResult { - content: vec![CallToolResultContent::TextContent(TextContent { + content: vec![ContentBlock::TextContent(TextContent { r#type: "text".to_string(), text: format!("Failed to start Codex session: {e}"), annotations: None, })], is_error: Some(true), + structured_content: None, }; - let _ = outgoing - .send(JSONRPCMessage::Response(JSONRPCResponse { - jsonrpc: JSONRPC_VERSION.into(), - id, - result: result.into(), - })) - .await; + outgoing.send_response(id.clone(), result.into()).await; return; } }; + let codex = Arc::new(codex); // Send initial SessionConfigured event. - let _ = outgoing - .send(codex_event_to_notification(&first_event)) - .await; + outgoing.send_event_as_notification(&first_event).await; // Use the original MCP request ID as the `sub_id` for the Codex submission so that // any events emitted for this tool-call can be correlated with the @@ -76,7 +73,7 @@ pub async fn run_codex_tool_session( }; let submission = Submission { - id: sub_id, + id: sub_id.clone(), op: Op::UserInput { items: vec![InputItem::Text { text: initial_prompt.clone(), @@ -88,84 +85,108 @@ pub async fn run_codex_tool_session( tracing::error!("Failed to submit initial prompt: {e}"); } - let mut last_agent_message: Option = None; - // Stream events until the task needs to pause for user interaction or // completes. loop { match codex.next_event().await { Ok(event) => { - let _ = outgoing.send(codex_event_to_notification(&event)).await; + outgoing.send_event_as_notification(&event).await; - match &event.msg { - EventMsg::AgentMessage(AgentMessageEvent { message }) => { - last_agent_message = Some(message.clone()); - } - EventMsg::ExecApprovalRequest(_) => { - let result = CallToolResult { - content: vec![CallToolResultContent::TextContent(TextContent { - r#type: "text".to_string(), - text: "EXEC_APPROVAL_REQUIRED".to_string(), - annotations: None, - })], - is_error: None, + match event.msg { + EventMsg::ExecApprovalRequest(ExecApprovalRequestEvent { + command, + cwd, + reason: _, + }) => { + let escaped_command = shlex::try_join(command.iter().map(|s| s.as_str())) + .unwrap_or_else(|_| command.join(" ")); + let message = format!( + "Allow Codex to run `{escaped_command}` in `{cwd}`?", + cwd = cwd.to_string_lossy() + ); + + let params = ExecApprovalElicitRequestParams { + message, + requested_schema: ElicitRequestParamsRequestedSchema { + r#type: "object".to_string(), + properties: json!({}), + required: None, + }, + codex_elicitation: "exec-approval".to_string(), + codex_mcp_tool_call_id: sub_id.clone(), + codex_event_id: event.id.clone(), + codex_command: command, + codex_cwd: cwd, }; - let _ = outgoing - .send(JSONRPCMessage::Response(JSONRPCResponse { - jsonrpc: JSONRPC_VERSION.into(), - id: id.clone(), - result: result.into(), - })) + let params_json = match serde_json::to_value(¶ms) { + Ok(value) => value, + Err(err) => { + let message = format!( + "Failed to serialize ExecApprovalElicitRequestParams: {err}" + ); + tracing::error!("{message}"); + + outgoing + .send_error( + id.clone(), + JSONRPCErrorError { + code: INVALID_PARAMS_ERROR_CODE, + message, + data: None, + }, + ) + .await; + + continue; + } + }; + + let on_response = outgoing + .send_request(ElicitRequest::METHOD, Some(params_json)) .await; - break; + + // Listen for the response on a separate task so we do + // not block the main loop of this function. + { + let codex = codex.clone(); + let event_id = event.id.clone(); + tokio::spawn(async move { + on_exec_approval_response(event_id, on_response, codex).await; + }); + } + + // Continue, don't break so the session continues. + continue; } EventMsg::ApplyPatchApprovalRequest(_) => { let result = CallToolResult { - content: vec![CallToolResultContent::TextContent(TextContent { + content: vec![ContentBlock::TextContent(TextContent { r#type: "text".to_string(), text: "PATCH_APPROVAL_REQUIRED".to_string(), annotations: None, })], is_error: None, + structured_content: None, }; - let _ = outgoing - .send(JSONRPCMessage::Response(JSONRPCResponse { - jsonrpc: JSONRPC_VERSION.into(), - id: id.clone(), - result: result.into(), - })) - .await; - break; + outgoing.send_response(id.clone(), result.into()).await; + // Continue, don't break so the session continues. + continue; } - EventMsg::TaskComplete(TaskCompleteEvent { - last_agent_message: _, - }) => { - let result = if let Some(msg) = last_agent_message { - CallToolResult { - content: vec![CallToolResultContent::TextContent(TextContent { - r#type: "text".to_string(), - text: msg, - annotations: None, - })], - is_error: None, - } - } else { - CallToolResult { - content: vec![CallToolResultContent::TextContent(TextContent { - r#type: "text".to_string(), - text: String::new(), - annotations: None, - })], - is_error: None, - } + EventMsg::TaskComplete(TaskCompleteEvent { last_agent_message }) => { + let text = match last_agent_message { + Some(msg) => msg.clone(), + None => "".to_string(), }; - let _ = outgoing - .send(JSONRPCMessage::Response(JSONRPCResponse { - jsonrpc: JSONRPC_VERSION.into(), - id: id.clone(), - result: result.into(), - })) - .await; + let result = CallToolResult { + content: vec![ContentBlock::TextContent(TextContent { + r#type: "text".to_string(), + text, + annotations: None, + })], + is_error: None, + structured_content: None, + }; + outgoing.send_response(id.clone(), result.into()).await; break; } EventMsg::SessionConfigured(_) => { @@ -177,6 +198,9 @@ pub async fn run_codex_tool_session( EventMsg::AgentReasoningDelta(_) => { // TODO: think how we want to support this in the MCP } + EventMsg::AgentMessage(AgentMessageEvent { .. }) => { + // TODO: think how we want to support this in the MCP + } EventMsg::Error(_) | EventMsg::TaskStarted | EventMsg::TokenCount(_) @@ -200,22 +224,87 @@ pub async fn run_codex_tool_session( } Err(e) => { let result = CallToolResult { - content: vec![CallToolResultContent::TextContent(TextContent { + content: vec![ContentBlock::TextContent(TextContent { r#type: "text".to_string(), text: format!("Codex runtime error: {e}"), annotations: None, })], is_error: Some(true), + // TODO(mbolin): Could present the error in a more + // structured way. + structured_content: None, }; - let _ = outgoing - .send(JSONRPCMessage::Response(JSONRPCResponse { - jsonrpc: JSONRPC_VERSION.into(), - id: id.clone(), - result: result.into(), - })) - .await; + outgoing.send_response(id.clone(), result.into()).await; break; } } } } + +async fn on_exec_approval_response( + event_id: String, + receiver: tokio::sync::oneshot::Receiver, + codex: Arc, +) { + let response = receiver.await; + let value = match response { + Ok(value) => value, + Err(err) => { + error!("request failed: {err:?}"); + return; + } + }; + + // Try to deserialize `value` and then make the appropriate call to `codex`. + let response = match serde_json::from_value::(value) { + Ok(response) => response, + Err(err) => { + error!("failed to deserialize ExecApprovalResponse: {err}"); + // If we cannot deserialize the response, we deny the request to be + // conservative. + ExecApprovalResponse { + decision: ReviewDecision::Denied, + } + } + }; + + if let Err(err) = codex + .submit(Op::ExecApproval { + id: event_id, + decision: response.decision, + }) + .await + { + error!("failed to submit ExecApproval: {err}"); + } +} + +// TODO(mbolin): ExecApprovalResponse does not conform to ElicitResult. See: +// - https://github.com/modelcontextprotocol/modelcontextprotocol/blob/f962dc1780fa5eed7fb7c8a0232f1fc83ef220cd/schema/2025-06-18/schema.json#L617-L636 +// - https://modelcontextprotocol.io/specification/draft/client/elicitation#protocol-messages +// It should have "action" and "content" fields. + +#[derive(Debug, Serialize, Deserialize)] +pub struct ExecApprovalResponse { + pub decision: ReviewDecision, +} + +/// Conforms to [`mcp_types::ElicitRequestParams`] so that it can be used as the +/// `params` field of an [`mcp_types::ElicitRequest`]. +#[derive(Debug, Serialize)] +pub struct ExecApprovalElicitRequestParams { + // These fields are required so that `params` + // conforms to ElicitRequestParams. + pub message: String, + + #[serde(rename = "requestedSchema")] + pub requested_schema: ElicitRequestParamsRequestedSchema, + + // These are additional fields the client can use to + // correlate the request with the codex tool call. + pub codex_elicitation: String, + pub codex_mcp_tool_call_id: String, + pub codex_event_id: String, + pub codex_command: Vec, + pub codex_cwd: PathBuf, +} diff --git a/codex-rs/mcp-server/src/lib.rs b/codex-rs/mcp-server/src/lib.rs index b2a7797fe6..1f1ecc3f2a 100644 --- a/codex-rs/mcp-server/src/lib.rs +++ b/codex-rs/mcp-server/src/lib.rs @@ -18,8 +18,15 @@ mod codex_tool_config; mod codex_tool_runner; mod json_to_toml; mod message_processor; +mod outgoing_message; use crate::message_processor::MessageProcessor; +use crate::outgoing_message::OutgoingMessage; +use crate::outgoing_message::OutgoingMessageSender; + +pub use crate::codex_tool_config::CodexToolCallParam; +pub use crate::codex_tool_runner::ExecApprovalElicitRequestParams; +pub use crate::codex_tool_runner::ExecApprovalResponse; /// Size of the bounded channels used to communicate between tasks. The value /// is a balance between throughput and memory usage – 128 messages should be @@ -35,7 +42,7 @@ pub async fn run_main(codex_linux_sandbox_exe: Option) -> IoResult<()> // Set up channels. let (incoming_tx, mut incoming_rx) = mpsc::channel::(CHANNEL_CAPACITY); - let (outgoing_tx, mut outgoing_rx) = mpsc::channel::(CHANNEL_CAPACITY); + let (outgoing_tx, mut outgoing_rx) = mpsc::channel::(CHANNEL_CAPACITY); // Task: read from stdin, push to `incoming_tx`. let stdin_reader_handle = tokio::spawn({ @@ -63,16 +70,15 @@ pub async fn run_main(codex_linux_sandbox_exe: Option) -> IoResult<()> // Task: process incoming messages. let processor_handle = tokio::spawn({ - let mut processor = MessageProcessor::new(outgoing_tx.clone(), codex_linux_sandbox_exe); + let outgoing_message_sender = OutgoingMessageSender::new(outgoing_tx); + let mut processor = MessageProcessor::new(outgoing_message_sender, codex_linux_sandbox_exe); async move { while let Some(msg) = incoming_rx.recv().await { match msg { - JSONRPCMessage::Request(r) => processor.process_request(r), - JSONRPCMessage::Response(r) => processor.process_response(r), + JSONRPCMessage::Request(r) => processor.process_request(r).await, + JSONRPCMessage::Response(r) => processor.process_response(r).await, JSONRPCMessage::Notification(n) => processor.process_notification(n), - JSONRPCMessage::BatchRequest(b) => processor.process_batch_request(b), JSONRPCMessage::Error(e) => processor.process_error(e), - JSONRPCMessage::BatchResponse(b) => processor.process_batch_response(b), } } @@ -83,7 +89,8 @@ pub async fn run_main(codex_linux_sandbox_exe: Option) -> IoResult<()> // Task: write outgoing messages to stdout. let stdout_writer_handle = tokio::spawn(async move { let mut stdout = io::stdout(); - while let Some(msg) = outgoing_rx.recv().await { + while let Some(outgoing_message) = outgoing_rx.recv().await { + let msg: JSONRPCMessage = outgoing_message.into(); match serde_json::to_string(&msg) { Ok(json) => { if let Err(e) = stdout.write_all(json.as_bytes()).await { diff --git a/codex-rs/mcp-server/src/message_processor.rs b/codex-rs/mcp-server/src/message_processor.rs index bf6f42e569..61c320edb9 100644 --- a/codex-rs/mcp-server/src/message_processor.rs +++ b/codex-rs/mcp-server/src/message_processor.rs @@ -1,19 +1,17 @@ use std::path::PathBuf; +use std::sync::Arc; use crate::codex_tool_config::CodexToolCallParam; use crate::codex_tool_config::create_tool_for_codex_tool_call_param; +use crate::outgoing_message::OutgoingMessageSender; use codex_core::config::Config as CodexConfig; use mcp_types::CallToolRequestParams; use mcp_types::CallToolResult; -use mcp_types::CallToolResultContent; use mcp_types::ClientRequest; -use mcp_types::JSONRPC_VERSION; -use mcp_types::JSONRPCBatchRequest; -use mcp_types::JSONRPCBatchResponse; +use mcp_types::ContentBlock; use mcp_types::JSONRPCError; use mcp_types::JSONRPCErrorError; -use mcp_types::JSONRPCMessage; use mcp_types::JSONRPCNotification; use mcp_types::JSONRPCRequest; use mcp_types::JSONRPCResponse; @@ -24,11 +22,10 @@ use mcp_types::ServerCapabilitiesTools; use mcp_types::ServerNotification; use mcp_types::TextContent; use serde_json::json; -use tokio::sync::mpsc; use tokio::task; pub(crate) struct MessageProcessor { - outgoing: mpsc::Sender, + outgoing: Arc, initialized: bool, codex_linux_sandbox_exe: Option, } @@ -37,17 +34,17 @@ impl MessageProcessor { /// Create a new `MessageProcessor`, retaining a handle to the outgoing /// `Sender` so handlers can enqueue messages to be written to stdout. pub(crate) fn new( - outgoing: mpsc::Sender, + outgoing: OutgoingMessageSender, codex_linux_sandbox_exe: Option, ) -> Self { Self { - outgoing, + outgoing: Arc::new(outgoing), initialized: false, codex_linux_sandbox_exe, } } - pub(crate) fn process_request(&mut self, request: JSONRPCRequest) { + pub(crate) async fn process_request(&mut self, request: JSONRPCRequest) { // Hold on to the ID so we can respond. let request_id = request.id.clone(); @@ -62,10 +59,10 @@ impl MessageProcessor { // Dispatch to a dedicated handler for each request type. match client_request { ClientRequest::InitializeRequest(params) => { - self.handle_initialize(request_id, params); + self.handle_initialize(request_id, params).await; } ClientRequest::PingRequest(params) => { - self.handle_ping(request_id, params); + self.handle_ping(request_id, params).await; } ClientRequest::ListResourcesRequest(params) => { self.handle_list_resources(params); @@ -89,10 +86,10 @@ impl MessageProcessor { self.handle_get_prompt(params); } ClientRequest::ListToolsRequest(params) => { - self.handle_list_tools(request_id, params); + self.handle_list_tools(request_id, params).await; } ClientRequest::CallToolRequest(params) => { - self.handle_call_tool(request_id, params); + self.handle_call_tool(request_id, params).await; } ClientRequest::SetLevelRequest(params) => { self.handle_set_level(params); @@ -104,8 +101,10 @@ impl MessageProcessor { } /// Handle a standalone JSON-RPC response originating from the peer. - pub(crate) fn process_response(&mut self, response: JSONRPCResponse) { + pub(crate) async fn process_response(&mut self, response: JSONRPCResponse) { tracing::info!("<- response: {:?}", response); + let JSONRPCResponse { id, result, .. } = response; + self.outgoing.notify_client_response(id, result).await } /// Handle a fire-and-forget JSON-RPC notification. @@ -145,42 +144,12 @@ impl MessageProcessor { } } - /// Handle a batch of requests and/or notifications. - pub(crate) fn process_batch_request(&mut self, batch: JSONRPCBatchRequest) { - tracing::info!("<- batch request containing {} item(s)", batch.len()); - for item in batch { - match item { - mcp_types::JSONRPCBatchRequestItem::JSONRPCRequest(req) => { - self.process_request(req); - } - mcp_types::JSONRPCBatchRequestItem::JSONRPCNotification(note) => { - self.process_notification(note); - } - } - } - } - /// Handle an error object received from the peer. pub(crate) fn process_error(&mut self, err: JSONRPCError) { tracing::error!("<- error: {:?}", err); } - /// Handle a batch of responses/errors. - pub(crate) fn process_batch_response(&mut self, batch: JSONRPCBatchResponse) { - tracing::info!("<- batch response containing {} item(s)", batch.len()); - for item in batch { - match item { - mcp_types::JSONRPCBatchResponseItem::JSONRPCResponse(resp) => { - self.process_response(resp); - } - mcp_types::JSONRPCBatchResponseItem::JSONRPCError(err) => { - self.process_error(err); - } - } - } - } - - fn handle_initialize( + async fn handle_initialize( &mut self, id: RequestId, params: ::Params, @@ -189,19 +158,12 @@ impl MessageProcessor { if self.initialized { // Already initialised: send JSON-RPC error response. - let error_msg = JSONRPCMessage::Error(JSONRPCError { - jsonrpc: JSONRPC_VERSION.into(), - id, - error: JSONRPCErrorError { - code: -32600, // Invalid Request - message: "initialize called more than once".to_string(), - data: None, - }, - }); - - if let Err(e) = self.outgoing.try_send(error_msg) { - tracing::error!("Failed to send initialization error: {e}"); - } + let error = JSONRPCErrorError { + code: -32600, // Invalid Request + message: "initialize called more than once".to_string(), + data: None, + }; + self.outgoing.send_error(id, error).await; return; } @@ -223,38 +185,34 @@ impl MessageProcessor { protocol_version: params.protocol_version.clone(), server_info: mcp_types::Implementation { name: "codex-mcp-server".to_string(), - version: mcp_types::MCP_SCHEMA_VERSION.to_string(), + version: env!("CARGO_PKG_VERSION").to_string(), + title: Some("Codex".to_string()), }, }; - self.send_response::(id, result); + self.send_response::(id, result) + .await; } - fn send_response(&self, id: RequestId, result: T::Result) + async fn send_response(&self, id: RequestId, result: T::Result) where T: ModelContextProtocolRequest, { // result has `Serialized` instance so should never fail #[expect(clippy::unwrap_used)] - let response = JSONRPCMessage::Response(JSONRPCResponse { - jsonrpc: JSONRPC_VERSION.into(), - id, - result: serde_json::to_value(result).unwrap(), - }); - - if let Err(e) = self.outgoing.try_send(response) { - tracing::error!("Failed to send response: {e}"); - } + let result = serde_json::to_value(result).unwrap(); + self.outgoing.send_response(id, result).await; } - fn handle_ping( + async fn handle_ping( &self, id: RequestId, params: ::Params, ) { tracing::info!("ping -> params: {:?}", params); let result = json!({}); - self.send_response::(id, result); + self.send_response::(id, result) + .await; } fn handle_list_resources( @@ -307,7 +265,7 @@ impl MessageProcessor { tracing::info!("prompts/get -> params: {:?}", params); } - fn handle_list_tools( + async fn handle_list_tools( &self, id: RequestId, params: ::Params, @@ -318,10 +276,11 @@ impl MessageProcessor { next_cursor: None, }; - self.send_response::(id, result); + self.send_response::(id, result) + .await; } - fn handle_call_tool( + async fn handle_call_tool( &self, id: RequestId, params: ::Params, @@ -333,14 +292,16 @@ impl MessageProcessor { if name != "codex" { // Tool not found – return error result so the LLM can react. let result = CallToolResult { - content: vec![CallToolResultContent::TextContent(TextContent { + content: vec![ContentBlock::TextContent(TextContent { r#type: "text".to_string(), text: format!("Unknown tool '{name}'"), annotations: None, })], is_error: Some(true), + structured_content: None, }; - self.send_response::(id, result); + self.send_response::(id, result) + .await; return; } @@ -350,7 +311,7 @@ impl MessageProcessor { Ok(cfg) => cfg, Err(e) => { let result = CallToolResult { - content: vec![CallToolResultContent::TextContent(TextContent { + content: vec![ContentBlock::TextContent(TextContent { r#type: "text".to_owned(), text: format!( "Failed to load Codex configuration from overrides: {e}" @@ -358,27 +319,31 @@ impl MessageProcessor { annotations: None, })], is_error: Some(true), + structured_content: None, }; - self.send_response::(id, result); + self.send_response::(id, result) + .await; return; } }, Err(e) => { let result = CallToolResult { - content: vec![CallToolResultContent::TextContent(TextContent { + content: vec![ContentBlock::TextContent(TextContent { r#type: "text".to_owned(), text: format!("Failed to parse configuration for Codex tool: {e}"), annotations: None, })], is_error: Some(true), + structured_content: None, }; - self.send_response::(id, result); + self.send_response::(id, result) + .await; return; } }, None => { let result = CallToolResult { - content: vec![CallToolResultContent::TextContent(TextContent { + content: vec![ContentBlock::TextContent(TextContent { r#type: "text".to_string(), text: "Missing arguments for codex tool-call; the `prompt` field is required." @@ -386,8 +351,10 @@ impl MessageProcessor { annotations: None, })], is_error: Some(true), + structured_content: None, }; - self.send_response::(id, result); + self.send_response::(id, result) + .await; return; } }; diff --git a/codex-rs/mcp-server/src/outgoing_message.rs b/codex-rs/mcp-server/src/outgoing_message.rs new file mode 100644 index 0000000000..a1eea65f25 --- /dev/null +++ b/codex-rs/mcp-server/src/outgoing_message.rs @@ -0,0 +1,165 @@ +use std::collections::HashMap; +use std::sync::atomic::AtomicI64; +use std::sync::atomic::Ordering; + +use codex_core::protocol::Event; +use mcp_types::JSONRPC_VERSION; +use mcp_types::JSONRPCError; +use mcp_types::JSONRPCErrorError; +use mcp_types::JSONRPCMessage; +use mcp_types::JSONRPCNotification; +use mcp_types::JSONRPCRequest; +use mcp_types::JSONRPCResponse; +use mcp_types::RequestId; +use mcp_types::Result; +use serde::Serialize; +use tokio::sync::Mutex; +use tokio::sync::mpsc; +use tokio::sync::oneshot; +use tracing::warn; + +pub(crate) struct OutgoingMessageSender { + next_request_id: AtomicI64, + sender: mpsc::Sender, + request_id_to_callback: Mutex>>, +} + +impl OutgoingMessageSender { + pub(crate) fn new(sender: mpsc::Sender) -> Self { + Self { + next_request_id: AtomicI64::new(0), + sender, + request_id_to_callback: Mutex::new(HashMap::new()), + } + } + + pub(crate) async fn send_request( + &self, + method: &str, + params: Option, + ) -> oneshot::Receiver { + let id = RequestId::Integer(self.next_request_id.fetch_add(1, Ordering::Relaxed)); + let outgoing_message_id = id.clone(); + let (tx_approve, rx_approve) = oneshot::channel(); + { + let mut request_id_to_callback = self.request_id_to_callback.lock().await; + request_id_to_callback.insert(id, tx_approve); + } + + let outgoing_message = OutgoingMessage::Request(OutgoingRequest { + id: outgoing_message_id, + method: method.to_string(), + params, + }); + let _ = self.sender.send(outgoing_message).await; + rx_approve + } + + pub(crate) async fn notify_client_response(&self, id: RequestId, result: Result) { + let entry = { + let mut request_id_to_callback = self.request_id_to_callback.lock().await; + request_id_to_callback.remove_entry(&id) + }; + + match entry { + Some((id, sender)) => { + if let Err(err) = sender.send(result) { + warn!("could not notify callback for {id:?} due to: {err:?}"); + } + } + None => { + warn!("could not find callback for {id:?}"); + } + } + } + + pub(crate) async fn send_response(&self, id: RequestId, result: Result) { + let outgoing_message = OutgoingMessage::Response(OutgoingResponse { id, result }); + let _ = self.sender.send(outgoing_message).await; + } + + pub(crate) async fn send_event_as_notification(&self, event: &Event) { + #[expect(clippy::expect_used)] + let params = Some(serde_json::to_value(event).expect("Event must serialize")); + let outgoing_message = OutgoingMessage::Notification(OutgoingNotification { + method: "codex/event".to_string(), + params, + }); + let _ = self.sender.send(outgoing_message).await; + } + + pub(crate) async fn send_error(&self, id: RequestId, error: JSONRPCErrorError) { + let outgoing_message = OutgoingMessage::Error(OutgoingError { id, error }); + let _ = self.sender.send(outgoing_message).await; + } +} + +/// Outgoing message from the server to the client. +pub(crate) enum OutgoingMessage { + Request(OutgoingRequest), + Notification(OutgoingNotification), + Response(OutgoingResponse), + Error(OutgoingError), +} + +impl From for JSONRPCMessage { + fn from(val: OutgoingMessage) -> Self { + use OutgoingMessage::*; + match val { + Request(OutgoingRequest { id, method, params }) => { + JSONRPCMessage::Request(JSONRPCRequest { + jsonrpc: JSONRPC_VERSION.into(), + id, + method, + params, + }) + } + Notification(OutgoingNotification { method, params }) => { + JSONRPCMessage::Notification(JSONRPCNotification { + jsonrpc: JSONRPC_VERSION.into(), + method, + params, + }) + } + Response(OutgoingResponse { id, result }) => { + JSONRPCMessage::Response(JSONRPCResponse { + jsonrpc: JSONRPC_VERSION.into(), + id, + result, + }) + } + Error(OutgoingError { id, error }) => JSONRPCMessage::Error(JSONRPCError { + jsonrpc: JSONRPC_VERSION.into(), + id, + error, + }), + } + } +} + +#[derive(Debug, Clone, PartialEq, Serialize)] +pub(crate) struct OutgoingRequest { + pub id: RequestId, + pub method: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub params: Option, +} + +#[derive(Debug, Clone, PartialEq, Serialize)] +pub(crate) struct OutgoingNotification { + pub method: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub params: Option, +} + +#[derive(Debug, Clone, PartialEq, Serialize)] +pub(crate) struct OutgoingResponse { + pub id: RequestId, + pub result: Result, +} + +#[derive(Debug, Clone, PartialEq, Serialize)] +pub(crate) struct OutgoingError { + pub error: JSONRPCErrorError, + pub id: RequestId, +} diff --git a/codex-rs/mcp-server/tests/common/mcp_process.rs b/codex-rs/mcp-server/tests/common/mcp_process.rs new file mode 100644 index 0000000000..42d15f7877 --- /dev/null +++ b/codex-rs/mcp-server/tests/common/mcp_process.rs @@ -0,0 +1,255 @@ +use std::path::Path; +use std::process::Stdio; +use std::sync::atomic::AtomicI64; +use std::sync::atomic::Ordering; +use tokio::io::AsyncBufReadExt; +use tokio::io::AsyncWriteExt; +use tokio::io::BufReader; +use tokio::process::Child; +use tokio::process::ChildStdin; +use tokio::process::ChildStdout; + +use anyhow::Context; +use assert_cmd::prelude::*; +use codex_mcp_server::CodexToolCallParam; +use mcp_types::CallToolRequestParams; +use mcp_types::ClientCapabilities; +use mcp_types::Implementation; +use mcp_types::InitializeRequestParams; +use mcp_types::JSONRPC_VERSION; +use mcp_types::JSONRPCMessage; +use mcp_types::JSONRPCNotification; +use mcp_types::JSONRPCRequest; +use mcp_types::JSONRPCResponse; +use mcp_types::ModelContextProtocolNotification; +use mcp_types::ModelContextProtocolRequest; +use mcp_types::RequestId; +use pretty_assertions::assert_eq; +use serde_json::json; +use std::process::Command as StdCommand; +use tokio::process::Command; + +pub struct McpProcess { + next_request_id: AtomicI64, + /// Retain this child process until the client is dropped. The Tokio runtime + /// will make a "best effort" to reap the process after it exits, but it is + /// not a guarantee. See the `kill_on_drop` documentation for details. + #[allow(dead_code)] + process: Child, + stdin: ChildStdin, + stdout: BufReader, +} + +impl McpProcess { + pub async fn new(codex_home: &Path) -> anyhow::Result { + // Use assert_cmd to locate the binary path and then switch to tokio::process::Command + let std_cmd = StdCommand::cargo_bin("codex-mcp-server") + .context("should find binary for codex-mcp-server")?; + + let program = std_cmd.get_program().to_owned(); + + let mut cmd = Command::new(program); + + cmd.stdin(Stdio::piped()); + cmd.stdout(Stdio::piped()); + cmd.env("CODEX_HOME", codex_home); + cmd.env("RUST_LOG", "debug"); + + let mut process = cmd + .kill_on_drop(true) + .spawn() + .context("codex-mcp-server proc should start")?; + let stdin = process + .stdin + .take() + .ok_or_else(|| anyhow::format_err!("mcp should have stdin fd"))?; + let stdout = process + .stdout + .take() + .ok_or_else(|| anyhow::format_err!("mcp should have stdout fd"))?; + let stdout = BufReader::new(stdout); + Ok(Self { + next_request_id: AtomicI64::new(0), + process, + stdin, + stdout, + }) + } + + /// Performs the initialization handshake with the MCP server. + pub async fn initialize(&mut self) -> anyhow::Result<()> { + let request_id = self.next_request_id.fetch_add(1, Ordering::Relaxed); + + let params = InitializeRequestParams { + capabilities: ClientCapabilities { + elicitation: Some(json!({})), + experimental: None, + roots: None, + sampling: None, + }, + client_info: Implementation { + name: "elicitation test".into(), + title: Some("Elicitation Test".into()), + version: "0.0.0".into(), + }, + protocol_version: mcp_types::MCP_SCHEMA_VERSION.into(), + }; + let params_value = serde_json::to_value(params)?; + + self.send_jsonrpc_message(JSONRPCMessage::Request(JSONRPCRequest { + jsonrpc: JSONRPC_VERSION.into(), + id: RequestId::Integer(request_id), + method: mcp_types::InitializeRequest::METHOD.into(), + params: Some(params_value), + })) + .await?; + + let initialized = self.read_jsonrpc_message().await?; + assert_eq!( + JSONRPCMessage::Response(JSONRPCResponse { + jsonrpc: JSONRPC_VERSION.into(), + id: RequestId::Integer(request_id), + result: json!({ + "capabilities": { + "tools": { + "listChanged": true + }, + }, + "serverInfo": { + "name": "codex-mcp-server", + "title": "Codex", + "version": "0.0.0" + }, + "protocolVersion": mcp_types::MCP_SCHEMA_VERSION + }) + }), + initialized + ); + + // Send notifications/initialized to ack the response. + self.send_jsonrpc_message(JSONRPCMessage::Notification(JSONRPCNotification { + jsonrpc: JSONRPC_VERSION.into(), + method: mcp_types::InitializedNotification::METHOD.into(), + params: None, + })) + .await?; + + Ok(()) + } + + /// Returns the id used to make the request so it can be used when + /// correlating notifications. + pub async fn send_codex_tool_call(&mut self, prompt: &str) -> anyhow::Result { + let codex_tool_call_params = CallToolRequestParams { + name: "codex".to_string(), + arguments: Some(serde_json::to_value(CodexToolCallParam { + prompt: prompt.to_string(), + model: None, + profile: None, + cwd: None, + approval_policy: None, + sandbox: None, + config: None, + })?), + }; + self.send_request( + mcp_types::CallToolRequest::METHOD, + Some(serde_json::to_value(codex_tool_call_params)?), + ) + .await + } + + async fn send_request( + &mut self, + method: &str, + params: Option, + ) -> anyhow::Result { + let request_id = self.next_request_id.fetch_add(1, Ordering::Relaxed); + + let message = JSONRPCMessage::Request(JSONRPCRequest { + jsonrpc: JSONRPC_VERSION.into(), + id: RequestId::Integer(request_id), + method: method.to_string(), + params, + }); + self.send_jsonrpc_message(message).await?; + Ok(request_id) + } + + pub async fn send_response( + &mut self, + id: RequestId, + result: serde_json::Value, + ) -> anyhow::Result<()> { + self.send_jsonrpc_message(JSONRPCMessage::Response(JSONRPCResponse { + jsonrpc: JSONRPC_VERSION.into(), + id, + result, + })) + .await + } + + async fn send_jsonrpc_message(&mut self, message: JSONRPCMessage) -> anyhow::Result<()> { + let payload = serde_json::to_string(&message)?; + self.stdin.write_all(payload.as_bytes()).await?; + self.stdin.write_all(b"\n").await?; + self.stdin.flush().await?; + Ok(()) + } + + async fn read_jsonrpc_message(&mut self) -> anyhow::Result { + let mut line = String::new(); + self.stdout.read_line(&mut line).await?; + let message = serde_json::from_str::(&line)?; + Ok(message) + } + + pub async fn read_stream_until_request_message(&mut self) -> anyhow::Result { + loop { + let message = self.read_jsonrpc_message().await?; + eprint!("message: {message:?}"); + + match message { + JSONRPCMessage::Notification(_) => { + eprintln!("notification: {message:?}"); + } + JSONRPCMessage::Request(jsonrpc_request) => { + return Ok(jsonrpc_request); + } + JSONRPCMessage::Error(_) => { + anyhow::bail!("unexpected JSONRPCMessage::Error: {message:?}"); + } + JSONRPCMessage::Response(_) => { + anyhow::bail!("unexpected JSONRPCMessage::Response: {message:?}"); + } + } + } + } + + pub async fn read_stream_until_response_message( + &mut self, + request_id: RequestId, + ) -> anyhow::Result { + loop { + let message = self.read_jsonrpc_message().await?; + eprint!("message: {message:?}"); + + match message { + JSONRPCMessage::Notification(_) => { + eprintln!("notification: {message:?}"); + } + JSONRPCMessage::Request(_) => { + anyhow::bail!("unexpected JSONRPCMessage::Request: {message:?}"); + } + JSONRPCMessage::Error(_) => { + anyhow::bail!("unexpected JSONRPCMessage::Error: {message:?}"); + } + JSONRPCMessage::Response(jsonrpc_response) => { + if jsonrpc_response.id == request_id { + return Ok(jsonrpc_response); + } + } + } + } + } +} diff --git a/codex-rs/mcp-server/tests/common/mock_model_server.rs b/codex-rs/mcp-server/tests/common/mock_model_server.rs new file mode 100644 index 0000000000..be7f3eb5b3 --- /dev/null +++ b/codex-rs/mcp-server/tests/common/mock_model_server.rs @@ -0,0 +1,47 @@ +use std::sync::atomic::AtomicUsize; +use std::sync::atomic::Ordering; + +use wiremock::Mock; +use wiremock::MockServer; +use wiremock::Respond; +use wiremock::ResponseTemplate; +use wiremock::matchers::method; +use wiremock::matchers::path; + +/// Create a mock server that will provide the responses, in order, for +/// requests to the `/v1/chat/completions` endpoint. +pub async fn create_mock_chat_completions_server(responses: Vec) -> MockServer { + let server = MockServer::start().await; + + let num_calls = responses.len(); + let seq_responder = SeqResponder { + num_calls: AtomicUsize::new(0), + responses, + }; + + Mock::given(method("POST")) + .and(path("/v1/chat/completions")) + .respond_with(seq_responder) + .expect(num_calls as u64) + .mount(&server) + .await; + + server +} + +struct SeqResponder { + num_calls: AtomicUsize, + responses: Vec, +} + +impl Respond for SeqResponder { + fn respond(&self, _: &wiremock::Request) -> ResponseTemplate { + let call_num = self.num_calls.fetch_add(1, Ordering::SeqCst); + match self.responses.get(call_num) { + Some(response) => ResponseTemplate::new(200) + .insert_header("content-type", "text/event-stream") + .set_body_raw(response.clone(), "text/event-stream"), + None => panic!("no response for {call_num}"), + } + } +} diff --git a/codex-rs/mcp-server/tests/common/mod.rs b/codex-rs/mcp-server/tests/common/mod.rs new file mode 100644 index 0000000000..61a5774bc4 --- /dev/null +++ b/codex-rs/mcp-server/tests/common/mod.rs @@ -0,0 +1,8 @@ +mod mcp_process; +mod mock_model_server; +mod responses; + +pub use mcp_process::McpProcess; +pub use mock_model_server::create_mock_chat_completions_server; +pub use responses::create_final_assistant_message_sse_response; +pub use responses::create_shell_sse_response; diff --git a/codex-rs/mcp-server/tests/common/responses.rs b/codex-rs/mcp-server/tests/common/responses.rs new file mode 100644 index 0000000000..a11c72d0f4 --- /dev/null +++ b/codex-rs/mcp-server/tests/common/responses.rs @@ -0,0 +1,59 @@ +use serde_json::json; +use std::path::Path; + +pub fn create_shell_sse_response( + command: Vec, + workdir: Option<&Path>, + timeout_ms: Option, + call_id: &str, +) -> anyhow::Result { + // The `arguments`` for the `shell` tool is a serialized JSON object. + let tool_call_arguments = serde_json::to_string(&json!({ + "command": command, + "workdir": workdir.map(|w| w.to_string_lossy()), + "timeout": timeout_ms + }))?; + let tool_call = json!({ + "choices": [ + { + "delta": { + "tool_calls": [ + { + "id": call_id, + "function": { + "name": "shell", + "arguments": tool_call_arguments + } + } + ] + }, + "finish_reason": "tool_calls" + } + ] + }); + + let sse = format!( + "data: {}\n\ndata: DONE\n\n", + serde_json::to_string(&tool_call)? + ); + Ok(sse) +} + +pub fn create_final_assistant_message_sse_response(message: &str) -> anyhow::Result { + let assistant_message = json!({ + "choices": [ + { + "delta": { + "content": message + }, + "finish_reason": "stop" + } + ] + }); + + let sse = format!( + "data: {}\n\ndata: DONE\n\n", + serde_json::to_string(&assistant_message)? + ); + Ok(sse) +} diff --git a/codex-rs/mcp-server/tests/elicitation.rs b/codex-rs/mcp-server/tests/elicitation.rs new file mode 100644 index 0000000000..7fd68d6775 --- /dev/null +++ b/codex-rs/mcp-server/tests/elicitation.rs @@ -0,0 +1,195 @@ +mod common; + +use std::path::Path; + +use codex_core::exec::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR; +use codex_core::protocol::ReviewDecision; +use codex_mcp_server::ExecApprovalElicitRequestParams; +use codex_mcp_server::ExecApprovalResponse; +use mcp_types::ElicitRequest; +use mcp_types::ElicitRequestParamsRequestedSchema; +use mcp_types::JSONRPC_VERSION; +use mcp_types::JSONRPCRequest; +use mcp_types::JSONRPCResponse; +use mcp_types::ModelContextProtocolRequest; +use mcp_types::RequestId; +use pretty_assertions::assert_eq; +use serde_json::json; +use tempfile::TempDir; +use tokio::time::timeout; + +use crate::common::McpProcess; +use crate::common::create_final_assistant_message_sse_response; +use crate::common::create_mock_chat_completions_server; +use crate::common::create_shell_sse_response; + +const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); + +/// Test that a shell command that is not on the "trusted" list triggers an +/// elicitation request to the MCP and that sending the approval runs the +/// command, as expected. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn test_shell_command_approval_triggers_elicitation() { + if std::env::var(CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR).is_ok() { + println!( + "Skipping test because it cannot execute when network is disabled in a Codex sandbox." + ); + return; + } + + // Apparently `#[tokio::test]` must return `()`, so we create a helper + // function that returns `Result` so we can use `?` in favor of `unwrap`. + if let Err(err) = shell_command_approval_triggers_elicitation().await { + panic!("failure: {err}"); + } +} + +async fn shell_command_approval_triggers_elicitation() -> anyhow::Result<()> { + // We use `git init` because it will not be on the "trusted" list. + let shell_command = vec!["git".to_string(), "init".to_string()]; + let workdir_for_shell_function_call = TempDir::new()?; + + // Configure the mock server so it makes two responses: + // 1. The first response is a shell function call that will trigger an + // elicitation request. + // 2. The second response is the final assistant message that should be + // returned after the elicitation is approved and the command is run. + let server = create_mock_chat_completions_server(vec![ + create_shell_sse_response( + shell_command.clone(), + Some(workdir_for_shell_function_call.path()), + Some(5_000), + "call1234", + )?, + create_final_assistant_message_sse_response("Enjoy your new git repo!")?, + ]) + .await; + + // Run `codex mcp` with a specific config.toml. + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path(), server.uri())?; + let mut mcp_process = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp_process.initialize()).await??; + + // Send a "codex" tool request, which should hit the completions endpoint. + // In turn, it should reply with a tool call, which the MCP should forward + // as an elicitation. + let codex_request_id = mcp_process.send_codex_tool_call("run `git init`").await?; + let elicitation_request = timeout( + DEFAULT_READ_TIMEOUT, + mcp_process.read_stream_until_request_message(), + ) + .await??; + + // This is the first request from the server, so the id should be 0 given + // how things are currently implemented. + let elicitation_request_id = RequestId::Integer(0); + let expected_elicitation_request = create_expected_elicitation_request( + elicitation_request_id.clone(), + shell_command.clone(), + workdir_for_shell_function_call.path(), + codex_request_id.to_string(), + // Internal Codex id: empirically it is 1, but this is + // admittedly an internal detail that could change. + "1".to_string(), + )?; + assert_eq!(expected_elicitation_request, elicitation_request); + + // Accept the `git init` request by responding to the elicitation. + mcp_process + .send_response( + elicitation_request_id, + serde_json::to_value(ExecApprovalResponse { + decision: ReviewDecision::Approved, + })?, + ) + .await?; + + // Verify the original `codex` tool call completes and that `git init` ran + // successfully. + let codex_response = timeout( + DEFAULT_READ_TIMEOUT, + mcp_process.read_stream_until_response_message(RequestId::Integer(codex_request_id)), + ) + .await??; + assert_eq!( + JSONRPCResponse { + jsonrpc: JSONRPC_VERSION.into(), + id: RequestId::Integer(codex_request_id), + result: json!({ + "content": [ + { + "text": "Enjoy your new git repo!", + "type": "text" + } + ] + }), + }, + codex_response + ); + + assert!( + workdir_for_shell_function_call.path().join(".git").is_dir(), + ".git folder should have been created" + ); + + Ok(()) +} + +/// Create a Codex config that uses the mock server as the model provider. +/// It also uses `approval_policy = "untrusted"` so that we exercise the +/// elicitation code path for shell commands. +fn create_config_toml(codex_home: &Path, server_uri: String) -> std::io::Result<()> { + let config_toml = codex_home.join("config.toml"); + std::fs::write( + config_toml, + format!( + r#" +model = "mock-model" +approval_policy = "untrusted" +sandbox_policy = "read-only" + +model_provider = "mock_provider" + +[model_providers.mock_provider] +name = "Mock provider for test" +base_url = "{server_uri}/v1" +wire_api = "chat" +request_max_retries = 0 +stream_max_retries = 0 +"# + ), + ) +} + +fn create_expected_elicitation_request( + elicitation_request_id: RequestId, + command: Vec, + workdir: &Path, + codex_mcp_tool_call_id: String, + codex_event_id: String, +) -> anyhow::Result { + let expected_message = format!( + "Allow Codex to run `{}` in `{}`?", + shlex::try_join(command.iter().map(|s| s.as_ref()))?, + workdir.to_string_lossy() + ); + Ok(JSONRPCRequest { + jsonrpc: JSONRPC_VERSION.into(), + id: elicitation_request_id, + method: ElicitRequest::METHOD.to_string(), + params: Some(serde_json::to_value(&ExecApprovalElicitRequestParams { + message: expected_message, + requested_schema: ElicitRequestParamsRequestedSchema { + r#type: "object".to_string(), + properties: json!({}), + required: None, + }, + codex_elicitation: "exec-approval".to_string(), + codex_mcp_tool_call_id, + codex_event_id, + codex_command: command, + codex_cwd: workdir.to_path_buf(), + })?), + }) +} diff --git a/codex-rs/mcp-types/README.md b/codex-rs/mcp-types/README.md index 2ac613ea96..66ea540cc4 100644 --- a/codex-rs/mcp-types/README.md +++ b/codex-rs/mcp-types/README.md @@ -2,7 +2,7 @@ Types for Model Context Protocol. Inspired by https://crates.io/crates/lsp-types. -As documented on https://modelcontextprotocol.io/specification/2025-03-26/basic: +As documented on https://modelcontextprotocol.io/specification/2025-06-18/basic: -- TypeScript schema is the source of truth: https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/schema/2025-03-26/schema.ts -- JSON schema is amenable to automated tooling: https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/schema/2025-03-26/schema.json +- TypeScript schema is the source of truth: https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/schema/2025-06-18/schema.ts +- JSON schema is amenable to automated tooling: https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/schema/2025-06-18/schema.json diff --git a/codex-rs/mcp-types/generate_mcp_types.py b/codex-rs/mcp-types/generate_mcp_types.py index ff11dbf0dc..38f57e9a1b 100755 --- a/codex-rs/mcp-types/generate_mcp_types.py +++ b/codex-rs/mcp-types/generate_mcp_types.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 # flake8: noqa: E501 +import argparse import json import subprocess import sys @@ -13,10 +14,13 @@ from pathlib import Path # Helper first so it is defined when other functions call it. from typing import Any, Literal -SCHEMA_VERSION = "2025-03-26" +SCHEMA_VERSION = "2025-06-18" JSONRPC_VERSION = "2.0" STANDARD_DERIVE = "#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]\n" +STANDARD_HASHABLE_DERIVE = ( + "#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Hash, Eq)]\n" +) # Will be populated with the schema's `definitions` map in `main()` so that # helper functions (for example `define_any_of`) can perform look-ups while @@ -26,19 +30,27 @@ DEFINITIONS: dict[str, Any] = {} CLIENT_REQUEST_TYPE_NAMES: list[str] = [] # Concrete *Notification types that make up the ServerNotification enum. SERVER_NOTIFICATION_TYPE_NAMES: list[str] = [] +# Enum types that will need a `allow(clippy::large_enum_variant)` annotation in +# order to compile without warnings. +LARGE_ENUMS = {"ServerResult"} def main() -> int: - num_args = len(sys.argv) - if num_args == 1: - schema_file = ( - Path(__file__).resolve().parent / "schema" / SCHEMA_VERSION / "schema.json" - ) - elif num_args == 2: - schema_file = Path(sys.argv[1]) - else: - print("Usage: python3 codegen.py ") - return 1 + parser = argparse.ArgumentParser( + description="Embed, cluster and analyse text prompts via the OpenAI API.", + ) + + default_schema_file = ( + Path(__file__).resolve().parent / "schema" / SCHEMA_VERSION / "schema.json" + ) + parser.add_argument( + "schema_file", + nargs="?", + default=default_schema_file, + help="schema.json file to process", + ) + args = parser.parse_args() + schema_file = args.schema_file lib_rs = Path(__file__).resolve().parent / "src/lib.rs" @@ -197,6 +209,8 @@ def add_definition(name: str, definition: dict[str, Any], out: list[str]) -> Non if name.endswith("Result"): out.extend(f"impl From<{name}> for serde_json::Value {{\n") out.append(f" fn from(value: {name}) -> Self {{\n") + out.append(" // Leave this as it should never fail\n") + out.append(" #[expect(clippy::unwrap_used)]\n") out.append(" serde_json::to_value(value).unwrap()\n") out.append(" }\n") out.append("}\n\n") @@ -211,20 +225,7 @@ def add_definition(name: str, definition: dict[str, Any], out: list[str]) -> Non any_of = definition.get("anyOf", []) if any_of: assert isinstance(any_of, list) - if name == "JSONRPCMessage": - # Special case for JSONRPCMessage because its definition in the - # JSON schema does not quite match how we think about this type - # definition in Rust. - deep_copied_any_of = json.loads(json.dumps(any_of)) - deep_copied_any_of[2] = { - "$ref": "#/definitions/JSONRPCBatchRequest", - } - deep_copied_any_of[5] = { - "$ref": "#/definitions/JSONRPCBatchResponse", - } - out.extend(define_any_of(name, deep_copied_any_of, description)) - else: - out.extend(define_any_of(name, any_of, description)) + out.extend(define_any_of(name, any_of, description)) return type_prop = definition.get("type", None) @@ -393,7 +394,7 @@ def define_string_enum( def define_untagged_enum(name: str, type_list: list[str], out: list[str]) -> None: - out.append(STANDARD_DERIVE) + out.append(STANDARD_HASHABLE_DERIVE) out.append("#[serde(untagged)]\n") out.append(f"pub enum {name} {{\n") for simple_type in type_list: @@ -439,6 +440,8 @@ def define_any_of( if serde := get_serde_annotation_for_anyof_type(name): out.append(serde + "\n") + if name in LARGE_ENUMS: + out.append("#[allow(clippy::large_enum_variant)]\n") out.append(f"pub enum {name} {{\n") if name == "ClientRequest": @@ -596,6 +599,8 @@ def rust_prop_name(name: str, is_optional: bool) -> RustProp: prop_name = "r#type" elif name == "ref": prop_name = "r#ref" + elif name == "enum": + prop_name = "r#enum" elif snake_case := to_snake_case(name): prop_name = snake_case is_rename = True diff --git a/codex-rs/mcp-types/schema/2025-06-18/schema.json b/codex-rs/mcp-types/schema/2025-06-18/schema.json new file mode 100644 index 0000000000..24ba4f6309 --- /dev/null +++ b/codex-rs/mcp-types/schema/2025-06-18/schema.json @@ -0,0 +1,2517 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "Annotations": { + "description": "Optional annotations for the client. The client can use annotations to inform how objects are used or displayed", + "properties": { + "audience": { + "description": "Describes who the intended customer of this object or data is.\n\nIt can include multiple entries to indicate content useful for multiple audiences (e.g., `[\"user\", \"assistant\"]`).", + "items": { + "$ref": "#/definitions/Role" + }, + "type": "array" + }, + "lastModified": { + "description": "The moment the resource was last modified, as an ISO 8601 formatted string.\n\nShould be an ISO 8601 formatted string (e.g., \"2025-01-12T15:00:58Z\").\n\nExamples: last activity timestamp in an open file, timestamp when the resource\nwas attached, etc.", + "type": "string" + }, + "priority": { + "description": "Describes how important this data is for operating the server.\n\nA value of 1 means \"most important,\" and indicates that the data is\neffectively required, while 0 means \"least important,\" and indicates that\nthe data is entirely optional.", + "maximum": 1, + "minimum": 0, + "type": "number" + } + }, + "type": "object" + }, + "AudioContent": { + "description": "Audio provided to or from an LLM.", + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "annotations": { + "$ref": "#/definitions/Annotations", + "description": "Optional annotations for the client." + }, + "data": { + "description": "The base64-encoded audio data.", + "format": "byte", + "type": "string" + }, + "mimeType": { + "description": "The MIME type of the audio. Different providers may support different audio types.", + "type": "string" + }, + "type": { + "const": "audio", + "type": "string" + } + }, + "required": [ + "data", + "mimeType", + "type" + ], + "type": "object" + }, + "BaseMetadata": { + "description": "Base interface for metadata with name (identifier) and title (display name) properties.", + "properties": { + "name": { + "description": "Intended for programmatic or logical use, but used as a display name in past specs or fallback (if title isn't present).", + "type": "string" + }, + "title": { + "description": "Intended for UI and end-user contexts — optimized to be human-readable and easily understood,\neven by those unfamiliar with domain-specific terminology.\n\nIf not provided, the name should be used for display (except for Tool,\nwhere `annotations.title` should be given precedence over using `name`,\nif present).", + "type": "string" + } + }, + "required": [ + "name" + ], + "type": "object" + }, + "BlobResourceContents": { + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "blob": { + "description": "A base64-encoded string representing the binary data of the item.", + "format": "byte", + "type": "string" + }, + "mimeType": { + "description": "The MIME type of this resource, if known.", + "type": "string" + }, + "uri": { + "description": "The URI of this resource.", + "format": "uri", + "type": "string" + } + }, + "required": [ + "blob", + "uri" + ], + "type": "object" + }, + "BooleanSchema": { + "properties": { + "default": { + "type": "boolean" + }, + "description": { + "type": "string" + }, + "title": { + "type": "string" + }, + "type": { + "const": "boolean", + "type": "string" + } + }, + "required": [ + "type" + ], + "type": "object" + }, + "CallToolRequest": { + "description": "Used by the client to invoke a tool provided by the server.", + "properties": { + "method": { + "const": "tools/call", + "type": "string" + }, + "params": { + "properties": { + "arguments": { + "additionalProperties": {}, + "type": "object" + }, + "name": { + "type": "string" + } + }, + "required": [ + "name" + ], + "type": "object" + } + }, + "required": [ + "method", + "params" + ], + "type": "object" + }, + "CallToolResult": { + "description": "The server's response to a tool call.", + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "content": { + "description": "A list of content objects that represent the unstructured result of the tool call.", + "items": { + "$ref": "#/definitions/ContentBlock" + }, + "type": "array" + }, + "isError": { + "description": "Whether the tool call ended in an error.\n\nIf not set, this is assumed to be false (the call was successful).\n\nAny errors that originate from the tool SHOULD be reported inside the result\nobject, with `isError` set to true, _not_ as an MCP protocol-level error\nresponse. Otherwise, the LLM would not be able to see that an error occurred\nand self-correct.\n\nHowever, any errors in _finding_ the tool, an error indicating that the\nserver does not support tool calls, or any other exceptional conditions,\nshould be reported as an MCP error response.", + "type": "boolean" + }, + "structuredContent": { + "additionalProperties": {}, + "description": "An optional JSON object that represents the structured result of the tool call.", + "type": "object" + } + }, + "required": [ + "content" + ], + "type": "object" + }, + "CancelledNotification": { + "description": "This notification can be sent by either side to indicate that it is cancelling a previously-issued request.\n\nThe request SHOULD still be in-flight, but due to communication latency, it is always possible that this notification MAY arrive after the request has already finished.\n\nThis notification indicates that the result will be unused, so any associated processing SHOULD cease.\n\nA client MUST NOT attempt to cancel its `initialize` request.", + "properties": { + "method": { + "const": "notifications/cancelled", + "type": "string" + }, + "params": { + "properties": { + "reason": { + "description": "An optional string describing the reason for the cancellation. This MAY be logged or presented to the user.", + "type": "string" + }, + "requestId": { + "$ref": "#/definitions/RequestId", + "description": "The ID of the request to cancel.\n\nThis MUST correspond to the ID of a request previously issued in the same direction." + } + }, + "required": [ + "requestId" + ], + "type": "object" + } + }, + "required": [ + "method", + "params" + ], + "type": "object" + }, + "ClientCapabilities": { + "description": "Capabilities a client may support. Known capabilities are defined here, in this schema, but this is not a closed set: any client can define its own, additional capabilities.", + "properties": { + "elicitation": { + "additionalProperties": true, + "description": "Present if the client supports elicitation from the server.", + "properties": {}, + "type": "object" + }, + "experimental": { + "additionalProperties": { + "additionalProperties": true, + "properties": {}, + "type": "object" + }, + "description": "Experimental, non-standard capabilities that the client supports.", + "type": "object" + }, + "roots": { + "description": "Present if the client supports listing roots.", + "properties": { + "listChanged": { + "description": "Whether the client supports notifications for changes to the roots list.", + "type": "boolean" + } + }, + "type": "object" + }, + "sampling": { + "additionalProperties": true, + "description": "Present if the client supports sampling from an LLM.", + "properties": {}, + "type": "object" + } + }, + "type": "object" + }, + "ClientNotification": { + "anyOf": [ + { + "$ref": "#/definitions/CancelledNotification" + }, + { + "$ref": "#/definitions/InitializedNotification" + }, + { + "$ref": "#/definitions/ProgressNotification" + }, + { + "$ref": "#/definitions/RootsListChangedNotification" + } + ] + }, + "ClientRequest": { + "anyOf": [ + { + "$ref": "#/definitions/InitializeRequest" + }, + { + "$ref": "#/definitions/PingRequest" + }, + { + "$ref": "#/definitions/ListResourcesRequest" + }, + { + "$ref": "#/definitions/ListResourceTemplatesRequest" + }, + { + "$ref": "#/definitions/ReadResourceRequest" + }, + { + "$ref": "#/definitions/SubscribeRequest" + }, + { + "$ref": "#/definitions/UnsubscribeRequest" + }, + { + "$ref": "#/definitions/ListPromptsRequest" + }, + { + "$ref": "#/definitions/GetPromptRequest" + }, + { + "$ref": "#/definitions/ListToolsRequest" + }, + { + "$ref": "#/definitions/CallToolRequest" + }, + { + "$ref": "#/definitions/SetLevelRequest" + }, + { + "$ref": "#/definitions/CompleteRequest" + } + ] + }, + "ClientResult": { + "anyOf": [ + { + "$ref": "#/definitions/Result" + }, + { + "$ref": "#/definitions/CreateMessageResult" + }, + { + "$ref": "#/definitions/ListRootsResult" + }, + { + "$ref": "#/definitions/ElicitResult" + } + ] + }, + "CompleteRequest": { + "description": "A request from the client to the server, to ask for completion options.", + "properties": { + "method": { + "const": "completion/complete", + "type": "string" + }, + "params": { + "properties": { + "argument": { + "description": "The argument's information", + "properties": { + "name": { + "description": "The name of the argument", + "type": "string" + }, + "value": { + "description": "The value of the argument to use for completion matching.", + "type": "string" + } + }, + "required": [ + "name", + "value" + ], + "type": "object" + }, + "context": { + "description": "Additional, optional context for completions", + "properties": { + "arguments": { + "additionalProperties": { + "type": "string" + }, + "description": "Previously-resolved variables in a URI template or prompt.", + "type": "object" + } + }, + "type": "object" + }, + "ref": { + "anyOf": [ + { + "$ref": "#/definitions/PromptReference" + }, + { + "$ref": "#/definitions/ResourceTemplateReference" + } + ] + } + }, + "required": [ + "argument", + "ref" + ], + "type": "object" + } + }, + "required": [ + "method", + "params" + ], + "type": "object" + }, + "CompleteResult": { + "description": "The server's response to a completion/complete request", + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "completion": { + "properties": { + "hasMore": { + "description": "Indicates whether there are additional completion options beyond those provided in the current response, even if the exact total is unknown.", + "type": "boolean" + }, + "total": { + "description": "The total number of completion options available. This can exceed the number of values actually sent in the response.", + "type": "integer" + }, + "values": { + "description": "An array of completion values. Must not exceed 100 items.", + "items": { + "type": "string" + }, + "type": "array" + } + }, + "required": [ + "values" + ], + "type": "object" + } + }, + "required": [ + "completion" + ], + "type": "object" + }, + "ContentBlock": { + "anyOf": [ + { + "$ref": "#/definitions/TextContent" + }, + { + "$ref": "#/definitions/ImageContent" + }, + { + "$ref": "#/definitions/AudioContent" + }, + { + "$ref": "#/definitions/ResourceLink" + }, + { + "$ref": "#/definitions/EmbeddedResource" + } + ] + }, + "CreateMessageRequest": { + "description": "A request from the server to sample an LLM via the client. The client has full discretion over which model to select. The client should also inform the user before beginning sampling, to allow them to inspect the request (human in the loop) and decide whether to approve it.", + "properties": { + "method": { + "const": "sampling/createMessage", + "type": "string" + }, + "params": { + "properties": { + "includeContext": { + "description": "A request to include context from one or more MCP servers (including the caller), to be attached to the prompt. The client MAY ignore this request.", + "enum": [ + "allServers", + "none", + "thisServer" + ], + "type": "string" + }, + "maxTokens": { + "description": "The maximum number of tokens to sample, as requested by the server. The client MAY choose to sample fewer tokens than requested.", + "type": "integer" + }, + "messages": { + "items": { + "$ref": "#/definitions/SamplingMessage" + }, + "type": "array" + }, + "metadata": { + "additionalProperties": true, + "description": "Optional metadata to pass through to the LLM provider. The format of this metadata is provider-specific.", + "properties": {}, + "type": "object" + }, + "modelPreferences": { + "$ref": "#/definitions/ModelPreferences", + "description": "The server's preferences for which model to select. The client MAY ignore these preferences." + }, + "stopSequences": { + "items": { + "type": "string" + }, + "type": "array" + }, + "systemPrompt": { + "description": "An optional system prompt the server wants to use for sampling. The client MAY modify or omit this prompt.", + "type": "string" + }, + "temperature": { + "type": "number" + } + }, + "required": [ + "maxTokens", + "messages" + ], + "type": "object" + } + }, + "required": [ + "method", + "params" + ], + "type": "object" + }, + "CreateMessageResult": { + "description": "The client's response to a sampling/create_message request from the server. The client should inform the user before returning the sampled message, to allow them to inspect the response (human in the loop) and decide whether to allow the server to see it.", + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "content": { + "anyOf": [ + { + "$ref": "#/definitions/TextContent" + }, + { + "$ref": "#/definitions/ImageContent" + }, + { + "$ref": "#/definitions/AudioContent" + } + ] + }, + "model": { + "description": "The name of the model that generated the message.", + "type": "string" + }, + "role": { + "$ref": "#/definitions/Role" + }, + "stopReason": { + "description": "The reason why sampling stopped, if known.", + "type": "string" + } + }, + "required": [ + "content", + "model", + "role" + ], + "type": "object" + }, + "Cursor": { + "description": "An opaque token used to represent a cursor for pagination.", + "type": "string" + }, + "ElicitRequest": { + "description": "A request from the server to elicit additional information from the user via the client.", + "properties": { + "method": { + "const": "elicitation/create", + "type": "string" + }, + "params": { + "properties": { + "message": { + "description": "The message to present to the user.", + "type": "string" + }, + "requestedSchema": { + "description": "A restricted subset of JSON Schema.\nOnly top-level properties are allowed, without nesting.", + "properties": { + "properties": { + "additionalProperties": { + "$ref": "#/definitions/PrimitiveSchemaDefinition" + }, + "type": "object" + }, + "required": { + "items": { + "type": "string" + }, + "type": "array" + }, + "type": { + "const": "object", + "type": "string" + } + }, + "required": [ + "properties", + "type" + ], + "type": "object" + } + }, + "required": [ + "message", + "requestedSchema" + ], + "type": "object" + } + }, + "required": [ + "method", + "params" + ], + "type": "object" + }, + "ElicitResult": { + "description": "The client's response to an elicitation request.", + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "action": { + "description": "The user action in response to the elicitation.\n- \"accept\": User submitted the form/confirmed the action\n- \"decline\": User explicitly declined the action\n- \"cancel\": User dismissed without making an explicit choice", + "enum": [ + "accept", + "cancel", + "decline" + ], + "type": "string" + }, + "content": { + "additionalProperties": { + "type": [ + "string", + "integer", + "boolean" + ] + }, + "description": "The submitted form data, only present when action is \"accept\".\nContains values matching the requested schema.", + "type": "object" + } + }, + "required": [ + "action" + ], + "type": "object" + }, + "EmbeddedResource": { + "description": "The contents of a resource, embedded into a prompt or tool call result.\n\nIt is up to the client how best to render embedded resources for the benefit\nof the LLM and/or the user.", + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "annotations": { + "$ref": "#/definitions/Annotations", + "description": "Optional annotations for the client." + }, + "resource": { + "anyOf": [ + { + "$ref": "#/definitions/TextResourceContents" + }, + { + "$ref": "#/definitions/BlobResourceContents" + } + ] + }, + "type": { + "const": "resource", + "type": "string" + } + }, + "required": [ + "resource", + "type" + ], + "type": "object" + }, + "EmptyResult": { + "$ref": "#/definitions/Result" + }, + "EnumSchema": { + "properties": { + "description": { + "type": "string" + }, + "enum": { + "items": { + "type": "string" + }, + "type": "array" + }, + "enumNames": { + "items": { + "type": "string" + }, + "type": "array" + }, + "title": { + "type": "string" + }, + "type": { + "const": "string", + "type": "string" + } + }, + "required": [ + "enum", + "type" + ], + "type": "object" + }, + "GetPromptRequest": { + "description": "Used by the client to get a prompt provided by the server.", + "properties": { + "method": { + "const": "prompts/get", + "type": "string" + }, + "params": { + "properties": { + "arguments": { + "additionalProperties": { + "type": "string" + }, + "description": "Arguments to use for templating the prompt.", + "type": "object" + }, + "name": { + "description": "The name of the prompt or prompt template.", + "type": "string" + } + }, + "required": [ + "name" + ], + "type": "object" + } + }, + "required": [ + "method", + "params" + ], + "type": "object" + }, + "GetPromptResult": { + "description": "The server's response to a prompts/get request from the client.", + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "description": { + "description": "An optional description for the prompt.", + "type": "string" + }, + "messages": { + "items": { + "$ref": "#/definitions/PromptMessage" + }, + "type": "array" + } + }, + "required": [ + "messages" + ], + "type": "object" + }, + "ImageContent": { + "description": "An image provided to or from an LLM.", + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "annotations": { + "$ref": "#/definitions/Annotations", + "description": "Optional annotations for the client." + }, + "data": { + "description": "The base64-encoded image data.", + "format": "byte", + "type": "string" + }, + "mimeType": { + "description": "The MIME type of the image. Different providers may support different image types.", + "type": "string" + }, + "type": { + "const": "image", + "type": "string" + } + }, + "required": [ + "data", + "mimeType", + "type" + ], + "type": "object" + }, + "Implementation": { + "description": "Describes the name and version of an MCP implementation, with an optional title for UI representation.", + "properties": { + "name": { + "description": "Intended for programmatic or logical use, but used as a display name in past specs or fallback (if title isn't present).", + "type": "string" + }, + "title": { + "description": "Intended for UI and end-user contexts — optimized to be human-readable and easily understood,\neven by those unfamiliar with domain-specific terminology.\n\nIf not provided, the name should be used for display (except for Tool,\nwhere `annotations.title` should be given precedence over using `name`,\nif present).", + "type": "string" + }, + "version": { + "type": "string" + } + }, + "required": [ + "name", + "version" + ], + "type": "object" + }, + "InitializeRequest": { + "description": "This request is sent from the client to the server when it first connects, asking it to begin initialization.", + "properties": { + "method": { + "const": "initialize", + "type": "string" + }, + "params": { + "properties": { + "capabilities": { + "$ref": "#/definitions/ClientCapabilities" + }, + "clientInfo": { + "$ref": "#/definitions/Implementation" + }, + "protocolVersion": { + "description": "The latest version of the Model Context Protocol that the client supports. The client MAY decide to support older versions as well.", + "type": "string" + } + }, + "required": [ + "capabilities", + "clientInfo", + "protocolVersion" + ], + "type": "object" + } + }, + "required": [ + "method", + "params" + ], + "type": "object" + }, + "InitializeResult": { + "description": "After receiving an initialize request from the client, the server sends this response.", + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "capabilities": { + "$ref": "#/definitions/ServerCapabilities" + }, + "instructions": { + "description": "Instructions describing how to use the server and its features.\n\nThis can be used by clients to improve the LLM's understanding of available tools, resources, etc. It can be thought of like a \"hint\" to the model. For example, this information MAY be added to the system prompt.", + "type": "string" + }, + "protocolVersion": { + "description": "The version of the Model Context Protocol that the server wants to use. This may not match the version that the client requested. If the client cannot support this version, it MUST disconnect.", + "type": "string" + }, + "serverInfo": { + "$ref": "#/definitions/Implementation" + } + }, + "required": [ + "capabilities", + "protocolVersion", + "serverInfo" + ], + "type": "object" + }, + "InitializedNotification": { + "description": "This notification is sent from the client to the server after initialization has finished.", + "properties": { + "method": { + "const": "notifications/initialized", + "type": "string" + }, + "params": { + "additionalProperties": {}, + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + } + }, + "type": "object" + } + }, + "required": [ + "method" + ], + "type": "object" + }, + "JSONRPCError": { + "description": "A response to a request that indicates an error occurred.", + "properties": { + "error": { + "properties": { + "code": { + "description": "The error type that occurred.", + "type": "integer" + }, + "data": { + "description": "Additional information about the error. The value of this member is defined by the sender (e.g. detailed error information, nested errors etc.)." + }, + "message": { + "description": "A short description of the error. The message SHOULD be limited to a concise single sentence.", + "type": "string" + } + }, + "required": [ + "code", + "message" + ], + "type": "object" + }, + "id": { + "$ref": "#/definitions/RequestId" + }, + "jsonrpc": { + "const": "2.0", + "type": "string" + } + }, + "required": [ + "error", + "id", + "jsonrpc" + ], + "type": "object" + }, + "JSONRPCMessage": { + "anyOf": [ + { + "$ref": "#/definitions/JSONRPCRequest" + }, + { + "$ref": "#/definitions/JSONRPCNotification" + }, + { + "$ref": "#/definitions/JSONRPCResponse" + }, + { + "$ref": "#/definitions/JSONRPCError" + } + ], + "description": "Refers to any valid JSON-RPC object that can be decoded off the wire, or encoded to be sent." + }, + "JSONRPCNotification": { + "description": "A notification which does not expect a response.", + "properties": { + "jsonrpc": { + "const": "2.0", + "type": "string" + }, + "method": { + "type": "string" + }, + "params": { + "additionalProperties": {}, + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + } + }, + "type": "object" + } + }, + "required": [ + "jsonrpc", + "method" + ], + "type": "object" + }, + "JSONRPCRequest": { + "description": "A request that expects a response.", + "properties": { + "id": { + "$ref": "#/definitions/RequestId" + }, + "jsonrpc": { + "const": "2.0", + "type": "string" + }, + "method": { + "type": "string" + }, + "params": { + "additionalProperties": {}, + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "properties": { + "progressToken": { + "$ref": "#/definitions/ProgressToken", + "description": "If specified, the caller is requesting out-of-band progress notifications for this request (as represented by notifications/progress). The value of this parameter is an opaque token that will be attached to any subsequent notifications. The receiver is not obligated to provide these notifications." + } + }, + "type": "object" + } + }, + "type": "object" + } + }, + "required": [ + "id", + "jsonrpc", + "method" + ], + "type": "object" + }, + "JSONRPCResponse": { + "description": "A successful (non-error) response to a request.", + "properties": { + "id": { + "$ref": "#/definitions/RequestId" + }, + "jsonrpc": { + "const": "2.0", + "type": "string" + }, + "result": { + "$ref": "#/definitions/Result" + } + }, + "required": [ + "id", + "jsonrpc", + "result" + ], + "type": "object" + }, + "ListPromptsRequest": { + "description": "Sent from the client to request a list of prompts and prompt templates the server has.", + "properties": { + "method": { + "const": "prompts/list", + "type": "string" + }, + "params": { + "properties": { + "cursor": { + "description": "An opaque token representing the current pagination position.\nIf provided, the server should return results starting after this cursor.", + "type": "string" + } + }, + "type": "object" + } + }, + "required": [ + "method" + ], + "type": "object" + }, + "ListPromptsResult": { + "description": "The server's response to a prompts/list request from the client.", + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "nextCursor": { + "description": "An opaque token representing the pagination position after the last returned result.\nIf present, there may be more results available.", + "type": "string" + }, + "prompts": { + "items": { + "$ref": "#/definitions/Prompt" + }, + "type": "array" + } + }, + "required": [ + "prompts" + ], + "type": "object" + }, + "ListResourceTemplatesRequest": { + "description": "Sent from the client to request a list of resource templates the server has.", + "properties": { + "method": { + "const": "resources/templates/list", + "type": "string" + }, + "params": { + "properties": { + "cursor": { + "description": "An opaque token representing the current pagination position.\nIf provided, the server should return results starting after this cursor.", + "type": "string" + } + }, + "type": "object" + } + }, + "required": [ + "method" + ], + "type": "object" + }, + "ListResourceTemplatesResult": { + "description": "The server's response to a resources/templates/list request from the client.", + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "nextCursor": { + "description": "An opaque token representing the pagination position after the last returned result.\nIf present, there may be more results available.", + "type": "string" + }, + "resourceTemplates": { + "items": { + "$ref": "#/definitions/ResourceTemplate" + }, + "type": "array" + } + }, + "required": [ + "resourceTemplates" + ], + "type": "object" + }, + "ListResourcesRequest": { + "description": "Sent from the client to request a list of resources the server has.", + "properties": { + "method": { + "const": "resources/list", + "type": "string" + }, + "params": { + "properties": { + "cursor": { + "description": "An opaque token representing the current pagination position.\nIf provided, the server should return results starting after this cursor.", + "type": "string" + } + }, + "type": "object" + } + }, + "required": [ + "method" + ], + "type": "object" + }, + "ListResourcesResult": { + "description": "The server's response to a resources/list request from the client.", + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "nextCursor": { + "description": "An opaque token representing the pagination position after the last returned result.\nIf present, there may be more results available.", + "type": "string" + }, + "resources": { + "items": { + "$ref": "#/definitions/Resource" + }, + "type": "array" + } + }, + "required": [ + "resources" + ], + "type": "object" + }, + "ListRootsRequest": { + "description": "Sent from the server to request a list of root URIs from the client. Roots allow\nservers to ask for specific directories or files to operate on. A common example\nfor roots is providing a set of repositories or directories a server should operate\non.\n\nThis request is typically used when the server needs to understand the file system\nstructure or access specific locations that the client has permission to read from.", + "properties": { + "method": { + "const": "roots/list", + "type": "string" + }, + "params": { + "additionalProperties": {}, + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "properties": { + "progressToken": { + "$ref": "#/definitions/ProgressToken", + "description": "If specified, the caller is requesting out-of-band progress notifications for this request (as represented by notifications/progress). The value of this parameter is an opaque token that will be attached to any subsequent notifications. The receiver is not obligated to provide these notifications." + } + }, + "type": "object" + } + }, + "type": "object" + } + }, + "required": [ + "method" + ], + "type": "object" + }, + "ListRootsResult": { + "description": "The client's response to a roots/list request from the server.\nThis result contains an array of Root objects, each representing a root directory\nor file that the server can operate on.", + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "roots": { + "items": { + "$ref": "#/definitions/Root" + }, + "type": "array" + } + }, + "required": [ + "roots" + ], + "type": "object" + }, + "ListToolsRequest": { + "description": "Sent from the client to request a list of tools the server has.", + "properties": { + "method": { + "const": "tools/list", + "type": "string" + }, + "params": { + "properties": { + "cursor": { + "description": "An opaque token representing the current pagination position.\nIf provided, the server should return results starting after this cursor.", + "type": "string" + } + }, + "type": "object" + } + }, + "required": [ + "method" + ], + "type": "object" + }, + "ListToolsResult": { + "description": "The server's response to a tools/list request from the client.", + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "nextCursor": { + "description": "An opaque token representing the pagination position after the last returned result.\nIf present, there may be more results available.", + "type": "string" + }, + "tools": { + "items": { + "$ref": "#/definitions/Tool" + }, + "type": "array" + } + }, + "required": [ + "tools" + ], + "type": "object" + }, + "LoggingLevel": { + "description": "The severity of a log message.\n\nThese map to syslog message severities, as specified in RFC-5424:\nhttps://datatracker.ietf.org/doc/html/rfc5424#section-6.2.1", + "enum": [ + "alert", + "critical", + "debug", + "emergency", + "error", + "info", + "notice", + "warning" + ], + "type": "string" + }, + "LoggingMessageNotification": { + "description": "Notification of a log message passed from server to client. If no logging/setLevel request has been sent from the client, the server MAY decide which messages to send automatically.", + "properties": { + "method": { + "const": "notifications/message", + "type": "string" + }, + "params": { + "properties": { + "data": { + "description": "The data to be logged, such as a string message or an object. Any JSON serializable type is allowed here." + }, + "level": { + "$ref": "#/definitions/LoggingLevel", + "description": "The severity of this log message." + }, + "logger": { + "description": "An optional name of the logger issuing this message.", + "type": "string" + } + }, + "required": [ + "data", + "level" + ], + "type": "object" + } + }, + "required": [ + "method", + "params" + ], + "type": "object" + }, + "ModelHint": { + "description": "Hints to use for model selection.\n\nKeys not declared here are currently left unspecified by the spec and are up\nto the client to interpret.", + "properties": { + "name": { + "description": "A hint for a model name.\n\nThe client SHOULD treat this as a substring of a model name; for example:\n - `claude-3-5-sonnet` should match `claude-3-5-sonnet-20241022`\n - `sonnet` should match `claude-3-5-sonnet-20241022`, `claude-3-sonnet-20240229`, etc.\n - `claude` should match any Claude model\n\nThe client MAY also map the string to a different provider's model name or a different model family, as long as it fills a similar niche; for example:\n - `gemini-1.5-flash` could match `claude-3-haiku-20240307`", + "type": "string" + } + }, + "type": "object" + }, + "ModelPreferences": { + "description": "The server's preferences for model selection, requested of the client during sampling.\n\nBecause LLMs can vary along multiple dimensions, choosing the \"best\" model is\nrarely straightforward. Different models excel in different areas—some are\nfaster but less capable, others are more capable but more expensive, and so\non. This interface allows servers to express their priorities across multiple\ndimensions to help clients make an appropriate selection for their use case.\n\nThese preferences are always advisory. The client MAY ignore them. It is also\nup to the client to decide how to interpret these preferences and how to\nbalance them against other considerations.", + "properties": { + "costPriority": { + "description": "How much to prioritize cost when selecting a model. A value of 0 means cost\nis not important, while a value of 1 means cost is the most important\nfactor.", + "maximum": 1, + "minimum": 0, + "type": "number" + }, + "hints": { + "description": "Optional hints to use for model selection.\n\nIf multiple hints are specified, the client MUST evaluate them in order\n(such that the first match is taken).\n\nThe client SHOULD prioritize these hints over the numeric priorities, but\nMAY still use the priorities to select from ambiguous matches.", + "items": { + "$ref": "#/definitions/ModelHint" + }, + "type": "array" + }, + "intelligencePriority": { + "description": "How much to prioritize intelligence and capabilities when selecting a\nmodel. A value of 0 means intelligence is not important, while a value of 1\nmeans intelligence is the most important factor.", + "maximum": 1, + "minimum": 0, + "type": "number" + }, + "speedPriority": { + "description": "How much to prioritize sampling speed (latency) when selecting a model. A\nvalue of 0 means speed is not important, while a value of 1 means speed is\nthe most important factor.", + "maximum": 1, + "minimum": 0, + "type": "number" + } + }, + "type": "object" + }, + "Notification": { + "properties": { + "method": { + "type": "string" + }, + "params": { + "additionalProperties": {}, + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + } + }, + "type": "object" + } + }, + "required": [ + "method" + ], + "type": "object" + }, + "NumberSchema": { + "properties": { + "description": { + "type": "string" + }, + "maximum": { + "type": "integer" + }, + "minimum": { + "type": "integer" + }, + "title": { + "type": "string" + }, + "type": { + "enum": [ + "integer", + "number" + ], + "type": "string" + } + }, + "required": [ + "type" + ], + "type": "object" + }, + "PaginatedRequest": { + "properties": { + "method": { + "type": "string" + }, + "params": { + "properties": { + "cursor": { + "description": "An opaque token representing the current pagination position.\nIf provided, the server should return results starting after this cursor.", + "type": "string" + } + }, + "type": "object" + } + }, + "required": [ + "method" + ], + "type": "object" + }, + "PaginatedResult": { + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "nextCursor": { + "description": "An opaque token representing the pagination position after the last returned result.\nIf present, there may be more results available.", + "type": "string" + } + }, + "type": "object" + }, + "PingRequest": { + "description": "A ping, issued by either the server or the client, to check that the other party is still alive. The receiver must promptly respond, or else may be disconnected.", + "properties": { + "method": { + "const": "ping", + "type": "string" + }, + "params": { + "additionalProperties": {}, + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "properties": { + "progressToken": { + "$ref": "#/definitions/ProgressToken", + "description": "If specified, the caller is requesting out-of-band progress notifications for this request (as represented by notifications/progress). The value of this parameter is an opaque token that will be attached to any subsequent notifications. The receiver is not obligated to provide these notifications." + } + }, + "type": "object" + } + }, + "type": "object" + } + }, + "required": [ + "method" + ], + "type": "object" + }, + "PrimitiveSchemaDefinition": { + "anyOf": [ + { + "$ref": "#/definitions/StringSchema" + }, + { + "$ref": "#/definitions/NumberSchema" + }, + { + "$ref": "#/definitions/BooleanSchema" + }, + { + "$ref": "#/definitions/EnumSchema" + } + ], + "description": "Restricted schema definitions that only allow primitive types\nwithout nested objects or arrays." + }, + "ProgressNotification": { + "description": "An out-of-band notification used to inform the receiver of a progress update for a long-running request.", + "properties": { + "method": { + "const": "notifications/progress", + "type": "string" + }, + "params": { + "properties": { + "message": { + "description": "An optional message describing the current progress.", + "type": "string" + }, + "progress": { + "description": "The progress thus far. This should increase every time progress is made, even if the total is unknown.", + "type": "number" + }, + "progressToken": { + "$ref": "#/definitions/ProgressToken", + "description": "The progress token which was given in the initial request, used to associate this notification with the request that is proceeding." + }, + "total": { + "description": "Total number of items to process (or total progress required), if known.", + "type": "number" + } + }, + "required": [ + "progress", + "progressToken" + ], + "type": "object" + } + }, + "required": [ + "method", + "params" + ], + "type": "object" + }, + "ProgressToken": { + "description": "A progress token, used to associate progress notifications with the original request.", + "type": [ + "string", + "integer" + ] + }, + "Prompt": { + "description": "A prompt or prompt template that the server offers.", + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "arguments": { + "description": "A list of arguments to use for templating the prompt.", + "items": { + "$ref": "#/definitions/PromptArgument" + }, + "type": "array" + }, + "description": { + "description": "An optional description of what this prompt provides", + "type": "string" + }, + "name": { + "description": "Intended for programmatic or logical use, but used as a display name in past specs or fallback (if title isn't present).", + "type": "string" + }, + "title": { + "description": "Intended for UI and end-user contexts — optimized to be human-readable and easily understood,\neven by those unfamiliar with domain-specific terminology.\n\nIf not provided, the name should be used for display (except for Tool,\nwhere `annotations.title` should be given precedence over using `name`,\nif present).", + "type": "string" + } + }, + "required": [ + "name" + ], + "type": "object" + }, + "PromptArgument": { + "description": "Describes an argument that a prompt can accept.", + "properties": { + "description": { + "description": "A human-readable description of the argument.", + "type": "string" + }, + "name": { + "description": "Intended for programmatic or logical use, but used as a display name in past specs or fallback (if title isn't present).", + "type": "string" + }, + "required": { + "description": "Whether this argument must be provided.", + "type": "boolean" + }, + "title": { + "description": "Intended for UI and end-user contexts — optimized to be human-readable and easily understood,\neven by those unfamiliar with domain-specific terminology.\n\nIf not provided, the name should be used for display (except for Tool,\nwhere `annotations.title` should be given precedence over using `name`,\nif present).", + "type": "string" + } + }, + "required": [ + "name" + ], + "type": "object" + }, + "PromptListChangedNotification": { + "description": "An optional notification from the server to the client, informing it that the list of prompts it offers has changed. This may be issued by servers without any previous subscription from the client.", + "properties": { + "method": { + "const": "notifications/prompts/list_changed", + "type": "string" + }, + "params": { + "additionalProperties": {}, + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + } + }, + "type": "object" + } + }, + "required": [ + "method" + ], + "type": "object" + }, + "PromptMessage": { + "description": "Describes a message returned as part of a prompt.\n\nThis is similar to `SamplingMessage`, but also supports the embedding of\nresources from the MCP server.", + "properties": { + "content": { + "$ref": "#/definitions/ContentBlock" + }, + "role": { + "$ref": "#/definitions/Role" + } + }, + "required": [ + "content", + "role" + ], + "type": "object" + }, + "PromptReference": { + "description": "Identifies a prompt.", + "properties": { + "name": { + "description": "Intended for programmatic or logical use, but used as a display name in past specs or fallback (if title isn't present).", + "type": "string" + }, + "title": { + "description": "Intended for UI and end-user contexts — optimized to be human-readable and easily understood,\neven by those unfamiliar with domain-specific terminology.\n\nIf not provided, the name should be used for display (except for Tool,\nwhere `annotations.title` should be given precedence over using `name`,\nif present).", + "type": "string" + }, + "type": { + "const": "ref/prompt", + "type": "string" + } + }, + "required": [ + "name", + "type" + ], + "type": "object" + }, + "ReadResourceRequest": { + "description": "Sent from the client to the server, to read a specific resource URI.", + "properties": { + "method": { + "const": "resources/read", + "type": "string" + }, + "params": { + "properties": { + "uri": { + "description": "The URI of the resource to read. The URI can use any protocol; it is up to the server how to interpret it.", + "format": "uri", + "type": "string" + } + }, + "required": [ + "uri" + ], + "type": "object" + } + }, + "required": [ + "method", + "params" + ], + "type": "object" + }, + "ReadResourceResult": { + "description": "The server's response to a resources/read request from the client.", + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "contents": { + "items": { + "anyOf": [ + { + "$ref": "#/definitions/TextResourceContents" + }, + { + "$ref": "#/definitions/BlobResourceContents" + } + ] + }, + "type": "array" + } + }, + "required": [ + "contents" + ], + "type": "object" + }, + "Request": { + "properties": { + "method": { + "type": "string" + }, + "params": { + "additionalProperties": {}, + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "properties": { + "progressToken": { + "$ref": "#/definitions/ProgressToken", + "description": "If specified, the caller is requesting out-of-band progress notifications for this request (as represented by notifications/progress). The value of this parameter is an opaque token that will be attached to any subsequent notifications. The receiver is not obligated to provide these notifications." + } + }, + "type": "object" + } + }, + "type": "object" + } + }, + "required": [ + "method" + ], + "type": "object" + }, + "RequestId": { + "description": "A uniquely identifying ID for a request in JSON-RPC.", + "type": [ + "string", + "integer" + ] + }, + "Resource": { + "description": "A known resource that the server is capable of reading.", + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "annotations": { + "$ref": "#/definitions/Annotations", + "description": "Optional annotations for the client." + }, + "description": { + "description": "A description of what this resource represents.\n\nThis can be used by clients to improve the LLM's understanding of available resources. It can be thought of like a \"hint\" to the model.", + "type": "string" + }, + "mimeType": { + "description": "The MIME type of this resource, if known.", + "type": "string" + }, + "name": { + "description": "Intended for programmatic or logical use, but used as a display name in past specs or fallback (if title isn't present).", + "type": "string" + }, + "size": { + "description": "The size of the raw resource content, in bytes (i.e., before base64 encoding or any tokenization), if known.\n\nThis can be used by Hosts to display file sizes and estimate context window usage.", + "type": "integer" + }, + "title": { + "description": "Intended for UI and end-user contexts — optimized to be human-readable and easily understood,\neven by those unfamiliar with domain-specific terminology.\n\nIf not provided, the name should be used for display (except for Tool,\nwhere `annotations.title` should be given precedence over using `name`,\nif present).", + "type": "string" + }, + "uri": { + "description": "The URI of this resource.", + "format": "uri", + "type": "string" + } + }, + "required": [ + "name", + "uri" + ], + "type": "object" + }, + "ResourceContents": { + "description": "The contents of a specific resource or sub-resource.", + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "mimeType": { + "description": "The MIME type of this resource, if known.", + "type": "string" + }, + "uri": { + "description": "The URI of this resource.", + "format": "uri", + "type": "string" + } + }, + "required": [ + "uri" + ], + "type": "object" + }, + "ResourceLink": { + "description": "A resource that the server is capable of reading, included in a prompt or tool call result.\n\nNote: resource links returned by tools are not guaranteed to appear in the results of `resources/list` requests.", + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "annotations": { + "$ref": "#/definitions/Annotations", + "description": "Optional annotations for the client." + }, + "description": { + "description": "A description of what this resource represents.\n\nThis can be used by clients to improve the LLM's understanding of available resources. It can be thought of like a \"hint\" to the model.", + "type": "string" + }, + "mimeType": { + "description": "The MIME type of this resource, if known.", + "type": "string" + }, + "name": { + "description": "Intended for programmatic or logical use, but used as a display name in past specs or fallback (if title isn't present).", + "type": "string" + }, + "size": { + "description": "The size of the raw resource content, in bytes (i.e., before base64 encoding or any tokenization), if known.\n\nThis can be used by Hosts to display file sizes and estimate context window usage.", + "type": "integer" + }, + "title": { + "description": "Intended for UI and end-user contexts — optimized to be human-readable and easily understood,\neven by those unfamiliar with domain-specific terminology.\n\nIf not provided, the name should be used for display (except for Tool,\nwhere `annotations.title` should be given precedence over using `name`,\nif present).", + "type": "string" + }, + "type": { + "const": "resource_link", + "type": "string" + }, + "uri": { + "description": "The URI of this resource.", + "format": "uri", + "type": "string" + } + }, + "required": [ + "name", + "type", + "uri" + ], + "type": "object" + }, + "ResourceListChangedNotification": { + "description": "An optional notification from the server to the client, informing it that the list of resources it can read from has changed. This may be issued by servers without any previous subscription from the client.", + "properties": { + "method": { + "const": "notifications/resources/list_changed", + "type": "string" + }, + "params": { + "additionalProperties": {}, + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + } + }, + "type": "object" + } + }, + "required": [ + "method" + ], + "type": "object" + }, + "ResourceTemplate": { + "description": "A template description for resources available on the server.", + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "annotations": { + "$ref": "#/definitions/Annotations", + "description": "Optional annotations for the client." + }, + "description": { + "description": "A description of what this template is for.\n\nThis can be used by clients to improve the LLM's understanding of available resources. It can be thought of like a \"hint\" to the model.", + "type": "string" + }, + "mimeType": { + "description": "The MIME type for all resources that match this template. This should only be included if all resources matching this template have the same type.", + "type": "string" + }, + "name": { + "description": "Intended for programmatic or logical use, but used as a display name in past specs or fallback (if title isn't present).", + "type": "string" + }, + "title": { + "description": "Intended for UI and end-user contexts — optimized to be human-readable and easily understood,\neven by those unfamiliar with domain-specific terminology.\n\nIf not provided, the name should be used for display (except for Tool,\nwhere `annotations.title` should be given precedence over using `name`,\nif present).", + "type": "string" + }, + "uriTemplate": { + "description": "A URI template (according to RFC 6570) that can be used to construct resource URIs.", + "format": "uri-template", + "type": "string" + } + }, + "required": [ + "name", + "uriTemplate" + ], + "type": "object" + }, + "ResourceTemplateReference": { + "description": "A reference to a resource or resource template definition.", + "properties": { + "type": { + "const": "ref/resource", + "type": "string" + }, + "uri": { + "description": "The URI or URI template of the resource.", + "format": "uri-template", + "type": "string" + } + }, + "required": [ + "type", + "uri" + ], + "type": "object" + }, + "ResourceUpdatedNotification": { + "description": "A notification from the server to the client, informing it that a resource has changed and may need to be read again. This should only be sent if the client previously sent a resources/subscribe request.", + "properties": { + "method": { + "const": "notifications/resources/updated", + "type": "string" + }, + "params": { + "properties": { + "uri": { + "description": "The URI of the resource that has been updated. This might be a sub-resource of the one that the client actually subscribed to.", + "format": "uri", + "type": "string" + } + }, + "required": [ + "uri" + ], + "type": "object" + } + }, + "required": [ + "method", + "params" + ], + "type": "object" + }, + "Result": { + "additionalProperties": {}, + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + } + }, + "type": "object" + }, + "Role": { + "description": "The sender or recipient of messages and data in a conversation.", + "enum": [ + "assistant", + "user" + ], + "type": "string" + }, + "Root": { + "description": "Represents a root directory or file that the server can operate on.", + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "name": { + "description": "An optional name for the root. This can be used to provide a human-readable\nidentifier for the root, which may be useful for display purposes or for\nreferencing the root in other parts of the application.", + "type": "string" + }, + "uri": { + "description": "The URI identifying the root. This *must* start with file:// for now.\nThis restriction may be relaxed in future versions of the protocol to allow\nother URI schemes.", + "format": "uri", + "type": "string" + } + }, + "required": [ + "uri" + ], + "type": "object" + }, + "RootsListChangedNotification": { + "description": "A notification from the client to the server, informing it that the list of roots has changed.\nThis notification should be sent whenever the client adds, removes, or modifies any root.\nThe server should then request an updated list of roots using the ListRootsRequest.", + "properties": { + "method": { + "const": "notifications/roots/list_changed", + "type": "string" + }, + "params": { + "additionalProperties": {}, + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + } + }, + "type": "object" + } + }, + "required": [ + "method" + ], + "type": "object" + }, + "SamplingMessage": { + "description": "Describes a message issued to or received from an LLM API.", + "properties": { + "content": { + "anyOf": [ + { + "$ref": "#/definitions/TextContent" + }, + { + "$ref": "#/definitions/ImageContent" + }, + { + "$ref": "#/definitions/AudioContent" + } + ] + }, + "role": { + "$ref": "#/definitions/Role" + } + }, + "required": [ + "content", + "role" + ], + "type": "object" + }, + "ServerCapabilities": { + "description": "Capabilities that a server may support. Known capabilities are defined here, in this schema, but this is not a closed set: any server can define its own, additional capabilities.", + "properties": { + "completions": { + "additionalProperties": true, + "description": "Present if the server supports argument autocompletion suggestions.", + "properties": {}, + "type": "object" + }, + "experimental": { + "additionalProperties": { + "additionalProperties": true, + "properties": {}, + "type": "object" + }, + "description": "Experimental, non-standard capabilities that the server supports.", + "type": "object" + }, + "logging": { + "additionalProperties": true, + "description": "Present if the server supports sending log messages to the client.", + "properties": {}, + "type": "object" + }, + "prompts": { + "description": "Present if the server offers any prompt templates.", + "properties": { + "listChanged": { + "description": "Whether this server supports notifications for changes to the prompt list.", + "type": "boolean" + } + }, + "type": "object" + }, + "resources": { + "description": "Present if the server offers any resources to read.", + "properties": { + "listChanged": { + "description": "Whether this server supports notifications for changes to the resource list.", + "type": "boolean" + }, + "subscribe": { + "description": "Whether this server supports subscribing to resource updates.", + "type": "boolean" + } + }, + "type": "object" + }, + "tools": { + "description": "Present if the server offers any tools to call.", + "properties": { + "listChanged": { + "description": "Whether this server supports notifications for changes to the tool list.", + "type": "boolean" + } + }, + "type": "object" + } + }, + "type": "object" + }, + "ServerNotification": { + "anyOf": [ + { + "$ref": "#/definitions/CancelledNotification" + }, + { + "$ref": "#/definitions/ProgressNotification" + }, + { + "$ref": "#/definitions/ResourceListChangedNotification" + }, + { + "$ref": "#/definitions/ResourceUpdatedNotification" + }, + { + "$ref": "#/definitions/PromptListChangedNotification" + }, + { + "$ref": "#/definitions/ToolListChangedNotification" + }, + { + "$ref": "#/definitions/LoggingMessageNotification" + } + ] + }, + "ServerRequest": { + "anyOf": [ + { + "$ref": "#/definitions/PingRequest" + }, + { + "$ref": "#/definitions/CreateMessageRequest" + }, + { + "$ref": "#/definitions/ListRootsRequest" + }, + { + "$ref": "#/definitions/ElicitRequest" + } + ] + }, + "ServerResult": { + "anyOf": [ + { + "$ref": "#/definitions/Result" + }, + { + "$ref": "#/definitions/InitializeResult" + }, + { + "$ref": "#/definitions/ListResourcesResult" + }, + { + "$ref": "#/definitions/ListResourceTemplatesResult" + }, + { + "$ref": "#/definitions/ReadResourceResult" + }, + { + "$ref": "#/definitions/ListPromptsResult" + }, + { + "$ref": "#/definitions/GetPromptResult" + }, + { + "$ref": "#/definitions/ListToolsResult" + }, + { + "$ref": "#/definitions/CallToolResult" + }, + { + "$ref": "#/definitions/CompleteResult" + } + ] + }, + "SetLevelRequest": { + "description": "A request from the client to the server, to enable or adjust logging.", + "properties": { + "method": { + "const": "logging/setLevel", + "type": "string" + }, + "params": { + "properties": { + "level": { + "$ref": "#/definitions/LoggingLevel", + "description": "The level of logging that the client wants to receive from the server. The server should send all logs at this level and higher (i.e., more severe) to the client as notifications/message." + } + }, + "required": [ + "level" + ], + "type": "object" + } + }, + "required": [ + "method", + "params" + ], + "type": "object" + }, + "StringSchema": { + "properties": { + "description": { + "type": "string" + }, + "format": { + "enum": [ + "date", + "date-time", + "email", + "uri" + ], + "type": "string" + }, + "maxLength": { + "type": "integer" + }, + "minLength": { + "type": "integer" + }, + "title": { + "type": "string" + }, + "type": { + "const": "string", + "type": "string" + } + }, + "required": [ + "type" + ], + "type": "object" + }, + "SubscribeRequest": { + "description": "Sent from the client to request resources/updated notifications from the server whenever a particular resource changes.", + "properties": { + "method": { + "const": "resources/subscribe", + "type": "string" + }, + "params": { + "properties": { + "uri": { + "description": "The URI of the resource to subscribe to. The URI can use any protocol; it is up to the server how to interpret it.", + "format": "uri", + "type": "string" + } + }, + "required": [ + "uri" + ], + "type": "object" + } + }, + "required": [ + "method", + "params" + ], + "type": "object" + }, + "TextContent": { + "description": "Text provided to or from an LLM.", + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "annotations": { + "$ref": "#/definitions/Annotations", + "description": "Optional annotations for the client." + }, + "text": { + "description": "The text content of the message.", + "type": "string" + }, + "type": { + "const": "text", + "type": "string" + } + }, + "required": [ + "text", + "type" + ], + "type": "object" + }, + "TextResourceContents": { + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "mimeType": { + "description": "The MIME type of this resource, if known.", + "type": "string" + }, + "text": { + "description": "The text of the item. This must only be set if the item can actually be represented as text (not binary data).", + "type": "string" + }, + "uri": { + "description": "The URI of this resource.", + "format": "uri", + "type": "string" + } + }, + "required": [ + "text", + "uri" + ], + "type": "object" + }, + "Tool": { + "description": "Definition for a tool the client can call.", + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + }, + "annotations": { + "$ref": "#/definitions/ToolAnnotations", + "description": "Optional additional tool information.\n\nDisplay name precedence order is: title, annotations.title, then name." + }, + "description": { + "description": "A human-readable description of the tool.\n\nThis can be used by clients to improve the LLM's understanding of available tools. It can be thought of like a \"hint\" to the model.", + "type": "string" + }, + "inputSchema": { + "description": "A JSON Schema object defining the expected parameters for the tool.", + "properties": { + "properties": { + "additionalProperties": { + "additionalProperties": true, + "properties": {}, + "type": "object" + }, + "type": "object" + }, + "required": { + "items": { + "type": "string" + }, + "type": "array" + }, + "type": { + "const": "object", + "type": "string" + } + }, + "required": [ + "type" + ], + "type": "object" + }, + "name": { + "description": "Intended for programmatic or logical use, but used as a display name in past specs or fallback (if title isn't present).", + "type": "string" + }, + "outputSchema": { + "description": "An optional JSON Schema object defining the structure of the tool's output returned in\nthe structuredContent field of a CallToolResult.", + "properties": { + "properties": { + "additionalProperties": { + "additionalProperties": true, + "properties": {}, + "type": "object" + }, + "type": "object" + }, + "required": { + "items": { + "type": "string" + }, + "type": "array" + }, + "type": { + "const": "object", + "type": "string" + } + }, + "required": [ + "type" + ], + "type": "object" + }, + "title": { + "description": "Intended for UI and end-user contexts — optimized to be human-readable and easily understood,\neven by those unfamiliar with domain-specific terminology.\n\nIf not provided, the name should be used for display (except for Tool,\nwhere `annotations.title` should be given precedence over using `name`,\nif present).", + "type": "string" + } + }, + "required": [ + "inputSchema", + "name" + ], + "type": "object" + }, + "ToolAnnotations": { + "description": "Additional properties describing a Tool to clients.\n\nNOTE: all properties in ToolAnnotations are **hints**.\nThey are not guaranteed to provide a faithful description of\ntool behavior (including descriptive properties like `title`).\n\nClients should never make tool use decisions based on ToolAnnotations\nreceived from untrusted servers.", + "properties": { + "destructiveHint": { + "description": "If true, the tool may perform destructive updates to its environment.\nIf false, the tool performs only additive updates.\n\n(This property is meaningful only when `readOnlyHint == false`)\n\nDefault: true", + "type": "boolean" + }, + "idempotentHint": { + "description": "If true, calling the tool repeatedly with the same arguments\nwill have no additional effect on the its environment.\n\n(This property is meaningful only when `readOnlyHint == false`)\n\nDefault: false", + "type": "boolean" + }, + "openWorldHint": { + "description": "If true, this tool may interact with an \"open world\" of external\nentities. If false, the tool's domain of interaction is closed.\nFor example, the world of a web search tool is open, whereas that\nof a memory tool is not.\n\nDefault: true", + "type": "boolean" + }, + "readOnlyHint": { + "description": "If true, the tool does not modify its environment.\n\nDefault: false", + "type": "boolean" + }, + "title": { + "description": "A human-readable title for the tool.", + "type": "string" + } + }, + "type": "object" + }, + "ToolListChangedNotification": { + "description": "An optional notification from the server to the client, informing it that the list of tools it offers has changed. This may be issued by servers without any previous subscription from the client.", + "properties": { + "method": { + "const": "notifications/tools/list_changed", + "type": "string" + }, + "params": { + "additionalProperties": {}, + "properties": { + "_meta": { + "additionalProperties": {}, + "description": "See [specification/2025-06-18/basic/index#general-fields] for notes on _meta usage.", + "type": "object" + } + }, + "type": "object" + } + }, + "required": [ + "method" + ], + "type": "object" + }, + "UnsubscribeRequest": { + "description": "Sent from the client to request cancellation of resources/updated notifications from the server. This should follow a previous resources/subscribe request.", + "properties": { + "method": { + "const": "resources/unsubscribe", + "type": "string" + }, + "params": { + "properties": { + "uri": { + "description": "The URI of the resource to unsubscribe from.", + "format": "uri", + "type": "string" + } + }, + "required": [ + "uri" + ], + "type": "object" + } + }, + "required": [ + "method", + "params" + ], + "type": "object" + } + } +} + diff --git a/codex-rs/mcp-types/src/lib.rs b/codex-rs/mcp-types/src/lib.rs index 0ed518535f..cf09d67e35 100644 --- a/codex-rs/mcp-types/src/lib.rs +++ b/codex-rs/mcp-types/src/lib.rs @@ -10,7 +10,7 @@ use serde::Serialize; use serde::de::DeserializeOwned; use std::convert::TryFrom; -pub const MCP_SCHEMA_VERSION: &str = "2025-03-26"; +pub const MCP_SCHEMA_VERSION: &str = "2025-06-18"; pub const JSONRPC_VERSION: &str = "2.0"; /// Paired request/response types for the Model Context Protocol (MCP). @@ -35,6 +35,12 @@ fn default_jsonrpc() -> String { pub struct Annotations { #[serde(default, skip_serializing_if = "Option::is_none")] pub audience: Option>, + #[serde( + rename = "lastModified", + default, + skip_serializing_if = "Option::is_none" + )] + pub last_modified: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub priority: Option, } @@ -50,6 +56,14 @@ pub struct AudioContent { pub r#type: String, // &'static str = "audio" } +/// Base interface for metadata with name (identifier) and title (display name) properties. +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] +pub struct BaseMetadata { + pub name: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub title: Option, +} + #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub struct BlobResourceContents { pub blob: String, @@ -58,6 +72,17 @@ pub struct BlobResourceContents { pub uri: String, } +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] +pub struct BooleanSchema { + #[serde(default, skip_serializing_if = "Option::is_none")] + pub default: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub description: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub title: Option, + pub r#type: String, // &'static str = "boolean" +} + #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub enum CallToolRequest {} @@ -75,29 +100,17 @@ pub struct CallToolRequestParams { } /// The server's response to a tool call. -/// -/// Any errors that originate from the tool SHOULD be reported inside the result -/// object, with `isError` set to true, _not_ as an MCP protocol-level error -/// response. Otherwise, the LLM would not be able to see that an error occurred -/// and self-correct. -/// -/// However, any errors in _finding_ the tool, an error indicating that the -/// server does not support tool calls, or any other exceptional conditions, -/// should be reported as an MCP error response. #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub struct CallToolResult { - pub content: Vec, + pub content: Vec, #[serde(rename = "isError", default, skip_serializing_if = "Option::is_none")] pub is_error: Option, -} - -#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] -#[serde(untagged)] -pub enum CallToolResultContent { - TextContent(TextContent), - ImageContent(ImageContent), - AudioContent(AudioContent), - EmbeddedResource(EmbeddedResource), + #[serde( + rename = "structuredContent", + default, + skip_serializing_if = "Option::is_none" + )] + pub structured_content: Option, } impl From for serde_json::Value { @@ -127,6 +140,8 @@ pub struct CancelledNotificationParams { /// Capabilities a client may support. Known capabilities are defined here, in this schema, but this is not a closed set: any client can define its own, additional capabilities. #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub struct ClientCapabilities { + #[serde(default, skip_serializing_if = "Option::is_none")] + pub elicitation: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub experimental: Option, #[serde(default, skip_serializing_if = "Option::is_none")] @@ -194,6 +209,7 @@ pub enum ClientResult { Result(Result), CreateMessageResult(CreateMessageResult), ListRootsResult(ListRootsResult), + ElicitResult(ElicitResult), } #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] @@ -208,9 +224,18 @@ impl ModelContextProtocolRequest for CompleteRequest { #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub struct CompleteRequestParams { pub argument: CompleteRequestParamsArgument, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub context: Option, pub r#ref: CompleteRequestParamsRef, } +/// Additional, optional context for completions +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] +pub struct CompleteRequestParamsContext { + #[serde(default, skip_serializing_if = "Option::is_none")] + pub arguments: Option, +} + /// The argument's information #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub struct CompleteRequestParamsArgument { @@ -222,7 +247,7 @@ pub struct CompleteRequestParamsArgument { #[serde(untagged)] pub enum CompleteRequestParamsRef { PromptReference(PromptReference), - ResourceReference(ResourceReference), + ResourceTemplateReference(ResourceTemplateReference), } /// The server's response to a completion/complete request @@ -248,6 +273,16 @@ impl From for serde_json::Value { } } +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] +#[serde(untagged)] +pub enum ContentBlock { + TextContent(TextContent), + ImageContent(ImageContent), + AudioContent(AudioContent), + ResourceLink(ResourceLink), + EmbeddedResource(EmbeddedResource), +} + #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub enum CreateMessageRequest {} @@ -325,6 +360,48 @@ impl From for serde_json::Value { #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub struct Cursor(String); +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] +pub enum ElicitRequest {} + +impl ModelContextProtocolRequest for ElicitRequest { + const METHOD: &'static str = "elicitation/create"; + type Params = ElicitRequestParams; + type Result = ElicitResult; +} + +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] +pub struct ElicitRequestParams { + pub message: String, + #[serde(rename = "requestedSchema")] + pub requested_schema: ElicitRequestParamsRequestedSchema, +} + +/// A restricted subset of JSON Schema. +/// Only top-level properties are allowed, without nesting. +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] +pub struct ElicitRequestParamsRequestedSchema { + pub properties: serde_json::Value, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub required: Option>, + pub r#type: String, // &'static str = "object" +} + +/// The client's response to an elicitation request. +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] +pub struct ElicitResult { + pub action: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub content: Option, +} + +impl From for serde_json::Value { + fn from(value: ElicitResult) -> Self { + // Leave this as it should never fail + #[expect(clippy::unwrap_used)] + serde_json::to_value(value).unwrap() + } +} + /// The contents of a resource, embedded into a prompt or tool call result. /// /// It is up to the client how best to render embedded resources for the benefit @@ -346,6 +423,18 @@ pub enum EmbeddedResourceResource { pub type EmptyResult = Result; +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] +pub struct EnumSchema { + #[serde(default, skip_serializing_if = "Option::is_none")] + pub description: Option, + pub r#enum: Vec, + #[serde(rename = "enumNames", default, skip_serializing_if = "Option::is_none")] + pub enum_names: Option>, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub title: Option, + pub r#type: String, // &'static str = "string" +} + #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub enum GetPromptRequest {} @@ -389,10 +478,12 @@ pub struct ImageContent { pub r#type: String, // &'static str = "image" } -/// Describes the name and version of an MCP implementation. +/// Describes the name and version of an MCP implementation, with an optional title for UI representation. #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub struct Implementation { pub name: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub title: Option, pub version: String, } @@ -442,24 +533,6 @@ impl ModelContextProtocolNotification for InitializedNotification { type Params = Option; } -#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] -#[serde(untagged)] -pub enum JSONRPCBatchRequestItem { - JSONRPCRequest(JSONRPCRequest), - JSONRPCNotification(JSONRPCNotification), -} - -pub type JSONRPCBatchRequest = Vec; - -#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] -#[serde(untagged)] -pub enum JSONRPCBatchResponseItem { - JSONRPCResponse(JSONRPCResponse), - JSONRPCError(JSONRPCError), -} - -pub type JSONRPCBatchResponse = Vec; - /// A response to a request that indicates an error occurred. #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub struct JSONRPCError { @@ -483,10 +556,8 @@ pub struct JSONRPCErrorError { pub enum JSONRPCMessage { Request(JSONRPCRequest), Notification(JSONRPCNotification), - BatchRequest(JSONRPCBatchRequest), Response(JSONRPCResponse), Error(JSONRPCError), - BatchResponse(JSONRPCBatchResponse), } /// A notification which does not expect a response. @@ -777,6 +848,19 @@ pub struct Notification { pub params: Option, } +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] +pub struct NumberSchema { + #[serde(default, skip_serializing_if = "Option::is_none")] + pub description: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub maximum: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub minimum: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub title: Option, + pub r#type: String, +} + #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub struct PaginatedRequest { pub method: String, @@ -817,6 +901,17 @@ impl ModelContextProtocolRequest for PingRequest { type Result = Result; } +/// Restricted schema definitions that only allow primitive types +/// without nested objects or arrays. +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] +#[serde(untagged)] +pub enum PrimitiveSchemaDefinition { + StringSchema(StringSchema), + NumberSchema(NumberSchema), + BooleanSchema(BooleanSchema), + EnumSchema(EnumSchema), +} + #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub enum ProgressNotification {} @@ -836,7 +931,7 @@ pub struct ProgressNotificationParams { pub total: Option, } -#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Hash, Eq)] #[serde(untagged)] pub enum ProgressToken { String(String), @@ -851,6 +946,8 @@ pub struct Prompt { #[serde(default, skip_serializing_if = "Option::is_none")] pub description: Option, pub name: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub title: Option, } /// Describes an argument that a prompt can accept. @@ -861,6 +958,8 @@ pub struct PromptArgument { pub name: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub required: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub title: Option, } #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] @@ -877,23 +976,16 @@ impl ModelContextProtocolNotification for PromptListChangedNotification { /// resources from the MCP server. #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub struct PromptMessage { - pub content: PromptMessageContent, + pub content: ContentBlock, pub role: Role, } -#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] -#[serde(untagged)] -pub enum PromptMessageContent { - TextContent(TextContent), - ImageContent(ImageContent), - AudioContent(AudioContent), - EmbeddedResource(EmbeddedResource), -} - /// Identifies a prompt. #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub struct PromptReference { pub name: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub title: Option, pub r#type: String, // &'static str = "ref/prompt" } @@ -939,7 +1031,7 @@ pub struct Request { pub params: Option, } -#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Hash, Eq)] #[serde(untagged)] pub enum RequestId { String(String), @@ -958,6 +1050,8 @@ pub struct Resource { pub name: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub size: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub title: Option, pub uri: String, } @@ -969,6 +1063,26 @@ pub struct ResourceContents { pub uri: String, } +/// A resource that the server is capable of reading, included in a prompt or tool call result. +/// +/// Note: resource links returned by tools are not guaranteed to appear in the results of `resources/list` requests. +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] +pub struct ResourceLink { + #[serde(default, skip_serializing_if = "Option::is_none")] + pub annotations: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub description: Option, + #[serde(rename = "mimeType", default, skip_serializing_if = "Option::is_none")] + pub mime_type: Option, + pub name: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub size: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub title: Option, + pub r#type: String, // &'static str = "resource_link" + pub uri: String, +} + #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub enum ResourceListChangedNotification {} @@ -977,13 +1091,6 @@ impl ModelContextProtocolNotification for ResourceListChangedNotification { type Params = Option; } -/// A reference to a resource or resource template definition. -#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] -pub struct ResourceReference { - pub r#type: String, // &'static str = "ref/resource" - pub uri: String, -} - /// A template description for resources available on the server. #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub struct ResourceTemplate { @@ -994,10 +1101,19 @@ pub struct ResourceTemplate { #[serde(rename = "mimeType", default, skip_serializing_if = "Option::is_none")] pub mime_type: Option, pub name: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub title: Option, #[serde(rename = "uriTemplate")] pub uri_template: String, } +/// A reference to a resource or resource template definition. +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] +pub struct ResourceTemplateReference { + pub r#type: String, // &'static str = "ref/resource" + pub uri: String, +} + #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub enum ResourceUpdatedNotification {} @@ -1140,6 +1256,7 @@ pub enum ServerRequest { PingRequest(PingRequest), CreateMessageRequest(CreateMessageRequest), ListRootsRequest(ListRootsRequest), + ElicitRequest(ElicitRequest), } #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] @@ -1172,6 +1289,21 @@ pub struct SetLevelRequestParams { pub level: LoggingLevel, } +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] +pub struct StringSchema { + #[serde(default, skip_serializing_if = "Option::is_none")] + pub description: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub format: Option, + #[serde(rename = "maxLength", default, skip_serializing_if = "Option::is_none")] + pub max_length: Option, + #[serde(rename = "minLength", default, skip_serializing_if = "Option::is_none")] + pub min_length: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub title: Option, + pub r#type: String, // &'static str = "string" +} + #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub enum SubscribeRequest {} @@ -1213,6 +1345,25 @@ pub struct Tool { #[serde(rename = "inputSchema")] pub input_schema: ToolInputSchema, pub name: String, + #[serde( + rename = "outputSchema", + default, + skip_serializing_if = "Option::is_none" + )] + pub output_schema: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub title: Option, +} + +/// An optional JSON Schema object defining the structure of the tool's output returned in +/// the structuredContent field of a CallToolResult. +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] +pub struct ToolOutputSchema { + #[serde(default, skip_serializing_if = "Option::is_none")] + pub properties: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub required: Option>, + pub r#type: String, // &'static str = "object" } /// A JSON Schema object defining the expected parameters for the tool. diff --git a/codex-rs/mcp-types/tests/initialize.rs b/codex-rs/mcp-types/tests/initialize.rs index 27902dce50..c69586f030 100644 --- a/codex-rs/mcp-types/tests/initialize.rs +++ b/codex-rs/mcp-types/tests/initialize.rs @@ -17,8 +17,8 @@ fn deserialize_initialize_request() { "method": "initialize", "params": { "capabilities": {}, - "clientInfo": { "name": "acme-client", "version": "1.2.3" }, - "protocolVersion": "2025-03-26" + "clientInfo": { "name": "acme-client", "title": "Acme", "version": "1.2.3" }, + "protocolVersion": "2025-06-18" } }"#; @@ -37,8 +37,8 @@ fn deserialize_initialize_request() { method: "initialize".into(), params: Some(json!({ "capabilities": {}, - "clientInfo": { "name": "acme-client", "version": "1.2.3" }, - "protocolVersion": "2025-03-26" + "clientInfo": { "name": "acme-client", "title": "Acme", "version": "1.2.3" }, + "protocolVersion": "2025-06-18" })), }; @@ -57,12 +57,14 @@ fn deserialize_initialize_request() { experimental: None, roots: None, sampling: None, + elicitation: None, }, client_info: Implementation { name: "acme-client".into(), + title: Some("Acme".to_string()), version: "1.2.3".into(), }, - protocol_version: "2025-03-26".into(), + protocol_version: "2025-06-18".into(), } ); } diff --git a/codex-rs/rust-toolchain.toml b/codex-rs/rust-toolchain.toml new file mode 100644 index 0000000000..72bafdf4b6 --- /dev/null +++ b/codex-rs/rust-toolchain.toml @@ -0,0 +1,3 @@ +[toolchain] +channel = "1.88.0" +components = [ "clippy", "rustfmt", "rust-src"] diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rs index 883250400d..37c2616d5b 100644 --- a/codex-rs/tui/src/app.rs +++ b/codex-rs/tui/src/app.rs @@ -18,8 +18,16 @@ use crossterm::event::KeyEvent; use crossterm::event::MouseEvent; use crossterm::event::MouseEventKind; use std::path::PathBuf; +use std::sync::Arc; +use std::sync::atomic::AtomicBool; +use std::sync::atomic::Ordering; use std::sync::mpsc::Receiver; use std::sync::mpsc::channel; +use std::thread; +use std::time::Duration; + +/// Time window for debouncing redraw requests. +const REDRAW_DEBOUNCE: Duration = Duration::from_millis(10); /// Top-level application state: which full-screen view is currently active. #[allow(clippy::large_enum_variant)] @@ -46,6 +54,9 @@ pub(crate) struct App<'a> { file_search: FileSearchManager, + /// True when a redraw has been scheduled but not yet executed. + pending_redraw: Arc, + /// Stored parameters needed to instantiate the ChatWidget later, e.g., /// after dismissing the Git-repo warning. chat_args: Option, @@ -60,7 +71,7 @@ struct ChatWidgetArgs { initial_images: Vec, } -impl<'a> App<'a> { +impl App<'_> { pub(crate) fn new( config: Config, initial_prompt: Option, @@ -70,6 +81,7 @@ impl<'a> App<'a> { ) -> Self { let (app_event_tx, app_event_rx) = channel(); let app_event_tx = AppEventSender::new(app_event_tx); + let pending_redraw = Arc::new(AtomicBool::new(false)); let scroll_event_helper = ScrollEventHelper::new(app_event_tx.clone()); // Spawn a dedicated thread for reading the crossterm event loop and @@ -83,7 +95,7 @@ impl<'a> App<'a> { app_event_tx.send(AppEvent::KeyEvent(key_event)); } crossterm::event::Event::Resize(_, _) => { - app_event_tx.send(AppEvent::Redraw); + app_event_tx.send(AppEvent::RequestRedraw); } crossterm::event::Event::Mouse(MouseEvent { kind: MouseEventKind::ScrollUp, @@ -152,6 +164,7 @@ impl<'a> App<'a> { app_state, config, file_search, + pending_redraw, chat_args, } } @@ -162,6 +175,28 @@ impl<'a> App<'a> { self.app_event_tx.clone() } + /// Schedule a redraw if one is not already pending. + #[allow(clippy::unwrap_used)] + fn schedule_redraw(&self) { + // Attempt to set the flag to `true`. If it was already `true`, another + // redraw is already pending so we can return early. + if self + .pending_redraw + .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst) + .is_err() + { + return; + } + + let tx = self.app_event_tx.clone(); + let pending_redraw = self.pending_redraw.clone(); + thread::spawn(move || { + thread::sleep(REDRAW_DEBOUNCE); + tx.send(AppEvent::Redraw); + pending_redraw.store(false, Ordering::SeqCst); + }); + } + pub(crate) fn run( &mut self, terminal: &mut tui::Tui, @@ -169,10 +204,13 @@ impl<'a> App<'a> { ) -> Result<()> { // Insert an event to trigger the first render. let app_event_tx = self.app_event_tx.clone(); - app_event_tx.send(AppEvent::Redraw); + app_event_tx.send(AppEvent::RequestRedraw); while let Ok(event) = self.app_event_rx.recv() { match event { + AppEvent::RequestRedraw => { + self.schedule_redraw(); + } AppEvent::Redraw => { self.draw_next_frame(terminal)?; } @@ -249,7 +287,7 @@ impl<'a> App<'a> { Vec::new(), )); self.app_state = AppState::Chat { widget: new_widget }; - self.app_event_tx.send(AppEvent::Redraw); + self.app_event_tx.send(AppEvent::RequestRedraw); } SlashCommand::ToggleMouseMode => { if let Err(e) = mouse_capture.toggle() { @@ -336,7 +374,7 @@ impl<'a> App<'a> { args.initial_images, )); self.app_state = AppState::Chat { widget }; - self.app_event_tx.send(AppEvent::Redraw); + self.app_event_tx.send(AppEvent::RequestRedraw); } GitWarningOutcome::Quit => { self.app_event_tx.send(AppEvent::ExitRequest); diff --git a/codex-rs/tui/src/app_event.rs b/codex-rs/tui/src/app_event.rs index fd6b2479ee..3aaa789760 100644 --- a/codex-rs/tui/src/app_event.rs +++ b/codex-rs/tui/src/app_event.rs @@ -8,6 +8,10 @@ use crate::slash_command::SlashCommand; pub(crate) enum AppEvent { CodexEvent(Event), + /// Request a redraw which will be debounced by the [`App`]. + RequestRedraw, + + /// Actually draw the next frame. Redraw, KeyEvent(KeyEvent), diff --git a/codex-rs/tui/src/bottom_pane/mod.rs b/codex-rs/tui/src/bottom_pane/mod.rs index e4ea1d3823..2a91655cc5 100644 --- a/codex-rs/tui/src/bottom_pane/mod.rs +++ b/codex-rs/tui/src/bottom_pane/mod.rs @@ -212,7 +212,7 @@ impl BottomPane<'_> { } pub(crate) fn request_redraw(&self) { - self.app_event_tx.send(AppEvent::Redraw) + self.app_event_tx.send(AppEvent::RequestRedraw) } /// Returns true when a popup inside the composer is visible. diff --git a/codex-rs/tui/src/bottom_pane/status_indicator_view.rs b/codex-rs/tui/src/bottom_pane/status_indicator_view.rs index d9ac57d7b9..de46ac2709 100644 --- a/codex-rs/tui/src/bottom_pane/status_indicator_view.rs +++ b/codex-rs/tui/src/bottom_pane/status_indicator_view.rs @@ -24,7 +24,7 @@ impl StatusIndicatorView { } } -impl<'a> BottomPaneView<'a> for StatusIndicatorView { +impl BottomPaneView<'_> for StatusIndicatorView { fn update_status_text(&mut self, text: String) -> ConditionalUpdate { self.update_text(text); ConditionalUpdate::NeedsRedraw diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index 860439ffb6..c22bbf9704 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -431,7 +431,7 @@ impl ChatWidget<'_> { } fn request_redraw(&mut self) { - self.app_event_tx.send(AppEvent::Redraw); + self.app_event_tx.send(AppEvent::RequestRedraw); } pub(crate) fn add_diff_output(&mut self, diff_output: String) { @@ -464,6 +464,8 @@ impl ChatWidget<'_> { if self.bottom_pane.is_task_running() { self.bottom_pane.clear_ctrl_c_quit_hint(); self.submit_op(Op::Interrupt); + self.answer_buffer.clear(); + self.reasoning_buffer.clear(); false } else if self.bottom_pane.ctrl_c_quit_hint_visible() { true diff --git a/codex-rs/tui/src/history_cell.rs b/codex-rs/tui/src/history_cell.rs index 0bfbc414b9..b481313405 100644 --- a/codex-rs/tui/src/history_cell.rs +++ b/codex-rs/tui/src/history_cell.rs @@ -17,6 +17,7 @@ use image::GenericImageView; use image::ImageReader; use lazy_static::lazy_static; use mcp_types::EmbeddedResourceResource; +use mcp_types::ResourceLink; use ratatui::prelude::*; use ratatui::style::Color; use ratatui::style::Modifier; @@ -331,8 +332,7 @@ impl HistoryCell { ) -> Option { match result { Ok(mcp_types::CallToolResult { content, .. }) => { - if let Some(mcp_types::CallToolResultContent::ImageContent(image)) = content.first() - { + if let Some(mcp_types::ContentBlock::ImageContent(image)) = content.first() { let raw_data = match base64::engine::general_purpose::STANDARD.decode(&image.data) { Ok(data) => data, @@ -405,21 +405,21 @@ impl HistoryCell { for tool_call_result in content { let line_text = match tool_call_result { - mcp_types::CallToolResultContent::TextContent(text) => { + mcp_types::ContentBlock::TextContent(text) => { format_and_truncate_tool_result( &text.text, TOOL_CALL_MAX_LINES, num_cols as usize, ) } - mcp_types::CallToolResultContent::ImageContent(_) => { + mcp_types::ContentBlock::ImageContent(_) => { // TODO show images even if they're not the first result, will require a refactor of `CompletedMcpToolCall` "".to_string() } - mcp_types::CallToolResultContent::AudioContent(_) => { + mcp_types::ContentBlock::AudioContent(_) => { "