Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(mcp): make list_tools refusals name what the caller can do (#6313)

M3gA-Mindc9431950d6f1
diff --git a/crates/openhuman-core/src/mcp/registry/ops.rs b/crates/openhuman-core/src/mcp/registry/ops.rs
index d97df2d4b4..bbb1f60e97 100644
--- a/crates/openhuman-core/src/mcp/registry/ops.rs
+++ b/crates/openhuman-core/src/mcp/registry/ops.rs
@@ -410,25 +410,127 @@ pub async fn mcp_clients_status(config: &Config) -> Result<Outcome<Value>, Strin
// ── list_tools ───────────────────────────────────────────────────────────────
+/// Who is listing tools, which decides the remedy a refusal names.
+///
+/// The RPC and the agent tool share one implementation but not one surface: a
+/// settings-UI caller can call `mcp_clients_connect`, a model can only call the
+/// tools on its belt. Naming a method the caller cannot reach is an instruction
+/// it cannot follow (#6313).
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum Caller {
+ /// The `openhuman.mcp_clients_*` RPC surface (settings UI, CLI).
+ Rpc,
+ /// The model, through the `mcp_registry_*` tools on its belt.
+ Agent,
+}
+
+impl Caller {
+ fn connect(self) -> &'static str {
+ match self {
+ Self::Rpc => "mcp_clients_connect",
+ Self::Agent => "mcp_registry_connect",
+ }
+ }
+
+ fn status(self) -> &'static str {
+ match self {
+ Self::Rpc => "mcp_clients_status",
+ Self::Agent => "mcp_registry_status",
+ }
+ }
+}
+
+/// What a `NotConnected` answer for `requested` means, read against every install.
+#[derive(Debug)]
+pub(crate) enum NotConnected {
+ /// `requested` is a registry qualified name installed as this one server:
+ /// list that server's tools instead.
+ Resolved(String),
+ /// A refusal that names what is actually wrong.
+ Refused(String),
+}
+
+/// `tinymcp` answers `NotConnected` for any id without a live connection,
+/// including one that was never installed and a registry qualified name passed
+/// where an install's `server_id` belongs. "Not connected" is true of that
+/// string and false of the server, so read it against the installs.
+pub(crate) fn explain_not_connected(
+ requested: &str,
+ installs: &[tinymcp::ConnStatus],
+ caller: Caller,
+) -> NotConnected {
+ if let Some(install) = installs.iter().find(|s| s.server_id == requested) {
+ return NotConnected::Refused(not_connected_message(install, caller));
+ }
+ let named: Vec<&str> = installs
+ .iter()
+ .filter(|s| s.qualified_name == requested)
+ .map(|s| s.server_id.as_str())
+ .collect();
+ match named.as_slice() {
+ [server_id] => NotConnected::Resolved((*server_id).to_string()),
+ [] => NotConnected::Refused(format!(
+ "no installed MCP server has server_id={requested}; server_id is the install's \
+ identifier, not its registry name — call {} to list installed servers",
+ caller.status()
+ )),
+ several => NotConnected::Refused(format!(
+ "{requested} is a registry name installed as several servers; pass one server_id: {}",
+ several.join(", ")
+ )),
+ }
+}
+
+/// An installed server with no live connection: say where it stands and how to connect it.
+fn not_connected_message(install: &tinymcp::ConnStatus, caller: Caller) -> String {
+ let mut message = format!(
+ "server_id={} is {}; connect it first via {}",
+ install.server_id,
+ install.status.as_str(),
+ caller.connect()
+ );
+ if let Some(error) = &install.last_error {
+ message.push_str(&format!(" (last error: {error})"));
+ }
+ message
+}
+
pub async fn mcp_clients_list_tools(
config: &Config,
server_id: String,
+ caller: Caller,
) -> Result<Outcome<Value>, String> {
- let server_id = require(&server_id, "server_id")?;
-
- /// What a caller has to do about it either way.
- fn connect_first(server_id: &str) -> String {
- format!("server_id={server_id} is not connected; connect it first via mcp_clients_connect")
- }
-
- let tools = resolve(config)?
- .dynamic()
- .list_tools(&server_id)
- .await
- .map_err(|error| {
+ let mut server_id = require(&server_id, "server_id")?;
+ let registry = resolve(config)?;
+ let registry = registry.dynamic();
+
+ let tools = match registry.list_tools(&server_id).await {
+ Ok(tools) => tools,
+ Err(tinymcp::Error::NotConnected { .. }) => {
+ let installs = registry.status().await.map_err(|error| error.to_string())?;
+ match explain_not_connected(&server_id, &installs, caller) {
+ NotConnected::Resolved(resolved) => {
+ tracing::debug!(
+ "[mcp-client] list_tools: {server_id} resolved to server_id={resolved}"
+ );
+ // `resolved` came from `installs`, so the lookup always finds it.
+ let tools = registry.list_tools(&resolved).await.map_err(|error| {
+ installs
+ .iter()
+ .find(|s| s.server_id == resolved)
+ .map_or_else(|| error.to_string(), |i| not_connected_message(i, caller))
+ })?;
+ server_id = resolved;
+ tools
+ }
+ NotConnected::Refused(message) => return Err(message),
+ }
+ }
+ Err(error) => {
tracing::debug!("[mcp-client] list_tools ({server_id}) failed: {error}");
- connect_first(&server_id)
- })?;
+ return Err(error.to_string());
+ }
+ };
let tools = super::tools_safe_for_agent(&server_id, tools);
let count = tools.len();
diff --git a/crates/openhuman-core/src/mcp/registry/ops_tests.rs b/crates/openhuman-core/src/mcp/registry/ops_tests.rs
index 292922dde3..eed37622a6 100644
--- a/crates/openhuman-core/src/mcp/registry/ops_tests.rs
+++ b/crates/openhuman-core/src/mcp/registry/ops_tests.rs
@@ -1,8 +1,9 @@
//! Unit tests for the MCP clients RPC handlers.
//!
-//! What is testable without a running service is the blank-identifier guard.
-//! The operations themselves are covered in `tinymcp`; what this layer adds is
-//! the RPC shape, which the end-to-end suites exercise against a live process.
+//! The operations themselves are covered in `tinymcp`. What this layer adds is
+//! the RPC shape, which the end-to-end suites exercise against a live process,
+//! and how a refusal reads to the caller, which a host over a temp workspace
+//! can pin without connecting anything.
use super::*;
@@ -17,3 +18,110 @@ fn a_blank_identifier_is_refused_with_the_field_name() {
fn an_identifier_is_trimmed_before_it_is_used() {
assert_eq!(require(" srv-1 ", "server_id").unwrap(), "srv-1");
}
+
+// ── list_tools refusals (#6313) ─────────────────────────────────────────────
+//
+// A real host over a temp workspace. `apply_config_doc` writes the install
+// store and dials nothing, so an installed server stays disconnected.
+
+const QUALIFIED: &str = "io.github.ryudi84/uuid";
+
+fn temp_config(workspace: &tempfile::TempDir) -> Config {
+ Config {
+ workspace_dir: workspace.path().join("workspace"),
+ action_dir: workspace.path().join("workspace"),
+ config_path: workspace.path().join("config.toml"),
+ ..Default::default()
+ }
+}
+
+/// Installs one stdio server under [`QUALIFIED`] and returns its `server_id`.
+async fn install_one(config: &Config) -> String {
+ let registry = resolve(config).unwrap();
+ let doc = json!({ "mcpServers": { QUALIFIED: { "command": "uuid-mcp" } } });
+ registry.dynamic().apply_config_doc(&doc).await.unwrap();
+ let installs = registry.dynamic().status().await.unwrap();
+ assert_eq!(installs.len(), 1, "{installs:?}");
+ assert_ne!(
+ installs[0].server_id, QUALIFIED,
+ "fixture: the install's id must differ from its registry name"
+ );
+ installs[0].server_id.clone()
+}
+
+#[tokio::test]
+async fn agent_refusal_names_the_connect_tool_on_its_belt() {
+ let workspace = tempfile::tempdir().unwrap();
+ let config = temp_config(&workspace);
+ let server_id = install_one(&config).await;
+
+ let error = mcp_clients_list_tools(&config, server_id.clone(), Caller::Agent)
+ .await
+ .expect_err("a disconnected server has no tools to list");
+ assert!(error.contains("mcp_registry_connect"), "{error}");
+ assert!(!error.contains("mcp_clients_connect"), "{error}");
+ assert!(error.contains(&server_id), "{error}");
+}
+
+#[tokio::test]
+async fn rpc_refusal_still_names_the_rpc_method() {
+ let workspace = tempfile::tempdir().unwrap();
+ let config = temp_config(&workspace);
+ let server_id = install_one(&config).await;
+
+ let error = mcp_clients_list_tools(&config, server_id, Caller::Rpc)
+ .await
+ .expect_err("a disconnected server has no tools to list");
+ assert!(error.contains("mcp_clients_connect"), "{error}");
+ assert!(!error.contains("mcp_registry_connect"), "{error}");
+}
+
+#[tokio::test]
+async fn a_qualified_name_resolves_to_its_install() {
+ let workspace = tempfile::tempdir().unwrap();
+ let config = temp_config(&workspace);
+ let server_id = install_one(&config).await;
+
+ // Resolved, then refused for the install it names — in terms of that
+ // install's server_id, not the registry name the caller passed.
+ let error = mcp_clients_list_tools(&config, QUALIFIED.to_string(), Caller::Agent)
+ .await
+ .expect_err("the resolved server is not connected");
+ assert!(
+ error.starts_with(&format!("server_id={server_id} is disconnected")),
+ "{error}"
+ );
+}
+
+#[tokio::test]
+async fn an_uninstalled_id_is_not_reported_as_not_connected() {
+ let workspace = tempfile::tempdir().unwrap();
+ let config = temp_config(&workspace);
+ install_one(&config).await;
+
+ let error = mcp_clients_list_tools(&config, "io.github.other/absent".into(), Caller::Agent)
+ .await
+ .expect_err("nothing is installed under that name");
+ assert!(!error.contains("not connected"), "{error}");
+ assert!(error.contains("no installed MCP server"), "{error}");
+ assert!(error.contains("mcp_registry_status"), "{error}");
+}
+
+#[test]
+fn a_registry_name_installed_twice_is_refused_with_both_ids() {
+ let install = |server_id: &str| tinymcp::ConnStatus {
+ server_id: server_id.into(),
+ qualified_name: QUALIFIED.into(),
+ display_name: "uuid".into(),
+ status: tinymcp::ServerStatus::Disconnected,
+ tool_count: 0,
+ last_error: None,
+ auth_hint: None,
+ };
+ let installs = [install("srv-a"), install("srv-b")];
+ let NotConnected::Refused(message) = explain_not_connected(QUALIFIED, &installs, Caller::Agent)
+ else {
+ panic!("an ambiguous name must not resolve");
+ };
+ assert!(message.contains("srv-a, srv-b"), "{message}");
+}
diff --git a/crates/openhuman-core/src/mcp/registry/schemas/handlers.rs b/crates/openhuman-core/src/mcp/registry/schemas/handlers.rs
index 882330c210..1eaa69f345 100644
--- a/crates/openhuman-core/src/mcp/registry/schemas/handlers.rs
+++ b/crates/openhuman-core/src/mcp/registry/schemas/handlers.rs
@@ -116,7 +116,14 @@ pub(super) fn handle_list_tools(params: Map<String, Value>) -> ControllerFuture
Box::pin(async move {
let config = config_rpc::load_config_with_timeout().await?;
let server_id = read_required::<String>(&params, "server_id")?;
- to_json(crate::mcp::registry::ops::mcp_clients_list_tools(&config, server_id).await?)
+ to_json(
+ crate::mcp::registry::ops::mcp_clients_list_tools(
+ &config,
+ server_id,
+ crate::mcp::registry::ops::Caller::Rpc,
+ )
+ .await?,
+ )
})
}
diff --git a/crates/openhuman-core/src/mcp/registry/tools.rs b/crates/openhuman-core/src/mcp/registry/tools.rs
index 5658af2cfb..ba2772dabb 100644
--- a/crates/openhuman-core/src/mcp/registry/tools.rs
+++ b/crates/openhuman-core/src/mcp/registry/tools.rs
@@ -206,7 +206,7 @@ impl Tool for McpRegistryListToolsTool {
async fn execute(&self, args: serde_json::Value) -> anyhow::Result<ToolResult> {
let sid = req_str(&args, "server_id")?;
emit!(
- ops::mcp_clients_list_tools(&self.config, sid).await,
+ ops::mcp_clients_list_tools(&self.config, sid, ops::Caller::Agent).await,
"mcp_registry_list_tools"
)
}
diff --git a/crates/openhuman-core/src/mcp/registry/tools_tests.rs b/crates/openhuman-core/src/mcp/registry/tools_tests.rs
index c249198e72..bbd524388f 100644
--- a/crates/openhuman-core/src/mcp/registry/tools_tests.rs
+++ b/crates/openhuman-core/src/mcp/registry/tools_tests.rs
@@ -60,16 +60,15 @@ async fn list_tools_requires_server_id() {
#[tokio::test]
async fn list_tools_errors_for_unconnected_server() {
- // A server_id that is not in the live connection map surfaces a
- // "connect first" hint rather than an empty success.
+ // A server_id that is not installed errors rather than succeeding empty,
+ // and says so rather than claiming it is merely not connected (#6313).
let err = McpRegistryListToolsTool::new(cfg())
.execute(json!({ "server_id": "definitely-not-connected-uuid" }))
.await
.expect_err("unconnected server must error");
- assert!(
- err.to_string().contains("not connected"),
- "expected connect-first hint, got: {err}"
- );
+ let err = err.to_string();
+ assert!(err.contains("no installed MCP server"), "{err}");
+ assert!(err.contains("mcp_registry_status"), "{err}");
}
/// The identity a registry tool presents to the model, captured from the
diff --git a/tests/domain_modules_e2e.rs b/tests/domain_modules_e2e.rs
index 2d965107fc..f8c040c2b8 100644
--- a/tests/domain_modules_e2e.rs
+++ b/tests/domain_modules_e2e.rs
@@ -980,8 +980,9 @@ async fn mcp_clients_read_paths_validate_before_reaching_outward() {
);
}
- // `list_tools` against a server that was never connected must say so, and
- // say what to do. This is the message a user sees most often.
+ // `list_tools` against a server that was never installed must say so, and
+ // say what to do. It is not "not connected": connecting something that is
+ // not installed is advice nobody can follow (#6313).
let disconnected = rpc(
&harness.rpc_base,
45_110,
@@ -995,12 +996,12 @@ async fn mcp_clients_read_paths_validate_before_reaching_outward() {
.unwrap_or_default()
.to_string();
assert!(
- message.contains("not connected"),
- "list_tools on a disconnected server must say it is not connected, got: {message}"
+ message.contains("no installed MCP server"),
+ "list_tools on an uninstalled server must say it is not installed, got: {message}"
);
assert!(
- message.contains("mcp_clients_connect"),
- "the refusal must name the remedy so a caller can act on it, got: {message}"
+ message.contains("mcp_clients_status"),
+ "the refusal must name the RPC remedy so a caller can act on it, got: {message}"
);
harness.join.abort();