fix(sdk): close MCP connections from Nanobot facade

The SDK opened MCP connections through AgentLoop.process_direct but
never called close_mcp, leaving stdio MCP generators to be finalized
during asyncio shutdown from a different task, producing a RuntimeError
about exiting a cancel scope in a different task.

Add aclose() that delegates to AgentLoop.close_mcp (which already
drains background tasks and closes MCP stacks), plus __aenter__ and
__aexit__ so the SDK works as an async context manager.

Fixes #4211
This commit is contained in:
axelray-dev
2026-06-06 15:35:28 +08:00
committed by Xubin Ren
parent 6a0368b32f
commit 57fa37dcfe
2 changed files with 46 additions and 0 deletions
+9
View File
@@ -101,4 +101,13 @@ class Nanobot:
messages=capture.messages,
)
async def aclose(self) -> None:
"""Release resources held by this instance (MCP connections, etc.)."""
await self._loop.close_mcp()
async def __aenter__(self) -> Nanobot:
return self
async def __aexit__(self, *exc: object) -> None:
await self.aclose()