Skip to content

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

feat(registry): add mcp.json config document support

Steven Enamakel2ce4998ab906
diff --git a/crates/openhuman-core/src/mcp/registry/mod.rs b/crates/openhuman-core/src/mcp/registry/mod.rs
index e944f1ae79..d1a2b99e9b 100644
--- a/crates/openhuman-core/src/mcp/registry/mod.rs
+++ b/crates/openhuman-core/src/mcp/registry/mod.rs
@@ -1,9 +1,10 @@
//! MCP Registry — the host half of the user-installed server surface.
//!
//! The registry itself moved to `tinymcp`: the Smithery and official catalogs,
-//! the SQLite store, the live connection map, the subprocess supervisor, the
-//! browser sign-in flow, and the setup secret vault all live there now. What is
-//! left here is what belongs to *this* application.
+//! the SQLite store, the live connection map, the subprocess supervisor and the
+//! browser sign-in flow all live there now. What is left here is what belongs
+//! to *this* application. The catalogs are browse-only: a server is declared
+//! in the user's `mcp.json` ([`config_doc`]), never installed from a listing.
//!
//! # Modules
//!
@@ -12,7 +13,8 @@
//! the extraction: an end-to-end test seeding the upstream response cache.
//! - [`ops`] — the `mcp_clients` RPC handlers, delegating to the service
//! [`super::host`] holds and publishing this application's own events.
-//! - [`setup_ops`] — the `mcp_setup` handlers, likewise.
+//! - [`config_doc`] — the `mcp.json` document: how the store renders as one,
+//! what a written one may say, and how the two are reconciled.
//! - `schemas` — the controller schemas and dispatch.
//! - [`supervisor_events`] — what the reconnect supervisor observed each
//! tick, as this domain's events; the Event Log and the notification bridge
@@ -35,14 +37,14 @@
#[cfg(feature = "mcp")]
pub mod bus;
#[cfg(feature = "mcp")]
+pub mod config_doc;
+#[cfg(feature = "mcp")]
pub(crate) mod helpers;
#[cfg(feature = "mcp")]
pub mod ops;
#[cfg(feature = "mcp")]
mod schemas;
#[cfg(feature = "mcp")]
-pub mod setup_ops;
-#[cfg(feature = "mcp")]
pub mod supervisor_events;
#[cfg(feature = "mcp")]
pub mod tools;
@@ -60,7 +62,7 @@ pub use schemas::{
/// keep their spelling.
pub mod types {
pub use tinymcp_bus::{
- ChatTurn, CommandKind, ConnStatus, ConnectedServerOverview, InstalledServer, McpTool,
+ CommandKind, ConnStatus, ConnectedServerOverview, InstalledServer, McpTool,
RegistryConnection as SmitheryConnection, RegistryServerDetail as SmitheryServerDetail,
RegistryServerSummary as SmitheryServerSummary, ServerStatus, Transport,
};
diff --git a/crates/openhuman-core/src/mcp/registry/ops.rs b/crates/openhuman-core/src/mcp/registry/ops.rs
index 2e1d3caf8f..2d7b91b66f 100644
--- a/crates/openhuman-core/src/mcp/registry/ops.rs
+++ b/crates/openhuman-core/src/mcp/registry/ops.rs
@@ -32,6 +32,7 @@ use crate::core::events::DomainEvent;
use crate::mcp::host;
use crate::rpc::RpcOutcome;
+use super::config_doc;
use super::helpers::{encode, inject_required_env_keys, require, resolve};
// ── registry_search ──────────────────────────────────────────────────────────
@@ -135,6 +136,280 @@ pub async fn mcp_clients_uninstall(
))
}
+// ── mcp.json ─────────────────────────────────────────────────────────────────
+
+/// Renders the install store as the `mcp.json` document.
+///
+/// Credential *names* ride along as `envKeys`; values never do. See
+/// [`super::config_doc`] for the contract.
+pub async fn mcp_clients_config_get(config: &Config) -> Result<RpcOutcome<Value>, String> {
+ let service = resolve(config)?;
+ let installed = service
+ .dynamic()
+ .installed_list()
+ .map_err(|error| error.to_string())?;
+
+ let stored_keys = stored_credential_names(service.dynamic().store(), &installed);
+ let count = installed.len();
+ let doc = config_doc::render(&installed, &stored_keys);
+
+ Ok(RpcOutcome::new(
+ doc,
+ vec![format!("config_get rendered {count} servers")],
+ ))
+}
+
+/// Replaces the install store with what `doc` declares.
+///
+/// A replace, not a merge: a server absent from the document is uninstalled,
+/// one not yet installed is added, and one whose dial changed is rewritten in
+/// place under the same `server_id`. Credentials are the exception — a
+/// declaration that says nothing about them leaves the stored ones alone,
+/// because a read never shows them and a round trip must not wipe them.
+///
+/// Every added or rewritten server that is enabled is connected in the
+/// background; the status poll reports how that went. The reply carries the
+/// re-rendered document and what changed, so the editor can show both.
+pub async fn mcp_clients_config_set(
+ config: &Config,
+ doc: Value,
+) -> Result<RpcOutcome<Value>, String> {
+ let declared = config_doc::parse(&doc)?;
+
+ // Keys are trimmed on the way in, so two spellings of one name can collide
+ // after the fact even though a JSON object cannot repeat a key.
+ {
+ let mut seen = std::collections::HashSet::new();
+ for entry in &declared {
+ if !seen.insert(entry.name.as_str()) {
+ return Err(format!("`{}` is declared twice", entry.name));
+ }
+ }
+ }
+
+ let service = resolve(config)?;
+ let registry = service.dynamic();
+ let store = registry.store();
+ let installed = registry
+ .installed_list()
+ .map_err(|error| error.to_string())?;
+
+ let mut added = Vec::new();
+ let mut updated = Vec::new();
+ let mut removed = Vec::new();
+ let mut to_connect: Vec<String> = Vec::new();
+
+ // Removals first, so a server renamed in the document (drop one key, add
+ // another) does not briefly hold two live connections to one process.
+ for server in &installed {
+ if declared
+ .iter()
+ .any(|entry| entry.name == server.qualified_name)
+ {
+ continue;
+ }
+ tracing::debug!(
+ server_id = %server.server_id,
+ name = %server.qualified_name,
+ "[mcp] config_set removing a server absent from the document"
+ );
+ registry
+ .uninstall(&server.server_id)
+ .await
+ .map_err(|error| error.to_string())?;
+ BUS.publish(DomainEvent::McpServerDisconnected {
+ server_id: server.server_id.clone(),
+ reason: Some("removed from mcp.json".to_string()),
+ });
+ removed.push(server.qualified_name.clone());
+ }
+
+ for entry in &declared {
+ let existing = installed
+ .iter()
+ .find(|server| server.qualified_name == entry.name);
+
+ match existing {
+ None => {
+ let server_id = uuid::Uuid::new_v4().to_string();
+ let row = config_doc::to_installed(entry, server_id.clone(), now_ms());
+ store
+ .insert_server(&row)
+ .map_err(|error| error.to_string())?;
+ if let Some(written) = &entry.credentials {
+ let merged = config_doc::merge_credentials(&Default::default(), written);
+ write_credentials(store, &server_id, &merged)?;
+ }
+ tracing::debug!(
+ server_id = %server_id,
+ name = %entry.name,
+ transport = entry.transport.dispatch_kind(),
+ "[mcp] config_set added a server"
+ );
+ BUS.publish(DomainEvent::McpServerInstalled {
+ server_id: server_id.clone(),
+ qualified_name: entry.name.clone(),
+ });
+ added.push(entry.name.clone());
+ if entry.enabled {
+ to_connect.push(server_id);
+ }
+ }
+ Some(existing) => {
+ let server_id = existing.server_id.clone();
+ let dial_changed = !config_doc::same_dial(entry, existing);
+ let mut stored = store
+ .load_env_values(&server_id)
+ .map_err(|error| error.to_string())?;
+
+ if dial_changed {
+ // The store has no "update the dial" statement, so the row
+ // is rewritten under the same id. Deleting it cascades the
+ // credentials, which is why they were read first.
+ registry.connections().disconnect(&server_id).await;
+ store
+ .delete_server(&server_id)
+ .map_err(|error| error.to_string())?;
+ let row = config_doc::to_installed(
+ entry,
+ server_id.clone(),
+ existing.installed_at,
+ );
+ store
+ .insert_server(&row)
+ .map_err(|error| error.to_string())?;
+ write_credentials(store, &server_id, &stored)?;
+ tracing::debug!(
+ server_id = %server_id,
+ name = %entry.name,
+ "[mcp] config_set rewrote a server whose dial changed"
+ );
+ }
+
+ let credentials_changed = match &entry.credentials {
+ Some(written) if !written.is_empty() => {
+ let merged = config_doc::merge_credentials(&stored, written);
+ let changed = merged != stored;
+ if changed {
+ write_credentials(store, &server_id, &merged)?;
+ stored = merged;
+ }
+ changed
+ }
+ _ => false,
+ };
+ drop(stored);
+
+ if dial_changed || credentials_changed {
+ updated.push(entry.name.clone());
+ if entry.enabled {
+ if credentials_changed && !dial_changed {
+ // A new credential only takes effect on a fresh
+ // session.
+ registry.connections().disconnect(&server_id).await;
+ }
+ to_connect.push(server_id);
+ }
+ }
+ }
+ }
+ }
+
+ // Connecting can take the transport's whole timeout per server; the editor
+ // must not wait on it. The status poll shows each attempt's outcome, and a
+ // failure is recorded against the server either way.
+ for server_id in to_connect {
+ let service = std::sync::Arc::clone(&service);
+ tokio::spawn(async move {
+ match service.dynamic().connect(&server_id).await {
+ Ok(outcome) => {
+ let tool_count = u32::try_from(outcome.tools.len()).unwrap_or(u32::MAX);
+ tracing::debug!(
+ server_id = %server_id,
+ tools = tool_count,
+ "[mcp] config_set connected a server"
+ );
+ BUS.publish(DomainEvent::McpServerConnected {
+ server_id,
+ tool_count,
+ });
+ }
+ Err(error) => {
+ tracing::warn!(
+ server_id = %server_id,
+ "[mcp] config_set could not connect a server: {error}"
+ );
+ }
+ }
+ });
+ }
+
+ let installed = registry
+ .installed_list()
+ .map_err(|error| error.to_string())?;
+ let stored_keys = stored_credential_names(store, &installed);
+ let mut rendered = config_doc::render(&installed, &stored_keys);
+
+ let note = format!(
+ "config_set added={} updated={} removed={}",
+ added.len(),
+ updated.len(),
+ removed.len()
+ );
+ if let Some(object) = rendered.as_object_mut() {
+ object.insert("added".into(), json!(added));
+ object.insert("updated".into(), json!(updated));
+ object.insert("removed".into(), json!(removed));
+ }
+
+ Ok(RpcOutcome::new(rendered, vec![note]))
+}
+
+/// The credential *names* stored for each server, keyed by `server_id`.
+///
+/// Values are loaded and dropped here; only the names leave this function.
+fn stored_credential_names(
+ store: &tinymcp::Store,
+ installed: &[tinymcp_bus::InstalledServer],
+) -> std::collections::BTreeMap<String, Vec<String>> {
+ installed
+ .iter()
+ .map(|server| {
+ let names = store
+ .load_env_values(&server.server_id)
+ .map(|values| values.into_keys().collect())
+ .unwrap_or_default();
+ (server.server_id.clone(), names)
+ })
+ .collect()
+}
+
+/// Stores a server's credentials and keeps the row's name list in step.
+fn write_credentials(
+ store: &tinymcp::Store,
+ server_id: &str,
+ values: &std::collections::BTreeMap<String, String>,
+) -> Result<(), String> {
+ store
+ .set_env_values(server_id, values)
+ .map_err(|error| error.to_string())?;
+ let names: Vec<String> = values.keys().cloned().collect();
+ store
+ .update_env_keys(server_id, &names)
+ .map_err(|error| error.to_string())
+}
+
+/// Now, in Unix epoch milliseconds, as the install rows record it.
+fn now_ms() -> i64 {
+ i64::try_from(
+ std::time::SystemTime::now()
+ .duration_since(std::time::UNIX_EPOCH)
+ .map(|elapsed| elapsed.as_millis())
+ .unwrap_or_default(),
+ )
+ .unwrap_or(i64::MAX)
+}
+
// ── auth detection and browser OAuth ─────────────────────────────────────────
pub async fn mcp_clients_detect_auth(