8000 Fix example servers SSE invocation · simonw/python-sdk@bbd9d05 · GitHub
[go: up one dir, main page]

Skip to content

Commit bbd9d05

Browse files
committed
Fix example servers SSE invocation
Reported-by: Simon Willison
1 parent 6c46bd2 commit bbd9d05

File tree

3 files changed

+60
-52
lines changed
  • examples/servers

3 files changed

+60
-52
lines changed

examples/servers/simple-prompt/mcp_simple_prompt/server.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,6 @@
44
from mcp.server import Server
55

66

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-
247
def create_messages(
258
context: str | None = None, topic: str | None = None
269
) -> list[types.PromptMessage]:
@@ -54,7 +37,19 @@ def create_messages(
5437
return messages
5538

5639

57-
async def _amain(port: int, transport: str) -> int:
40+
@click.group()
41+
def cli():
42+
pass
43+
44+
45+
@click.option("--port", default=8000, help="Port to listen on for SSE")
46+
@click.option(
47+
"--transport",
48+
type=click.Choice(["stdio", "sse"]),
49+
default="stdio",
50+
help="Transport type",
51+
)
52+
def main(port: int, transport: str) -> int:
5853
app = Server("mcp-simple-prompt")
5954

6055
@app.list_prompts()
@@ -63,7 +58,7 @@ async def list_prompts() -> list[types.Prompt]:
6358
types.Prompt(
6459
name="simple",
6560
description="A simple prompt that can take optional context and topic "
66-
"arguments",
61+
"arguments",
6762
arguments=[
6863
types.PromptArgument(
6964
name="context",
@@ -103,14 +98,16 @@ async def get_prompt(
10398

10499
sse = SseServerTransport("/messages")
105100

106-
async def handle_sse(scope, receive, send):
107-
async with sse.connect_sse(scope, receive, send) as streams:
101+
async def handle_sse(request):
102+
async with sse.connect_sse(
103+
request.scope, request.receive, request._send
104+
) as streams:
108105
await app.run(
109106
streams[0], streams[1], app.create_initialization_options()
110107
)
111108

112-
async def handle_messages(scope, receive, send):
113-
await sse.handle_post_message(scope, receive, send)
109+
async def handle_messages(request):
110+
await sse.handle_post_message(request.scope, request.receive, request._send)
114111

115112
starlette_app = Starlette(
116113
debug=True,
@@ -126,7 +123,12 @@ async def handle_messages(scope, receive, send):
126123
else:
127124
from mcp.server.stdio import stdio_server
128125

129-
async with stdio_server() as streams:
130-
await app.run(streams[0], streams[1], app.create_initialization_options())
126+
async def arun():
127+
async with stdio_server() as streams:
128+
await app.run(
129+
streams[0], streams[1], app.create_initialization_options()
130+
)
131+
132+
anyio.run(arun)
131133

132134
return 0

examples/servers/simple-resource/mcp_simple_resource/server.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
from mcp.server import AnyUrl, Server
55

66

7+
SAMPLE_RESOURCES = {
8+
"greeting": "Hello! This is a sample text resource.",
9+
"help": "This server provides a few sample text resources for testing.",
10+
"about": "This is the simple-resource MCP server implementation.",
11+
}
12+
13+
714
@click.group()
815
def cli():
916
pass
@@ -18,17 +25,6 @@ def cli():
1825
help="Transport type",
1926
)
2027
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:
3228
app = Server("mcp-simple-resource")
3329

3430
@app.list_resources()
@@ -60,14 +56,16 @@ async def read_resource(uri: AnyUrl) -> str | bytes:
6056

6157
sse = SseServerTransport("/messages")
6258

63-
async def handle_sse(scope, receive, send):
64-
async with sse.connect_sse(scope, receive, send) as streams:
59+
async def handle_sse(request):
60+
async with sse.connect_sse(
61+
request.scope, request.receive, request._send
62+
) as streams:
6563
await app.run(
6664
streams[0], streams[1], app.create_initialization_options()
6765
)
6866

69-
async def handle_messages(scope, receive, send):
70-
await sse.handle_post_message(scope, receive, send)
67+
async def handle_messages(request):
68+
await sse.handle_post_message(request.scope, request.receive, request._send)
7169

7270
starlette_app = Starlette(
7371
debug=True,
@@ -83,7 +81,12 @@ async def handle_messages(scope, receive, send):
8381
else:
8482
from mcp.server.stdio import stdio_server
8583

86-
async with stdio_server() as streams:
87-
await app.run(streams[0], streams[1], app.create_initialization_options())
84+
async def arun():
85+
async with stdio_server() as streams:
86+
await app.run(
87+
streams[0], streams[1], app.create_initialization_options()
88+
)
89+
90+
anyio.run(arun)
8891

8992
return 0

examples/servers/simple-tool/mcp_simple_tool/server.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ def cli():
3131
help="Transport type",
3232
)
3333
def main(port: int, transport: str) -> int:
34-
return anyio.run(_amain, port, transport)
35-
36-
37-
async def _amain(port: int, transport: str) -> int:
3834
app = Server("mcp-website-fetcher")
3935

4036
@app.call_tool()
@@ -73,14 +69,16 @@ async def list_tools() -> list[types.Tool]:
7369

7470
sse = SseServerTransport("/messages")
7571

76-
async def handle_sse(scope, receive, send):
77-
async with sse.connect_sse(scope, receive, send) as streams:
72+
async def handle_sse(request):
73+
async with sse.connect_sse(
74+
request.scope, request.receive, request._send
75+
) as streams:
7876
await app.run(
7977
streams[0], streams[1], app.create_initialization_options()
8078
)
8179

82-
async def handle_messages(scope, receive, send):
83-
await sse.handle_post_message(scope, receive, send)
80+
async def handle_messages(request):
81+
await sse.handle_post_message(request.scope, request.receive, request._send)
8482

8583
starlette_app = Starlette(
8684
debug=True,
@@ -96,7 +94,12 @@ async def handle_messages(scope, receive, send):
9694
else:
9795
from mcp.server.stdio import stdio_server
9896

99-
async with stdio_server() as streams:
100-
await app.run(streams[0], streams[1], app.create_initialization_options())
97+
async def arun():
98+
async with stdio_server() as streams:
99+
await app.run(
100+
streams[0], streams[1], app.create_initialization_options()
101+
)
102+
103+
anyio.run(arun)
101104

102105
return 0

0 commit comments

Comments
 (0)
0