mirror of
https://github.com/openai/codex.git
synced 2026-12-02 00:31:03 +00:00
mcp resource read: simplify origin routing
This commit is contained in:
@@ -7,9 +7,9 @@ const MAX_MCP_RESOURCE_ORIGINS: usize = 1024;
|
||||
pub(crate) struct McpResourceOrigin {
|
||||
pub(crate) server: String,
|
||||
pub(crate) tool: String,
|
||||
pub(crate) connector_id: Option<String>,
|
||||
pub(crate) connector_id: String,
|
||||
pub(crate) link_id: Option<String>,
|
||||
pub(crate) resource_uri: Option<String>,
|
||||
pub(crate) resource_uri: String,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -34,24 +34,15 @@ impl McpResourceOrigin {
|
||||
else {
|
||||
return None;
|
||||
};
|
||||
let (connector_id, link_id, resource_uri) = app_context
|
||||
.as_ref()
|
||||
.map(|context| {
|
||||
(
|
||||
Some(context.connector_id.clone()),
|
||||
context.link_id.clone(),
|
||||
context.resource_uri.clone(),
|
||||
)
|
||||
})
|
||||
.unwrap_or_default();
|
||||
let app_context = app_context.as_ref()?;
|
||||
Some((
|
||||
id,
|
||||
Self {
|
||||
server: server.clone(),
|
||||
tool: tool.clone(),
|
||||
connector_id,
|
||||
link_id,
|
||||
resource_uri,
|
||||
connector_id: app_context.connector_id.clone(),
|
||||
link_id: app_context.link_id.clone(),
|
||||
resource_uri: app_context.resource_uri.clone()?,
|
||||
},
|
||||
))
|
||||
}
|
||||
@@ -64,32 +55,15 @@ impl McpResourceOriginIndex {
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn insert_if_absent(
|
||||
&mut self,
|
||||
call_id: &str,
|
||||
origin: McpResourceOrigin,
|
||||
) -> McpResourceOrigin {
|
||||
if let Some(origin) = self.get(call_id) {
|
||||
return origin;
|
||||
}
|
||||
self.insert(call_id.to_string(), origin.clone());
|
||||
origin
|
||||
}
|
||||
|
||||
pub(crate) fn seed<'a>(&mut self, items: impl IntoIterator<Item = &'a ThreadItem>) {
|
||||
for (call_id, origin) in items.into_iter().filter_map(McpResourceOrigin::from_item) {
|
||||
self.insert(call_id.to_string(), origin);
|
||||
}
|
||||
}
|
||||
|
||||
fn insert(&mut self, call_id: String, origin: McpResourceOrigin) {
|
||||
if let Some(index) = self
|
||||
.0
|
||||
.iter()
|
||||
.position(|(existing_call_id, _)| existing_call_id == &call_id)
|
||||
{
|
||||
self.0.remove(index);
|
||||
}
|
||||
pub(crate) fn insert(&mut self, call_id: String, origin: McpResourceOrigin) {
|
||||
self.0
|
||||
.retain(|(existing_call_id, _)| existing_call_id != &call_id);
|
||||
self.0.push_back((call_id, origin));
|
||||
if self.0.len() > MAX_MCP_RESOURCE_ORIGINS {
|
||||
self.0.pop_front();
|
||||
|
||||
@@ -21,17 +21,6 @@ fn bounds_entries_and_keeps_updated_origins() {
|
||||
assert_eq!(index.get("call-1"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_does_not_replace_live_origin() {
|
||||
let mut index = McpResourceOriginIndex::default();
|
||||
insert(&mut index, "call-1", "live-connector");
|
||||
|
||||
let resolved = index.insert_if_absent("call-1", origin_with_app_context("history-connector"));
|
||||
|
||||
assert_eq!(resolved, origin_with_app_context("live-connector"));
|
||||
assert_eq!(index.get("call-1"), Some(resolved));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn seeds_origins_from_materialized_items() {
|
||||
let mut index = McpResourceOriginIndex::default();
|
||||
@@ -72,8 +61,8 @@ fn origin_with_app_context(connector_id: &str) -> McpResourceOrigin {
|
||||
McpResourceOrigin {
|
||||
server: "plugin-runtime".to_string(),
|
||||
tool: "search".to_string(),
|
||||
connector_id: Some(connector_id.to_string()),
|
||||
connector_id: connector_id.to_string(),
|
||||
link_id: Some("link".to_string()),
|
||||
resource_uri: Some("ui://shared/widget.html".to_string()),
|
||||
resource_uri: "ui://shared/widget.html".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
use super::mcp_processor::with_mcp_tool_call_thread_id_meta;
|
||||
use super::*;
|
||||
use crate::mcp_resource_origin::McpResourceOrigin;
|
||||
use anyhow::Context;
|
||||
use codex_mcp::CODEX_APPS_MCP_SERVER_NAME;
|
||||
use codex_mcp::MCP_TOOL_CODEX_APPS_META_KEY;
|
||||
use codex_mcp::ToolInfo;
|
||||
use serde_json::Value;
|
||||
|
||||
const LEGACY_CODEX_APPS_MCP_SERVER_NAME: &str = "codex-connectors-mcp";
|
||||
const PLUGIN_RUNTIME_MCP_SERVER_NAME: &str = "plugin-runtime";
|
||||
const LEGACY_CODEX_APPS_IMPLEMENTATION_NAME: &str = "codex-connectors-mcp";
|
||||
const PLUGIN_RUNTIME_IMPLEMENTATION_NAME: &str = "plugin-runtime";
|
||||
const FETCH_RESOURCE_TOOL_NAME: &str = "fetch_resource";
|
||||
|
||||
pub(super) async fn read_thread_mcp_resource(
|
||||
@@ -28,11 +30,11 @@ pub(super) async fn read_thread_mcp_resource(
|
||||
let runtime = thread.current_mcp_runtime().await;
|
||||
let connection = runtime.manager().server_connection(server).await?;
|
||||
match connection.server_info().name.as_str() {
|
||||
LEGACY_CODEX_APPS_MCP_SERVER_NAME => {
|
||||
LEGACY_CODEX_APPS_IMPLEMENTATION_NAME => {
|
||||
let result = connection.read_resource(uri).await?;
|
||||
return Ok(serde_json::to_value(result)?);
|
||||
}
|
||||
PLUGIN_RUNTIME_MCP_SERVER_NAME => {}
|
||||
PLUGIN_RUNTIME_IMPLEMENTATION_NAME => {}
|
||||
implementation => {
|
||||
anyhow::bail!("unsupported codex_apps MCP server implementation `{implementation}`")
|
||||
}
|
||||
@@ -41,29 +43,26 @@ pub(super) async fn read_thread_mcp_resource(
|
||||
let origin =
|
||||
resolve_mcp_resource_origin(thread_state_manager, thread_id, thread, origin_call_id)
|
||||
.await?;
|
||||
if origin.connector_id.is_none() {
|
||||
anyhow::bail!("originating MCP tool call `{origin_call_id}` has no app context");
|
||||
}
|
||||
if origin.server != server {
|
||||
anyhow::bail!(
|
||||
"originating MCP tool call server `{}` does not match resource server `{server}`",
|
||||
origin.server
|
||||
);
|
||||
}
|
||||
if origin.resource_uri.as_deref() != Some(uri) {
|
||||
if origin.resource_uri != uri {
|
||||
anyhow::bail!(
|
||||
"originating MCP tool call resource URI {:?} does not match requested URI `{uri}`",
|
||||
"originating MCP tool call resource URI `{}` does not match requested URI `{uri}`",
|
||||
origin.resource_uri
|
||||
);
|
||||
}
|
||||
|
||||
let tool_info = connection.tool_info(&origin.tool).ok_or_else(|| {
|
||||
anyhow::anyhow!(
|
||||
let tool_info = connection.tool_info(&origin.tool).with_context(|| {
|
||||
format!(
|
||||
"originating MCP tool `{}` is not available on server `{server}`",
|
||||
origin.tool
|
||||
)
|
||||
})?;
|
||||
let meta = build_plugin_runtime_fetch_resource_meta(&origin, uri, tool_info)?;
|
||||
let meta = build_plugin_runtime_fetch_resource_meta(&origin, uri, &tool_info)?;
|
||||
let meta = with_mcp_tool_call_thread_id_meta(Some(meta), thread_id_string);
|
||||
let result = connection
|
||||
.call_tool(
|
||||
@@ -104,16 +103,15 @@ async fn resolve_mcp_resource_origin(
|
||||
.rev()
|
||||
.find_map(|turn| McpResourceOrigin::find(&turn.items, origin_call_id));
|
||||
let mut thread_state = thread_state.lock().await;
|
||||
history_origin
|
||||
.map(|origin| {
|
||||
thread_state
|
||||
.mcp_resource_origins
|
||||
.insert_if_absent(origin_call_id, origin)
|
||||
})
|
||||
.or_else(|| thread_state.mcp_resource_origin(origin_call_id))
|
||||
.ok_or_else(|| {
|
||||
anyhow::anyhow!("originating MCP tool call `{origin_call_id}` was not found")
|
||||
})
|
||||
if let Some(origin) = thread_state.mcp_resource_origin(origin_call_id) {
|
||||
return Ok(origin);
|
||||
}
|
||||
let origin = history_origin
|
||||
.with_context(|| format!("originating MCP tool call `{origin_call_id}` was not found"))?;
|
||||
thread_state
|
||||
.mcp_resource_origins
|
||||
.insert(origin_call_id.to_string(), origin.clone());
|
||||
Ok(origin)
|
||||
}
|
||||
|
||||
fn build_plugin_runtime_fetch_resource_meta(
|
||||
@@ -121,42 +119,45 @@ fn build_plugin_runtime_fetch_resource_meta(
|
||||
uri: &str,
|
||||
tool_info: &ToolInfo,
|
||||
) -> anyhow::Result<serde_json::Value> {
|
||||
// Resolve the current server-owned tool identity.
|
||||
let tool_meta = tool_info
|
||||
.tool
|
||||
.meta
|
||||
.as_ref()
|
||||
.ok_or_else(|| anyhow::anyhow!("originating MCP tool is missing metadata"))?;
|
||||
.context("originating MCP tool is missing metadata")?;
|
||||
let connector_id = tool_info
|
||||
.connector_id
|
||||
.as_deref()
|
||||
.ok_or_else(|| anyhow::anyhow!("originating MCP tool is missing connector_id"))?;
|
||||
.context("originating MCP tool is missing connector_id")?;
|
||||
let link_id = tool_meta
|
||||
.0
|
||||
.get("link_id")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.ok_or_else(|| anyhow::anyhow!("originating MCP tool is missing link_id"))?;
|
||||
.and_then(Value::as_str)
|
||||
.context("originating MCP tool is missing link_id")?;
|
||||
let codex_apps_meta = tool_meta
|
||||
.0
|
||||
.get(MCP_TOOL_CODEX_APPS_META_KEY)
|
||||
.and_then(serde_json::Value::as_object)
|
||||
.ok_or_else(|| anyhow::anyhow!("originating MCP tool is missing _codex_apps metadata"))?;
|
||||
.and_then(Value::as_object)
|
||||
.context("originating MCP tool is missing _codex_apps metadata")?;
|
||||
let resource_uri = mcp_app_resource_uri(&tool_meta.0)
|
||||
.ok_or_else(|| anyhow::anyhow!("originating MCP tool is missing its app resource URI"))?;
|
||||
.context("originating MCP tool is missing its app resource URI")?;
|
||||
|
||||
// Cross-check persisted provenance, the request, and current tool metadata.
|
||||
if resource_uri != uri {
|
||||
anyhow::bail!(
|
||||
"originating MCP tool resource URI `{resource_uri}` does not match requested URI `{uri}`"
|
||||
);
|
||||
}
|
||||
if Some(connector_id) != origin.connector_id.as_deref() {
|
||||
if connector_id != origin.connector_id {
|
||||
anyhow::bail!(
|
||||
"originating MCP tool connector `{connector_id}` does not match app context connector {:?}",
|
||||
"originating MCP tool connector `{connector_id}` does not match app context connector `{}`",
|
||||
origin.connector_id
|
||||
);
|
||||
}
|
||||
|
||||
let synthetic_link = codex_apps_meta
|
||||
.get("synthetic_link")
|
||||
.and_then(serde_json::Value::as_bool)
|
||||
.and_then(Value::as_bool)
|
||||
== Some(true);
|
||||
match origin.link_id.as_deref() {
|
||||
Some(origin_link_id) if origin_link_id == link_id => {}
|
||||
@@ -166,80 +167,53 @@ fn build_plugin_runtime_fetch_resource_meta(
|
||||
|
||||
let tool_resource_route = codex_apps_meta
|
||||
.get("resource_uri")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.ok_or_else(|| anyhow::anyhow!("originating MCP tool is missing its resource route"))?;
|
||||
.and_then(Value::as_str)
|
||||
.context("originating MCP tool is missing its resource route")?;
|
||||
let route_segments = tool_resource_route
|
||||
.strip_prefix('/')
|
||||
.map(|route| route.split('/').collect::<Vec<_>>())
|
||||
.filter(|segments| segments.len() == 3 && segments.iter().all(|part| !part.is_empty()))
|
||||
.ok_or_else(|| anyhow::anyhow!("originating MCP tool has an invalid resource route"))?;
|
||||
.context("originating MCP tool has an invalid resource route")?;
|
||||
if route_segments[0] != connector_id || route_segments[1] != link_id {
|
||||
anyhow::bail!("originating MCP tool resource route does not match connector and link");
|
||||
}
|
||||
|
||||
let mut fetch_resource_meta = serde_json::Map::from_iter([
|
||||
(
|
||||
"resource_uri".to_string(),
|
||||
serde_json::Value::String(format!(
|
||||
"/{connector_id}/{link_id}/{FETCH_RESOURCE_TOOL_NAME}"
|
||||
)),
|
||||
),
|
||||
(
|
||||
"contains_mcp_source".to_string(),
|
||||
serde_json::Value::Bool(
|
||||
codex_apps_meta
|
||||
.get("contains_mcp_source")
|
||||
.and_then(serde_json::Value::as_bool)
|
||||
== Some(true),
|
||||
),
|
||||
),
|
||||
]);
|
||||
// Construct the canonical route without forwarding client-provided routing.
|
||||
let mut fetch_resource_meta = serde_json::json!({
|
||||
"resource_uri": format!("/{connector_id}/{link_id}/{FETCH_RESOURCE_TOOL_NAME}"),
|
||||
"contains_mcp_source": codex_apps_meta
|
||||
.get("contains_mcp_source")
|
||||
.and_then(Value::as_bool)
|
||||
== Some(true),
|
||||
});
|
||||
if synthetic_link {
|
||||
fetch_resource_meta.insert("synthetic_link".to_string(), serde_json::Value::Bool(true));
|
||||
fetch_resource_meta["synthetic_link"] = Value::Bool(true);
|
||||
}
|
||||
|
||||
let mut meta = serde_json::Map::from_iter([
|
||||
(
|
||||
MCP_TOOL_CODEX_APPS_META_KEY.to_string(),
|
||||
serde_json::Value::Object(fetch_resource_meta),
|
||||
),
|
||||
(
|
||||
"x-codex-turn-metadata".to_string(),
|
||||
serde_json::json!({
|
||||
"mcp_request_meta": {
|
||||
"selected_connector_ids": [connector_id],
|
||||
}
|
||||
}),
|
||||
),
|
||||
]);
|
||||
let mut meta = serde_json::json!({
|
||||
"_codex_apps": fetch_resource_meta,
|
||||
"x-codex-turn-metadata": {
|
||||
"mcp_request_meta": {
|
||||
"selected_connector_ids": [connector_id],
|
||||
}
|
||||
},
|
||||
});
|
||||
if let Some(connector_name) = tool_info.connector_name.as_ref() {
|
||||
meta.insert(
|
||||
"connector_name".to_string(),
|
||||
serde_json::Value::String(connector_name.clone()),
|
||||
);
|
||||
meta["connector_name"] = Value::String(connector_name.clone());
|
||||
}
|
||||
if let Some(connector_description) = tool_info.namespace_description.as_ref() {
|
||||
meta.insert(
|
||||
"connector_description".to_string(),
|
||||
serde_json::Value::String(connector_description.clone()),
|
||||
);
|
||||
meta["connector_description"] = Value::String(connector_description.clone());
|
||||
}
|
||||
Ok(serde_json::Value::Object(meta))
|
||||
Ok(meta)
|
||||
}
|
||||
|
||||
fn mcp_app_resource_uri(meta: &serde_json::Map<String, serde_json::Value>) -> Option<&str> {
|
||||
fn mcp_app_resource_uri(meta: &serde_json::Map<String, Value>) -> Option<&str> {
|
||||
meta.get("ui")
|
||||
.and_then(serde_json::Value::as_object)
|
||||
.and_then(Value::as_object)
|
||||
.and_then(|ui| ui.get("resourceUri"))
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.or_else(|| {
|
||||
meta.get("ui/resourceUri")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
})
|
||||
.or_else(|| {
|
||||
meta.get("openai/outputTemplate")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
})
|
||||
.and_then(Value::as_str)
|
||||
.or_else(|| meta.get("ui/resourceUri").and_then(Value::as_str))
|
||||
.or_else(|| meta.get("openai/outputTemplate").and_then(Value::as_str))
|
||||
}
|
||||
|
||||
fn plugin_runtime_fetch_resource_response(
|
||||
@@ -250,13 +224,11 @@ fn plugin_runtime_fetch_resource_response(
|
||||
}
|
||||
let mut structured_content = result
|
||||
.structured_content
|
||||
.ok_or_else(|| anyhow::anyhow!("plugin-runtime fetch_resource returned no contents"))?;
|
||||
.context("plugin-runtime fetch_resource returned no contents")?;
|
||||
let contents = structured_content
|
||||
.get_mut("contents")
|
||||
.and_then(serde_json::Value::as_array_mut)
|
||||
.ok_or_else(|| {
|
||||
anyhow::anyhow!("plugin-runtime fetch_resource returned invalid contents")
|
||||
})?;
|
||||
.and_then(Value::as_array_mut)
|
||||
.context("plugin-runtime fetch_resource returned invalid contents")?;
|
||||
for content in contents {
|
||||
if let Some(content) = content.as_object_mut()
|
||||
&& !content.contains_key("_meta")
|
||||
|
||||
@@ -15,7 +15,7 @@ const TOOL_NAME: &str = "catalog_a_search";
|
||||
#[test]
|
||||
fn rejects_connector_link_uri_and_route_identity_mismatches() {
|
||||
let mut mismatched_connector = origin(Some(LINK_ID));
|
||||
mismatched_connector.connector_id = Some("catalog-b".to_string());
|
||||
mismatched_connector.connector_id = "catalog-b".to_string();
|
||||
assert_error_contains(
|
||||
build_plugin_runtime_fetch_resource_meta(&mismatched_connector, RESOURCE_URI, &tool_info()),
|
||||
"does not match app context connector",
|
||||
@@ -118,9 +118,9 @@ fn origin(link_id: Option<&str>) -> McpResourceOrigin {
|
||||
McpResourceOrigin {
|
||||
server: CODEX_APPS_MCP_SERVER_NAME.to_string(),
|
||||
tool: TOOL_NAME.to_string(),
|
||||
connector_id: Some(CONNECTOR_ID.to_string()),
|
||||
connector_id: CONNECTOR_ID.to_string(),
|
||||
link_id: link_id.map(str::to_string),
|
||||
resource_uri: Some(RESOURCE_URI.to_string()),
|
||||
resource_uri: RESOURCE_URI.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -124,7 +124,6 @@ pub struct McpConnectionManager {
|
||||
pub struct McpServerConnection {
|
||||
server_name: String,
|
||||
managed_client: ManagedClient,
|
||||
tools: Vec<ToolInfo>,
|
||||
}
|
||||
|
||||
impl McpServerConnection {
|
||||
@@ -134,9 +133,10 @@ impl McpServerConnection {
|
||||
}
|
||||
|
||||
/// Returns the filtered metadata for a currently available raw MCP tool.
|
||||
pub fn tool_info(&self, tool: &str) -> Option<&ToolInfo> {
|
||||
self.tools
|
||||
.iter()
|
||||
pub fn tool_info(&self, tool: &str) -> Option<ToolInfo> {
|
||||
self.managed_client
|
||||
.listed_tools()
|
||||
.into_iter()
|
||||
.find(|tool_info| tool_info.tool.name.as_ref() == tool)
|
||||
}
|
||||
|
||||
@@ -147,24 +147,59 @@ impl McpServerConnection {
|
||||
arguments: Option<serde_json::Value>,
|
||||
meta: Option<serde_json::Value>,
|
||||
) -> Result<CallToolResult> {
|
||||
call_tool_with_client(
|
||||
&self.managed_client,
|
||||
&self.server_name,
|
||||
tool,
|
||||
arguments,
|
||||
meta,
|
||||
)
|
||||
.await
|
||||
let server = &self.server_name;
|
||||
if !self.managed_client.tool_filter.allows(tool) {
|
||||
return Err(anyhow!(
|
||||
"tool '{tool}' is disabled for MCP server '{server}'"
|
||||
));
|
||||
}
|
||||
|
||||
let result: rmcp::model::CallToolResult = self
|
||||
.managed_client
|
||||
.client
|
||||
.call_tool(
|
||||
tool.to_string(),
|
||||
arguments,
|
||||
meta,
|
||||
self.managed_client.tool_timeout,
|
||||
)
|
||||
.await
|
||||
.with_context(|| format!("tool call failed for `{server}/{tool}`"))?;
|
||||
|
||||
let content = result
|
||||
.content
|
||||
.into_iter()
|
||||
.map(|content| {
|
||||
serde_json::to_value(content)
|
||||
.unwrap_or_else(|_| serde_json::Value::String("<content>".to_string()))
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(CallToolResult {
|
||||
content,
|
||||
structured_content: result.structured_content,
|
||||
is_error: result.is_error,
|
||||
meta: result.meta.and_then(|meta| serde_json::to_value(meta).ok()),
|
||||
})
|
||||
}
|
||||
|
||||
/// Read a resource through this pinned server connection.
|
||||
pub async fn read_resource(&self, uri: &str) -> Result<ReadResourceResult> {
|
||||
read_resource_with_client(
|
||||
&self.managed_client,
|
||||
&self.server_name,
|
||||
ReadResourceRequestParams::new(uri),
|
||||
)
|
||||
.await
|
||||
self.read_resource_params(ReadResourceRequestParams::new(uri))
|
||||
.await
|
||||
}
|
||||
|
||||
async fn read_resource_params(
|
||||
&self,
|
||||
params: ReadResourceRequestParams,
|
||||
) -> Result<ReadResourceResult> {
|
||||
let server = &self.server_name;
|
||||
let uri = params.uri.clone();
|
||||
self.managed_client
|
||||
.client
|
||||
.read_resource(params, self.managed_client.tool_timeout)
|
||||
.await
|
||||
.with_context(|| format!("resources/read failed for `{server}` ({uri})"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -530,7 +565,7 @@ impl McpConnectionManager {
|
||||
}
|
||||
}
|
||||
|
||||
/// Acquires a live connection to `server` and snapshots its filtered tools.
|
||||
/// Acquires a live connection to `server`.
|
||||
///
|
||||
/// Unlike presentation-oriented APIs, this waits for authoritative server
|
||||
/// initialization and never falls back to cached startup metadata.
|
||||
@@ -538,7 +573,6 @@ impl McpConnectionManager {
|
||||
let managed_client = self.client_by_name(server).await?;
|
||||
Ok(McpServerConnection {
|
||||
server_name: server.to_string(),
|
||||
tools: managed_client.listed_tools(),
|
||||
managed_client,
|
||||
})
|
||||
}
|
||||
@@ -598,6 +632,7 @@ impl McpConnectionManager {
|
||||
.client()
|
||||
.await
|
||||
.context("failed to get client")?;
|
||||
|
||||
let list_start = Instant::now();
|
||||
let fetch_start = Instant::now();
|
||||
let fetch_ticket = managed_client
|
||||
@@ -802,8 +837,10 @@ impl McpConnectionManager {
|
||||
arguments: Option<serde_json::Value>,
|
||||
meta: Option<serde_json::Value>,
|
||||
) -> Result<CallToolResult> {
|
||||
let client = self.client_by_name(server).await?;
|
||||
call_tool_with_client(&client, server, tool, arguments, meta).await
|
||||
self.server_connection(server)
|
||||
.await?
|
||||
.call_tool(tool, arguments, meta)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn server_supports_sandbox_state_meta_capability(
|
||||
@@ -854,8 +891,10 @@ impl McpConnectionManager {
|
||||
server: &str,
|
||||
params: ReadResourceRequestParams,
|
||||
) -> Result<ReadResourceResult> {
|
||||
let managed = self.client_by_name(server).await?;
|
||||
read_resource_with_client(&managed, server, params).await
|
||||
self.server_connection(server)
|
||||
.await?
|
||||
.read_resource_params(params)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Returns presentation metadata without waiting for uncached clients still initializing.
|
||||
@@ -921,55 +960,6 @@ impl McpConnectionManager {
|
||||
}
|
||||
}
|
||||
|
||||
async fn call_tool_with_client(
|
||||
client: &ManagedClient,
|
||||
server: &str,
|
||||
tool: &str,
|
||||
arguments: Option<serde_json::Value>,
|
||||
meta: Option<serde_json::Value>,
|
||||
) -> Result<CallToolResult> {
|
||||
if !client.tool_filter.allows(tool) {
|
||||
return Err(anyhow!(
|
||||
"tool '{tool}' is disabled for MCP server '{server}'"
|
||||
));
|
||||
}
|
||||
|
||||
let result: rmcp::model::CallToolResult = client
|
||||
.client
|
||||
.call_tool(tool.to_string(), arguments, meta, client.tool_timeout)
|
||||
.await
|
||||
.with_context(|| format!("tool call failed for `{server}/{tool}`"))?;
|
||||
|
||||
let content = result
|
||||
.content
|
||||
.into_iter()
|
||||
.map(|content| {
|
||||
serde_json::to_value(content)
|
||||
.unwrap_or_else(|_| serde_json::Value::String("<content>".to_string()))
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(CallToolResult {
|
||||
content,
|
||||
structured_content: result.structured_content,
|
||||
is_error: result.is_error,
|
||||
meta: result.meta.and_then(|meta| serde_json::to_value(meta).ok()),
|
||||
})
|
||||
}
|
||||
|
||||
async fn read_resource_with_client(
|
||||
managed: &ManagedClient,
|
||||
server: &str,
|
||||
params: ReadResourceRequestParams,
|
||||
) -> Result<ReadResourceResult> {
|
||||
let uri = params.uri.clone();
|
||||
managed
|
||||
.client
|
||||
.read_resource(params, managed.tool_timeout)
|
||||
.await
|
||||
.with_context(|| format!("resources/read failed for `{server}` ({uri})"))
|
||||
}
|
||||
|
||||
impl Drop for McpConnectionManager {
|
||||
fn drop(&mut self) {
|
||||
self.startup_cancellation_token.cancel();
|
||||
|
||||
Reference in New Issue
Block a user