From e0a5f737ac4d06f4de08a669891fbb7a21d2ed15 Mon Sep 17 00:00:00 2001 From: damocles Date: Sun, 22 Nov 2026 19:26:33 +0100 Subject: [PATCH] hive-matrix-mcp: resolve a bare server name through matrix-sdk's discovery before connecting --- hive-matrix-mcp/src/client.rs | 64 +++++++++++++++++++++++++++++++++++ nix/agent-modules/matrix.nix | 4 ++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/hive-matrix-mcp/src/client.rs b/hive-matrix-mcp/src/client.rs index 7436181bb..c854be741 100644 --- a/hive-matrix-mcp/src/client.rs +++ b/hive-matrix-mcp/src/client.rs @@ -85,6 +85,8 @@ pub async fn build_and_restore( state_dir: &Path, is_primary: bool, ) -> Result { + let resolved = resolve_homeserver(homeserver).await?; + let homeserver = resolved.as_str(); let Some((user_id, device_id)) = whoami(homeserver, token.expose()).await? else { // Homeserver rejected our token — stale session after a homeserver // state wipe or token expiry. Discard it so we don't loop on it @@ -167,6 +169,29 @@ pub async fn build_and_restore( Ok(client) } +/// The homeserver URL a configured `homeserver` names. A value with a scheme +/// already is the URL and is used as given, with no lookup; a bare server name +/// goes through [`discover`]. +async fn resolve_homeserver(homeserver: &str) -> Result { + if homeserver.contains("://") { + return Ok(homeserver.to_owned()); + } + discover(homeserver).await +} + +/// `homeserver`'s URL by matrix-sdk's own discovery +/// (`.well-known/matrix/client`). The whoami probe runs before the real client +/// exists and needs the same URL, so a store-less client is built here just to +/// ask it. +async fn discover(homeserver: &str) -> Result { + let probe = Client::builder() + .server_name_or_homeserver_url(homeserver) + .build() + .await + .with_context(|| format!("discover the matrix homeserver for {homeserver}"))?; + Ok(probe.homeserver().as_str().trim_end_matches('/').to_owned()) +} + /// Whoami probe — used at startup to recover the `user_id` + `device_id` the /// registration response carried but hive-c0re didn't persist alongside the /// access token. Cheaper than teaching the hive-c0re side to persist them, @@ -438,6 +463,45 @@ mod tests { use super::*; + #[tokio::test] + async fn a_server_name_resolves_through_its_well_known() { + use wiremock::matchers::{method, path}; + use wiremock::{Mock, MockServer, ResponseTemplate}; + + // The homeserver itself: answers the client-versions probe. + let homeserver = MockServer::start().await; + Mock::given(method("GET")) + .and(path("/_matrix/client/versions")) + .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({ + "versions": ["v1.11"], + }))) + .mount(&homeserver) + .await; + // A bare name (here the loopback host:port) whose well-known names it. + let name = MockServer::start().await; + Mock::given(method("GET")) + .and(path("/.well-known/matrix/client")) + .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({ + "m.homeserver": { "base_url": format!("{}/", homeserver.uri()) }, + }))) + .mount(&name) + .await; + + // `discover` takes a URL here because the sdk only tries https for a + // bare name, which a loopback stub cannot serve. + assert_eq!( + discover(&name.uri()).await.expect("discovered"), + homeserver.uri() + ); + } + + #[tokio::test] + async fn a_homeserver_url_is_used_as_given_without_a_lookup() { + // Nothing listens here: any lookup would fail. + let url = "http://127.0.0.1:1"; + assert_eq!(resolve_homeserver(url).await.expect("as given"), url); + } + /// An account as the homeserver and this device describe it, recording /// which of the two writes [`recover_or_enable`] made. #[derive(Default)] diff --git a/nix/agent-modules/matrix.nix b/nix/agent-modules/matrix.nix index 3d4231308..c6fec6b55 100644 --- a/nix/agent-modules/matrix.nix +++ b/nix/agent-modules/matrix.nix @@ -118,7 +118,9 @@ in default = null; example = "https://matrix.example.org"; description = '' - Homeserver URL for this account. When null (the default), + Homeserver for this account: a URL, or a bare server name + such as `example.org` whose `/.well-known/matrix/client` + names the homeserver. When null (the default), the account falls back to `services.hyperhive.agent.matrix.url`. Set it for an account on a different homeserver than the agent's default (e.g. an external public-matrix account).