From 73353785a023925e35f7913b4ba8c49449f96233 Mon Sep 17 00:00:00 2001 From: Xubin Ren <52506698+Re-bin@users.noreply.github.com> Date: Sat, 6 Jun 2026 15:29:13 +0800 Subject: [PATCH] docs(sdk): document Nanobot teardown --- docs/python-sdk.md | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/docs/python-sdk.md b/docs/python-sdk.md index 5ee66a349..14609c143 100644 --- a/docs/python-sdk.md +++ b/docs/python-sdk.md @@ -11,8 +11,8 @@ from nanobot import Nanobot async def main() -> None: - bot = Nanobot.from_config() - result = await bot.run("What time is it in Tokyo?") + async with Nanobot.from_config() as bot: + result = await bot.run("What time is it in Tokyo?") print(result.content) @@ -21,6 +21,8 @@ asyncio.run(main()) `Nanobot.from_config()` reuses your normal `~/.nanobot/config.json`, so the SDK follows the same provider, model, tools, and workspace defaults as the CLI unless you override them. +Use `async with` when possible so MCP connections and background cleanup work are closed before the event loop exits. If you manage the instance manually, call `await bot.aclose()` in a `finally` block. + ## Common Patterns ### Use a specific config or workspace @@ -83,6 +85,15 @@ Run the agent once and return a `RunResult`. | `session_key` | `str` | `"sdk:default"` | Session identifier for conversation isolation. Different keys get independent history. | | `hooks` | `list[AgentHook] \| None` | `None` | Lifecycle hooks for this run only. | +### `await bot.aclose()` + +Release resources held by the SDK instance, including MCP connections. The async context manager calls this automatically: + +```python +async with Nanobot.from_config() as bot: + result = await bot.run("Summarize this repo") +``` + ### `RunResult` | Field | Type | Description |