mirror of
https://github.com/openai/codex.git
synced 2026-11-21 22:50:37 +00:00
## What changed - Make `McpRuntime` own the published MCP configuration, connections, elicitation routing, and selected capability roots for a thread. - Capture immutable MCP bindings for model steps and tool calls so in-flight work keeps a consistent connection set and approval authority while refreshed state is published atomically. - Mark MCP state dirty when relevant configuration, plugins, environments, authentication, or elicitation capabilities change, then rebuild it before the next sampling request or out-of-band MCP operation. - Separate config reloads from server invalidation: `ReloadMcpConfig` applies resolved MCP inputs, while `RefreshMcpServers` requests reinitialization from the thread's latest state. ## Testing - Cover refreshed state visibility for existing turns, stable step bindings, current approval authority, cancelled refresh retries, resource-client reconciliation, and Apps recovery between sampling requests. GitOrigin-RevId: 59eabb1aa8dc083426bd18ef4d3630508f376401
125 lines
4.0 KiB
Rust
125 lines
4.0 KiB
Rust
use std::sync::Arc;
|
|
use std::sync::Weak;
|
|
|
|
use anyhow::Context;
|
|
use anyhow::Result;
|
|
use codex_protocol::mcp::Resource;
|
|
use codex_protocol::mcp::ResourceContent;
|
|
use rmcp::model::PaginatedRequestParams;
|
|
use rmcp::model::ReadResourceRequestParams;
|
|
|
|
use crate::McpRuntime;
|
|
use crate::connection_manager::McpConnectionSet;
|
|
|
|
/// One page of resources returned by an MCP server.
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub struct McpResourcePage {
|
|
/// Resources advertised on this page.
|
|
pub resources: Vec<Resource>,
|
|
/// Opaque cursor to supply when requesting the next page.
|
|
pub next_cursor: Option<String>,
|
|
}
|
|
|
|
/// Contents returned after reading one MCP resource.
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub struct McpResourceReadResult {
|
|
/// Text or blob content returned for the requested resource.
|
|
pub contents: Vec<ResourceContent>,
|
|
}
|
|
|
|
/// Access to MCP resources through the latest runtime.
|
|
#[derive(Clone)]
|
|
pub struct McpResourceClient {
|
|
runtime: Arc<McpRuntime>,
|
|
}
|
|
|
|
/// Opaque identity for the connection set currently used by an MCP resource client.
|
|
#[derive(Clone)]
|
|
pub struct McpResourceClientCacheKey(Weak<McpConnectionSet>);
|
|
|
|
impl PartialEq for McpResourceClientCacheKey {
|
|
fn eq(&self, other: &Self) -> bool {
|
|
self.0.ptr_eq(&other.0)
|
|
}
|
|
}
|
|
|
|
impl Eq for McpResourceClientCacheKey {}
|
|
|
|
impl std::fmt::Debug for McpResourceClient {
|
|
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
formatter
|
|
.debug_struct("McpResourceClient")
|
|
.finish_non_exhaustive()
|
|
}
|
|
}
|
|
|
|
impl McpResourceClient {
|
|
/// Creates a resource client that follows the thread's latest published runtime.
|
|
pub fn new(runtime: Arc<McpRuntime>) -> Self {
|
|
Self { runtime }
|
|
}
|
|
|
|
/// Returns the identity of the connection set used by this client.
|
|
pub fn cache_key(&self) -> McpResourceClientCacheKey {
|
|
McpResourceClientCacheKey(Arc::downgrade(&self.runtime.latest_connections()))
|
|
}
|
|
|
|
/// Returns whether this client can address the named server.
|
|
///
|
|
/// This does not wait for server startup.
|
|
pub async fn has_server(&self, server: &str) -> bool {
|
|
self.runtime.latest_connections().contains_server(server)
|
|
}
|
|
|
|
/// Lists one resource page from the named server.
|
|
pub async fn list_resources(
|
|
&self,
|
|
server: &str,
|
|
cursor: Option<String>,
|
|
) -> Result<McpResourcePage> {
|
|
let params =
|
|
cursor.map(|cursor| PaginatedRequestParams::default().with_cursor(Some(cursor)));
|
|
let result = self
|
|
.runtime
|
|
.latest_connections()
|
|
.list_resources(server, params)
|
|
.await?;
|
|
let resources = result
|
|
.resources
|
|
.into_iter()
|
|
.map(resource_from_rmcp)
|
|
.collect::<Result<Vec<_>>>()?;
|
|
Ok(McpResourcePage {
|
|
resources,
|
|
next_cursor: result.next_cursor,
|
|
})
|
|
}
|
|
|
|
/// Reads one resource from the named server.
|
|
pub async fn read_resource(&self, server: &str, uri: &str) -> Result<McpResourceReadResult> {
|
|
let params = ReadResourceRequestParams::new(uri.to_string());
|
|
let result = self
|
|
.runtime
|
|
.latest_connections()
|
|
.read_resource(server, params)
|
|
.await?;
|
|
let contents = result
|
|
.contents
|
|
.into_iter()
|
|
.map(resource_content_from_rmcp)
|
|
.collect::<Result<Vec<_>>>()?;
|
|
Ok(McpResourceReadResult { contents })
|
|
}
|
|
}
|
|
|
|
fn resource_from_rmcp(resource: rmcp::model::Resource) -> Result<Resource> {
|
|
let value = serde_json::to_value(resource).context("failed to serialize MCP resource")?;
|
|
Resource::from_mcp_value(value).context("failed to convert MCP resource")
|
|
}
|
|
|
|
fn resource_content_from_rmcp(content: rmcp::model::ResourceContents) -> Result<ResourceContent> {
|
|
let value =
|
|
serde_json::to_value(content).context("failed to serialize MCP resource content")?;
|
|
serde_json::from_value(value).context("failed to convert MCP resource content")
|
|
}
|