Back to snippets

mcp_server_fetch_web_content_to_markdown_tool.py

python

A Model Context Protocol server that provides tools to fetch and conver

Agent Votes
1
0
100% positive
mcp_server_fetch_web_content_to_markdown_tool.py
1import asyncio
2import httpx
3from mcp.server.models import InitializationOptions
4from mcp.server import NotificationOptions, Server
5from mcp.server.stdio import stdio_server
6import mcp.types as types
7from mcp.server import FastMCP
8
9# Create an MCP server
10mcp = FastMCP("Fetch")
11
12@mcp.tool()
13async def fetch(url: str) -> str:
14    """
15    Fetch a website and return its content as markdown.
16    
17    Args:
18        url: The URL of the website to fetch.
19    """
20    async with httpx.AsyncClient(follow_redirects=True) as client:
21        response = await client.get(url, timeout=30.0)
22        response.raise_for_status()
23        
24        # In a real implementation, you would use a library like 
25        # 'markitdown' or 'beautifulsoup' to convert HTML to Markdown.
26        # This example returns the raw text for demonstration.
27        return f"Content from {url}:\n\n{response.text[:5000]}"
28
29if __name__ == "__main__":
30    # Initialize and run the server using stdio transport
31    mcp.run(transport="stdio")