Skip to content

Commit 1917066

Browse files
committed
update examples
1 parent dfa7c6a commit 1917066

File tree

2 files changed

+24
-12
lines changed
  • examples/servers

2 files changed

+24
-12
lines changed

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

+12-6
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,23 @@ async def get_prompt(
9090
if transport == "sse":
9191
from mcp.server.sse import SseServerTransport
9292
from starlette.applications import Starlette
93+
from starlette.responses import Response
9394
from starlette.routing import Mount, Route
9495

9596
sse = SseServerTransport("/messages/")
9697

9798
async def handle_sse(request):
98-
async with sse.connect_sse(
99-
request.scope, request.receive, request._send
100-
) as streams:
101-
await app.run(
102-
streams[0], streams[1], app.create_initialization_options()
103-
)
99+
with anyio.CancelScope() as cancel_scope:
100+
async with sse.connect_sse(
101+
request.scope,
102+
request.receive,
103+
request._send,
104+
lambda: cancel_scope.cancel(),
105+
) as streams:
106+
await app.run(
107+
streams[0], streams[1], app.create_initialization_options()
108+
)
109+
return Response(status_code=200)
104110

105111
starlette_app = Starlette(
106112
debug=True,

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

+12-6
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,23 @@ async def read_resource(uri: FileUrl) -> str | bytes:
4646
if transport == "sse":
4747
from mcp.server.sse import SseServerTransport
4848
from starlette.applications import Starlette
49+
from starlette.responses import Response
4950
from starlette.routing import Mount, Route
5051

5152
sse = SseServerTransport("/messages/")
5253

5354
async def handle_sse(request):
54-
async with sse.connect_sse(
55-
request.scope, request.receive, request._send
56-
) as streams:
57-
await app.run(
58-
streams[0], streams[1], app.create_initialization_options()
59-
)
55+
with anyio.CancelScope() as cancel_scope:
56+
async with sse.connect_sse(
57+
request.scope,
58+
request.receive,
59+
request._send,
60+
lambda: cancel_scope.cancel(),
61+
) as streams:
62+
await app.run(
63+
streams[0], streams[1], app.create_initialization_options()
64+
)
65+
return Response(status_code=200)
6066

6167
starlette_app = Starlette(
6268
debug=True,

0 commit comments

Comments
 (0)