mirror of
https://github.com/openai/codex.git
synced 2026-11-17 14:58:36 +00:00
Update rmcp to 3.1.2 (#39101)
## What changed - Upgrade `rmcp` from 3.0.0 to 3.1.2. - Use `rmcp`'s native JSON-RPC decoding and remove the local compatibility layer for multi-round-trip tool results. - Preserve response metadata on `input_required` SSE results. - Support OAuth protected-resource metadata discovery and include the discovered resource audience when refreshing tokens. ## Testing - Add coverage for metadata preservation on modern SSE `input_required` responses. - Add an OAuth startup scenario that discovers protected-resource metadata and verifies the refresh-token resource audience. GitOrigin-RevId: 689f009b4a33552b640304111b74585b4009eeb8
This commit is contained in:
1 parent
050aa077b5
commit
7500ab4c8d
11 files changed
+182
-262
No files matched your search
@@ -45,8 +45,6 @@ use tracing::debug;
|
||||
use tracing::info;
|
||||
use tracing::warn;
|
||||
|
||||
use crate::incoming_jsonrpc::deserialize_incoming_jsonrpc_message;
|
||||
|
||||
static PROCESS_COUNTER: AtomicUsize = AtomicUsize::new(1);
|
||||
// Tool results can make valid MCP responses large, so keep the protocol
|
||||
// ceiling well above ordinary messages while still bounding hostile input.
|
||||
@@ -452,7 +450,7 @@ impl ExecutorProcessTransport {
|
||||
None => return None,
|
||||
};
|
||||
let line = Self::trim_trailing_carriage_return(line);
|
||||
match deserialize_incoming_jsonrpc_message(&line) {
|
||||
match serde_json::from_slice(&line) {
|
||||
Ok(message) => return Some(message),
|
||||
Err(error) => {
|
||||
debug!(
|
||||
|
||||
@@ -57,8 +57,6 @@ use tokio::sync::oneshot;
|
||||
|
||||
use crate::event_notification_transport::MAX_EVENT_NOTIFICATION_BYTES;
|
||||
use crate::http_client_redirect::SameOriginRedirectHttpClient;
|
||||
use crate::incoming_jsonrpc::deserialize_incoming_jsonrpc_message;
|
||||
use crate::incoming_jsonrpc::normalize_sse_jsonrpc_message;
|
||||
use crate::local_stdio_transport::MAX_MCP_STDIO_LINE_BYTES;
|
||||
|
||||
mod www_authenticate;
|
||||
@@ -357,11 +355,7 @@ impl StreamableHttpClient for StreamableHttpClientAdapter {
|
||||
}
|
||||
match content_type.as_deref() {
|
||||
Some(content_type) if content_type.starts_with(EVENT_STREAM_MIME_TYPE) => {
|
||||
let mut event_stream = sse_stream_from_body(
|
||||
body_stream,
|
||||
maximum_response_bytes,
|
||||
is_discovery_request || uses_modern_protocol,
|
||||
);
|
||||
let mut event_stream = sse_stream_from_body(body_stream, maximum_response_bytes);
|
||||
if mcp_method.as_deref() == Some(DiscoverRequestMethod::VALUE) {
|
||||
while let Some(event) = event_stream.next().await {
|
||||
let event = event.map_err(StreamableHttpError::Sse)?;
|
||||
@@ -375,7 +369,7 @@ impl StreamableHttpClient for StreamableHttpClientAdapter {
|
||||
continue;
|
||||
}
|
||||
|
||||
let response = deserialize_incoming_jsonrpc_message(data.as_bytes())
|
||||
let response = serde_json::from_slice(data.as_bytes())
|
||||
.map_err(StreamableHttpError::Deserialize)?;
|
||||
let response = legacy_discovery_fallback_response(
|
||||
&message, response, /*allow_uncorrelated_http_rejection*/ false,
|
||||
@@ -425,8 +419,8 @@ impl StreamableHttpClient for StreamableHttpClientAdapter {
|
||||
}
|
||||
Some(content_type) if content_type.starts_with(JSON_MIME_TYPE) => {
|
||||
let body = collect_body(&mut body_stream, maximum_response_bytes).await?;
|
||||
let response_message = deserialize_incoming_jsonrpc_message(&body)
|
||||
.map_err(StreamableHttpError::Deserialize)?;
|
||||
let response_message =
|
||||
serde_json::from_slice(&body).map_err(StreamableHttpError::Deserialize)?;
|
||||
Ok(StreamableHttpPostResponse::Json(
|
||||
legacy_discovery_fallback_response(
|
||||
&message,
|
||||
@@ -593,11 +587,7 @@ impl StreamableHttpClient for StreamableHttpClientAdapter {
|
||||
.and_then(|value| value.to_str().ok())
|
||||
.is_some_and(|version| version == ProtocolVersion::V_2026_07_28.as_str());
|
||||
let maximum_response_bytes = uses_modern_protocol.then_some(MAX_MCP_STDIO_LINE_BYTES);
|
||||
Ok(sse_stream_from_body(
|
||||
body_stream,
|
||||
maximum_response_bytes,
|
||||
uses_modern_protocol,
|
||||
))
|
||||
Ok(sse_stream_from_body(body_stream, maximum_response_bytes))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -948,7 +938,6 @@ async fn collect_body(
|
||||
fn sse_stream_from_body(
|
||||
body_stream: HttpResponseBodyStream,
|
||||
maximum_event_bytes: Option<usize>,
|
||||
modern_session: bool,
|
||||
) -> BoxStream<'static, std::result::Result<Sse, sse_stream::Error>> {
|
||||
SseStream::from_bytes_stream(stream::unfold(
|
||||
(body_stream, SseEventSizeLimit::new(maximum_event_bytes)),
|
||||
@@ -966,16 +955,6 @@ fn sse_stream_from_body(
|
||||
}
|
||||
},
|
||||
))
|
||||
.map(move |event| {
|
||||
event.map(|mut event| {
|
||||
if let Some(payload) = event.data.as_deref()
|
||||
&& let Some(normalized) = normalize_sse_jsonrpc_message(payload, modern_session)
|
||||
{
|
||||
event.data = Some(normalized);
|
||||
}
|
||||
event
|
||||
})
|
||||
})
|
||||
.boxed()
|
||||
}
|
||||
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
//! Compatibility decoding for multi-round-trip tool results that rmcp's
|
||||
//! untagged result union does not yet represent correctly.
|
||||
|
||||
use rmcp::model::InputRequiredResult;
|
||||
use rmcp::model::JsonRpcResponse;
|
||||
use rmcp::model::ServerJsonRpcMessage;
|
||||
use rmcp::model::ServerResult;
|
||||
use serde_json::Value;
|
||||
|
||||
/// Decode at the transport boundary, before rmcp's untagged `ServerResult`
|
||||
/// deserializer can mistake a modern result for a completed tool call.
|
||||
pub(crate) fn deserialize_incoming_jsonrpc_message(
|
||||
bytes: &[u8],
|
||||
) -> serde_json::Result<ServerJsonRpcMessage> {
|
||||
let message: Value = serde_json::from_slice(bytes)?;
|
||||
|
||||
if message
|
||||
.pointer("/result/resultType")
|
||||
.and_then(Value::as_str)
|
||||
== Some("input_required")
|
||||
{
|
||||
let response: JsonRpcResponse<InputRequiredResult> = serde_json::from_value(message)?;
|
||||
return Ok(ServerJsonRpcMessage::response(
|
||||
ServerResult::InputRequiredResult(response.result),
|
||||
response.id,
|
||||
));
|
||||
}
|
||||
|
||||
serde_json::from_value(message)
|
||||
}
|
||||
|
||||
/// Normalize an SSE payload before rmcp parses it internally. SSE does not
|
||||
/// expose a typed-message hook, so response-level MRTR metadata cannot be
|
||||
/// retained until the upstream untagged-result decoder is fixed.
|
||||
pub(crate) fn normalize_sse_jsonrpc_message(payload: &str, modern_session: bool) -> Option<String> {
|
||||
if !modern_session {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut message: Value = serde_json::from_str(payload).ok()?;
|
||||
if message
|
||||
.pointer("/result/resultType")
|
||||
.and_then(Value::as_str)
|
||||
!= Some("input_required")
|
||||
{
|
||||
return None;
|
||||
}
|
||||
|
||||
message
|
||||
.get_mut("result")
|
||||
.and_then(Value::as_object_mut)?
|
||||
.remove("_meta")?;
|
||||
serde_json::to_string(&message).ok()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "incoming_jsonrpc_tests.rs"]
|
||||
mod tests;
|
||||
@@ -1,151 +0,0 @@
|
||||
use pretty_assertions::assert_eq;
|
||||
use rmcp::model::JsonRpcMessage;
|
||||
use rmcp::model::ServerResult;
|
||||
use serde_json::json;
|
||||
|
||||
use super::deserialize_incoming_jsonrpc_message;
|
||||
use super::normalize_sse_jsonrpc_message;
|
||||
|
||||
#[test]
|
||||
fn discovery_accepts_metadata_namespaced_server_identity() {
|
||||
let message = json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"result": {
|
||||
"resultType": "complete",
|
||||
"supportedVersions": ["2026-07-28"],
|
||||
"capabilities": {"tools": {}},
|
||||
"ttlMs": 0,
|
||||
"cacheScope": "private",
|
||||
"_meta": {
|
||||
"io.modelcontextprotocol/serverInfo": {
|
||||
"name": "modern-server",
|
||||
"version": "1.0.0",
|
||||
},
|
||||
"retained": "metadata",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
let decoded = deserialize_incoming_jsonrpc_message(&serde_json::to_vec(&message).unwrap())
|
||||
.expect("metadata-only identity must decode as discovery");
|
||||
let JsonRpcMessage::Response(response) = decoded else {
|
||||
panic!("expected a discovery response");
|
||||
};
|
||||
let ServerResult::DiscoverResult(result) = response.result else {
|
||||
panic!("metadata-only identity must not become a completed tool result");
|
||||
};
|
||||
assert_eq!(
|
||||
result.server_info().map(|server_info| server_info.name),
|
||||
Some("modern-server".to_owned())
|
||||
);
|
||||
assert_eq!(
|
||||
result.meta.and_then(|meta| meta.get("retained").cloned()),
|
||||
Some(json!("metadata"))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extension_result_with_discovery_like_fields_remains_a_custom_result() {
|
||||
let result = json!({
|
||||
"supportedVersions": ["custom-extension-v1"],
|
||||
"capabilities": {"custom": true},
|
||||
});
|
||||
let message = json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": 2,
|
||||
"result": result,
|
||||
});
|
||||
|
||||
let decoded = deserialize_incoming_jsonrpc_message(&serde_json::to_vec(&message).unwrap())
|
||||
.expect("legacy extension result must deserialize normally");
|
||||
let JsonRpcMessage::Response(response) = decoded else {
|
||||
panic!("expected a legacy extension response");
|
||||
};
|
||||
let ServerResult::CustomResult(custom) = response.result else {
|
||||
panic!("legacy extension response must remain a custom result");
|
||||
};
|
||||
assert_eq!(custom.0, result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn input_required_discriminator_wins_over_tool_result_metadata() {
|
||||
let message = json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": "tool-call",
|
||||
"result": {
|
||||
"resultType": "input_required",
|
||||
"requestState": "opaque-server-state",
|
||||
"_meta": {"retained": "response-metadata"},
|
||||
},
|
||||
});
|
||||
|
||||
let decoded = deserialize_incoming_jsonrpc_message(&serde_json::to_vec(&message).unwrap())
|
||||
.expect("input-required result must decode");
|
||||
let JsonRpcMessage::Response(response) = decoded else {
|
||||
panic!("expected an input-required response");
|
||||
};
|
||||
let ServerResult::InputRequiredResult(result) = response.result else {
|
||||
panic!("input_required must not become an empty completed tool result");
|
||||
};
|
||||
assert_eq!(result.request_state.as_deref(), Some("opaque-server-state"));
|
||||
assert_eq!(
|
||||
result.meta.and_then(|meta| meta.get("retained").cloned()),
|
||||
Some(json!("response-metadata"))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sse_discovery_preserves_metadata_namespaced_identity_without_normalization() {
|
||||
let message = json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"result": {
|
||||
"resultType": "complete",
|
||||
"supportedVersions": ["2026-07-28"],
|
||||
"capabilities": {},
|
||||
"ttlMs": 0,
|
||||
"cacheScope": "private",
|
||||
"_meta": {
|
||||
"io.modelcontextprotocol/serverInfo": {
|
||||
"name": "sse-server",
|
||||
"version": "1.0.0",
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
assert_eq!(
|
||||
normalize_sse_jsonrpc_message(&message.to_string(), /*modern_session*/ true),
|
||||
None
|
||||
);
|
||||
|
||||
let decoded = deserialize_incoming_jsonrpc_message(&serde_json::to_vec(&message).unwrap())
|
||||
.expect("rmcp must natively decode metadata-only SSE discovery");
|
||||
let JsonRpcMessage::Response(response) = decoded else {
|
||||
panic!("expected a discovery response");
|
||||
};
|
||||
let ServerResult::DiscoverResult(result) = response.result else {
|
||||
panic!("metadata-only identity must decode as discovery without normalization");
|
||||
};
|
||||
assert_eq!(
|
||||
result.server_info().map(|server_info| server_info.name),
|
||||
Some("sse-server".to_owned())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_sse_payload_preserves_intermediate_response_metadata() {
|
||||
let message = json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": "legacy-extension",
|
||||
"result": {
|
||||
"resultType": "input_required",
|
||||
"_meta": {"preserved": "legacy-extension-metadata"},
|
||||
},
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
normalize_sse_jsonrpc_message(&message.to_string(), /*modern_session*/ false),
|
||||
None
|
||||
);
|
||||
}
|
||||
@@ -6,7 +6,6 @@ mod http_client_adapter;
|
||||
mod http_client_redirect;
|
||||
mod http_headers;
|
||||
mod in_process_transport;
|
||||
mod incoming_jsonrpc;
|
||||
mod local_stdio_transport;
|
||||
mod logging_client_handler;
|
||||
mod oauth;
|
||||
|
||||
@@ -23,8 +23,6 @@ use tokio::sync::Mutex;
|
||||
use tracing::debug;
|
||||
use tracing::warn;
|
||||
|
||||
use crate::incoming_jsonrpc::deserialize_incoming_jsonrpc_message;
|
||||
|
||||
/// Match the existing executor stdio transport and the production MCP limit.
|
||||
pub(crate) const MAX_MCP_STDIO_LINE_BYTES: usize = 8 * 1024 * 1024;
|
||||
|
||||
@@ -118,7 +116,7 @@ impl LocalStdioTransport {
|
||||
fn decode_pending_message(&mut self) -> Option<RxJsonRpcMessage<RoleClient>> {
|
||||
let line = std::mem::take(&mut self.pending_line);
|
||||
let line = line.strip_suffix(b"\r").unwrap_or(&line);
|
||||
match deserialize_incoming_jsonrpc_message(line) {
|
||||
match serde_json::from_slice(line) {
|
||||
Ok(message) => Some(message),
|
||||
Err(error) => {
|
||||
debug!(
|
||||
|
||||
Reference in new issue
Block a user