Skip to content

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

20.0 KB
Raw Blame
use std::collections::{BTreeMap, BTreeSet};

use serde_json::{json, Map, Value};

use crate::config::Config;
use crate::core::all;
use crate::core::runtime::DispatchCtx;
use crate::core::Outcome;
use crate::core::{ControllerSchema, FieldSchema, TypeSchema};
use crate::mcp::audit::McpWriteListQuery;
use crate::mcp::server::McpToolSpec;

use super::providers::capability_provider_diagnostics;
use tinytools::{
    McpAllowlistDiagnostics, McpServerAllowlistSummary, McpWriteAuditHealth, ToolPolicyDiagnostics,
    ToolPolicyPosture, ToolRegistryEntry, ToolRegistryHealth, ToolRegistryList,
    ToolRegistryTransport,
};

const REGISTRY_ENTRY_VERSION: &str = env!("CARGO_PKG_VERSION");
const POLICY_SURFACES: &[&str] = &[
    "security.policy_info",
    "approval.list_pending",
    "approval.list_recent_decisions",
    "approval.decide",
    "tool_registry.list",
    "tool_registry.get",
    "tool_registry.diagnostics",
];

/// Return the current read-only tool registry snapshot.
pub fn list_tools(ctx: &DispatchCtx) -> Outcome<ToolRegistryList> {
    let tools = registry_entries(ctx);
    log::debug!(
        "[tool_registry] list_tools completed entries={}",
        tools.len()
    );
    Outcome::new(ToolRegistryList { tools }, vec![])
}

/// Return redacted diagnostics for policy/tool visibility reviews.
pub async fn diagnostics() -> Result<Outcome<ToolPolicyDiagnostics>, String> {
    log::debug!("[tool_registry] diagnostics loading_config");
    let config = Config::load_or_init().await.map_err(|err| {
        log::warn!("[tool_registry] diagnostics config_load_failed error={err}");
        format!("failed to load config for tool registry diagnostics: {err}")
    })?;
    Ok(diagnostics_for_config(&config).await)
}

/// Return redacted diagnostics using a specific config snapshot.
pub async fn diagnostics_for_config(config: &Config) -> Outcome<ToolPolicyDiagnostics> {
    log::debug!("[tool_registry] diagnostics_for_config start");

    let tools = registry_entries_for_config(config);
    let total_tools = tools.len();
    let enabled_tools = tools.iter().filter(|entry| entry.enabled).count();
    let mcp_stdio_tools = tools
        .iter()
        .filter(|entry| entry.transport == ToolRegistryTransport::McpStdio)
        .count();
    let json_rpc_tools = tools
        .iter()
        .filter(|entry| entry.transport == ToolRegistryTransport::JsonRpc)
        .count();
    let possible_write_surfaces = tools
        .iter()
        .filter(|entry| looks_write_capable(&entry.tool_id))
        .map(|entry| entry.tool_id.clone())
        .collect::<Vec<_>>();
    let policy_surfaces = policy_surface_ids();
    let posture = posture_from_config(config);
    let mcp_allowlists = mcp_allowlists_from_config(config);
    let mcp_write_audit = mcp_write_audit_health(config).await;
    let recent_denials = tinytools::registry::denials::list(25);
    let capability_providers = capability_provider_diagnostics(config);

    log::trace!(
        "[tool_registry] diagnostics_for_config counted total_tools={} enabled_tools={} mcp_stdio_tools={} json_rpc_tools={} possible_write_surfaces={} policy_surfaces={}",
        total_tools,
        enabled_tools,
        mcp_stdio_tools,
        json_rpc_tools,
        possible_write_surfaces.len(),
        policy_surfaces.len()
    );

    let diagnostics = ToolPolicyDiagnostics {
        total_tools,
        enabled_tools,
        mcp_stdio_tools,
        json_rpc_tools,
        possible_write_surfaces,
        policy_surfaces,
        posture,
        mcp_allowlists,
        mcp_write_audit,
        recent_denials,
        capability_providers,
    };
    log::debug!(
        "[tool_registry] diagnostics_for_config completed total_tools={} enabled_tools={} mcp_stdio_tools={} json_rpc_tools={} possible_write_surfaces={} policy_surfaces={} providers_total={} providers_enabled={} providers_trusted={} providers_trusted_enabled={} provider_errors={}",
        diagnostics.total_tools,
        diagnostics.enabled_tools,
        diagnostics.mcp_stdio_tools,
        diagnostics.json_rpc_tools,
        diagnostics.possible_write_surfaces.len(),
        diagnostics.policy_surfaces.len(),
        diagnostics.capability_providers.total_providers,
        diagnostics.capability_providers.enabled_providers,
        diagnostics.capability_providers.trusted_providers,
        diagnostics.capability_providers.trusted_enabled_providers,
        diagnostics.capability_providers.registry_errors.len()
    );
    Outcome::new(diagnostics, vec![])
}

