Skip to content

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

feat(mcp): add tool dispatch with parameter validation and specs

Steven Enamakelba4a9ed0546d
diff --git a/crates/openhuman-core/src/mcp/server/tools/dispatch.rs b/crates/openhuman-core/src/mcp/server/tools/dispatch.rs
index d4b360c5e9..8b7d4467dd 100644
--- a/crates/openhuman-core/src/mcp/server/tools/dispatch.rs
+++ b/crates/openhuman-core/src/mcp/server/tools/dispatch.rs
@@ -9,41 +9,35 @@ use crate::security::{SecurityPolicy, ToolOperation};
use super::super::write_dispatch;
use super::params::{build_rpc_params, validate_controller_params};
-use super::specs::{
- base_tool_specs, list_tools_result_for_config, list_tools_result_from_specs,
- tool_specs_for_config,
-};
-use super::types::ToolCallError;
+use super::specs::{base_tool_specs, tool_specs_for_loaded_config};
+use super::types::McpToolSpec;
+use tinymcp::ToolCallError;
-pub async fn list_tools_result() -> Value {
+/// The tools `tools/list` advertises and `tools/call` accepts: every tool the
+/// loaded config can serve, or only the config-independent base set when the
+/// config will not load. `method` names the MCP method for the log line.
+async fn available_tool_specs(method: &str) -> Vec<McpToolSpec> {
match config_rpc::load_config_with_timeout().await {
- Ok(config) => list_tools_result_for_config(&config),
+ Ok(config) => tool_specs_for_loaded_config(&config),
Err(err) => {
log::warn!(
- "[mcp_server] tools/list config load failed; omitting config-gated tools: {err}"
+ "[mcp_server] {method} config load failed; omitting config-gated tools: {err}"
);
- list_tools_result_from_specs(base_tool_specs())
+ base_tool_specs()
}
}
}
+pub async fn list_tool_specs() -> Vec<McpToolSpec> {
+ available_tool_specs("tools/list").await
+}
+
pub async fn call_tool(
name: &str,
arguments: Value,
client_info: &str,
) -> Result<Value, ToolCallError> {
- let specs = match config_rpc::load_config_with_timeout().await {
- Ok(config) => tool_specs_for_config(
- &config,
- crate::search::providers::backend_credential_available(&config),
- ),
- Err(err) => {
- log::warn!(
- "[mcp_server] tools/call config load failed; omitting config-gated tools: {err}"
- );
- base_tool_specs()
- }
- };
+ let specs = available_tool_specs("tools/call").await;
let spec = specs
.into_iter()
.find(|tool| tool.name == name)
@@ -320,7 +314,7 @@ async fn list_subagents() -> Result<Value, ToolCallError> {
async fn run_subagent_tool(params: &Map<String, Value>) -> Result<Value, ToolCallError> {
use super::super::subagent_depth;
- use super::params::required_non_empty_string;
+ use tinymcp::server::args::required_non_empty_string;
let agent_id = required_non_empty_string(params, "agent_id")?;
let prompt = required_non_empty_string(params, "prompt")?;
diff --git a/crates/openhuman-core/src/mcp/server/tools/mod.rs b/crates/openhuman-core/src/mcp/server/tools/mod.rs
index 5e65a02d51..1bec7a5a88 100644
--- a/crates/openhuman-core/src/mcp/server/tools/mod.rs
+++ b/crates/openhuman-core/src/mcp/server/tools/mod.rs
@@ -1,10 +1,14 @@
//! MCP tool catalog, parameter validation, and dispatch logic.
//!
//! Split into focused sub-modules:
-//! - `types` — `McpToolSpec`, `ToolCallError`, shared constants
-//! - `specs` — tool spec builders and schema helpers
-//! - `params` — argument parsing and RPC param construction
-//! - `dispatch` — `call_tool`, `list_tools_result`, agent/subagent handlers
+//! - `types` — `McpToolSpec` and the argument/limit constants
+//! - `specs` — tool spec builders, schema helpers, and the conversion to
+//! `tinymcp::ServerToolSpec`
+//! - `params` — OpenHuman's per-tool argument policy and RPC param
+//! construction, over `tinymcp::server::args`' generic validators
+//! - `dispatch` — `call_tool`, `list_tool_specs`, agent/subagent handlers
+//!
+//! `ToolCallError` is `tinymcp`'s, re-exported here for the sibling modules.
//! ## Compile-time gate (`mcp` feature)
//!
@@ -25,11 +29,11 @@ mod types;
// Public API consumed by the rest of `mcp::server`
#[cfg(feature = "mcp")]
-pub use dispatch::{call_tool, list_tools_result, tool_error, tool_success};
+pub use dispatch::{call_tool, list_tool_specs, tool_error, tool_success};
#[cfg(feature = "mcp")]
-pub use specs::tool_specs;
+pub use specs::{server_tool_spec, tool_specs};
#[cfg(feature = "mcp")]
-pub use types::ToolCallError;
+pub use tinymcp::ToolCallError;
pub use types::McpToolSpec;
diff --git a/crates/openhuman-core/src/mcp/server/tools/specs.rs b/crates/openhuman-core/src/mcp/server/tools/specs.rs
index 1f359371c2..122092da47 100644
--- a/crates/openhuman-core/src/mcp/server/tools/specs.rs
+++ b/crates/openhuman-core/src/mcp/server/tools/specs.rs
@@ -1,4 +1,5 @@
use serde_json::{json, Value};
+use tinymcp::ServerToolSpec;
use super::types::{McpToolSpec, DEFAULT_LIMIT, MAX_LIMIT, SEARCH_MAX_RESULTS};
@@ -281,11 +282,13 @@ pub fn searxng_tool_spec() -> McpToolSpec {
}
}
-pub fn list_tools_result_for_config(config: &crate::config::Config) -> Value {
- list_tools_result_from_specs(tool_specs_for_config(
+/// Every tool this config can serve: the base set plus the search tools with
+/// a usable provider.
+pub fn tool_specs_for_loaded_config(config: &crate::config::Config) -> Vec<McpToolSpec> {
+ tool_specs_for_config(
config,
crate::search::providers::backend_credential_available(config),
- ))
+ )
}
/// Base tools plus the search tools this config can serve.
@@ -310,20 +313,11 @@ pub fn tool_specs_for_config(
specs
}
-pub fn list_tools_result_from_specs(specs: Vec<McpToolSpec>) -> Value {
- let tools = specs
- .into_iter()
- .map(|tool| {
- json!({
- "name": tool.name,
- "title": tool.title,
- "description": tool.description,
- "inputSchema": tool.input_schema,
- "annotations": tool.annotations,
- })
- })
- .collect::<Vec<_>>();
- json!({ "tools": tools })
+/// A catalog entry as `tinymcp` advertises it in `tools/list`.
+pub fn server_tool_spec(spec: &McpToolSpec) -> ServerToolSpec {
+ ServerToolSpec::new(spec.name, spec.description, spec.input_schema.clone())
+ .with_title(spec.title)
+ .with_annotations(spec.annotations.clone())
}
// ── Schema builder helpers ────────────────────────────────────────────────────
diff --git a/crates/openhuman-core/src/mcp/server/tools_tests.rs b/crates/openhuman-core/src/mcp/server/tools_tests.rs
index 28479a9fc3..1395cce4a8 100644
--- a/crates/openhuman-core/src/mcp/server/tools_tests.rs
+++ b/crates/openhuman-core/src/mcp/server/tools_tests.rs
@@ -1,6 +1,16 @@
-use super::specs::list_tools_result_for_config;
+use super::specs::tool_specs_for_loaded_config;
use super::*;
+/// The `tools/list` result this config advertises, rendered through the same
+/// `tinymcp` conversion the served catalog uses.
+fn list_tools_result_for_config(config: &crate::config::Config) -> Value {
+ let tools = tool_specs_for_loaded_config(config)
+ .iter()
+ .map(|spec| server_tool_spec(spec).to_json())
+ .collect::<Vec<_>>();
+ json!({ "tools": tools })
+}
+
#[path = "tools_tests_params_and_specs_tests.rs"]
mod params_and_specs_tests;
#[path = "tools_tests_tag_and_dispatch_tests.rs"]
diff --git a/crates/openhuman-core/src/mcp/server/tools_tests_params_and_specs_tests.rs b/crates/openhuman-core/src/mcp/server/tools_tests_params_and_specs_tests.rs
index c919241dfa..6b0ccab343 100644
--- a/crates/openhuman-core/src/mcp/server/tools_tests_params_and_specs_tests.rs
+++ b/crates/openhuman-core/src/mcp/server/tools_tests_params_and_specs_tests.rs
@@ -332,26 +332,6 @@ fn memory_search_accepts_k_at_max() {
assert_eq!(params["k"], MAX_LIMIT);
}
-#[test]
-fn tool_call_error_invalid_params_maps_to_jsonrpc_invalid_params() {
- let err = ToolCallError::InvalidParams("missing query".to_string());
- assert_eq!(err.code(), -32602);
- assert_eq!(err.jsonrpc_message(), "Invalid params");
- assert_eq!(err.message(), "missing query");
-}
-
-#[test]
-fn tool_call_error_internal_maps_to_jsonrpc_internal_error() {
- // Server-side failures (config load, missing resources) must surface
- // as `-32603 Internal error`, not `-32602 Invalid params`, so the MCP
- // client doesn't mislead the user / LLM into retrying with different
- // arguments.
- let err = ToolCallError::Internal("disk read failed".to_string());
- assert_eq!(err.code(), -32603);
- assert_eq!(err.jsonrpc_message(), "Internal error");
- assert_eq!(err.message(), "disk read failed");
-}
-
#[test]
fn memory_recall_requires_query() {
let err = build_rpc_params("memory.recall", json!({})).expect_err("must reject");