mirror of
https://github.com/openai/codex.git
synced 2026-11-20 22:40:37 +00:00
## Why An MCP connection or tool catalog can change after a model step captures its available tools. Calls from that step must not be rerouted to a replacement client or run against a catalog revision the model did not see. ## What changed - Add `McpBinding` to capture the ready clients, visible tools, resources, and server metadata for one sampling request. - Prepare calls against the captured client and reject them if the tool catalog changes before call preparation and execution finish. - Keep resource access and cache identity tied to the binding's exact connection set, and omit servers that do not have a ready client. - Preserve each client's unfiltered tool catalog so bindings can apply the current filter consistently. ## Testing Add coverage for connection replacement, stale catalog rejection, serialized call preparation and refresh, ready-client capture, and binding-scoped resource identity. GitOrigin-RevId: a25da4d2c7f5ed06c41caa0c6d15c464c1698314
219 lines
7.7 KiB
Rust
219 lines
7.7 KiB
Rust
use std::collections::HashMap;
|
|
use std::sync::Arc;
|
|
use std::sync::Weak;
|
|
|
|
use anyhow::Context;
|
|
use anyhow::Result;
|
|
use anyhow::anyhow;
|
|
use codex_rmcp_client::RmcpClient;
|
|
use rmcp::model::ListResourceTemplatesResult;
|
|
use rmcp::model::ListResourcesResult;
|
|
use rmcp::model::PaginatedRequestParams;
|
|
use rmcp::model::ReadResourceRequestParams;
|
|
use rmcp::model::ReadResourceResult;
|
|
use rmcp::model::Resource;
|
|
use rmcp::model::ResourceTemplate;
|
|
use tokio::task::JoinSet;
|
|
use tracing::warn;
|
|
|
|
use crate::rmcp_client::ManagedClient;
|
|
|
|
/// The ready clients captured for one model step.
|
|
pub(crate) struct McpBindingClients {
|
|
clients: HashMap<String, Arc<ManagedClient>>,
|
|
identity: McpBindingClientIdentity,
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub(crate) struct McpBindingClientIdentity(Vec<(String, Weak<RmcpClient>)>);
|
|
|
|
impl PartialEq for McpBindingClientIdentity {
|
|
fn eq(&self, other: &Self) -> bool {
|
|
self.0.len() == other.0.len()
|
|
&& self.0.iter().zip(&other.0).all(
|
|
|((server, client), (other_server, other_client))| {
|
|
server == other_server && client.ptr_eq(other_client)
|
|
},
|
|
)
|
|
}
|
|
}
|
|
|
|
impl Eq for McpBindingClientIdentity {}
|
|
|
|
impl McpBindingClients {
|
|
pub(crate) fn new(clients: HashMap<String, Arc<ManagedClient>>) -> Self {
|
|
let mut identity = clients
|
|
.iter()
|
|
.map(|(server, client)| (server.clone(), Arc::downgrade(&client.client)))
|
|
.collect::<Vec<_>>();
|
|
identity.sort_by(|left, right| left.0.cmp(&right.0));
|
|
Self {
|
|
clients,
|
|
identity: McpBindingClientIdentity(identity),
|
|
}
|
|
}
|
|
|
|
pub(crate) fn client(&self, server: &str) -> Option<Arc<ManagedClient>> {
|
|
self.clients.get(server).cloned()
|
|
}
|
|
|
|
pub(crate) fn contains_server(&self, server: &str) -> bool {
|
|
self.clients.contains_key(server)
|
|
}
|
|
|
|
pub(crate) fn identity(&self) -> McpBindingClientIdentity {
|
|
self.identity.clone()
|
|
}
|
|
|
|
pub(crate) async fn list_resources(
|
|
&self,
|
|
server: &str,
|
|
params: Option<PaginatedRequestParams>,
|
|
) -> Result<ListResourcesResult> {
|
|
let managed = self
|
|
.client(server)
|
|
.ok_or_else(|| anyhow!("MCP server '{server}' was not ready for this step"))?;
|
|
managed
|
|
.client
|
|
.list_resources(params, managed.tool_timeout)
|
|
.await
|
|
.with_context(|| format!("resources/list failed for `{server}`"))
|
|
}
|
|
|
|
pub(crate) async fn list_resource_templates(
|
|
&self,
|
|
server: &str,
|
|
params: Option<PaginatedRequestParams>,
|
|
) -> Result<ListResourceTemplatesResult> {
|
|
let managed = self
|
|
.client(server)
|
|
.ok_or_else(|| anyhow!("MCP server '{server}' was not ready for this step"))?;
|
|
managed
|
|
.client
|
|
.list_resource_templates(params, managed.tool_timeout)
|
|
.await
|
|
.with_context(|| format!("resources/templates/list failed for `{server}`"))
|
|
}
|
|
|
|
pub(crate) async fn read_resource(
|
|
&self,
|
|
server: &str,
|
|
params: ReadResourceRequestParams,
|
|
) -> Result<ReadResourceResult> {
|
|
let managed = self
|
|
.client(server)
|
|
.ok_or_else(|| anyhow!("MCP server '{server}' was not ready for this step"))?;
|
|
let uri = params.uri.clone();
|
|
managed
|
|
.client
|
|
.read_resource(params, managed.tool_timeout)
|
|
.await
|
|
.with_context(|| format!("resources/read failed for `{server}` ({uri})"))
|
|
}
|
|
|
|
pub(crate) async fn list_all_resources(
|
|
&self,
|
|
include_server: impl Fn(&str) -> bool,
|
|
) -> HashMap<String, Vec<Resource>> {
|
|
let mut join_set = JoinSet::new();
|
|
for (server_name, managed) in self
|
|
.clients
|
|
.iter()
|
|
.filter(|(server_name, _)| include_server(server_name))
|
|
{
|
|
let server_name = server_name.clone();
|
|
let client = Arc::clone(&managed.client);
|
|
let timeout = managed.tool_timeout;
|
|
join_set.spawn(async move {
|
|
let mut collected = Vec::new();
|
|
let mut cursor: Option<String> = None;
|
|
loop {
|
|
let params = cursor.as_ref().map(|next| {
|
|
PaginatedRequestParams::default().with_cursor(Some(next.clone()))
|
|
});
|
|
let response = match client.list_resources(params, timeout).await {
|
|
Ok(result) => result,
|
|
Err(error) => return (server_name, Err(error)),
|
|
};
|
|
collected.extend(response.resources);
|
|
match response.next_cursor {
|
|
Some(next) if cursor.as_ref() == Some(&next) => {
|
|
return (
|
|
server_name,
|
|
Err(anyhow!("resources/list returned duplicate cursor")),
|
|
);
|
|
}
|
|
Some(next) => cursor = Some(next),
|
|
None => return (server_name, Ok(collected)),
|
|
}
|
|
}
|
|
});
|
|
}
|
|
collect_resource_results(&mut join_set, "resources").await
|
|
}
|
|
|
|
pub(crate) async fn list_all_resource_templates(
|
|
&self,
|
|
include_server: impl Fn(&str) -> bool,
|
|
) -> HashMap<String, Vec<ResourceTemplate>> {
|
|
let mut join_set = JoinSet::new();
|
|
for (server_name, managed) in self
|
|
.clients
|
|
.iter()
|
|
.filter(|(server_name, _)| include_server(server_name))
|
|
{
|
|
let server_name = server_name.clone();
|
|
let client = Arc::clone(&managed.client);
|
|
let timeout = managed.tool_timeout;
|
|
join_set.spawn(async move {
|
|
let mut collected = Vec::new();
|
|
let mut cursor: Option<String> = None;
|
|
loop {
|
|
let params = cursor.as_ref().map(|next| {
|
|
PaginatedRequestParams::default().with_cursor(Some(next.clone()))
|
|
});
|
|
let response = match client.list_resource_templates(params, timeout).await {
|
|
Ok(result) => result,
|
|
Err(error) => return (server_name, Err(error)),
|
|
};
|
|
collected.extend(response.resource_templates);
|
|
match response.next_cursor {
|
|
Some(next) if cursor.as_ref() == Some(&next) => {
|
|
return (
|
|
server_name,
|
|
Err(anyhow!(
|
|
"resources/templates/list returned duplicate cursor"
|
|
)),
|
|
);
|
|
}
|
|
Some(next) => cursor = Some(next),
|
|
None => return (server_name, Ok(collected)),
|
|
}
|
|
}
|
|
});
|
|
}
|
|
collect_resource_results(&mut join_set, "resource templates").await
|
|
}
|
|
}
|
|
|
|
async fn collect_resource_results<T: Send + 'static>(
|
|
join_set: &mut JoinSet<(String, Result<Vec<T>>)>,
|
|
kind: &str,
|
|
) -> HashMap<String, Vec<T>> {
|
|
let mut resources = HashMap::new();
|
|
while let Some(result) = join_set.join_next().await {
|
|
match result {
|
|
Ok((server, Ok(server_resources))) => {
|
|
resources.insert(server, server_resources);
|
|
}
|
|
Ok((server, Err(error))) => {
|
|
warn!("Failed to list {kind} for MCP server '{server}': {error:#}");
|
|
}
|
|
Err(error) => {
|
|
warn!("Task panic when listing {kind} for MCP server: {error:#}");
|
|
}
|
|
}
|
|
}
|
|
resources
|
|
}
|