Fix example servers SSE invocation

Reported-by: Simon Willison
This commit is contained in:
David Soria Parra
2024-11-22 10:45:24 +00:00
parent 6c46bd283a
commit bbd9d05851
3 changed files with 60 additions and 52 deletions

View File

@@ -4,23 +4,6 @@ import mcp.types as types
from mcp.server import 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)
def create_messages(
context: str | None = None, topic: str | None = None
) -> list[types.PromptMessage]:
@@ -54,7 +37,19 @@ def create_messages(
return messages
async def _amain(port: int, transport: str) -> int:
@click.group()
def cli():
pass
@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:
app = Server("mcp-simple-prompt")
@app.list_prompts()
@@ -63,7 +58,7 @@ async def _amain(port: int, transport: str) -> int:
types.Prompt(
name="simple",
description="A simple prompt that can take optional context and topic "
"arguments",
"arguments",
arguments=[
types.PromptArgument(
name="context",
@@ -103,14 +98,16 @@ async def _amain(port: int, transport: str) -> int:
sse = SseServerTransport("/messages")
async def handle_sse(scope, receive, send):
async with sse.connect_sse(scope, receive, send) as streams:
async def handle_sse(request):
async with sse.connect_sse(
request.scope, request.receive, request._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)
async def handle_messages(request):
await sse.handle_post_message(request.scope, request.receive, request._send)
starlette_app = Starlette(
debug=True,
@@ -126,7 +123,12 @@ async def _amain(port: int, transport: str) -> int:
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())
async def arun():
async with stdio_server() as streams:
await app.run(
streams[0], streams[1], app.create_initialization_options()
)
anyio.run(arun)
return 0