mirror of
https://github.com/aljazceru/mcp-python-sdk.git
synced 2025-12-19 14:54:24 +01:00
Fix example servers SSE invocation
Reported-by: Simon Willison
This commit is contained in:
@@ -4,23 +4,6 @@ import mcp.types as types
|
|||||||
from mcp.server import Server
|
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(
|
def create_messages(
|
||||||
context: str | None = None, topic: str | None = None
|
context: str | None = None, topic: str | None = None
|
||||||
) -> list[types.PromptMessage]:
|
) -> list[types.PromptMessage]:
|
||||||
@@ -54,7 +37,19 @@ def create_messages(
|
|||||||
return 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 = Server("mcp-simple-prompt")
|
||||||
|
|
||||||
@app.list_prompts()
|
@app.list_prompts()
|
||||||
@@ -103,14 +98,16 @@ async def _amain(port: int, transport: str) -> int:
|
|||||||
|
|
||||||
sse = SseServerTransport("/messages")
|
sse = SseServerTransport("/messages")
|
||||||
|
|
||||||
async def handle_sse(scope, receive, send):
|
async def handle_sse(request):
|
||||||
async with sse.connect_sse(scope, receive, send) as streams:
|
async with sse.connect_sse(
|
||||||
|
request.scope, request.receive, request._send
|
||||||
|
) as streams:
|
||||||
await app.run(
|
await app.run(
|
||||||
streams[0], streams[1], app.create_initialization_options()
|
streams[0], streams[1], app.create_initialization_options()
|
||||||
)
|
)
|
||||||
|
|
||||||
async def handle_messages(scope, receive, send):
|
async def handle_messages(request):
|
||||||
await sse.handle_post_message(scope, receive, send)
|
await sse.handle_post_message(request.scope, request.receive, request._send)
|
||||||
|
|
||||||
starlette_app = Starlette(
|
starlette_app = Starlette(
|
||||||
debug=True,
|
debug=True,
|
||||||
@@ -126,7 +123,12 @@ async def _amain(port: int, transport: str) -> int:
|
|||||||
else:
|
else:
|
||||||
from mcp.server.stdio import stdio_server
|
from mcp.server.stdio import stdio_server
|
||||||
|
|
||||||
|
async def arun():
|
||||||
async with stdio_server() as streams:
|
async with stdio_server() as streams:
|
||||||
await app.run(streams[0], streams[1], app.create_initialization_options())
|
await app.run(
|
||||||
|
streams[0], streams[1], app.create_initialization_options()
|
||||||
|
)
|
||||||
|
|
||||||
|
anyio.run(arun)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
@@ -4,6 +4,13 @@ import mcp.types as types
|
|||||||
from mcp.server import AnyUrl, Server
|
from mcp.server import AnyUrl, Server
|
||||||
|
|
||||||
|
|
||||||
|
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.",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@click.group()
|
@click.group()
|
||||||
def cli():
|
def cli():
|
||||||
pass
|
pass
|
||||||
@@ -18,17 +25,6 @@ def cli():
|
|||||||
help="Transport type",
|
help="Transport type",
|
||||||
)
|
)
|
||||||
def main(port: int, transport: str) -> int:
|
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 = Server("mcp-simple-resource")
|
||||||
|
|
||||||
@app.list_resources()
|
@app.list_resources()
|
||||||
@@ -60,14 +56,16 @@ async def _amain(port: int, transport: str) -> int:
|
|||||||
|
|
||||||
sse = SseServerTransport("/messages")
|
sse = SseServerTransport("/messages")
|
||||||
|
|
||||||
async def handle_sse(scope, receive, send):
|
async def handle_sse(request):
|
||||||
async with sse.connect_sse(scope, receive, send) as streams:
|
async with sse.connect_sse(
|
||||||
|
request.scope, request.receive, request._send
|
||||||
|
) as streams:
|
||||||
await app.run(
|
await app.run(
|
||||||
streams[0], streams[1], app.create_initialization_options()
|
streams[0], streams[1], app.create_initialization_options()
|
||||||
)
|
)
|
||||||
|
|
||||||
async def handle_messages(scope, receive, send):
|
async def handle_messages(request):
|
||||||
await sse.handle_post_message(scope, receive, send)
|
await sse.handle_post_message(request.scope, request.receive, request._send)
|
||||||
|
|
||||||
starlette_app = Starlette(
|
starlette_app = Starlette(
|
||||||
debug=True,
|
debug=True,
|
||||||
@@ -83,7 +81,12 @@ async def _amain(port: int, transport: str) -> int:
|
|||||||
else:
|
else:
|
||||||
from mcp.server.stdio import stdio_server
|
from mcp.server.stdio import stdio_server
|
||||||
|
|
||||||
|
async def arun():
|
||||||
async with stdio_server() as streams:
|
async with stdio_server() as streams:
|
||||||
await app.run(streams[0], streams[1], app.create_initialization_options())
|
await app.run(
|
||||||
|
streams[0], streams[1], app.create_initialization_options()
|
||||||
|
)
|
||||||
|
|
||||||
|
anyio.run(arun)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
@@ -31,10 +31,6 @@ def cli():
|
|||||||
help="Transport type",
|
help="Transport type",
|
||||||
)
|
)
|
||||||
def main(port: int, transport: str) -> int:
|
def main(port: int, transport: str) -> int:
|
||||||
return anyio.run(_amain, port, transport)
|
|
||||||
|
|
||||||
|
|
||||||
async def _amain(port: int, transport: str) -> int:
|
|
||||||
app = Server("mcp-website-fetcher")
|
app = Server("mcp-website-fetcher")
|
||||||
|
|
||||||
@app.call_tool()
|
@app.call_tool()
|
||||||
@@ -73,14 +69,16 @@ async def _amain(port: int, transport: str) -> int:
|
|||||||
|
|
||||||
sse = SseServerTransport("/messages")
|
sse = SseServerTransport("/messages")
|
||||||
|
|
||||||
async def handle_sse(scope, receive, send):
|
async def handle_sse(request):
|
||||||
async with sse.connect_sse(scope, receive, send) as streams:
|
async with sse.connect_sse(
|
||||||
|
request.scope, request.receive, request._send
|
||||||
|
) as streams:
|
||||||
await app.run(
|
await app.run(
|
||||||
streams[0], streams[1], app.create_initialization_options()
|
streams[0], streams[1], app.create_initialization_options()
|
||||||
)
|
)
|
||||||
|
|
||||||
async def handle_messages(scope, receive, send):
|
async def handle_messages(request):
|
||||||
await sse.handle_post_message(scope, receive, send)
|
await sse.handle_post_message(request.scope, request.receive, request._send)
|
||||||
|
|
||||||
starlette_app = Starlette(
|
starlette_app = Starlette(
|
||||||
debug=True,
|
debug=True,
|
||||||
@@ -96,7 +94,12 @@ async def _amain(port: int, transport: str) -> int:
|
|||||||
else:
|
else:
|
||||||
from mcp.server.stdio import stdio_server
|
from mcp.server.stdio import stdio_server
|
||||||
|
|
||||||
|
async def arun():
|
||||||
async with stdio_server() as streams:
|
async with stdio_server() as streams:
|
||||||
await app.run(streams[0], streams[1], app.create_initialization_options())
|
await app.run(
|
||||||
|
streams[0], streams[1], app.create_initialization_options()
|
||||||
|
)
|
||||||
|
|
||||||
|
anyio.run(arun)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
Reference in New Issue
Block a user