use crate::lang::CARD;
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};
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 schema = serde_json::json!({
"type": "object",
"properties": {"cmd": {"type": "string", "description": "e.g. u1#3"}},
"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}")),
}));
let mut r = CallToolResult::success(blocks);
r.is_error = Some(rc != 0); Ok(r.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 tick =
ProgressNotificationParam::new(token.clone(), s as f64).with_message(format!("still working · {s} s"));
peer.notify_progress(tick).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 fn serve() -> i32 {
let mut fence = vec![crate::settings::get().cache.value.clone()];
fence.extend(std::env::current_dir());
crate::cli::fence(fence);
crate::provider::block(async {
match Fats.serve(rmcp::transport::stdio()).await {
Ok(s) => s.waiting().await.map_or(1, |_| 0),
Err(e) => {
eprintln!("fats mcp: {e}");
1
}
}
})
.unwrap_or_else(|| {
eprintln!("fats mcp: no async runtime");
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());
}
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 })
})
}
}