Watch
0
0
Fork
You've already forked hyperhive
0

hive-matrix-mcp: resolve a bare server name through matrix-sdk's discovery before connecting

This commit is contained in:
damocles 2026-11-22 19:26:33 +01:00 • committed by mara
commit e0a5f737ac
2 changed files with 67 additions and 1 deletions

View file

@ -85,6 +85,8 @@ pub async fn build_and_restore(
state_dir: &Path,
is_primary: bool,
) -> Result<Client> {
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<String> {
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<String> {
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)]

View file

@ -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).