Skip to content

Commit a8e0831

Browse files
committed
Use anyio instead of asyncio in lowlevel server
1 parent 26055d9 commit a8e0831

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/mcp/server/lowlevel/server.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ async def main():
6767

6868
from __future__ import annotations as _annotations
6969

70-
import asyncio
7170
import contextvars
7271
import json
7372
import logging
@@ -556,19 +555,20 @@ async def execute_async():
556555
logger.exception(f"Async execution failed for {tool_name}")
557556
self.async_operations.fail_operation(operation.token, str(e))
558557

559-
asyncio.create_task(execute_async())
560-
561-
# Return operation result with immediate content
562-
logger.info(f"Returning async operation result for {tool_name}")
563-
return types.ServerResult(
564-
types.CallToolResult(
565-
content=immediate_content,
566-
operation=types.AsyncResultProperties(
567-
token=operation.token,
568-
keepAlive=operation.keep_alive,
569-
),
558+
async with anyio.create_task_group() as tg:
559+
tg.start_soon(execute_async)
560+
561+
# Return operation result with immediate content
562+
logger.info(f"Returning async operation result for {tool_name}")
563+
return types.ServerResult(
564+
types.CallToolResult(
565+
content=immediate_content,
566+
operation=types.AsyncResultProperties(
567+
token=operation.token,
568+
keepAlive=operation.keep_alive,
569+
),
570+
)
570571
)
571-
)
572572

573573
# tool call
574574
results = await func(tool_name, arguments)

0 commit comments

Comments
 (0)