use crate::lang::{CARD, Out, WORDS, bin, local_path};
use rmcp::model::{
CallToolRequestParams, CallToolResponse, CallToolResult, ContentBlock, Implementation, ListToolsResult,
PaginatedRequestParams, ProgressNotificationParam, ProgressToken, ServerCapabilities, ServerConfig, Tool,
ToolAnnotations,
};
use rmcp::service::{Peer, RequestContext};
use rmcp::{ErrorData, RoleServer, ServerHandler, ServiceExt};
use std::path::PathBuf;
pub const DESCRIPTION: &str = "Web pages, PDFs and local documents as small cached Markdown with ids. \
cmd = one fats command line, e.g. `https://example.com/menu.pdf` (fetch → outline with ids) or \
`u1#3` / `u1 tuna price` (read section 3 / the lines with those words). First time? Call fats(\"guide\").";
pub fn instructions() -> String {
format!(
"fats turns web pages, PDFs and documents into small, clean, cached Markdown. The one tool `fats` takes \
the same command line as the `fats` CLI and returns the same text.\n\n{CARD}\n\n\
Every reply starts with a `=` line (what happened, which id); ids it prints (u1, u1#3, l4) are what the \
next cmd takes. A `!` row names what failed and, after →, the command that fixes it. Pages stay cached \
in this session: re-reading u1#3 fetches nothing. Local files: the working directory and the cache only. \
An empty cmd prints the status. Anonymous usage stats (no URL, search or key): \
`fats telemetry show` = what is sent · off: `fats setup telemetry off`.\n\nFirst time? Call fats(\"guide\")"
)
}
fn tool() -> Tool {
let cmd = serde_json::json!({"type": "string", "description": "e.g. u1#3"});
let schema = serde_json::json!({"type": "object", "properties": {"cmd": cmd}, "required": ["cmd"]});
let schema = schema.as_object().cloned().unwrap_or_default();
let hints = ToolAnnotations::new().read_only(false).destructive(false).open_world(true);
Tool::new("fats", DESCRIPTION, schema).with_annotations(hints)
}
struct Fats;
impl ServerHandler for Fats {
fn get_info(&self) -> ServerConfig {
ServerConfig::new(ServerCapabilities::builder().enable_tools().build())
.with_server_info(Implementation::new("fats", env!("CARGO_PKG_VERSION")))
.with_instructions(instructions())
}
async fn list_tools(
&self,
_req: Option<PaginatedRequestParams>,
_ctx: RequestContext<RoleServer>,
) -> Result<ListToolsResult, ErrorData> {
Ok(ListToolsResult::with_all_items(vec![tool()]))
}
async fn call_tool(
&self,
req: CallToolRequestParams,
ctx: RequestContext<RoleServer>,
) -> Result<CallToolResponse, ErrorData> {
if req.name != "fats" {
return Err(ErrorData::invalid_params(format!("unknown tool {} · the one tool is fats", req.name), None));
}
let Some(cmd) = req.arguments.as_ref().and_then(|a| a.get("cmd")).and_then(|v| v.as_str()) else {
return Err(ErrorData::invalid_params("arguments.cmd: want a string, e.g. u1#3", None));
};
let (cmd, asked) = (vec![cmd.to_string()], vec![cmd.to_string()]);
let work = tokio::task::spawn_blocking(move || crate::cli::run(&cmd));
let (text, rc, image) = ticking(work, ctx.meta.get_progress_token(), &ctx.peer)
.await
.map_err(|e| ErrorData::internal_error(format!("fats crashed: {e}"), None))?;
tokio::task::spawn_blocking(move || crate::telemetry::tick(&asked));
let mut blocks = vec![ContentBlock::text(text)];
blocks.extend(image.map(|p| match pixels(std::path::Path::new(&p)) {
Some((b64, mime)) => ContentBlock::image(b64, mime),
None => ContentBlock::text(format!("! not_viewable · Claude reads png, jpeg, gif, webp → open {p}")),
}));
Ok(if rc == 0 { CallToolResult::success(blocks) } else { CallToolResult::error(blocks) }.into())
}
}
async fn ticking<T>(
mut work: tokio::task::JoinHandle<T>,
token: Option<ProgressToken>,
peer: &Peer<RoleServer>,
) -> Result<T, tokio::task::JoinError> {
let Some(token) = token else { return work.await };
let mut s = 0;
loop {
if let Ok(done) = tokio::time::timeout(crate::cli::TICK, &mut work).await {
return done;
}
s += crate::cli::TICK.as_secs();
let msg = format!("still working · {s} s");
peer.notify_progress(ProgressNotificationParam::new(token.clone(), s as f64).with_message(msg)).await.ok();
}
}
fn pixels(path: &std::path::Path) -> Option<(String, &'static str)> {
let b64 = |b: Vec<u8>| base64::Engine::encode(&base64::engine::general_purpose::STANDARD, b);
let (mut reader, mut limits) =
(image::ImageReader::open(path).ok()?.with_guessed_format().ok()?, image::Limits::default());
(limits.max_image_width, limits.max_image_height, limits.max_alloc) = (Some(12_000), Some(12_000), Some(256 << 20));
reader.limits(limits);
let (format, raw) = (reader.format()?, std::fs::read(path).ok().filter(|b| b.len() <= 3_700_000));
let img = reader.decode().ok()?;
let (size, _) = crate::outline::viewed(img.width(), img.height(), crate::outline::STD);
if let Some(b) = raw.filter(|_| size == (img.width(), img.height())) {
return Some((b64(b), format.to_mime_type()));
}
let mut out = std::io::Cursor::new(Vec::new());
img.thumbnail_exact(size.0, size.1).to_rgb8().write_to(&mut out, image::ImageFormat::Jpeg).ok()?;
Some((b64(out.into_inner()), "image/jpeg"))
}
pub type Host = (&'static str, Vec<&'static str>, &'static str, &'static str);
pub fn added_at(h: &Host) -> Option<&'static str> {
let here = crate::cli::dir_key(&std::env::current_dir().unwrap_or_default().to_string_lossy());
let json = |v: serde_json::Value| {
let local = v["projects"].as_object().into_iter().flatten().filter(|(k, _)| crate::cli::dir_key(k) == here);
[&v].into_iter().chain(local.map(|p| p.1)).any(|v| !v["mcpServers"]["fats"].is_null())
};
let toml = |t: &str| t.lines().any(|l| l.trim() == "[mcp_servers.fats]");
let on = |t: String| toml(&t) || t.contains("fats") && serde_json::from_str(&t).is_ok_and(json);
h.1.iter().copied().find(|p| std::fs::read_to_string(path_of(p)).is_ok_and(on))
}
fn names(added: Option<bool>, sep: &str) -> String {
let on = |h: &&Host| added.is_none_or(|a| a == added_at(h).is_some());
WORDS.mcp_hosts.iter().filter(on).map(|h| h.0).collect::<Vec<_>>().join(sep)
}
pub fn hosts_row(b: &str) -> String {
let (added, left) = (names(Some(true), ", "), names(Some(false), "|"));
let add = if left.is_empty() { String::new() } else { format!(" · add: {b} setup mcp {left}") };
match (crate::cli::FENCE.get(), added.is_empty()) {
(Some(_), _) => "mcp: serving (this process) · local files from the working directory + the cache".into(),
(None, true) => format!("mcp: not added to {}{add}", names(None, ", ")),
(None, false) => format!("mcp: added to {added} · stdio, one tool fats(cmd){add}"),
}
}
pub fn buttons(p: &str) -> impl Iterator<Item = String> {
let row = |h: &Host| format!("Add to {}{}", h.3, ["", " · added ✓"][usize::from(added_at(h).is_some())]);
WORDS.mcp_hosts.iter().filter(move |_| p == "mcp").map(row)
}
pub fn pressed(p: &str, at: usize, dry: bool) -> Option<String> {
add(&[WORDS.mcp_hosts.get(at).filter(|_| p == "mcp")?.0], dry).lines.pop()
}
fn path_of(p: &str) -> std::path::PathBuf {
let set = p.split_once('=').and_then(|(v, p)| Some(crate::settings::env(v)? + &p[p.rfind('/')?..]));
local_path(set.as_deref().unwrap_or(shown(p)))
}
pub fn add(host: &[&str], dry: bool) -> Out {
let Some(name) = host.first() else { return Out::new(vec![format!("= setup {}", hosts_row(bin()))]) };
let Some(h) = WORDS.mcp_hosts.iter().find(|h| h.0 == *name) else {
return Out::ask(&format!("? setup mcp {name} · hosts: {} setup mcp {}", bin(), names(None, "|")));
};
match (added_at(h), h.2.contains("{b}")) {
(Some(at), _) => Out::new(vec![format!("= setup mcp {name} already added · {} · $0", shown(at))]),
(None, true) => run(h, dry),
(None, false) if dry => Out::new(vec![format!("= dry setup mcp {name} · would write {} · $0", h.1[0])]),
(None, false) => write(h),
}
}
fn shown(p: &str) -> &str {
p.rsplit('=').next().unwrap_or(p)
}
fn server() -> Vec<String> {
let exe = std::env::current_exe().unwrap_or_else(|_| bin().into());
let same = |p: &PathBuf| std::fs::canonicalize(p).ok() == std::fs::canonicalize(&exe).ok();
let path = on_path(bin()).filter(same).unwrap_or(exe).display().to_string();
if cfg!(windows) { vec!["cmd".into(), "/c".into(), path] } else { vec![path] }
}
fn on_path(name: &str) -> Option<PathBuf> {
let ext: &[&str] = if cfg!(windows) { &[".exe", ".cmd", ".bat"] } else { &[""] };
let dirs = std::env::var_os("PATH")?;
std::env::split_paths(&dirs)
.flat_map(|d| ext.iter().map(move |e| d.join(format!("{name}{e}"))))
.find(|p| p.is_file())
}
fn run(h: &Host, dry: bool) -> Out {
let server = server();
let words: Vec<String> =
h.2.split(' ').flat_map(|w| if w == "{b}" { server.clone() } else { vec![w.into()] }).collect();
let quote = |w: &String| if w.contains(' ') { format!("'{w}'") } else { w.clone() };
let line = words.iter().map(quote).collect::<Vec<_>>().join(" ");
let (name, tool) = (h.0, words[0].as_str());
let fail = |code: &str, why: String| {
Out::fail(code, format!("= ! {code} setup mcp {name} · nothing added · $0"), format!("! {code} {name} · {why}"))
};
let Some(exe) = on_path(tool) else {
return fail("no_host", format!("{tool} not on PATH · install {} first, or add by hand: {line}", h.3));
};
if dry {
return Out::new(vec![format!("= dry setup mcp {name} · would run {line} · $0")]);
}
let ran = std::process::Command::new(exe).args(&words[1..]).stdin(std::process::Stdio::null()).output();
match ran {
Ok(_) if added_at(h).is_some() => Out::new(vec![format!("= setup mcp {name} added · {line} · $0")]),
Ok(o) if o.status.success() => fail("host_failed", format!("ran {line}, fats not in {}", shown(h.1[0]))),
Ok(o) => {
let said = [o.stderr, o.stdout].map(|b| String::from_utf8_lossy(&b).trim().to_string());
let first = said.iter().find_map(|s| s.lines().next()).unwrap_or("no output");
fail("host_failed", format!("rc {} · {first}", o.status.code().unwrap_or(-1)))
}
Err(e) => fail("host_failed", format!("{tool} did not start: {e}")),
}
}
fn write(h: &Host) -> Out {
let (name, at) = (h.0, shown(h.1[0]));
let server = [server(), vec!["mcp".into()]].concat();
let entry = serde_json::json!({"command": server[0], "args": server[1..]});
let fail = |why: String| {
let row = format!("! host_failed {name} · {at} {why} · add by hand: mcpServers.fats = {entry}");
Out::fail("host_failed", format!("= ! host_failed setup mcp {name} · nothing written · $0"), row)
};
let file = path_of(h.1[0]);
let text = std::fs::read_to_string(&file).unwrap_or_default();
let mut v: serde_json::Value = match serde_json::from_str(if text.trim().is_empty() { "{}" } else { &text }) {
Ok(v) => v,
Err(e) => return fail(format!("is not JSON ({e})")),
};
let servers = v.as_object_mut().map(|o| o.entry("mcpServers").or_insert_with(|| serde_json::json!({})));
let Some(servers) = servers.and_then(serde_json::Value::as_object_mut) else {
return fail("holds no mcpServers object".into());
};
servers.insert("fats".into(), entry.clone());
match crate::cache::jsave(&file, &v) {
Ok(()) => Out::new(vec![format!("= setup mcp {name} added · {at} · $0")]),
Err(e) => fail(format!("not written: {e}")),
}
}
pub fn serve() -> i32 {
let mut fence = vec![crate::settings::get().cache.value.clone()];
fence.extend(std::env::current_dir());
crate::cli::fence(fence);
let served = crate::provider::block(async {
let running = Fats.serve(rmcp::transport::stdio()).await.map_err(|e| e.to_string())?;
running.waiting().await.map(|_| 0).map_err(|e| e.to_string())
});
served.unwrap_or_else(|| Err("no async runtime".into())).inspect_err(|e| eprintln!("fats mcp: {e}")).unwrap_or(1)
}
#[cfg(test)]
mod tests {
use super::*;
fn saved(name: &str, bytes: &[u8]) -> (crate::scratch::Scratch, std::path::PathBuf) {
let dir = crate::scratch::Scratch::new(&format!("mcp-{name}"));
std::fs::write(dir.join(name), bytes).expect("write");
let file = dir.join(name);
(dir, file)
}
#[test]
fn big_file_goes_as_jpeg() {
let mut seed = 7u32;
let noisy = image::RgbaImage::from_fn(1000, 1000, |_, _| {
seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
image::Rgba(seed.to_be_bytes())
});
let mut png = std::io::Cursor::new(Vec::new());
noisy.write_to(&mut png, image::ImageFormat::Png).expect("png");
assert!(png.get_ref().len() > 3_700_000, "the raw file is over the cap");
let (b64, mime) = pixels(&saved("noisy.png", png.get_ref()).1).expect("pixels");
assert_eq!(mime, "image/jpeg");
assert!(b64.len() < 5_000_000, "{} B of base64", b64.len());
}
#[test]
fn huge_header_is_refused_fast() {
let mut png = std::io::Cursor::new(Vec::new());
image::RgbImage::new(1, 1).write_to(&mut png, image::ImageFormat::Png).expect("png");
let mut png = png.into_inner();
png[16..24].copy_from_slice(&[0, 0, 0x75, 0x30, 0, 0, 0x75, 0x30]); let crc = crc32(&png[12..29]);
png[29..33].copy_from_slice(&crc.to_be_bytes());
let t0 = std::time::Instant::now();
assert!(pixels(&saved("huge.png", &png).1).is_none());
assert!(t0.elapsed().as_millis() < 500, "{:?}", t0.elapsed());
let mut wide = std::io::Cursor::new(Vec::new());
image::GrayImage::new(12_001, 1).write_to(&mut wide, image::ImageFormat::Png).expect("png");
assert!(pixels(&saved("wide.png", wide.get_ref()).1).is_none());
}
#[test]
fn a_dir_key_is_one_spelling() {
let key = crate::cli::dir_key;
let keys = [r"\\?\C:\Work\fats", r"C:\Work\fats", "C:/Work/fats", "c:/work/fats"].map(key);
assert_eq!(keys.iter().all(|k| *k == keys[0]), cfg!(windows), "{keys:?}");
assert_eq!(key(r"\\?\UNC\srv\share"), key(r"\\srv\share"), "the verbatim prefix is no part of a key");
assert_eq!(key("/home/me/Work"), if cfg!(windows) { r"\home\me\work" } else { "/home/me/Work" });
}
fn crc32(bytes: &[u8]) -> u32 {
!bytes.iter().fold(!0u32, |c, &b| {
(0..8).fold(c ^ u32::from(b), |c, _| if c & 1 == 1 { 0xEDB8_8320 ^ (c >> 1) } else { c >> 1 })
})
}
}