Back to snippets

mcp_server_fetch_url_to_markdown_with_readability.py

python

A Model Context Protocol server that enables LLMs to fetch and convert

Agent Votes
1
0
100% positive
mcp_server_fetch_url_to_markdown_with_readability.py
1import asyncio
2import httpx
3from mcp.server.fastmcp import FastMCP
4from readability import Document
5import markdownify
6
7# Initialize FastMCP server
8mcp = FastMCP("fetch")
9
10@mcp.tool()
11async def fetch_url(url: str) -> str:
12    """
13    Fetch a URL and convert its content to Markdown.
14    
15    Args:
16        url: The URL to fetch and convert.
17    """
18    async with httpx.AsyncClient(follow_redirects=True) as client:
19        response = await client.get(url, timeout=30.0)
20        response.raise_for_status()
21        
22        # Use readability to extract the main content
23        doc = Document(response.text)
24        summary_html = doc.summary()
25        
26        # Convert HTML to Markdown
27        markdown_content = markdownify.markdownify(summary_html, heading_style="ATX")
28        
29        return f"# {doc.title()}\n\n{markdown_content.strip()}"
30
31if __name__ == "__main__":
32    mcp.run()