chadtmiller.com/hud
A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts
place.hud.hudkit 0.6.0 Latest 5 packages
0
0
Sign-in asks for what HUD's views do, never transition:generic
crates/hud/src/main.rs+7−18
| @@ -47,9 +47,6 @@ Configuration, from the environment: | ||
HUD_OAUTH on or off: sign-in, the write and canvas tools, the | ||
workspace, and queries through the account's PDS (on | ||
when HUD_PUBLIC_URL is https, which on requires) | ||
HUD_OAUTH_SCOPE the atproto scope asked of accounts' servers, with | ||
atproto (atproto transition:generic); add | ||
transition:chat.bsky for Bluesky chats | ||
HUD_SPACES on or off: keep each person's canvases in spaces on | ||
their own PDS, private to them, where it offers spaces | ||
and their sign-in allows it, else in HUD_DATA_DIR | ||
| @@ -195,20 +192,12 @@ fn config() -> Result<Config, String> { | ||
Some("off") => false, | ||
Some(v) => return Err(format!("HUD_OAUTH: {v:?} is not on or off")), | ||
}; | ||
let oauth_scope = match var("HUD_OAUTH_SCOPE")? { | ||
Some(s) => { | ||
let tokens: Vec<&str> = s.split_whitespace().collect(); | ||
let printable = | ||
s.chars().all(|c| c.is_ascii_graphic() || c == ' ') && !s.contains(['"', '\\']); | ||
if !tokens.contains(&"atproto") || !printable { | ||
return Err(format!( | ||
"HUD_OAUTH_SCOPE: {s:?} is not a scope with atproto, as atproto transition:generic" | ||
)); | ||
} | ||
tokens.join(" ") | ||
} | ||
None => hud_core::config::DEFAULT_OAUTH_SCOPE.to_string(), | ||
}; | ||
if var("HUD_OAUTH_SCOPE")?.is_some() { | ||
return Err( | ||
"HUD_OAUTH_SCOPE: HUD no longer takes a scope; it asks accounts' servers for what its views do (docs/sign-in.md)" | ||
.into(), | ||
); | ||
} | ||
let spaces = match var("HUD_SPACES")?.as_deref() { | ||
None | Some("off") => false, | ||
Some("on") if !oauth => { | ||
| @@ -220,7 +209,7 @@ fn config() -> Result<Config, String> { | ||
Some("on") => true, | ||
Some(v) => return Err(format!("HUD_SPACES: {v:?} is not on or off")), | ||
}; | ||
let oauth_scope = hud_core::config::oauth_scope(&oauth_scope, spaces); | ||
let oauth_scope = hud_core::scope::asked(spaces); | ||
let cache_dir = var("HUD_CACHE_DIR")?.map_or_else(|| PathBuf::from("./cache"), PathBuf::from); | ||
// Set but empty is asking nowhere, unlike every other variable's empty. | ||
let favicons = match std::env::var("HUD_FAVICONS") { | ||
crates/hud-core/src/canvas/space.rs+2−10
| @@ -994,16 +994,8 @@ mod tests { | ||
crate::config::SPACE_SCOPE, | ||
format!("space:{SPACE_TYPE}?authority=*&manage=create&manage=update&manage=delete") | ||
); | ||
assert_eq!( | ||
crate::config::oauth_scope("atproto transition:generic", false), | ||
"atproto transition:generic" | ||
); | ||
let asked = crate::config::oauth_scope("atproto transition:generic", true); | ||
assert_eq!( | ||
asked, | ||
format!("atproto transition:generic {}", crate::config::SPACE_SCOPE) | ||
); | ||
assert_eq!(crate::config::oauth_scope(&asked, true), asked); | ||
let asked = crate::scope::asked(true); | ||
assert!(!grants(&crate::scope::asked(false))); | ||
// Granted as asked, or written out again by the PDS with the DID | ||
// and the type's collections. | ||
assert!(grants(&asked)); | ||
crates/hud-core/src/canvas/tools.rs+1−1
| @@ -494,7 +494,7 @@ mod tests { | ||
cache_memory_bytes: 1 << 24, | ||
data_dir: dir, | ||
oauth: true, | ||
oauth_scope: crate::config::DEFAULT_OAUTH_SCOPE.into(), | ||
oauth_scope: crate::scope::asked(false), | ||
spaces: false, | ||
plc_url: "http://127.0.0.1:9".into(), | ||
handle_resolver: None, | ||
crates/hud-core/src/oauth/tests/spaces.rs+2−2
| @@ -323,7 +323,7 @@ async fn list_spaces( | ||
async fn spaced() -> (Arc<Hud>, Fake, String) { | ||
let (h, f, base) = world_with(|c| { | ||
c.spaces = true; | ||
c.oauth_scope = crate::config::oauth_scope(&c.oauth_scope, true); | ||
c.oauth_scope = crate::scope::asked(true); | ||
}) | ||
.await; | ||
{ | ||
| @@ -451,7 +451,7 @@ async fn where_canvases_go_and_why() { | ||
| ||
// With spaces off, the scope asks for none. | ||
let (h, _f, _base) = world().await; | ||
assert_eq!(h.config.oauth_scope, "atproto transition:generic"); | ||
assert!(!h.config.oauth_scope.contains("space:")); | ||
} | ||
| ||
#[tokio::test] | ||
crates/hud-core/src/oauth/client.rs+1−0
| @@ -559,6 +559,7 @@ impl Hud { | ||
token_endpoint: flow.server.token.clone(), | ||
revocation_endpoint: flow.server.revocation.clone(), | ||
scope: t.scope, | ||
asked: auth.scope.clone(), | ||
access_token: t.access, | ||
refresh_token: t.refresh, | ||
expires_at: t.expires_at, | ||
crates/hud-core/src/oauth/mod.rs+29−1
| @@ -31,6 +31,7 @@ pub(crate) mod store; | ||
mod tests; | ||
| ||
use crate::Hud; | ||
use crate::tools::Refusal; | ||
use atproto::jose; | ||
use atproto::keys::{Curve, Keypair}; | ||
use issuer::{Client, Code, Request}; | ||
| @@ -62,6 +63,10 @@ const CODE_LIFE: u64 = 300; | ||
const SPENT_LIFE: u64 = 3600; | ||
/// The most requests, sign-ins in progress and codes kept at once. | ||
const PENDING_MAX: usize = 10_000; | ||
/// What the workspace's methods answer a call refused for want of scope | ||
/// from someone who signed in before HUD asked for what it asks now. | ||
pub const SIGN_IN_AGAIN: &str = | ||
"HUD asks for more now than when you signed in: sign in again to allow this"; | ||
| ||
/// Seconds since 1970. | ||
pub fn now() -> u64 { | ||
| @@ -488,7 +493,8 @@ impl Auth { | ||
issuer: pds.into(), | ||
token_endpoint: format!("{pds}/oauth/token"), | ||
revocation_endpoint: None, | ||
scope: "atproto transition:generic".into(), | ||
scope: self.scope.clone(), | ||
asked: self.scope.clone(), | ||
access_token: jose::random(16), | ||
refresh_token: None, | ||
expires_at: now() + 3600, | ||
| @@ -643,4 +649,26 @@ impl Hud { | ||
.as_ref() | ||
.ok_or_else(|| "Sign-in is off on this server".to_string()) | ||
} | ||
| ||
/// Whether `account` signed in asking for less than HUD asks for now | ||
/// (`scope::behind`): signing in again grants the rest. | ||
pub async fn behind(&self, account: &Account) -> bool { | ||
let Some(auth) = &self.oauth else { | ||
return false; | ||
}; | ||
crate::scope::behind(&account.session.lock().await.asked, &auth.scope) | ||
} | ||
| ||
/// What a PDS's refusal of a call as `account` for want of `scope` | ||
/// (`ScopeMissingError`) answers. Signed in before HUD asked for what | ||
/// it asks now, signing in again (`Refusal::SignIn`: an MCP host's 401 | ||
/// challenge; the workspace's methods say `SIGN_IN_AGAIN`, as their | ||
/// page and app offer it). Else the sign-in does not allow it, which | ||
/// signing in again would not change. | ||
pub async fn scope_refused(&self, account: &Account, scope: &str) -> Refusal { | ||
match self.behind(account).await { | ||
true => Refusal::SignIn, | ||
false => Refusal::Error(format!("Your sign-in does not allow {scope}")), | ||
} | ||
} | ||
} | ||
crates/hud-core/src/oauth/store.rs+12−1
| @@ -61,6 +61,10 @@ pub(crate) struct Session { | ||
pub revocation_endpoint: Option<String>, | ||
/// The atproto scope granted. | ||
pub scope: String, | ||
/// The atproto scope HUD asked for when it signed in (`scope::asked` | ||
/// then), which a PDS may have granted less of: what `scope::behind` | ||
/// holds against what HUD asks for now. | ||
pub asked: String, | ||
pub access_token: String, | ||
pub refresh_token: Option<String>, | ||
/// When the atproto access token expires, in seconds since 1970. | ||
| @@ -89,6 +93,7 @@ impl Session { | ||
"tokenEndpoint": self.token_endpoint, | ||
"revocationEndpoint": self.revocation_endpoint, | ||
"scope": self.scope, | ||
"asked": self.asked, | ||
"accessToken": self.access_token, | ||
"refreshToken": self.refresh_token, | ||
"expiresAt": self.expires_at, | ||
| @@ -153,6 +158,8 @@ impl Session { | ||
token_endpoint: text("tokenEndpoint")?, | ||
revocation_endpoint: maybe("revocationEndpoint")?, | ||
scope: text("scope")?, | ||
// Kept before what was asked was: what was granted. | ||
asked: text("asked").or_else(|| text("scope"))?, | ||
access_token: text("accessToken")?, | ||
refresh_token: maybe("refreshToken")?, | ||
expires_at: number(v, "expiresAt")?, | ||
| @@ -237,7 +244,8 @@ mod tests { | ||
issuer: "https://pds.example".into(), | ||
token_endpoint: "https://pds.example/oauth/token".into(), | ||
revocation_endpoint: None, | ||
scope: "atproto transition:generic".into(), | ||
scope: "atproto repo:app.bsky.feed.like".into(), | ||
asked: "atproto repo:app.bsky.feed.like space:place.hud.workspace".into(), | ||
access_token: "at".into(), | ||
refresh_token: Some("rt".into()), | ||
expires_at: 10, | ||
| @@ -274,6 +282,9 @@ mod tests { | ||
let mut old = s.to_json(); | ||
old.as_object_mut().unwrap().remove("kind"); | ||
assert_eq!(Session::from_json(&old).unwrap().kind, Kind::Mcp); | ||
// A session kept before what was asked was: what was granted. | ||
old.as_object_mut().unwrap().remove("asked"); | ||
assert_eq!(Session::from_json(&old).unwrap().asked, s.scope); | ||
} | ||
| ||
#[test] | ||
crates/hud-core/src/oauth/tests.rs+72−8
| @@ -52,7 +52,7 @@ fn config(plc: &str) -> crate::Config { | ||
cache_memory_bytes: 1 << 24, | ||
data_dir: dir, | ||
oauth: true, | ||
oauth_scope: crate::config::DEFAULT_OAUTH_SCOPE.into(), | ||
oauth_scope: crate::scope::asked(false), | ||
spaces: false, | ||
plc_url: plc.into(), | ||
handle_resolver: None, | ||
| @@ -133,7 +133,7 @@ fn metadata_documents() { | ||
assert_eq!(c["token_endpoint_auth_signing_alg"], "ES256"); | ||
assert_eq!(c["jwks_uri"], "https://hud.test/oauth/jwks.json"); | ||
assert_eq!(c["dpop_bound_access_tokens"], json!(true)); | ||
assert_eq!(c["scope"], "atproto transition:generic"); | ||
assert_eq!(c["scope"], json!(crate::scope::asked(false))); | ||
assert_eq!(c["application_type"], "web"); | ||
assert_eq!(c["client_uri"], ORIGIN); | ||
// The key published is the one that signs, and outlives a restart. | ||
| @@ -868,6 +868,21 @@ async fn create_record(State(f): State<Fake>, headers: HeaderMap, body: String) | ||
} | ||
let input: Value = serde_json::from_str(&body).unwrap(); | ||
assert_eq!(input["repo"], DID); | ||
// Held to the scope granted, as a PDS holds a write. | ||
let collection = input["collection"].as_str().unwrap(); | ||
let granted = w.granted.clone().unwrap_or_else(|| w.scope.clone()); | ||
let allowed = format!("repo:{collection}"); | ||
if !granted | ||
.split(' ') | ||
.any(|t| t == "transition:generic" || t == allowed) | ||
{ | ||
let message = format!("Missing required scope \"{allowed}?action=create\""); | ||
return json_response( | ||
StatusCode::FORBIDDEN, | ||
json!({ "error": "ScopeMissingError", "message": message }), | ||
&nonce, | ||
); | ||
} | ||
w.writes += 1; | ||
w.created.push(input["record"].clone()); | ||
let uri = format!( | ||
| @@ -1195,6 +1210,58 @@ async fn signs_in_writes_refreshes_and_revokes() { | ||
assert_eq!(f.lock().unwrap().revocations, 1); | ||
} | ||
| ||
/// HUD writes only what it asks to write. A sign-in that asked for less | ||
/// than HUD asks for now is behind: a write its PDS refuses for want of | ||
/// scope asks for sign-in again. One that asked for all of it but was | ||
/// granted less is told so, as signing in again would not change it. | ||
#[tokio::test] | ||
async fn writes_held_to_the_scope_asked() { | ||
let (h, f, base) = world().await; | ||
let id = client(&h); | ||
let (access, _) = signed_in(&h, &f, &base, &id).await; | ||
let account = h.auth().unwrap().account(&access).unwrap(); | ||
assert!(!h.behind(&account).await); | ||
let like = json!({ "collection": "app.bsky.feed.like", "record": { | ||
"subject": { "uri": format!("at://{OTHER}/app.bsky.feed.post/3k2a"), "cid": CID }, | ||
"createdAt": "2026-11-14T12:00:00.000Z" } }); | ||
let made = h | ||
.call_tool("create_record", Some(&like), Some(&account)) | ||
.await; | ||
assert!(made.unwrap().is_ok()); | ||
// A collection no view writes is refused before the PDS is asked. | ||
let follow = json!({ "collection": "app.bsky.graph.follow", "record": { | ||
"subject": OTHER, "createdAt": "2026-11-14T12:00:00.000Z" } }); | ||
let writes = f.lock().unwrap().writes; | ||
let refused = h | ||
.call_tool("create_record", Some(&follow), Some(&account)) | ||
.await; | ||
assert!(matches!( | ||
refused.unwrap(), | ||
Err(Refusal::Error(e)) if e.starts_with("HUD does not write app.bsky.graph.follow records") | ||
)); | ||
assert_eq!(f.lock().unwrap().writes, writes); | ||
// Granted less than asked: what the sign-in does not allow. | ||
f.lock().unwrap().granted = Some("atproto".into()); | ||
let (access, _) = signed_in(&h, &f, &base, &id).await; | ||
let narrow = h.auth().unwrap().account(&access).unwrap(); | ||
let refused = h | ||
.call_tool("create_record", Some(&like), Some(&narrow)) | ||
.await; | ||
assert_eq!( | ||
refused.unwrap().err(), | ||
Some(Refusal::Error( | ||
"Your sign-in does not allow repo:app.bsky.feed.like?action=create".into() | ||
)) | ||
); | ||
// Signed in before HUD asked for it: sign in again. | ||
narrow.session.lock().await.asked = "atproto transition:generic".into(); | ||
assert!(h.behind(&narrow).await); | ||
let refused = h | ||
.call_tool("create_record", Some(&like), Some(&narrow)) | ||
.await; | ||
assert_eq!(refused.unwrap().err(), Some(Refusal::SignIn)); | ||
} | ||
| ||
#[tokio::test] | ||
async fn codes_spent_once_and_held_to_their_request() { | ||
let (h, f, base) = world().await; | ||
| @@ -1624,7 +1691,8 @@ async fn query_joins_ask_as_the_person() { | ||
(method("getAuthorFeed"), None, false) | ||
] | ||
); | ||
// Signed in: through the account's PDS, each to its own service. | ||
// Signed in: through the account's PDS to a service HUD asks to call | ||
// as the account; to any other, straight, as no one. | ||
let (access, _) = signed_in(&h, &f, &base, &id).await; | ||
let account = h.auth().unwrap().account(&access).unwrap(); | ||
let (got, asked) = ask(crate::xrpc::Caller::Account(&account)).await; | ||
| @@ -1632,11 +1700,7 @@ async fn query_joins_ask_as_the_person() { | ||
assert_eq!( | ||
asked, | ||
[ | ||
( | ||
method("getActorFeeds"), | ||
Some("did:web:other.test#bsky_appview".into()), | ||
true | ||
), | ||
(method("getActorFeeds"), None, false), | ||
( | ||
method("getAuthorFeed"), | ||
Some("did:web:api.bsky.app#bsky_appview".into()), | ||
crates/hud-core/src/apps.rs+10−14
| @@ -71,20 +71,19 @@ pub struct Entry { | ||
} | ||
| ||
/// The app `app`'s entry points, in the views' order: every launcher its | ||
/// bundled method views offered where sign-in asks for `asked` offer | ||
/// (`View::offered`), its params made the person's (`me`: their DID and | ||
/// handle). With no one signed in, those that need someone are left out: | ||
/// one naming the person (`$me`) or feeds by name (`feeds::names_feeds`, as | ||
/// "Saved feeds"), or one whose view's query needs an account (its | ||
/// `signIn`). | ||
pub fn entries(app: &str, me: Option<(&str, Option<&str>)>, asked: Option<&str>) -> Vec<Entry> { | ||
/// bundled method views offer, its params made the person's (`me`: their | ||
/// DID and handle). With no one signed in, those that need someone are | ||
/// left out: one naming the person (`$me`) or feeds by name | ||
/// (`feeds::names_feeds`, as "Saved feeds"), or one whose view's query | ||
/// needs an account (its `signIn`). | ||
pub fn entries(app: &str, me: Option<(&str, Option<&str>)>) -> Vec<Entry> { | ||
let mut out = Vec::new(); | ||
for b in view::bundled() { | ||
let v = &b.view; | ||
let Some(method) = v.method.as_deref() else { | ||
continue; | ||
}; | ||
if domain(method).as_deref() != Some(app) || !v.offered(asked) { | ||
if domain(method).as_deref() != Some(app) { | ||
continue; | ||
} | ||
for l in &v.launch { | ||
| @@ -214,10 +213,7 @@ mod tests { | ||
#[test] | ||
fn entry_points() { | ||
let titles = |app: &str, me: Option<(&str, Option<&str>)>| -> Vec<String> { | ||
entries(app, me, None) | ||
.into_iter() | ||
.map(|e| e.title) | ||
.collect() | ||
entries(app, me).into_iter().map(|e| e.title).collect() | ||
}; | ||
assert_eq!(titles("bsky.app", None), ["Discover"]); | ||
let me = Some(("did:plc:me", Some("me.test"))); | ||
| @@ -232,11 +228,11 @@ mod tests { | ||
] | ||
); | ||
// Feeds by name are the account's: a call the query tool resolves for it. | ||
let yours = &entries("bsky.app", me, None)[2].call; | ||
let yours = &entries("bsky.app", me)[2].call; | ||
assert_eq!(yours["args"]["params"], json!({ "feeds": "saved" })); | ||
assert_eq!(titles("knotless.dev", None), ["Everyone’s activity"]); | ||
assert!(titles("plyr.fm", me).is_empty()); | ||
let mine = entries("bsky.app", me, None); | ||
let mine = entries("bsky.app", me); | ||
assert_eq!( | ||
mine[0].call, | ||
json!({ "name": "query", "args": { "method": "app.bsky.feed.getAuthorFeed", | ||
crates/hud-core/src/config.rs+6−19
| @@ -10,32 +10,19 @@ | ||
use std::net::IpAddr; | ||
use std::path::PathBuf; | ||
| ||
/// The atproto scope asked for where `HUD_OAUTH_SCOPE` names none. | ||
pub const DEFAULT_OAUTH_SCOPE: &str = "atproto transition:generic"; | ||
| ||
/// About the most bytes the in-memory caches hold together where | ||
/// `HUD_CACHE_MEMORY_BYTES` says none: 64 MiB. | ||
pub const CACHE_MEMORY_BYTES: usize = 64 << 20; | ||
| ||
/// The permission to keep canvases in the account's spaces of HUD's | ||
/// workspace type (`canvas::space::SPACE_TYPE`), and to read, and manage, | ||
/// those of others it is a member of: asked for besides the scope with | ||
/// `HUD_SPACES=on`. A PDS refuses the whole sign-in (`invalid_scope`) while | ||
/// the type's lexicon is not published, so it is never asked for by | ||
/// default. The one place it is written. | ||
/// those of others it is a member of: asked for besides the rest of the | ||
/// scope (`scope::asked`) with `HUD_SPACES=on`. A PDS refuses the whole | ||
/// sign-in (`invalid_scope`) while the type's lexicon is not published, so | ||
/// it is never asked for by default. The one place it is written. | ||
pub const SPACE_SCOPE: &str = | ||
"space:place.hud.workspace?authority=*&manage=create&manage=update&manage=delete"; | ||
| ||
/// The scope asked for: `scope`, and with `spaces`, `SPACE_SCOPE` after it | ||
/// unless it is there already. | ||
pub fn oauth_scope(scope: &str, spaces: bool) -> String { | ||
let mut tokens: Vec<&str> = scope.split_whitespace().collect(); | ||
if spaces && !tokens.contains(&SPACE_SCOPE) { | ||
tokens.push(SPACE_SCOPE); | ||
} | ||
tokens.join(" ") | ||
} | ||
| ||
/// The server's configuration, already checked. | ||
#[derive(Clone, Debug)] | ||
pub struct Config { | ||
| @@ -63,8 +50,8 @@ pub struct Config { | ||
/// the write and canvas tools, the workspace, and queries through the | ||
/// account's PDS. Off, every tool is answered as no one. | ||
pub oauth: bool, | ||
/// The atproto scope HUD asks an account's server for | ||
/// (`HUD_OAUTH_SCOPE`, with `SPACE_SCOPE` when `spaces` is on). | ||
/// The atproto scope HUD asks an account's server for: what it does, | ||
/// with `SPACE_SCOPE` when `spaces` is on (`scope::asked`). | ||
pub oauth_scope: String, | ||
/// Whether a person's canvases are kept in spaces on their own PDS, | ||
/// where it offers them and their sign-in allows it, the server's files | ||