fn posture_from_config(config: &Config) -> ToolPolicyPosture {
    ToolPolicyPosture {
        autonomy_level: format!("{:?}", config.autonomy.level).to_lowercase(),
        workspace_only: config.autonomy.workspace_only,
        max_actions_per_hour: config.autonomy.max_actions_per_hour,
        require_approval_for_medium_risk: config.autonomy.require_approval_for_medium_risk,
        block_high_risk_commands: config.autonomy.block_high_risk_commands,
    }
}

fn mcp_allowlists_from_config(config: &Config) -> McpAllowlistDiagnostics {
    let enabled = config.mcp_client.enabled;
    let server_count = config.mcp_client.servers.len();
    let mut enabled_server_count = 0;
    let mut servers = Vec::new();
    for server in &config.mcp_client.servers {
        if server.enabled {
            enabled_server_count += 1;
        }
        servers.push(McpServerAllowlistSummary {
            name: server.name.clone(),
            enabled: server.enabled,
            allowed_tools_count: server.allowed_tools.len(),
            disallowed_tools_count: server.disallowed_tools.len(),
            has_allowlist: !server.allowed_tools.is_empty(),
            has_denylist: !server.disallowed_tools.is_empty(),
        });
    }
    McpAllowlistDiagnostics {
        enabled,
        server_count,
        enabled_server_count,
        servers,
    }
}

/// How many recent writes the audit log is asked for.
///
/// The store has no count method, so the probe lists and counts. The value is
/// therefore a floor: `recent_rows` saturates here rather than reporting a
/// true total, which is enough for the question this health field answers —
/// is the audit log receiving writes at all.
const RECENT_WRITE_SAMPLE: u64 = tinymcp_bus::MAX_LIST_LIMIT;

/// Whether the write-audit log is recording, and roughly how much.
///
/// Reads through `mcp::audit`, which is where the writer puts rows. It used to
/// query the `mcp_writes` table inside the memory engine's chunk database
/// directly; when the log moved to its own store, that query stayed behind and
/// began counting a table nothing writes any more — so this reported zero
/// writes in the last 24 hours on every install, indefinitely, while the log
/// underneath it was healthy. A diagnostic that cannot fail loudly must at
/// least read the same place the writer wrote.
async fn mcp_write_audit_health(config: &Config) -> McpWriteAuditHealth {
    let since_ms = chrono::Utc::now()
        .timestamp_millis()
        .saturating_sub(24 * 60 * 60 * 1000);
    let query = McpWriteListQuery {
        limit: Some(RECENT_WRITE_SAMPLE),
        offset: None,
        since_ms: u64::try_from(since_ms).ok(),
        ..Default::default()
    };
    let result = crate::mcp::audit::list_writes(config, &query)
        .await
        .map(|writes| writes.len() as u64);

    match result {
        Ok(count) => McpWriteAuditHealth {
            enabled: true,
            recent_rows: Some(count),
            last_error: None,
        },
        Err(err) => McpWriteAuditHealth {
            enabled: true,
            recent_rows: None,
            last_error: Some(err.to_string()),
        },
    }
}

