-
Notifications
You must be signed in to change notification settings - Fork 12.1k
[rollout-trace] Add a trace ID to MCP calls. #22326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cassirer-openai
merged 5 commits into
main
from
cassirer/05-12/add-trace-id-to-mcp-calls
May 13, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7ea9929
[rollout-trace] Add a trace ID to MCP calls.
cassirer-openai f311f19
Don't panic.
cassirer-openai 4a379ce
Test the real MCP dispatch path.
cassirer-openai ce2177e
Merge branch 'main' into cassirer/05-12/add-trace-id-to-mcp-calls
cassirer-openai 73c53f2
Merge branch 'main' into cassirer/05-12/add-trace-id-to-mcp-calls
cassirer-openai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| //! Hot-path helpers for correlating concrete MCP executions with rollout traces. | ||
| //! | ||
| //! Core decides when an MCP request is actually going to execute. The trace | ||
| //! crate owns the globally unique ID, the trace event that preserves it in the | ||
| //! reduced artifact, and the bridge-private MCP request metadata key. | ||
|
|
||
| use crate::McpCallId; | ||
| use serde_json::Value as JsonValue; | ||
|
|
||
| const MCP_CALL_ID_META_KEY: &str = "codex_bridge_mcp_call_id"; | ||
|
|
||
| /// No-op capable handle for one concrete MCP backend call. | ||
| #[derive(Clone, Debug)] | ||
| pub struct McpCallTraceContext { | ||
| mcp_call_id: Option<McpCallId>, | ||
| } | ||
|
|
||
| impl McpCallTraceContext { | ||
| /// Builds a context that records nothing and leaves request metadata unchanged. | ||
| pub fn disabled() -> Self { | ||
| Self { mcp_call_id: None } | ||
| } | ||
|
|
||
| /// Builds the trace handle for one concrete MCP execution. | ||
| pub(crate) fn enabled(mcp_call_id: McpCallId) -> Self { | ||
| Self { | ||
| mcp_call_id: Some(mcp_call_id), | ||
| } | ||
| } | ||
|
|
||
| /// Returns the trace-owned MCP call ID when rollout tracing is enabled. | ||
| pub(crate) fn mcp_call_id(&self) -> Option<&str> { | ||
| self.mcp_call_id.as_deref() | ||
| } | ||
|
|
||
| /// Adds bridge-private MCP correlation metadata to one outgoing request. | ||
| pub fn add_request_meta(&self, meta: Option<JsonValue>) -> Option<JsonValue> { | ||
| let Some(mcp_call_id) = self.mcp_call_id() else { | ||
| return meta; | ||
| }; | ||
|
|
||
| match meta { | ||
| Some(JsonValue::Object(mut map)) => { | ||
| map.insert( | ||
| MCP_CALL_ID_META_KEY.to_string(), | ||
| JsonValue::String(mcp_call_id.to_string()), | ||
| ); | ||
| Some(JsonValue::Object(map)) | ||
| } | ||
| None => { | ||
| let mut map = serde_json::Map::new(); | ||
| map.insert( | ||
| MCP_CALL_ID_META_KEY.to_string(), | ||
| JsonValue::String(mcp_call_id.to_string()), | ||
| ); | ||
| Some(JsonValue::Object(map)) | ||
| } | ||
| // This should never happen but if it does then we'll fallback to | ||
| // a noop rather than any breaking behavior. The tracing is best | ||
| // effort after all. | ||
| Some(_) => meta, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use serde_json::json; | ||
|
|
||
| use super::MCP_CALL_ID_META_KEY; | ||
| use super::McpCallTraceContext; | ||
|
|
||
| #[test] | ||
| fn disabled_mcp_trace_leaves_request_meta_unchanged() { | ||
| let meta = Some(json!({"source": "test"})); | ||
|
|
||
| assert_eq!( | ||
| McpCallTraceContext::disabled().add_request_meta(meta.clone()), | ||
| meta | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn enabled_mcp_trace_adds_bridge_correlation_meta() { | ||
| let trace = McpCallTraceContext::enabled("mcp-call-id".to_string()); | ||
| let meta = trace | ||
| .add_request_meta(Some(json!({"source": "test"}))) | ||
| .expect("enabled trace keeps request metadata"); | ||
| let object = meta | ||
| .as_object() | ||
| .expect("MCP request metadata remains an object"); | ||
|
|
||
| assert_eq!(object["source"], json!("test")); | ||
| assert_eq!( | ||
| object[MCP_CALL_ID_META_KEY], | ||
| json!(trace.mcp_call_id().expect("enabled trace has an ID")) | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.