mirror of
https://github.com/aljazceru/mcp-python-sdk.git
synced 2025-12-19 14:54:24 +01:00
add simple resource example
This commit is contained in:
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
import sys
|
||||
|
||||
from server import main
|
||||
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,89 @@
|
||||
import anyio
|
||||
import click
|
||||
import mcp.types as types
|
||||
from mcp.server import AnyUrl, Server
|
||||
|
||||
|
||||
@click.group()
|
||||
def cli():
|
||||
pass
|
||||
|
||||
|
||||
@cli.command()
|
||||
@click.option("--port", default=8000, help="Port to listen on for SSE")
|
||||
@click.option(
|
||||
"--transport",
|
||||
type=click.Choice(["stdio", "sse"]),
|
||||
default="stdio",
|
||||
help="Transport type",
|
||||
)
|
||||
def main(port: int, transport: str) -> int:
|
||||
return anyio.run(_amain, port, transport)
|
||||
|
||||
|
||||
SAMPLE_RESOURCES = {
|
||||
"greeting": "Hello! This is a sample text resource.",
|
||||
"help": "This server provides a few sample text resources for testing.",
|
||||
"about": "This is the simple-resource MCP server implementation.",
|
||||
}
|
||||
|
||||
|
||||
async def _amain(port: int, transport: str) -> int:
|
||||
app = Server("mcp-simple-resource")
|
||||
|
||||
@app.list_resources()
|
||||
async def list_resources() -> list[types.Resource]:
|
||||
return [
|
||||
types.Resource(
|
||||
uri=AnyUrl(f"file:///{name}.txt"),
|
||||
name=name,
|
||||
description=f"A sample text resource named {name}",
|
||||
mimeType="text/plain",
|
||||
)
|
||||
for name in SAMPLE_RESOURCES.keys()
|
||||
]
|
||||
|
||||
@app.read_resource()
|
||||
async def read_resource(uri: AnyUrl) -> str | bytes:
|
||||
assert uri.path is not None
|
||||
name = uri.path.replace(".txt", "").lstrip("/")
|
||||
|
||||
if name not in SAMPLE_RESOURCES:
|
||||
raise ValueError(f"Unknown resource: {uri}")
|
||||
|
||||
return SAMPLE_RESOURCES[name]
|
||||
|
||||
if transport == "sse":
|
||||
from mcp.server.sse import SseServerTransport
|
||||
from starlette.applications import Starlette
|
||||
from starlette.routing import Route
|
||||
|
||||
sse = SseServerTransport("/messages")
|
||||
|
||||
async def handle_sse(scope, receive, send):
|
||||
async with sse.connect_sse(scope, receive, send) as streams:
|
||||
await app.run(
|
||||
streams[0], streams[1], app.create_initialization_options()
|
||||
)
|
||||
|
||||
async def handle_messages(scope, receive, send):
|
||||
await sse.handle_post_message(scope, receive, send)
|
||||
|
||||
starlette_app = Starlette(
|
||||
debug=True,
|
||||
routes=[
|
||||
Route("/sse", endpoint=handle_sse),
|
||||
Route("/messages", endpoint=handle_messages, methods=["POST"]),
|
||||
],
|
||||
)
|
||||
|
||||
import uvicorn
|
||||
|
||||
uvicorn.run(starlette_app, host="0.0.0.0", port=port)
|
||||
else:
|
||||
from mcp.server.stdio import stdio_server
|
||||
|
||||
async with stdio_server() as streams:
|
||||
await app.run(streams[0], streams[1], app.create_initialization_options())
|
||||
|
||||
return 0
|
||||
Reference in New Issue
Block a user