/// Look up one registry entry by stable `tool_id`.
pub fn get_tool(ctx: &DispatchCtx, tool_id: &str) -> Result<Outcome<ToolRegistryEntry>, String> {
    let normalized = tool_id.trim();
    if normalized.is_empty() {
        return Err("tool_id must be a non-empty string".to_string());
    }

    let tool = registry_entries(ctx)
        .into_iter()
        .find(|entry| entry.tool_id == normalized)
        .ok_or_else(|| format!("tool not found in registry: {normalized}"))?;

    log::debug!(
        "[tool_registry] get_tool completed tool_id={} transport={:?}",
        tool.tool_id,
        tool.transport
    );
    Ok(Outcome::new(tool, vec![]))
}

/// Build sorted registry entries from the current MCP and controller metadata.
///
/// This includes:
/// 1. MCP stdio server tools (existing `mcp::server` surface)
/// 2. Controller-backed tools (existing `tools` namespace)
/// 3. Connected MCP client server tools (new `mcp_clients` domain)
///
/// The connected-client tools come from whichever workspace `mcp::init` claimed
/// as the process default. That is right for the shipped app, which opens one;
/// a caller that holds a `Config` should prefer
/// [`registry_entries_for_config`], which names the workspace it means. The
/// connections are read for the context `ctx` the caller was handed.
pub fn registry_entries(ctx: &DispatchCtx) -> Vec<ToolRegistryEntry> {
    build_registry_entries(McpSource::Ctx(ctx))
}

/// The same snapshot, with connected-client tools read from `config`'s
/// workspace rather than the process default.
///
/// The two differ only when more than one workspace is open in a process.
/// `mcp::host::resolve` returns a lone host but `None` once a second exists, so
/// the ambient form silently reports nothing connected in a test binary where
/// several cases each open their own temporary workspace — which is how
/// `tool_registry_entries_include_connected_mcp_client_tools` came to pass
/// alone and fail beside its neighbours.
pub fn registry_entries_for_config(config: &Config) -> Vec<ToolRegistryEntry> {
    build_registry_entries(McpSource::Config(config))
}

