|
| 1 | +import anyio |
| 2 | +import click |
| 3 | +import httpx |
| 4 | +import mcp.types as types |
| 5 | +from mcp.server import Server |
| 6 | + |
| 7 | + |
| 8 | +async def fetch_website( |
| 9 | + url: str, |
| 10 | +) -> list[types.TextContent | types.ImageContent | types.EmbeddedResource]: |
| 11 | + async with httpx.AsyncClient(follow_redirects=True) as client: |
| 12 | + response = await client.get(url) |
| 13 | + response.raise_for_status() |
| 14 | + return [types.TextContent(type="text", text=response.text)] |
| 15 | + |
| 16 | + |
| 17 | +@click.group() |
| 18 | +def cli(): |
| 19 | + pass |
| 20 | + |
| 21 | + |
| 22 | +@cli.command() |
| 23 | +@click.option("--port", default=8000, help="Port to listen on for SSE") |
| 24 | +@click.option( |
| 25 | + "--transport", |
| 26 | + type=click.Choice(["stdio", "sse"]), |
| 27 | + default="stdio", |
| 28 | + help="Transport type", |
| 29 | +) |
| 30 | +def main(port: int, transport: str) -> int: |
| 31 | + return anyio.run(_amain, port, transport) |
| 32 | + |
| 33 | + |
| 34 | +async def _amain(port: int, transport: str) -> int: |
| 35 | + app = Server("mcp-website-fetcher") |
| 36 | + |
| 37 | + @app.call_tool() |
| 38 | + async def fetch_tool( |
| 39 | + name: str, arguments: dict |
| 40 | + ) -> list[types.TextContent | types.ImageContent | types.EmbeddedResource]: |
| 41 | + if name != "fetch": |
| 42 | + raise ValueError(f"Unknown tool: {name}") |
| 43 | + if "url" not in arguments: |
| 44 | + raise ValueError("Missing required argument 'url'") |
| 45 | + return await fetch_website(arguments["url"]) |
| 46 | + |
| 47 | + @app.list_tools() |
| 48 | + async def list_tools() -> list[types.Tool]: |
| 49 | + return [ |
| 50 | + types.Tool( |
| 51 | + name="fetch", |
| 52 | + description="Fetches a website and returns its content", |
| 53 | + inputSchema={ |
| 54 | + "type": "object", |
| 55 | + "required": ["url"], |
| 56 | + "properties": { |
| 57 | + "url": { |
| 58 | + "type": "string", |
| 59 | + "description": "URL to fetch", |
| 60 | + } |
| 61 | + }, |
| 62 | + }, |
| 63 | + ) |
| 64 | + ] |
| 65 | + |
| 66 | + if transport == "sse": |
| 67 | + from mcp.server.sse import SseServerTransport |
| 68 | + from starlette.applications import Starlette |
| 69 | + from starlette.routing import Route |
| 70 | + |
| 71 | + sse = SseServerTransport("/messages") |
| 72 | + |
| 73 | + async def handle_sse(scope, receive, send): |
| 74 | + async with sse.connect_sse(scope, receive, send) as streams: |
| 75 | + await app.run( |
| 76 | + streams[0], streams[1], app.create_initialization_options() |
| 77 | + ) |
| 78 | + |
| 79 | + async def handle_messages(scope, receive, send): |
| 80 | + await sse.handle_post_message(scope, receive, send) |
| 81 | + |
| 82 | + starlette_app = Starlette( |
| 83 | + debug=True, |
| 84 | + routes=[ |
| 85 | + Route("/sse", endpoint=handle_sse), |
| 86 | + Route("/messages", endpoint=handle_messages, methods=["POST"]), |
| 87 | + ], |
| 88 | + ) |
| 89 | + |
| 90 | + import uvicorn |
| 91 | + |
| 92 | + uvicorn.run(starlette_app, host="0.0.0.0", port=port) |
| 93 | + else: |
| 94 | + from mcp.server.stdio import stdio_server |
| 95 | + |
| 96 | + async with stdio_server() as streams: |
| 97 | + await app.run(streams[0], streams[1], app.create_initialization_options()) |
| 98 | + |
| 99 | + return 0 |
0 commit comments