|
| 1 | +import anyio |
| 2 | +import click |
| 3 | +import mcp.types as types |
| 4 | +from mcp.server import AnyUrl, Server |
| 5 | + |
| 6 | + |
| 7 | +@click.group() |
| 8 | +def cli(): |
| 9 | + pass |
| 10 | + |
| 11 | + |
| 12 | +@cli.command() |
| 13 | +@click.option("--port", default=8000, help="Port to listen on for SSE") |
| 14 | +@click.option( |
| 15 | + "--transport", |
| 16 | + type=click.Choice(["stdio", "sse"]), |
| 17 | + default="stdio", |
| 18 | + help="Transport type", |
| 19 | +) |
| 20 | +def main(port: int, transport: str) -> int: |
| 21 | + return anyio.run(_amain, port, transport) |
| 22 | + |
| 23 | + |
| 24 | +SAMPLE_RESOURCES = { |
| 25 | + "greeting": "Hello! This is a sample text resource.", |
| 26 | + "help": "This server provides a few sample text resources for testing.", |
| 27 | + "about": "This is the simple-resource MCP server implementation.", |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +async def _amain(port: int, transport: str) -> int: |
| 32 | + app = Server("mcp-simple-resource") |
| 33 | + |
| 34 | + @app.list_resources() |
| 35 | + async def list_resources() -> list[types.Resource]: |
| 36 | + return [ |
| 37 | + types.Resource( |
| 38 | + uri=AnyUrl(f"file:///{name}.txt"), |
| 39 | + name=name, |
| 40 | + description=f"A sample text resource named {name}", |
| 41 | + mimeType="text/plain", |
| 42 | + ) |
| 43 | + for name in SAMPLE_RESOURCES.keys() |
| 44 | + ] |
| 45 | + |
| 46 | + @app.read_resource() |
| 47 | + async def read_resource(uri: AnyUrl) -> str | bytes: |
| 48 | + assert uri.path is not None |
| 49 | + name = uri.path.replace(".txt", "").lstrip("/") |
| 50 | + |
| 51 | + if name not in SAMPLE_RESOURCES: |
| 52 | + raise ValueError(f"Unknown resource: {uri}") |
| 53 | + |
| 54 | + return SAMPLE_RESOURCES[name] |
| 55 | + |
| 56 | + if transport == "sse": |
| 57 | + from mcp.server.sse import SseServerTransport |
| 58 | + from starlette.applications import Starlette |
| 59 | + from starlette.routing import Route |
| 60 | + |
| 61 | + sse = SseServerTransport("/messages") |
| 62 | + |
| 63 | + async def handle_sse(scope, receive, send): |
| 64 | + async with sse.connect_sse(scope, receive, send) as streams: |
| 65 | + await app.run( |
| 66 | + streams[0], streams[1], app.create_initialization_options() |
| 67 | + ) |
| 68 | + |
| 69 | + async def handle_messages(scope, receive, send): |
| 70 | + await sse.handle_post_message(scope, receive, send) |
| 71 | + |
| 72 | + starlette_app = Starlette( |
| 73 | + debug=True, |
| 74 | + routes=[ |
| 75 | + Route("/sse", endpoint=handle_sse), |
| 76 | + Route("/messages", endpoint=handle_messages, methods=["POST"]), |
| 77 | + ], |
| 78 | + ) |
| 79 | + |
| 80 | + import uvicorn |
| 81 | + |
| 82 | + uvicorn.run(starlette_app, host="0.0.0.0", port=port) |
| 83 | + else: |
| 84 | + from mcp.server.stdio import stdio_server |
| 85 | + |
| 86 | + async with stdio_server() as streams: |
| 87 | + await app.run(streams[0], streams[1], app.create_initialization_options()) |
| 88 | + |
| 89 | + return 0 |
0 commit comments