Skip to main content

agentmux_srv/server/app_api/
mcp_actor.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Who may add, change, bind, unbind or probe an MCP server whose command would start on this computer.
5//!
6//! A catalog (global) server is usually a host command, and a probe starts a stored server's command on this
7//! computer whoever asked. A container agent starts only what runs in its container, so these commands refuse
8//! one. A container agent's own private row (`mcp.upsert`, `mcp.delete`, `mcp.bind`, `mcp.unbind`: always its
9//! own, by `check_s1`) starts in its container and is not refused. Every call that asks is recorded with the
10//! actor it came from; a host agent, the window and a connection that proved nothing are not refused. A
11//! connection registered as an agent whose runtime can't be told (the id doesn't resolve, is ambiguous, has no
12//! definition, or the store can't be read) is refused like a container agent's.
13
14use crate::backend::rpc::actor::{actor_of, Actor};
15use crate::backend::rpc_types::RpcContext;
16use crate::backend::storage::store::Store;
17
18/// What a container agent is told when one of these commands is refused it.
19pub(crate) const REFUSAL: &str = "CONTAINER_AGENT: this would start a program on this computer, which a container agent can't ask for. Ask a host agent, or have the person add it in Connectors.";
20
21/// What an agent whose runtime can't be told is refused with.
22pub(crate) const UNKNOWN_REFUSAL: &str = "CONTAINER_AGENT: couldn't tell where this agent runs, so this would not be a program started for it on this computer. Ask a host agent, or have the person add it in Connectors.";
23
24/// Whether the agent connected as `id` runs in a container: `None` when that can't be told.
25fn runs_in_a_container(store: &Store, id: &str) -> Option<bool> {
26    let def_id = crate::backend::agent_resolve::resolve_agent_id(store, id).ok()?;
27    let def = store.agent_def_get(&def_id).ok()??;
28    Some(def.runtime().is_container())
29}
30
31/// Whether a catalog server starts only in containers: it reaches container agents and no host agent by
32/// reach (`McpServerReach`), and every agent it is bound to runs in a container. A server bound to no one, to
33/// an agent whose runtime can't be told, or that reaches every agent, is not.
34pub(crate) fn catalog_server_runs_only_in_containers(mstore: &Store, identity_store: &Store, mcp_id: &str) -> bool {
35    let reach = identity_store.mcp_server_reach_all().ok().and_then(|all| all.get(mcp_id).copied()).unwrap_or_default();
36    if reach.all_agents || !reach.in_containers {
37        return false;
38    }
39    let Ok(bound) = mstore.mcp_server_bound_agent_ids(mcp_id) else {
40        return false;
41    };
42    !bound.is_empty() && bound.iter().all(|id| runs_in_a_container(mstore, id) == Some(true))
43}
44
45/// `Err` with [`REFUSAL`] when `ctx` is a connection of a container agent, and with [`UNKNOWN_REFUSAL`] when it is
46/// an agent's whose runtime can't be told; otherwise records the actor and goes on.
47pub(crate) fn refuse_container_agent(store: &Store, ctx: &RpcContext, command: &str) -> Result<(), String> {
48    let actor = actor_of(ctx);
49    if let Actor::Agent(id) = &actor {
50        let refusal = match runs_in_a_container(store, id) {
51            Some(false) => None,
52            Some(true) => Some(REFUSAL),
53            None => Some(UNKNOWN_REFUSAL),
54        };
55        if let Some(why) = refusal {
56            tracing::warn!(command, actor = %actor.label(), "MCP server command refused: it would start on this computer");
57            return Err(why.to_string());
58        }
59    }
60    tracing::info!(command, actor = %actor.label(), "MCP server command asked for");
61    Ok(())
62}