
Build an MCP Server for Agentic Web Scraping and Real-Time LLM Grounding
Large Language Models LLMs operate in a vacuum. To build autonomous agents that perform market research, track public pricing across ecommerce sites, or...
AlterLab handles this automatically — scrape any URL with one API call. No infrastructure required.
Try it freeLarge Language Models (LLMs) operate in a vacuum. To build autonomous agents that perform market research, track public pricing across e-commerce sites, or analyze real estate listings, you must provide them with real-time access to the web. Static Retrieval-Augmented Generation (RAG) is insufficient for data that changes hourly. Agents need the ability to reach out, fetch current pages, and read the contents.
The Model Context Protocol (MCP) standardizes how AI models connect to external tools. Instead of writing custom tool-calling logic for every agent framework (LangChain, LlamaIndex, AutoGen), you write an MCP server once. Any MCP-compatible client—including Claude Desktop—can then discover and execute your tools automatically.
This tutorial demonstrates how to build an MCP server that gives your AI agents the ability to read the web. We will build a Python-based server that exposes a single tool for data extraction, utilizing an external infrastructure layer to handle headless browsers and proxy rotation.
The Architecture of Agentic Scraping
When an agent needs real-time data, it enters a standard tool-calling loop. The MCP architecture cleanly separates the reasoning engine from the execution environment.
By isolating the extraction logic within an MCP server, your agent does not need to know about timeouts, HTTP headers, or network retries. It simply requests a URL and receives text.
Core Concept: Preparing Data for the Context Window
Before writing the server, we must address the most common failure point in agentic scraping: token limits.
Raw HTML from modern single-page applications is bloated with inline CSS, SVG paths, and minified JavaScript. Feeding an 800KB HTML file into an agent's context window will instantly exhaust token limits and degrade the model's reasoning capabilities.
The solution is converting HTML into clean Markdown before returning it to the agent. This strips the structural noise while preserving the semantic hierarchy (headings, links, tables) that the LLM needs to understand the page structure.
Test raw data extraction and Markdown conversion before feeding it to your agent
Data Extraction: cURL vs. Python
To implement the extraction, we use AlterLab. When your agent requests a URL, the MCP server will fire an API request to fetch the cleaned data.
Here is the exact same extraction operation demonstrated in both cURL and Python. Notice the format="markdown" parameter, which is critical for LLM consumption.
# cURL Implementation
curl -X POST https://api.alterlab.io/v1/scrape \
-H "X-API-Key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"url": "https://example.com",
"format": "markdown",
"render_js": true
}'# Python SDK Implementation
from alterlab import AlterLabSync
client = AlterLabSync("YOUR_API_KEY")
# The highlight below shows the critical LLM-optimization parameters
response = client.scrape(
url="https://example.com",
mode="js"
)
print(response["content"])If you are building complex data pipelines, checking the Python SDK documentation will provide advanced configuration options for specific site architectures.
Building the MCP Server
We will use the official mcp Python package provided by Anthropic. This package abstracts away the JSON-RPC messages and standard I/O handling, allowing you to define tools using standard Python decorators and type hints.
Prerequisites
Initialize a new Python project and install the required dependencies:
mkdir agent-scraper-mcp
cd agent-scraper-mcp
python -m venv venv
source venv/bin/activate
pip install mcp alterlab pydanticThe Server Code
Create a file named server.py. This script initializes the MCP server and registers the web scraping tool. The descriptive docstrings inside the tool definition are critical—the MCP protocol passes these descriptions directly to the LLM so it knows when and how to use the tool.
from alterlab import AlterLabSync
client = AlterLabSync(api_key="YOUR_API_KEY")
result = client.scrape(
"https://example.com",
extraction_schema={"type": "object", "properties": {"title": {"type": "string"}, "price": {"type": "string"}, "description": {"type": "string"}}},
)
print(result["filtered_content"]) # dict matching your schemaHandling Anti-Bot and Dynamic Content
You might wonder why we don't just use Python's requests library inside the MCP tool.
When agents operate autonomously, they frequently encounter Cloudflare challenges, Datadome blocks, and pages that require extensive JavaScript rendering to populate the DOM. If your agent's requests.get() call returns a 403 Forbidden or an empty HTML skeleton, the agent will hallucinate an answer based on the failure message or simply crash the workflow.
By delegating the extraction to an infrastructure layer with robust anti-bot handling, the MCP server guarantees that the agent receives the actual page content. The agent focuses purely on semantic reasoning, while the API handles proxy rotation, headless browser management, and fingerprinting.
Connecting the Server to an Agent
MCP servers typically communicate over Standard I/O (stdio). This means the agent framework spawns the server as a subprocess and communicates via standard input and output streams.
Testing with Claude Desktop
The easiest way to test your new server is by plugging it into Claude Desktop. You need to modify Claude's configuration file to point to your Python script.
Configuration file locations:
- macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - Windows:
%APPDATA%\Claude\claude_desktop_config.json
Add your server to the mcpServers object:
{
"mcpServers": {
"web-scraper": {
"command": "/path/to/your/agent-scraper-mcp/venv/bin/python",
"args": [
"/path/to/your/agent-scraper-mcp/server.py"
],
"env": {
"ALTERLAB_API_KEY": "your_api_key_here"
}
}
}
}Restart Claude Desktop. You will now see a small "plug" icon indicating available tools. You can issue prompts like:
"Read the documentation at https://docs.python.org/3/library/asyncio.html and summarize the latest changes to the TaskGroup API."
Claude will recognize that it lacks real-time knowledge of that URL, invoke the scrape_public_url tool via MCP, wait for the Markdown response, and formulate a correct, grounded answer based on the live page content.
Production Considerations for Agentic Pipelines
When moving from local testing to production agent deployments (e.g., deploying on AWS or running background workers with LangGraph), keep these architectural principles in mind:
- Timeout Management: Web extraction can take anywhere from 1 to 15 seconds depending on the target's rendering complexity. Ensure your MCP client and the overlying LLM API calls have appropriate timeout buffers configured.
- Context Window Protection: The truncation logic in the
server.pysnippet (content[:100000]) is critical. Unbounded scraping returns will triggercontext_length_exceedederrors from your LLM provider. - Structured Data: If your agent specifically needs JSON output instead of Markdown, you can define a secondary tool in your MCP server (
extract_structured_data) and utilize Cortex AI to map the DOM into a predefined JSON schema. Read the API docs for implementation details on schema enforcement.
Takeaways
Building an MCP server bridges the gap between static LLM reasoning and real-time internet data.
- Use the Model Context Protocol to write tool definitions once, allowing any compliant agent framework to discover and use your extraction capabilities.
- Never feed raw HTML into an agent. Always convert to Markdown to preserve context windows and reduce token costs.
- Offload browser management and proxy rotation to dedicated infrastructure so your AI agents can focus strictly on reasoning and analysis.
By implementing this architecture, you transform isolated language models into capable, internet-aware research assistants.
Was this article helpful?
Frequently Asked Questions
Related Articles

