offline-mcp 0.4.0

Model Context Protocol server exposing the Offline Protocol registry to AI agents
Documentation
//! MCP resources: every registry artifact addressable under the
//! `offline://` URI scheme, plus llms.txt digests.

use offline_registry::{llms, Registry};
use serde_json::{json, Value};

pub fn resource_list(registry: &Registry) -> Vec<Value> {
    let mut out = vec![
        json!({
            "uri": "offline://llms.txt",
            "name": "llms.txt",
            "description": "Compact index of the entire Offline Protocol ecosystem (packages, skills, workflows, templates)",
            "mimeType": "text/plain"
        }),
        json!({
            "uri": "offline://llms-full.txt",
            "name": "llms-full.txt",
            "description": "Complete ecosystem digest: every skill document and workflow inline",
            "mimeType": "text/plain"
        }),
    ];
    for s in registry.skills() {
        out.push(json!({
            "uri": format!("offline://skills/{}", s.name),
            "name": format!("Skill: {}", s.title),
            "description": s.description,
            "mimeType": "text/markdown"
        }));
    }
    for w in registry.workflows() {
        out.push(json!({
            "uri": format!("offline://workflows/{}", w.name),
            "name": format!("Workflow: {}", w.title),
            "description": w.description,
            "mimeType": "application/json"
        }));
    }
    for p in registry.packages() {
        out.push(json!({
            "uri": format!("offline://packages/{}", file_key(&p.name)),
            "name": format!("Package: {}", p.name),
            "description": p.description,
            "mimeType": "application/json"
        }));
    }
    for t in registry.templates() {
        out.push(json!({
            "uri": format!("offline://templates/{}", t.name),
            "name": format!("Template: {}", t.title),
            "description": t.description,
            "mimeType": "application/json"
        }));
    }
    out
}

/// Read one resource by URI; returns (mimeType, text).
pub fn read_resource(registry: &Registry, uri: &str) -> Result<(String, String), String> {
    if uri == "offline://llms.txt" {
        return Ok(("text/plain".into(), llms::llms_txt(registry)));
    }
    if uri == "offline://llms-full.txt" {
        let mut out = Vec::new();
        llms::write_llms_full_txt(registry, &mut out).map_err(|e| e.to_string())?;
        let text = String::from_utf8(out).map_err(|e| e.to_string())?;
        return Ok(("text/plain".into(), text));
    }
    if let Some(name) = uri.strip_prefix("offline://skills/") {
        let s = registry
            .skill(name)
            .ok_or_else(|| format!("skill `{name}` not found"))?;
        return Ok((
            "text/markdown".into(),
            format!("# {}\n\n{}", s.title, s.body),
        ));
    }
    if let Some(name) = uri.strip_prefix("offline://workflows/") {
        let w = registry
            .workflow(name)
            .ok_or_else(|| format!("workflow `{name}` not found"))?;
        return Ok((
            "application/json".into(),
            serde_json::to_string_pretty(w).expect("workflow serializes"),
        ));
    }
    if let Some(key) = uri.strip_prefix("offline://packages/") {
        let p = registry
            .packages()
            .find(|p| file_key(&p.name) == key)
            .ok_or_else(|| format!("package `{key}` not found"))?;
        return Ok((
            "application/json".into(),
            serde_json::to_string_pretty(p).expect("package serializes"),
        ));
    }
    if let Some(name) = uri.strip_prefix("offline://templates/") {
        let t = registry
            .template(name)
            .ok_or_else(|| format!("template `{name}` not found"))?;
        return Ok((
            "application/json".into(),
            serde_json::to_string_pretty(t).expect("template serializes"),
        ));
    }
    Err(format!("unknown resource uri `{uri}`"))
}

fn file_key(package_name: &str) -> String {
    package_name
        .rsplit('/')
        .next()
        .unwrap_or(package_name)
        .to_string()
}