use std::collections::HashMap;
use std::sync::Arc;
use anyhow::{Context, Result, bail};
use rustdoc_types::Crate;
use tokio::sync::Mutex;
const DOCS_RS: &str = "https://docs.rs";
const USER_AGENT: &str = "rustdocs-mcp/0.1 (open-grok MCP server)";
pub async fn load_rustdoc(
client: &reqwest::Client,
cache: &Mutex<HashMap<String, Arc<Crate>>>,
krate: &str,
version: &str,
) -> Result<Arc<Crate>> {
let key = format!("{krate}@{version}");
{
let cache = cache.lock().await;
if let Some(docs) = cache.get(&key) {
return Ok(Arc::clone(docs));
}
}
let url = format!("{DOCS_RS}/crate/{krate}/{version}/json.zst");
let bytes = client
.get(&url)
.header("User-Agent", USER_AGENT)
.send()
.await
.with_context(|| format!("failed to download rustdoc JSON: {url}"))?;
if !bytes.status().is_success() {
bail!("docs.rs returned HTTP {} for {url}", bytes.status());
}
let bytes = bytes.bytes().await?;
let json = zstd::stream::decode_all(bytes.as_ref())
.with_context(|| format!("failed to decompress rustdoc JSON for {krate}@{version}"))?;
let docs: Crate = match serde_json::from_slice(&json) {
Ok(c) => c,
Err(e) => {
let format_version = serde_json::from_slice::<serde_json::Value>(&json)
.ok()
.and_then(|v| v.get("format_version").and_then(|f| f.as_u64()))
.unwrap_or(0);
bail!(
"cannot parse rustdoc JSON for {krate}@{version} (format_version {format_version}, expected {}): {e}",
rustdoc_types::FORMAT_VERSION
);
}
};
if docs.format_version != rustdoc_types::FORMAT_VERSION {
bail!(
"rustdoc JSON for {krate}@{version} has format_version {}, but this build of rustdocs-mcp supports {}",
docs.format_version,
rustdoc_types::FORMAT_VERSION
);
}
let docs = Arc::new(docs);
let mut cache = cache.lock().await;
cache.insert(key, Arc::clone(&docs));
Ok(docs)
}
pub async fn get_source(
client: &reqwest::Client,
krate: &str,
version: &str,
path: &str,
) -> Result<String> {
let url = format!("https://static.crates.io/crates/{krate}/{krate}-{version}.crate");
let resp = client
.get(&url)
.header("User-Agent", USER_AGENT)
.send()
.await
.with_context(|| format!("failed to fetch crate archive: {url}"))?;
if !resp.status().is_success() {
bail!("static.crates.io returned HTTP {} for {url}", resp.status());
}
let bytes = resp.bytes().await?;
let gz = flate2::read::GzDecoder::new(bytes.as_ref());
let mut archive = tar::Archive::new(gz);
let wanted = format!("{krate}-{version}/{path}");
let mut found: Option<Vec<u8>> = None;
for entry in archive.entries()? {
let mut entry = entry?;
let entry_path = entry.path()?.into_owned();
let entry_path = entry_path.to_string_lossy().replace('\\', "/");
if entry_path == wanted {
let mut buf = Vec::new();
use std::io::Read as _;
entry.read_to_end(&mut buf)?;
found = Some(buf);
break;
}
}
let Some(bytes) = found else {
bail!("file '{path}' not found in {krate}-{version}.crate");
};
String::from_utf8(bytes).with_context(|| format!("source file '{path}' is not valid UTF-8"))
}