UNPKG

mcp-wayback-machine

Version:

MCP server and CLI tool for interacting with the Wayback Machine without API keys

83 lines 3.06 kB
/** * * List Screenshots tool — finds captures that have screenshots via CDX cross-reference. */ import * as z from "zod"; import { CdxResponse } from "../schemas.js"; import { HttpError } from "../utils/http.js"; import { HttpUrl, formatTimestamp } from "../utils/validation.js"; export const ListScreenshots = z.object({ url: HttpUrl.meta({ description: "The URL to find screenshots for" }), limit: z .int() .min(1) .default(10) .meta({ description: "Maximum number of screenshot results" }), }); /** * List available screenshots for a URL by cross-referencing the CDX API. * Screenshots are stored at web.archive.org/screenshot/{url}/* and are * generated when captures are made with capture_screenshot=1. */ export async function listScreenshots(input, ctx) { const { url, limit } = input; try { // Search for screenshot entries in CDX const screenshotPrefix = `web.archive.org/screenshot/${url}/*`; const cdxUrl = new URL("https://web.archive.org/cdx/search/cdx"); cdxUrl.searchParams.set("url", screenshotPrefix); cdxUrl.searchParams.set("output", "json"); cdxUrl.searchParams.set("limit", String(limit)); cdxUrl.searchParams.set("fl", "timestamp,original"); const data = await ctx.fetchJSON(cdxUrl.toString(), CdxResponse); // First row is headers if (data.length <= 1) { return { success: true, message: `No screenshots found for ${url}`, totalScreenshots: 0, screenshots: [], }; } const screenshots = data .slice(1) .filter((row) => row[0] !== undefined && row[0].length >= 14) .map((row) => { const ts = String(row[0]); return { timestamp: ts, date: formatTimestamp(ts), screenshotUrl: `https://web.archive.org/web/${ts}im_/${url}`, originalUrl: row[1] ?? "", }; }); return { success: true, message: `Found ${screenshots.length.toString()} screenshot(s) for ${url}`, totalScreenshots: screenshots.length, screenshots, }; } catch (error) { if (error instanceof HttpError && error.status === 404) { return { success: true, message: `No screenshots found for ${url}`, totalScreenshots: 0, screenshots: [], }; } if (error instanceof HttpError) { return { success: false, message: `Failed to list screenshots: ${error.message}`, totalScreenshots: 0, }; } return { success: false, message: `Failed to list screenshots: ${error instanceof Error ? error.message : "Unknown error"}`, totalScreenshots: 0, }; } } //# sourceMappingURL=screenshots.js.map