/// Where the connected MCP client servers are read from.
#[derive(Clone, Copy)]
enum McpSource<'a> {
    /// The workspace `config` names.
    Config(&'a Config),
    /// The process default host, as seen by the context handed in.
    Ctx(&'a DispatchCtx),
}

/// The shared body. `source` selects the MCP host; everything else is identical.
fn build_registry_entries(source: McpSource<'_>) -> Vec<ToolRegistryEntry> {
    let mut entries = BTreeMap::new();

    for spec in crate::mcp::server::tool_specs() {
        let entry = mcp_tool_entry(spec);
        insert_registry_entry(&mut entries, entry, "mcp_stdio");
    }

    for schema in crate::tools::all_tools_controller_schemas() {
        let entry = controller_tool_entry(&schema);
        insert_registry_entry(&mut entries, entry, "controller");
    }

    // Enumerate tools from all currently-connected MCP client servers.
    // `block_in_place` requires the multi-threaded tokio runtime; fall back
    // silently to an empty list in single-threaded contexts (e.g. unit tests).
    let client_tools = {
        use crate::mcp::registry::connections;
        match tokio::runtime::Handle::try_current() {
            Ok(handle) => {
                // Only use block_in_place when we are on the multi-threaded
                // runtime (kind = MultiThread). The current-thread runtime
                // (kind = CurrentThread) panics on block_in_place.
                if handle.runtime_flavor() == tokio::runtime::RuntimeFlavor::MultiThread {
                    tokio::task::block_in_place(|| {
                        handle.block_on(async {
                            match source {
                                McpSource::Config(config) => {
                                    connections::all_connected_tools_for_config(config).await
                                }
                                McpSource::Ctx(ctx) => connections::all_connected_tools(ctx).await,
                            }
                        })
                    })
                } else {
                    Vec::new()
                }
            }
            Err(_) => Vec::new(),
        }
    };

    for (server_id, _qualified_name_placeholder, tool) in client_tools {
        let tool_id = format!("mcp-client::{server_id}::{}", tool.name);
        let entry = ToolRegistryEntry {
            tool_id: tool_id.clone(),
            name: tool.name.clone(),
            title: title_from_function(&tool.name),
            description: tool.description.unwrap_or_default(),
            version: REGISTRY_ENTRY_VERSION.to_string(),
            transport: ToolRegistryTransport::McpStdio,
            route: json!({
                "protocol": "mcp-client",
                "rpc_method": "openhuman.mcp_clients_tool_call",
                "server_id": server_id,
                "tool_name": tool.name,
            }),
            input_schema: tool.input_schema,
            output_schema: mcp_output_schema(),
            allowed_agents: vec!["*".to_string()],
            tags: tags_for_tool_id(&tool_id, "mcp_client"),
            enabled: true,
            health: ToolRegistryHealth::Available,
        };
        insert_registry_entry(&mut entries, entry, "mcp_client");
    }

    entries.into_values().collect()
}

fn insert_registry_entry(
    entries: &mut BTreeMap<String, ToolRegistryEntry>,
    entry: ToolRegistryEntry,
    source: &str,
) {
    let key = entry.tool_id.clone();
    if entries.contains_key(&key) {
        // Duplicate tool IDs can arrive from external MCP servers that reuse
        // well-known names.  First-write-wins: log and skip the duplicate
        // rather than panicking or silently overwriting in production.
        log::warn!(
            "[tool_registry] duplicate tool_id={} from source={}; skipping",
            key,
            source
        );
        return;
    }
    entries.insert(key, entry);
}

fn mcp_tool_entry(spec: McpToolSpec) -> ToolRegistryEntry {
    let tool_id = spec.name.to_string();
    ToolRegistryEntry {
        tool_id: tool_id.clone(),
        name: spec.name.to_string(),
        title: spec.title.to_string(),
        description: spec.description.to_string(),
        version: REGISTRY_ENTRY_VERSION.to_string(),
        transport: ToolRegistryTransport::McpStdio,
        route: json!({
            "protocol": "mcp",
            "method": "tools/call",
            "tool": spec.name,
            "rpc_method": spec.rpc_method,
        }),
        input_schema: spec.input_schema,
        output_schema: mcp_output_schema(),
        allowed_agents: vec!["*".to_string()],
        tags: tags_for_tool_id(&tool_id, "mcp"),
        enabled: true,
        health: ToolRegistryHealth::Available,
    }
}

fn controller_tool_entry(schema: &ControllerSchema) -> ToolRegistryEntry {
    let tool_id = schema.method_name();
    ToolRegistryEntry {
        tool_id: tool_id.clone(),
        name: tool_id.clone(),
        title: title_from_function(schema.function),
        description: schema.description.to_string(),
        version: REGISTRY_ENTRY_VERSION.to_string(),
        transport: ToolRegistryTransport::JsonRpc,
        route: json!({
            "protocol": "json_rpc",
            "method": all::rpc_method_name(schema),
            "controller": schema.method_name(),
        }),
        input_schema: schema_fields_to_json_schema(&schema.inputs),
        output_schema: schema_fields_to_json_schema(&schema.outputs),
        allowed_agents: vec!["*".to_string()],
        tags: tags_for_tool_id(&tool_id, "controller"),
        enabled: true,
        health: ToolRegistryHealth::Available,
    }
}

fn schema_fields_to_json_schema(fields: &[FieldSchema]) -> Value {
    let mut properties = Map::new();
    let mut required = Vec::new();

    for field in fields {
        properties.insert(field.name.to_string(), field_schema_to_json(field));
        if field.required {
            required.push(Value::String(field.name.to_string()));
        }
    }

    json!({
        "type": "object",
        "properties": properties,
        "required": required,
        "additionalProperties": false,
    })
}

fn field_schema_to_json(field: &FieldSchema) -> Value {
    let mut schema = type_schema_to_json(&field.ty);
    match schema.as_object_mut() {
        Some(object) => {
            object.insert(
                "description".to_string(),
                Value::String(field.comment.to_string()),
            );
        }
        None => {
            schema = json!({
                "description": field.comment,
                "anyOf": [schema],
            });
        }
    }
    schema
}

fn type_schema_to_json(ty: &TypeSchema) -> Value {
    match ty {
        TypeSchema::Bool => json!({ "type": "boolean" }),
        TypeSchema::I64 | TypeSchema::U64 => json!({ "type": "integer" }),
        TypeSchema::BoundedU64 { min, max } => json!({
            "type": "integer",
            "minimum": min,
            "maximum": max,
        }),
        TypeSchema::F64 => json!({ "type": "number" }),
        TypeSchema::String => json!({ "type": "string" }),
        TypeSchema::Json => json!({}),
        TypeSchema::Bytes => json!({ "type": "string", "contentEncoding": "base64" }),
        TypeSchema::Array(inner) => json!({
            "type": "array",
            "items": type_schema_to_json(inner),
        }),
        TypeSchema::Map(inner) => json!({
            "type": "object",
            "additionalProperties": type_schema_to_json(inner),
        }),
        TypeSchema::Option(inner) => json!({
            "anyOf": [
                type_schema_to_json(inner),
                { "type": "null" }
            ],
        }),
        TypeSchema::Enum { variants } => json!({
            "type": "string",
            "enum": variants,
        }),
        TypeSchema::Object { fields } => schema_fields_to_json_schema(fields),
        TypeSchema::Ref(name) => json!({
            "$ref": format!("#/$defs/{name}"),
        }),
    }
}

fn mcp_output_schema() -> Value {
    json!({
        "type": "object",
        "properties": {
            "content": {
                "type": "array",
                "items": {
                    "type": "object",
                    "additionalProperties": true
                }
            },
            "isError": { "type": "boolean" }
        },
        "additionalProperties": true,
    })
}

fn tags_for_tool_id(tool_id: &str, source: &str) -> Vec<String> {
    let mut tags = vec![source.to_string()];
    if let Some(namespace) = tool_id.split('.').next() {
        push_unique(&mut tags, namespace);
    }
    if tool_id.contains("search") || tool_id.contains("recall") {
        push_unique(&mut tags, "retrieval");
    }
    if tool_id.contains("memory") || tool_id.contains("tree") {
        push_unique(&mut tags, "memory");
    }
    tags
}

fn push_unique(tags: &mut Vec<String>, tag: &str) {
    if !tag.is_empty() && !tags.iter().any(|existing| existing == tag) {
        tags.push(tag.to_string());
    }
}

fn looks_write_capable(tool_id: &str) -> bool {
    const MARKERS: &[&str] = &[
        "add", "apply", "create", "decide", "delete", "email", "execute", "forget", "ingest",
        "post", "put", "remove", "run", "send", "store", "update", "write",
    ];
    let lower = tool_id.to_ascii_lowercase();
    MARKERS.iter().any(|marker| {
        lower == *marker
            || lower.contains(&format!(".{marker}"))
            || lower.contains(&format!("_{marker}"))
            || lower.contains(&format!("{marker}."))
            || lower.contains(&format!("{marker}_"))
    })
}

fn policy_surface_ids() -> Vec<String> {
    let mut ids = POLICY_SURFACES
        .iter()
        .copied()
        .map(String::from)
        .collect::<BTreeSet<_>>();

    ids.extend(
        all::all_controller_schemas()
            .into_iter()
            .map(|schema| schema.method_name())
            .filter(|tool_id| is_policy_surface(tool_id)),
    );

    ids.into_iter().collect()
}

fn is_policy_surface(tool_id: &str) -> bool {
    POLICY_SURFACES.contains(&tool_id)
        || tool_id.starts_with("security.")
        || tool_id.starts_with("approval.")
}

fn title_from_function(function: &str) -> String {
    function
        .split('_')
        .filter(|part| !part.is_empty())
        .map(|part| {
            let mut chars = part.chars();
            match chars.next() {
                Some(first) => format!("{}{}", first.to_uppercase(), chars.as_str()),
                None => String::new(),
            }
        })
        .collect::<Vec<_>>()
        .join(" ")
}

#[cfg(test)]
#[path = "ops_tests.rs"]
mod tests;