Skip to main content
Glama
README.md
# ⚑ MarkScrape

[![CI](https://github.com/jenishpatel/markscrape/actions/workflows/ci.yml/badge.svg)](https://github.com/jenishpatel/markscrape/actions/workflows/ci.yml)
[![License: MIT](https://img.shields.io/badge/License-MIT-emerald.svg)](LICENSE)

Fast, self-hostable **Web-to-Markdown, PDF, Crawl & `llms.txt` API** for AI agents and RAG pipelines.

Built with TypeScript, Fastify, Mozilla Readability, `turndown-plugin-gfm`, SQLite WAL caching, and an SSRF security firewallβ€”with **zero credit multipliers**.

---

## ✨ Features

* **Clean GFM Markdown:** Preserves Markdown tables (`| col | col |`) and `<pre><code>` language fences (` ```typescript `, ` ```python `).
* **Zero-Friction `GET /r/*`:** Hit `GET /r/https://example.com` in any browser or `curl` to get raw `text/markdown` instantly (supports `X-Target-Selector` & `X-Remove-Selector`).
* **AI Context Controls:** Extract `toc`, filter a single `section`, cap output with `maxTokens`, or split into heading-aware RAG `chunks` (`chunkSize`).
* **Change Tracking (`diff`):** SQLite-backed `POST /v1/diff` detects changed pages (`addedLines`, `removedLines`) so RAG pipelines never re-embed unchanged docs.
* **Smart Anti-Bot & JS Detection:** Returns explicit `CAPTCHA_DETECTED`, `BOT_BLOCKED`, or `JS_RENDER_REQUIRED` codes instead of feeding challenge garbage to your LLM.
* **Headless JS & PDF Support:** Optional `renderJs: true` via `playwright-core` for SPAs, plus automatic PDF-to-text via `unpdf`.
* **Site Crawler, Mapper, Batch & Auto-`llms.txt`:** `POST /v1/crawl`, `POST /v1/map`, `POST /v1/batch/scrape` (up to 25 URLs), and `POST /v1/llms-txt`.
* **Built-in Stdio MCP Server:** Plug directly into Cursor, Claude, or Antigravity (`node dist/mcp.js`).
* **Security Hardened:** DNS-resolving SSRF firewall (blocks loopback, private LANs, cloud metadata `169.254.169.254`, and unsafe redirects), 10 MB payload cap, SHA-256 hashed API keys, and DOMPurify-sanitized playground preview.

---

## πŸš€ Quick Start

```bash
pnpm install
pnpm run dev
```

Open **[http://localhost:3001](http://localhost:3001)** for the interactive Playground UI.

### Docker (1-Command Run)
```bash
# Pull prebuilt image from GitHub Container Registry
docker run -p 3001:3001 -e ADMIN_SECRET=your_secret ghcr.io/jenishpatel/markscrape:latest

# Or build locally
docker build -t markscrape .
docker run -p 3001:3001 -e ADMIN_SECRET=your_secret markscrape
```

### Stdio MCP Server (`mcp.json`)
```json
{
  "mcpServers": {
    "markscrape": {
      "command": "node",
      "args": ["/absolute/path/to/markscrape/dist/mcp.js"]
    }
  }
}
```
> **Build first:** Run `pnpm run build` before using the MCP server.

---

## πŸ“‘ API Reference

### 1. Instant URL Reader (Keyless)
```bash
curl http://localhost:3001/r/https://example.com
# Optional selector headers:
# -H "X-Target-Selector: article.docs" -H "X-Remove-Selector: nav, footer"
```

### 2. Full Scrape (`POST /v1/scrape`)
```bash
curl -X POST http://localhost:3001/v1/scrape \
  -H "Content-Type: application/json" \
  -H "X-API-Key: mk_live_..." \
  -d '{
    "url": "https://example.com",
    "formats": ["markdown", "links", "images", "metadata", "toc"],
    "onlyMainContent": true,
    "renderJs": false,
    "section": "Introduction",
    "maxTokens": 2000,
    "chunkSize": 1000,
    "diff": false,
    "maxAge": 300
  }'
```

### 3. Crawler, Mapper, Batch, Diff & `llms.txt`
* `POST /v1/crawl` β€” `{ "url": "https://docs.example.com", "limit": 20, "maxDepth": 2 }` (`202 Accepted` -> `jobId`)
* `GET /v1/crawl/:id` β€” Poll crawl job status (`running` / `completed`) and results
* `POST /v1/diff` β€” `{ "url": "https://docs.example.com" }` (returns `changed`, `addedLines`, `removedLines`)
* `POST /v1/map` β€” `{ "url": "https://docs.example.com", "limit": 100 }`
* `POST /v1/batch/scrape` β€” `{ "urls": ["https://...", "https://..."] }` *(max 25)*
* `POST /v1/llms-txt` β€” `{ "url": "https://docs.example.com", "limit": 15 }`

---

## πŸ”’ Production Security Checklist

Before exposing MarkScrape on the public internet:
1. Set environment variables directly in your shell or process manager (MarkScrape reads them from `process.env` β€” not from a `.env` file at runtime unless you use `node --env-file=.env dist/index.js`).
2. Set a strong `ADMIN_SECRET` β€” this locks `GET /v1/keys` and `DELETE /v1/keys/:id` behind `x-admin-secret` header authentication.
3. `NODE_ENV=production` is set automatically in the Docker image β€” set it in your shell/systemd too.
4. Lower the global IP rate limit in `src/index.ts` (e.g. `max: 60`) and put Cloudflare in front of your domain.

---

## πŸ§ͺ Tests

```bash
pnpm run build
pnpm exec tsx src/test-ai-dx.ts
pnpm exec tsx src/test-crawl.ts
pnpm exec tsx src/test-ab-pro.ts
pnpm exec tsx src/test-p1.ts
pnpm exec tsx src/test-p2-freemium.ts
pnpm run test
pnpm run test:e2e
```

## πŸ“„ License
[MIT](LICENSE)