Skip to content

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

feat(mcp): add write dispatch for server tools

Steven Enamakel151efa6d462a
diff --git a/crates/openhuman-core/src/mcp/server/handler.rs b/crates/openhuman-core/src/mcp/server/handler.rs
index 8c8484cbed..0b9539c554 100644
--- a/crates/openhuman-core/src/mcp/server/handler.rs
+++ b/crates/openhuman-core/src/mcp/server/handler.rs
@@ -7,15 +7,17 @@
use serde_json::{Map, Value};
use tinymcp_bus::{ServerHostCall, ServerHostReply, ToolCallError};
+use crate::core::runtime::DispatchCtx;
+
use super::{resources, subagent_depth, tools};
const INSTRUCTIONS: &str = "OpenHuman MCP exposes first-level core integration: inspect the live tool catalog with core.list_tools or core.tool_instructions, inspect subagents with agent.list_subagents, run a standalone subagent with agent.run_subagent, use web_search or web_answer for live web lookups (and searxng_search when self-hosted search is enabled), and use memory.recall (answer with citations), memory.fetch or memory.list to read memory, and memory.learn or memory.forget to change it.";
/// Apply the host-owned part of a validated TinyMCP server callback.
-pub(super) async fn dispatch(call: ServerHostCall) -> ServerHostReply {
+pub(super) async fn dispatch(ctx: DispatchCtx, call: ServerHostCall) -> ServerHostReply {
match call {
ServerHostCall::ListTools { .. } => {
- let specs = tools::list_tool_specs()
+ let specs = tools::list_tool_specs(&ctx)
.await
.iter()
.map(tools::server_tool_spec)
@@ -42,7 +44,7 @@ pub(super) async fn dispatch(call: ServerHostCall) -> ServerHostReply {
);
let result = subagent_depth::scope(
depth,
- tools::call_tool(&name, Value::Object(arguments), &source_type),
+ tools::call_tool(&ctx, &name, Value::Object(arguments), &source_type),
)
.await;
result.map_or_else(reply_error, |value| ServerHostReply::Success { value })
diff --git a/crates/openhuman-core/src/mcp/server/module.rs b/crates/openhuman-core/src/mcp/server/module.rs
index a6fc9db38b..8728f22585 100644
--- a/crates/openhuman-core/src/mcp/server/module.rs
+++ b/crates/openhuman-core/src/mcp/server/module.rs
@@ -210,7 +210,7 @@ impl ServerSession {
}
async fn open_with_port(port: Arc<dyn ServerPort>) -> Result<Self, ServerError> {
- Self::open_with_port_and_dispatch(port, |call| Box::pin(handler::dispatch(call))).await
+ Self::open_with_port_and_dispatch(port, host_dispatch()).await
}
pub(super) async fn open_with_port_and_dispatch<F, Fut>(
@@ -270,7 +270,7 @@ impl ServerSession {
.map_err(ServerError::Module)?;
Ok(Self {
port,
- callback_dispatch: Arc::new(|call| Box::pin(handler::dispatch(call))),
+ callback_dispatch: Arc::new(host_dispatch()),
id,
next_operation: AtomicU64::new(1),
operation: tokio::sync::Mutex::new(()),
diff --git a/crates/openhuman-core/src/mcp/server/tools/params.rs b/crates/openhuman-core/src/mcp/server/tools/params.rs
index b65f01f78d..81aba27fcb 100644
--- a/crates/openhuman-core/src/mcp/server/tools/params.rs
+++ b/crates/openhuman-core/src/mcp/server/tools/params.rs
@@ -142,6 +142,7 @@ pub(crate) fn build_rpc_params_for_test(
}
async fn validate_tool_arguments(
+ ctx: &DispatchCtx,
tool_name: &str,
arguments: Value,
) -> Result<Map<String, Value>, ToolCallError> {
diff --git a/crates/openhuman-core/src/mcp/server/write_dispatch.rs b/crates/openhuman-core/src/mcp/server/write_dispatch.rs
index ce45f3f1f9..37d1abb228 100644
--- a/crates/openhuman-core/src/mcp/server/write_dispatch.rs
+++ b/crates/openhuman-core/src/mcp/server/write_dispatch.rs
@@ -7,7 +7,6 @@
use crate::core::runtime::DispatchCtx;
use serde_json::{json, Map, Value};
-use crate::config::rpc as config_rpc;
use crate::config::Config;
use crate::core::all;
use crate::mcp::audit::{self, NewMcpWriteRecord};
@@ -15,8 +14,11 @@ use crate::security::{SecurityPolicy, ToolOperation};
use super::tools::{tool_error, tool_success, ToolCallError};
-pub(super) async fn load_write_config(tool_name: &str) -> Result<Config, ToolCallError> {
- match config_rpc::load_config_with_timeout().await {
+pub(super) async fn load_write_config(
+ ctx: &DispatchCtx,
+ tool_name: &str,
+) -> Result<Config, ToolCallError> {
+ match ctx.load_config().await {
Ok(config) => Ok(config),
Err(err) => {
log::warn!(
@@ -51,6 +53,7 @@ pub(super) fn enforce_write_policy_for_config(
/// Dispatch a write tool to its underlying RPC method with provenance and
/// audit logging.
pub(super) async fn dispatch_write_tool(
+ ctx: &DispatchCtx,
tool_name: &str,
rpc_method: &str,
params: &Map<String, Value>,
@@ -72,7 +75,7 @@ pub(super) async fn dispatch_write_tool(
"[mcp_server] write dispatch invoking rpc"
);
- match all::try_invoke_registered_rpc(DispatchCtx::ambient(), rpc_method, params.clone()).await {
+ match all::try_invoke_registered_rpc(ctx.clone(), rpc_method, params.clone()).await {
Some(Ok(value)) => {
let item_id = extract_item_id(&value);
audit_write(
@@ -191,6 +194,7 @@ pub(super) fn audit_write_rejection(
}
pub(super) fn audit_write_rejection_without_config(
+ ctx: &DispatchCtx,
tool_name: &str,
audit_arguments: &Value,
client_info: &str,
@@ -207,10 +211,11 @@ pub(super) fn audit_write_rejection_without_config(
let client_info = client_info.to_string();
let error_message = error_message.to_string();
let args_summary = summarize_write_args(&tool_name, audit_arguments);
+ let ctx = ctx.clone();
match tokio::runtime::Handle::try_current() {
Ok(handle) => {
std::mem::drop(handle.spawn(async move {
- match config_rpc::load_config_with_timeout().await {
+ match ctx.load_config().await {
Ok(config) => audit_write(
&config,
NewMcpWriteRecord {