~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0
158 lines · 7.3 KB Raw History
//! What the server is told by its operator: where it listens, the origin it
//! is reached at, where it keeps its caches and how much audio and memory
//! they hold, where it keeps sign-ins, whether sign-in is on and what it
//! asks for, whether canvases go in people's own spaces, the PLC directory
//! it reads,
//! whether outbound requests may reach private addresses, who its
//! operators are and whether it keeps its log lines for them. The binary
//! reads it from the environment (`HUD_*`); everything else takes it as
//! given.

use std::net::IpAddr;
use std::path::PathBuf;

/// The PDS a new account is made at where `HUD_SIGN_UP_PDS` names none.
pub const SIGN_UP_PDS: &str = "https://selfhosted.social";
/// 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 name HUD gives itself, as an atproto OAuth client, where
/// `HUD_CLIENT_NAME` names none.
pub const CLIENT_NAME: &str = "HUD";

/// How HUD shows itself to accounts' servers as their OAuth client: the
/// name and logo a PDS's consent screen shows (`HUD_CLIENT_NAME`,
/// `HUD_CLIENT_LOGO`), as for an instance serving one app under its name.
#[derive(Clone, Debug, PartialEq)]
pub struct Client {
    /// Its `client_name`, by default `CLIENT_NAME`.
    pub name: String,
    /// Its `logo_uri`, an `https` URL; none for no logo.
    pub logo: Option<String>,
}

impl Default for Client {
    fn default() -> Client {
        Client {
            name: CLIENT_NAME.into(),
            logo: None,
        }
    }
}

/// 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 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 server's configuration, already checked.
#[derive(Clone, Debug)]
pub struct Config {
    /// The port it listens on (`HUD_PORT`).
    pub port: u16,
    /// The address it listens on (`HUD_BIND`).
    pub bind: IpAddr,
    /// The origin it is reached at, as `https://hud.example`: no path and no
    /// trailing `/` (`HUD_PUBLIC_URL`). Blob URLs and the view's CSP name it,
    /// and an `Origin` header equal to it is let through to `/mcp`.
    pub public_origin: String,
    /// Where the lexicon and blob caches are kept on disk (`HUD_CACHE_DIR`).
    pub cache_dir: PathBuf,
    /// The most bytes of audio blobs kept on disk (`HUD_BLOB_CACHE_BYTES`).
    pub blob_cache_bytes: u64,
    /// About the most bytes the caches kept in memory hold together
    /// (`HUD_CACHE_MEMORY_BYTES`): identities, lexicons, joins' records,
    /// counts, feed generators and saved feeds, each its share.
    pub cache_memory_bytes: usize,
    /// Where sign-ins are kept: the client key, sessions and registered
    /// clients, under `oauth/`; and the workspace's canvases, under
    /// `canvas/` (`HUD_DATA_DIR`, by default the cache's).
    pub data_dir: PathBuf,
    /// Whether sign-in is on (`HUD_OAUTH`): the authorization endpoints,
    /// 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 of what the views HUD serves do, with the
    /// workspace's own where it is served, and `SPACE_SCOPE` when `spaces`
    /// is on (`scope::asked`): what HUD holds its writes and calls to, and
    /// what it asks an account's server that refuses `oauth_include`.
    pub oauth_scope: String,
    /// What HUD asks an account's server for first where the operator's
    /// apps name permission sets (`scope::including`): their `include:`s,
    /// and what of `oauth_scope` no set holds. None where they name none.
    pub oauth_include: Option<String>,
    /// Whether HUD serves its workspace and its bundled apps
    /// (`HUD_WORKSPACE`): the workspace's page and methods, the canvas
    /// tools, the playground and the bundled views. Off, it serves the
    /// operator's apps (`HUD_APPS`) alone, and asks for what they do.
    pub workspace: bool,
    /// 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
    /// otherwise (`HUD_SPACES`): off, in the server's files alone.
    pub spaces: bool,
    /// The PLC directory `did:plc` documents are read from, no trailing `/`
    /// (`HUD_PLC_URL`).
    pub plc_url: String,
    /// A service whose `com.atproto.identity.resolveHandle` resolves
    /// handles, in place of DNS and `/.well-known/atproto-did`
    /// (`HUD_HANDLE_RESOLVER`): for tests against a local PDS whose
    /// handles resolve nowhere else.
    pub handle_resolver: Option<String>,
    /// Whether outbound requests may use `http` and reach private,
    /// loopback and other non-public addresses (`HUD_ALLOW_PRIVATE=1`): for
    /// development and tests against local servers, never in production.
    pub allow_private: bool,
    /// The backlink index counts come from (`HUD_BACKLINKS`).
    pub backlinks: crate::backlinks::Backlinks,
    /// The favicon service apps' icons are asked of, the encoded URL of an
    /// app's site following it (`HUD_FAVICONS`, by default
    /// `icons::FAVICONS`); none to ask nowhere.
    pub favicons: Option<String>,
    /// The image CDN public records' images are served from, Bluesky's
    /// `<cdn>/<size>/plain/<did>/<cid>@jpeg`, no trailing `/`
    /// (`HUD_IMAGE_CDN`, by default `blobs::IMAGE_CDN`); none for the blob
    /// proxy alone.
    pub image_cdn: Option<String>,
    /// The relay asked which repositories hold people's themes
    /// (`com.atproto.sync.listReposByCollection`), no trailing `/`
    /// (`HUD_RELAY`, by default `theme::RELAY`); none to list only an
    /// account's.
    pub relay: Option<String>,
    /// The PDS "Create one" makes a new account at, its origin
    /// (`HUD_SIGN_UP_PDS`, by default `SIGN_UP_PDS`): HUD asks its
    /// authorization server to sign the person up (`prompt=create`).
    pub sign_up_pds: String,
    /// How HUD shows itself to accounts' servers as their OAuth client
    /// (`HUD_CLIENT_NAME`, `HUD_CLIENT_LOGO`).
    pub client: Client,
    /// The accounts that run this server, by DID (`HUD_OPERATOR_DIDS`):
    /// they alone read its kept lines (`logs`).
    pub operators: Vec<String>,
    /// Whether the server keeps each line it logs, to read back, in
    /// `<data_dir>/logs.db` (`HUD_LOGS`).
    pub logs: bool,
    /// How many lines at info and below it keeps (`HUD_LOGS_KEEP`, by
    /// default `logs::KEEP`).
    pub logs_keep: u64,
}

impl Config {
    /// Where blob URLs point: the proxy, and the image CDN if there is one.
    pub fn blob_urls(&self) -> crate::blobs::Urls<'_> {
        crate::blobs::Urls {
            origin: &self.public_origin,
            cdn: self.image_cdn.as_deref(),
        }
    }

    /// The image CDN's origin, as a CSP names it; none with no CDN.
    pub fn image_cdn_origin(&self) -> Option<String> {
        let url = url::Url::parse(self.image_cdn.as_deref()?).ok()?;
        Some(url.origin().ascii_serialization())
    }
}