Oxylabs Alternative: Practical Developer Guide for Web Scraping Pipelines
Build reliable scraping pipelines with anti-bot handling, auto-escalation, and monitoring using a practical AltLab-compatible solution. Follow the 3-step setup, integrate with your existing workflow, and control costs at $0.0002 per request.
Herald Blog Service

Data pipeline architecture for large-scale web scraping projects
Learn how to design a scalable, resilient data pipeline for web scraping at scale—covering ingestion, queuing, processing, storage, and monitoring with proven patterns and tools.
Herald Blog Service

Python aiohttp vs. httpx for High-Throughput Web Scraping
A benchmark‑driven comparison of aiohttp and httpx for asynchronous scraping, with concrete numbers, code snippets, and guidance on when to choose each library.
Herald Blog Service
Popular Posts
Recommended

Selenium Bot Detection: Why You Get Flagged and How to Fix It

Ethical Web Scraping: Robots.txt, Rate Limits, and ToS

Why Your Headless Browser Gets Detected (and How to Fix It)

Mastering Playwright Stealth for Agentic Web Workflows

How to Scrape E-Commerce Sites for AI Agents Using Playwright and LLMs
Newsletter
Scraping insights and API tips. No spam.
Recommended Reading

Selenium Bot Detection: Why You Get Flagged and How to Fix It

Ethical Web Scraping: Robots.txt, Rate Limits, and ToS

Why Your Headless Browser Gets Detected (and How to Fix It)

Mastering Playwright Stealth for Agentic Web Workflows

How to Scrape E-Commerce Sites for AI Agents Using Playwright and LLMs
Stay in the Loop
Get scraping insights, API tips, and platform updates. No spam — we only send when we have something worth reading.
Explore AlterLab
Web Scraping API Resources
Part of the Web Scraping API Documentation cluster
Complete API reference with 5-tier auto-escalation — Curl to challenge resolution.
Pillar pageConfigure Tier 4 browser rendering for SPAs and dynamic content.
Scrape pages behind login using session management.
Real success rates and cost data across all 5 tiers.
MCP Server, Python SDK, and Firecrawl-compatible API for AI agent workflows.