Purpose
fetch-guard addresses a core problem: LLMs need web content, but raw HTML is both noisy and potentially hostile. A fetched page may contain hidden text, invisible Unicode, off-screen elements, or even prompt injection attempts embedded in the content itself. This pipeline strips all of that before the content reaches the model.
It is an MCP server and CLI tool for fetching URLs and returning clean, LLM-friendly Markdown, along with metadata and multi-layer prompt injection defenses.
Key Capabilities
Three-Layer Injection Defense
-
Pre-extraction sanitization: Removes hidden elements (display:none, visibility:hidden, opacity:0, font-size:0, transform:scale(0), clip:rect(0,0,0,0), zero-height overflow containers, and elements whose foreground color matches the background color), elements hidden via CSS class/ID rules in <style> tags, off-screen positioned content, aria-hidden elements, <noscript> and <template> tags, and 26 categories of non-printing Unicode characters (including bidirectional isolates and Unicode Tags). This step runs before content extraction, so trafilatura never sees the attack vectors.
-
Pattern scanning: Runs a four-stage scan on the extracted text and metadata fields. The first stage applies 50 compiled regex patterns covering system prompt overrides, ignore previous instructions, role injection, forged conversation tags, and hidden instruction markers, with support for English, Spanish, French, German, Japanese, Simplified Chinese, and Portuguese. The second stage normalizes text via NFKC and confusable character mapping, then rescans to catch homoglyph bypasses (for example, replacing Latin letters with Cyrillic or mathematical Unicode characters). The third stage looks for base64, hex-encoded, and URL percent-encoded blocks, decodes them, and scans them against high-severity patterns. The fourth stage ROT13-decodes the entire document and scans it against high-severity patterns. Metadata fields (title, description, og:title, etc.) are scanned independently, and matches are namespaced by source field.
-
Session-salted output wrapping: Generates a random 8-character hex salt per invocation and wraps the body in <fetch-content-{salt}> tag. Because the salt value is unpredictable, injected content cannot forge the wrapping boundary.
Single-tool MCP server
This is a single-tool MCP server that exposes only one tool—fetch—running the complete extraction pipeline behind a consistent interface. No tool selection, routing, or multi-step workflows. One URL input, one structured result output, configurable via parameters.
Pipeline 13-step sequence
Run a 13-step sequence from URL to structured output:
- /llms.txt precheck: Check /llms.txt at the domain root before the full fetch. If the requested URL is the domain root and /llms.txt exists, that content completely replaces the regular HTML pipeline.
- Fetch: Make a static HTTP request via requests, or when --js is set, use Playwright to drive browser rendering. There is no automatic fallback between the two: --js is an explicit opt-in.
- Edge detection: Classify the response, identifying bot blocking (Cloudflare challenges, 403/429/503 with blocking signatures, LinkedIn custom 999), paywalls (subscription prompts, premium overlays), and login walls (login redirects, members-only mode).
- Automatic retry: Bot blocking triggers one retry using a full Chrome User-Agent string, then reports. Paywalls and login walls are reported immediately without retry.
- Content type routing: Non-HTML responses take a fast path: JSON is rendered as a fenced code block, RSS/Atom feeds are parsed into structured summaries, CSV is converted to a Markdown table (capped at 2,000 rows), and plain text passes through directly. Binary content types are rejected.
- HTML sanitization: Strip hidden elements (including extended CSS visibility techniques, color-matched text, and <template> tags), off-screen positioned content, aria-hidden nodes, <noscript> tags, and non-printing Unicode. Return statistics for all removed items.
- Content extraction: trafilatura converts the sanitized HTML into Markdown that preserves links.
- Metadata extraction: Extract title, author, date, description, canonical URL, and image from three sources in priority order.Like: JSON-LD, Open Graph, then meta tags.
- Link extraction: Two modes: domains returns a sorted list of unique external domains, full returns all external URLs grouped by domain along with anchor text.
- Injection scanning: A four-stage scan (see above). Each match records the pattern name, severity (high/medium), and a 60-character context snippet.
- Size protection + truncation: By default, content exceeding 2MB (before extraction) or 20KB (after extraction) raises an error and suggests a max_words value. Setting --max-words disables both limits and truncates instead.
- Salt wrapping: The body is wrapped in session-salted tags to achieve defense in depth.
- Output formatting: The CLI produces five plain-text sections (status header, body, metadata, links, injection details). The MCP server returns a structured JSON dictionary with the same data.
Usage
Installation
For JavaScript rendering (optional):
Configure the MCP client: add the following to the MCP client configuration. Works with Claude Code, Claude Desktop, Cursor, or any MCP-compatible client.
Via uvx (recommended):
Via pip install:
Note: The Docker image does not include Playwright. JavaScript rendering (js: true) is unavailable when running via Docker. For JS rendering, use uvx or pip install.
Verify the connection
Have the AI assistant fetch any URL. If it returns structured content with a status header, metadata, and risk assessment, it is connected.
Claude Code Skill
Copy resources/fetch-guard/ into .claude/skills/fetch-guard/ in your project, or use the standalone command file resources/fetch-guard.md as a Claude Code command.
CLI output
Five sections are printed to stdout:
- Status header: URL, fetch timestamp, risk flag (OK or INJECTION WARNING), purification statistics, detected edge case information
- Body: clean Markdown wrapped in <fetch-content-{salt}> tags
- Metadata: a JSON block containing title, author, date, description, canonical URL, images
- External links: a domain list or a full URL breakdown grouped by domain
- Injection details: each matched pattern name, severity, and context snippet (only present when a pattern is detected)
Strict mode
When --strict is set and the risk level is HIGH, the CLI exits with code 2, and the MCP server raises an error response. In both cases the full results are still available.
Use cases
- Applications that need to safely provide web content to LLMs
- Building MCP clients (Claude Code, Claude Desktop, Cursor, etc.) that need reliable web scraping
- Automated content processing pipelines that need defense against prompt injection
- Scraping tasks that need to handle edge cases such as bot blocking, paywalls, and login walls
Limitations
- The Docker image does not include Playwright, so JavaScript rendering (js: true) is unavailable when running via Docker.
- There is no automatic fallback between static requests and Playwright rendering; --js is an explicit opt-in.
- By default, content exceeding 2MB (before extraction) or 20KB (after extraction) raises an error; setting --max-words disables the limit and truncates instead.
- Binary content types are rejected.
- CSV-to-Markdown table conversion is capped at 2,000 rows.
Testing and CI
The test suite has two layers:
- Unit tests (424, all mocked—no network calls): each module has a corresponding test file. CI runs the full suite against Python 3.10, 3.12, and 3.13 on every push and PR.
- Live integration tests (54 entries, real network): the live suite is data-driven: five YAML catalogs in tests/catalogs/ define URL entries with typed assertions. A single parameterized runner (test_catalog.py) evaluates all entries. Live tests run automatically at release time via a separate live-tests.yml workflow.
CI runs via GitHub Actions on pushes and PRs to main, testing against Python 3.10, 3.12, and 3.13.