From 805228e91ec79b7345616a78ef87bde569204688 Mon Sep 17 00:00:00 2001 From: Peixian Gong Date: Tue, 3 Mar 2026 19:56:05 +0800 Subject: [PATCH 01/10] fix: add shell=True for npm subprocess calls on Windows On Windows, npm is installed as npm.cmd (a batch script), not a direct executable. When subprocess.run() is called with a list like ['npm', 'install'] without shell=True, Python's CreateProcess cannot locate npm.cmd, resulting in: FileNotFoundError: [WinError 2] The system cannot find the file specified This fix adds a sys.platform == 'win32' check before each npm subprocess call. On Windows, it uses shell=True with a string command so the shell can resolve npm.cmd. On other platforms, the original list-based call is preserved unchanged. Affected locations: - _get_bridge_dir(): npm install, npm run build - channels_login(): npm start No behavioral change on Linux/macOS. --- nanobot/cli/commands.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/nanobot/cli/commands.py b/nanobot/cli/commands.py index ddefb94..065eb71 100644 --- a/nanobot/cli/commands.py +++ b/nanobot/cli/commands.py @@ -837,12 +837,19 @@ def _get_bridge_dir() -> Path: shutil.copytree(source, user_bridge, ignore=shutil.ignore_patterns("node_modules", "dist")) # Install and build + is_win = sys.platform == "win32" try: console.print(" Installing dependencies...") - subprocess.run(["npm", "install"], cwd=user_bridge, check=True, capture_output=True) + if is_win: + subprocess.run("npm install", cwd=user_bridge, check=True, capture_output=True, shell=True) + else: + subprocess.run(["npm", "install"], cwd=user_bridge, check=True, capture_output=True) console.print(" Building...") - subprocess.run(["npm", "run", "build"], cwd=user_bridge, check=True, capture_output=True) + if is_win: + subprocess.run("npm run build", cwd=user_bridge, check=True, capture_output=True, shell=True) + else: + subprocess.run(["npm", "run", "build"], cwd=user_bridge, check=True, capture_output=True) console.print("[green]✓[/green] Bridge ready\n") except subprocess.CalledProcessError as e: @@ -876,7 +883,10 @@ def channels_login(): env["AUTH_DIR"] = str(get_runtime_subdir("whatsapp-auth")) try: - subprocess.run(["npm", "start"], cwd=bridge_dir, check=True, env=env) + if sys.platform == "win32": + subprocess.run("npm start", cwd=bridge_dir, check=True, env=env, shell=True) + else: + subprocess.run(["npm", "start"], cwd=bridge_dir, check=True, env=env) except subprocess.CalledProcessError as e: console.print(f"[red]Bridge failed: {e}[/red]") except FileNotFoundError: From 58fc34d3f42b483236d3819fe03d000aa5e2536a Mon Sep 17 00:00:00 2001 From: Peixian Gong Date: Wed, 4 Mar 2026 13:43:30 +0800 Subject: [PATCH 02/10] refactor: use shutil.which() instead of shell=True for npm calls Replace platform-specific shell=True logic with shutil.which('npm') to resolve the full path to the npm executable. This is cleaner because: - No shell=True needed (safer, no shell injection risk) - No platform-specific branching (sys.platform checks removed) - Works identically on Windows, macOS, and Linux - shutil.which() resolves npm.cmd on Windows automatically The npm path check that already existed in _get_bridge_dir() is now reused as the resolved path for subprocess calls. The same pattern is applied to channels_login(). --- nanobot/cli/commands.py | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/nanobot/cli/commands.py b/nanobot/cli/commands.py index 065eb71..e538688 100644 --- a/nanobot/cli/commands.py +++ b/nanobot/cli/commands.py @@ -809,7 +809,8 @@ def _get_bridge_dir() -> Path: return user_bridge # Check for npm - if not shutil.which("npm"): + npm_path = shutil.which("npm") + if not npm_path: console.print("[red]npm not found. Please install Node.js >= 18.[/red]") raise typer.Exit(1) @@ -837,19 +838,12 @@ def _get_bridge_dir() -> Path: shutil.copytree(source, user_bridge, ignore=shutil.ignore_patterns("node_modules", "dist")) # Install and build - is_win = sys.platform == "win32" try: console.print(" Installing dependencies...") - if is_win: - subprocess.run("npm install", cwd=user_bridge, check=True, capture_output=True, shell=True) - else: - subprocess.run(["npm", "install"], cwd=user_bridge, check=True, capture_output=True) + subprocess.run([npm_path, "install"], cwd=user_bridge, check=True, capture_output=True) console.print(" Building...") - if is_win: - subprocess.run("npm run build", cwd=user_bridge, check=True, capture_output=True, shell=True) - else: - subprocess.run(["npm", "run", "build"], cwd=user_bridge, check=True, capture_output=True) + subprocess.run([npm_path, "run", "build"], cwd=user_bridge, check=True, capture_output=True) console.print("[green]✓[/green] Bridge ready\n") except subprocess.CalledProcessError as e: @@ -864,6 +858,7 @@ def _get_bridge_dir() -> Path: @channels_app.command("login") def channels_login(): """Link device via QR code.""" + import shutil import subprocess from nanobot.config.loader import load_config @@ -882,15 +877,15 @@ def channels_login(): env["BRIDGE_TOKEN"] = bridge_token env["AUTH_DIR"] = str(get_runtime_subdir("whatsapp-auth")) + npm_path = shutil.which("npm") + if not npm_path: + console.print("[red]npm not found. Please install Node.js.[/red]") + raise typer.Exit(1) + try: - if sys.platform == "win32": - subprocess.run("npm start", cwd=bridge_dir, check=True, env=env, shell=True) - else: - subprocess.run(["npm", "start"], cwd=bridge_dir, check=True, env=env) + subprocess.run([npm_path, "start"], cwd=bridge_dir, check=True, env=env) except subprocess.CalledProcessError as e: console.print(f"[red]Bridge failed: {e}[/red]") - except FileNotFoundError: - console.print("[red]npm not found. Please install Node.js.[/red]") # ============================================================================ From 4990c7478b53d98d1579258a1e9a013ac760539a Mon Sep 17 00:00:00 2001 From: SJK-py Date: Fri, 13 Mar 2026 03:28:01 -0700 Subject: [PATCH 03/10] suppress unnecessary cron notifications Appends a strict instruction to background task prompts (cron and heartbeat) directing the agent to return a `` token if there is nothing material to report. Adds conditional logic to intercept this token and suppress the outbound message to the user, preventing notification spam from autonomous background checks. --- nanobot/cli/commands.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nanobot/cli/commands.py b/nanobot/cli/commands.py index e538688..c4aa868 100644 --- a/nanobot/cli/commands.py +++ b/nanobot/cli/commands.py @@ -452,6 +452,7 @@ def gateway( "[Scheduled Task] Timer finished.\n\n" f"Task '{job.name}' has been triggered.\n" f"Scheduled instruction: {job.payload.message}" + "**IMPORTANT NOTICE:** If there is nothing material to report, reply only with ." ) # Prevent the agent from scheduling new cron jobs during execution @@ -474,7 +475,7 @@ def gateway( if isinstance(message_tool, MessageTool) and message_tool._sent_in_turn: return response - if job.payload.deliver and job.payload.to and response: + if job.payload.deliver and job.payload.to and response and "" not in response: from nanobot.bus.events import OutboundMessage await bus.publish_outbound(OutboundMessage( channel=job.payload.channel or "cli", From e6c1f520ac720bdda1c0c0a2378763fe5023ac13 Mon Sep 17 00:00:00 2001 From: SJK-py Date: Fri, 13 Mar 2026 03:31:42 -0700 Subject: [PATCH 04/10] suppress unnecessary heartbeat notifications Appends a strict instruction to background task prompts (cron and heartbeat) directing the agent to return a `` token if there is nothing material to report. Adds conditional logic to intercept this token and suppress the outbound message to the user, preventing notification spam from autonomous background checks. --- nanobot/heartbeat/service.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/nanobot/heartbeat/service.py b/nanobot/heartbeat/service.py index 831ae85..916c813 100644 --- a/nanobot/heartbeat/service.py +++ b/nanobot/heartbeat/service.py @@ -153,9 +153,15 @@ class HeartbeatService: logger.info("Heartbeat: OK (nothing to report)") return + taskmessage = tasks + "\n\n**IMPORTANT NOTICE:** If there is nothing material to report, reply only with ." + logger.info("Heartbeat: tasks found, executing...") if self.on_execute: - response = await self.on_execute(tasks) + response = await self.on_execute(taskmessage) + + if response and "" in response: + logger.info("Heartbeat: OK (silenced by agent)") + return if response and self.on_notify: logger.info("Heartbeat: completed, delivering response") await self.on_notify(response) From 411b059dd22884ba7b54d6c8c00bcc4add95bfb0 Mon Sep 17 00:00:00 2001 From: Xubin Ren Date: Sat, 14 Mar 2026 09:29:56 +0000 Subject: [PATCH 05/10] refactor: replace with structured post-run evaluation - Add nanobot/utils/evaluator.py: lightweight LLM tool-call to decide notify/silent after background task execution - Remove magic token injection from heartbeat and cron prompts - Clean session history (no more pollution) - Add tests for evaluator and updated heartbeat three-phase flow --- nanobot/cli/commands.py | 22 ++++---- nanobot/heartbeat/service.py | 21 ++++---- nanobot/utils/evaluator.py | 92 +++++++++++++++++++++++++++++++++ tests/test_evaluator.py | 63 ++++++++++++++++++++++ tests/test_heartbeat_service.py | 92 +++++++++++++++++++++++++++++++++ 5 files changed, 272 insertions(+), 18 deletions(-) create mode 100644 nanobot/utils/evaluator.py create mode 100644 tests/test_evaluator.py diff --git a/nanobot/cli/commands.py b/nanobot/cli/commands.py index c4aa868..d8aa411 100644 --- a/nanobot/cli/commands.py +++ b/nanobot/cli/commands.py @@ -448,14 +448,14 @@ def gateway( """Execute a cron job through the agent.""" from nanobot.agent.tools.cron import CronTool from nanobot.agent.tools.message import MessageTool + from nanobot.utils.evaluator import evaluate_response + reminder_note = ( "[Scheduled Task] Timer finished.\n\n" f"Task '{job.name}' has been triggered.\n" f"Scheduled instruction: {job.payload.message}" - "**IMPORTANT NOTICE:** If there is nothing material to report, reply only with ." ) - # Prevent the agent from scheduling new cron jobs during execution cron_tool = agent.tools.get("cron") cron_token = None if isinstance(cron_tool, CronTool): @@ -475,13 +475,17 @@ def gateway( if isinstance(message_tool, MessageTool) and message_tool._sent_in_turn: return response - if job.payload.deliver and job.payload.to and response and "" not in response: - from nanobot.bus.events import OutboundMessage - await bus.publish_outbound(OutboundMessage( - channel=job.payload.channel or "cli", - chat_id=job.payload.to, - content=response - )) + if job.payload.deliver and job.payload.to and response: + should_notify = await evaluate_response( + response, job.payload.message, provider, agent.model, + ) + if should_notify: + from nanobot.bus.events import OutboundMessage + await bus.publish_outbound(OutboundMessage( + channel=job.payload.channel or "cli", + chat_id=job.payload.to, + content=response, + )) return response cron.on_job = on_cron_job diff --git a/nanobot/heartbeat/service.py b/nanobot/heartbeat/service.py index 916c813..2242802 100644 --- a/nanobot/heartbeat/service.py +++ b/nanobot/heartbeat/service.py @@ -139,6 +139,8 @@ class HeartbeatService: async def _tick(self) -> None: """Execute a single heartbeat tick.""" + from nanobot.utils.evaluator import evaluate_response + content = self._read_heartbeat_file() if not content: logger.debug("Heartbeat: HEARTBEAT.md missing or empty") @@ -153,18 +155,19 @@ class HeartbeatService: logger.info("Heartbeat: OK (nothing to report)") return - taskmessage = tasks + "\n\n**IMPORTANT NOTICE:** If there is nothing material to report, reply only with ." - logger.info("Heartbeat: tasks found, executing...") if self.on_execute: - response = await self.on_execute(taskmessage) + response = await self.on_execute(tasks) - if response and "" in response: - logger.info("Heartbeat: OK (silenced by agent)") - return - if response and self.on_notify: - logger.info("Heartbeat: completed, delivering response") - await self.on_notify(response) + if response: + should_notify = await evaluate_response( + response, tasks, self.provider, self.model, + ) + if should_notify and self.on_notify: + logger.info("Heartbeat: completed, delivering response") + await self.on_notify(response) + else: + logger.info("Heartbeat: silenced by post-run evaluation") except Exception: logger.exception("Heartbeat execution failed") diff --git a/nanobot/utils/evaluator.py b/nanobot/utils/evaluator.py new file mode 100644 index 0000000..6110471 --- /dev/null +++ b/nanobot/utils/evaluator.py @@ -0,0 +1,92 @@ +"""Post-run evaluation for background tasks (heartbeat & cron). + +After the agent executes a background task, this module makes a lightweight +LLM call to decide whether the result warrants notifying the user. +""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +from loguru import logger + +if TYPE_CHECKING: + from nanobot.providers.base import LLMProvider + +_EVALUATE_TOOL = [ + { + "type": "function", + "function": { + "name": "evaluate_notification", + "description": "Decide whether the user should be notified about this background task result.", + "parameters": { + "type": "object", + "properties": { + "should_notify": { + "type": "boolean", + "description": "true = result contains actionable/important info the user should see; false = routine or empty, safe to suppress", + }, + "reason": { + "type": "string", + "description": "One-sentence reason for the decision", + }, + }, + "required": ["should_notify"], + }, + }, + } +] + +_SYSTEM_PROMPT = ( + "You are a notification gate for a background agent. " + "You will be given the original task and the agent's response. " + "Call the evaluate_notification tool to decide whether the user " + "should be notified.\n\n" + "Notify when the response contains actionable information, errors, " + "completed deliverables, or anything the user explicitly asked to " + "be reminded about.\n\n" + "Suppress when the response is a routine status check with nothing " + "new, a confirmation that everything is normal, or essentially empty." +) + + +async def evaluate_response( + response: str, + task_context: str, + provider: LLMProvider, + model: str, +) -> bool: + """Decide whether a background-task result should be delivered to the user. + + Uses a lightweight tool-call LLM request (same pattern as heartbeat + ``_decide()``). Falls back to ``True`` (notify) on any failure so + that important messages are never silently dropped. + """ + try: + llm_response = await provider.chat_with_retry( + messages=[ + {"role": "system", "content": _SYSTEM_PROMPT}, + {"role": "user", "content": ( + f"## Original task\n{task_context}\n\n" + f"## Agent response\n{response}" + )}, + ], + tools=_EVALUATE_TOOL, + model=model, + max_tokens=256, + temperature=0.0, + ) + + if not llm_response.has_tool_calls: + logger.warning("evaluate_response: no tool call returned, defaulting to notify") + return True + + args = llm_response.tool_calls[0].arguments + should_notify = args.get("should_notify", True) + reason = args.get("reason", "") + logger.info("evaluate_response: should_notify={}, reason={}", should_notify, reason) + return bool(should_notify) + + except Exception: + logger.exception("evaluate_response failed, defaulting to notify") + return True diff --git a/tests/test_evaluator.py b/tests/test_evaluator.py new file mode 100644 index 0000000..08d068b --- /dev/null +++ b/tests/test_evaluator.py @@ -0,0 +1,63 @@ +import pytest + +from nanobot.utils.evaluator import evaluate_response +from nanobot.providers.base import LLMProvider, LLMResponse, ToolCallRequest + + +class DummyProvider(LLMProvider): + def __init__(self, responses: list[LLMResponse]): + super().__init__() + self._responses = list(responses) + + async def chat(self, *args, **kwargs) -> LLMResponse: + if self._responses: + return self._responses.pop(0) + return LLMResponse(content="", tool_calls=[]) + + def get_default_model(self) -> str: + return "test-model" + + +def _eval_tool_call(should_notify: bool, reason: str = "") -> LLMResponse: + return LLMResponse( + content="", + tool_calls=[ + ToolCallRequest( + id="eval_1", + name="evaluate_notification", + arguments={"should_notify": should_notify, "reason": reason}, + ) + ], + ) + + +@pytest.mark.asyncio +async def test_should_notify_true() -> None: + provider = DummyProvider([_eval_tool_call(True, "user asked to be reminded")]) + result = await evaluate_response("Task completed with results", "check emails", provider, "m") + assert result is True + + +@pytest.mark.asyncio +async def test_should_notify_false() -> None: + provider = DummyProvider([_eval_tool_call(False, "routine check, nothing new")]) + result = await evaluate_response("All clear, no updates", "check status", provider, "m") + assert result is False + + +@pytest.mark.asyncio +async def test_fallback_on_error() -> None: + class FailingProvider(DummyProvider): + async def chat(self, *args, **kwargs) -> LLMResponse: + raise RuntimeError("provider down") + + provider = FailingProvider([]) + result = await evaluate_response("some response", "some task", provider, "m") + assert result is True + + +@pytest.mark.asyncio +async def test_no_tool_call_fallback() -> None: + provider = DummyProvider([LLMResponse(content="I think you should notify", tool_calls=[])]) + result = await evaluate_response("some response", "some task", provider, "m") + assert result is True diff --git a/tests/test_heartbeat_service.py b/tests/test_heartbeat_service.py index 9ce8912..2a6b20e 100644 --- a/tests/test_heartbeat_service.py +++ b/tests/test_heartbeat_service.py @@ -123,6 +123,98 @@ async def test_trigger_now_returns_none_when_decision_is_skip(tmp_path) -> None: assert await service.trigger_now() is None +@pytest.mark.asyncio +async def test_tick_notifies_when_evaluator_says_yes(tmp_path, monkeypatch) -> None: + """Phase 1 run -> Phase 2 execute -> Phase 3 evaluate=notify -> on_notify called.""" + (tmp_path / "HEARTBEAT.md").write_text("- [ ] check deployments", encoding="utf-8") + + provider = DummyProvider([ + LLMResponse( + content="", + tool_calls=[ + ToolCallRequest( + id="hb_1", + name="heartbeat", + arguments={"action": "run", "tasks": "check deployments"}, + ) + ], + ), + ]) + + executed: list[str] = [] + notified: list[str] = [] + + async def _on_execute(tasks: str) -> str: + executed.append(tasks) + return "deployment failed on staging" + + async def _on_notify(response: str) -> None: + notified.append(response) + + service = HeartbeatService( + workspace=tmp_path, + provider=provider, + model="openai/gpt-4o-mini", + on_execute=_on_execute, + on_notify=_on_notify, + ) + + async def _eval_notify(*a, **kw): + return True + + monkeypatch.setattr("nanobot.utils.evaluator.evaluate_response", _eval_notify) + + await service._tick() + assert executed == ["check deployments"] + assert notified == ["deployment failed on staging"] + + +@pytest.mark.asyncio +async def test_tick_suppresses_when_evaluator_says_no(tmp_path, monkeypatch) -> None: + """Phase 1 run -> Phase 2 execute -> Phase 3 evaluate=silent -> on_notify NOT called.""" + (tmp_path / "HEARTBEAT.md").write_text("- [ ] check status", encoding="utf-8") + + provider = DummyProvider([ + LLMResponse( + content="", + tool_calls=[ + ToolCallRequest( + id="hb_1", + name="heartbeat", + arguments={"action": "run", "tasks": "check status"}, + ) + ], + ), + ]) + + executed: list[str] = [] + notified: list[str] = [] + + async def _on_execute(tasks: str) -> str: + executed.append(tasks) + return "everything is fine, no issues" + + async def _on_notify(response: str) -> None: + notified.append(response) + + service = HeartbeatService( + workspace=tmp_path, + provider=provider, + model="openai/gpt-4o-mini", + on_execute=_on_execute, + on_notify=_on_notify, + ) + + async def _eval_silent(*a, **kw): + return False + + monkeypatch.setattr("nanobot.utils.evaluator.evaluate_response", _eval_silent) + + await service._tick() + assert executed == ["check status"] + assert notified == [] + + @pytest.mark.asyncio async def test_decide_retries_transient_error_then_succeeds(tmp_path, monkeypatch) -> None: provider = DummyProvider([ From 4dde195a287ca98c16f6d347c0a80ca27111bac6 Mon Sep 17 00:00:00 2001 From: lihua Date: Fri, 13 Mar 2026 16:37:48 +0800 Subject: [PATCH 06/10] init --- nanobot/config/schema.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nanobot/config/schema.py b/nanobot/config/schema.py index 7471966..aa3e676 100644 --- a/nanobot/config/schema.py +++ b/nanobot/config/schema.py @@ -140,7 +140,7 @@ class MCPServerConfig(Base): url: str = "" # HTTP/SSE: endpoint URL headers: dict[str, str] = Field(default_factory=dict) # HTTP/SSE: custom headers tool_timeout: int = 30 # seconds before a tool call is cancelled - + enabled_tools: list[str] = Field(default_factory=list) # Only register these tools; empty = all tools class ToolsConfig(Base): """Tools configuration.""" From 40fad91ec219dbcd17b86bccfca928d550c4a2c1 Mon Sep 17 00:00:00 2001 From: lihua Date: Fri, 13 Mar 2026 16:40:25 +0800 Subject: [PATCH 07/10] =?UTF-8?q?=E6=B3=A8=E5=86=8Cmcp=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=8C=87=E5=AE=9Atool?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nanobot/agent/tools/mcp.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/nanobot/agent/tools/mcp.py b/nanobot/agent/tools/mcp.py index 400979b..8c5c6ba 100644 --- a/nanobot/agent/tools/mcp.py +++ b/nanobot/agent/tools/mcp.py @@ -138,11 +138,17 @@ async def connect_mcp_servers( await session.initialize() tools = await session.list_tools() + enabled_tools = set(cfg.enabled_tools) if cfg.enabled_tools else None + registered_count = 0 for tool_def in tools.tools: + if enabled_tools and tool_def.name not in enabled_tools: + logger.debug("MCP: skipping tool '{}' from server '{}' (not in enabledTools)", tool_def.name, name) + continue wrapper = MCPToolWrapper(session, name, tool_def, tool_timeout=cfg.tool_timeout) registry.register(wrapper) logger.debug("MCP: registered tool '{}' from server '{}'", wrapper.name, name) + registered_count += 1 - logger.info("MCP server '{}': connected, {} tools registered", name, len(tools.tools)) + logger.info("MCP server '{}': connected, {} tools registered", name, registered_count) except Exception as e: logger.error("MCP server '{}': failed to connect: {}", name, e) From a1241ee68ccb333abd905f526823583e56c8220b Mon Sep 17 00:00:00 2001 From: Xubin Ren Date: Sat, 14 Mar 2026 10:26:15 +0000 Subject: [PATCH 08/10] fix(mcp): clarify enabledTools filtering semantics - support both raw and wrapped MCP tool names - treat [\"*\"] as all tools and [] as no tools - add warnings, tests, and README docs for enabledTools --- README.md | 22 +++++ nanobot/agent/tools/mcp.py | 36 ++++++- nanobot/config/schema.py | 2 +- tests/test_mcp_tool.py | 187 ++++++++++++++++++++++++++++++++++++- 4 files changed, 241 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e7bb41d..bc27255 100644 --- a/README.md +++ b/README.md @@ -1112,6 +1112,28 @@ Use `toolTimeout` to override the default 30s per-call timeout for slow servers: } ``` +Use `enabledTools` to register only a subset of tools from an MCP server: + +```json +{ + "tools": { + "mcpServers": { + "filesystem": { + "command": "npx", + "args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/dir"], + "enabledTools": ["read_file", "mcp_filesystem_write_file"] + } + } + } +} +``` + +`enabledTools` accepts either the raw MCP tool name (for example `read_file`) or the wrapped nanobot tool name (for example `mcp_filesystem_write_file`). + +- Omit `enabledTools`, or set it to `["*"]`, to register all tools. +- Set `enabledTools` to `[]` to register no tools from that server. +- Set `enabledTools` to a non-empty list of names to register only that subset. + MCP tools are automatically discovered and registered on startup. The LLM can use them alongside built-in tools — no extra configuration needed. diff --git a/nanobot/agent/tools/mcp.py b/nanobot/agent/tools/mcp.py index 8c5c6ba..cebfbd2 100644 --- a/nanobot/agent/tools/mcp.py +++ b/nanobot/agent/tools/mcp.py @@ -138,16 +138,46 @@ async def connect_mcp_servers( await session.initialize() tools = await session.list_tools() - enabled_tools = set(cfg.enabled_tools) if cfg.enabled_tools else None + enabled_tools = set(cfg.enabled_tools) + allow_all_tools = "*" in enabled_tools registered_count = 0 + matched_enabled_tools: set[str] = set() + available_raw_names = [tool_def.name for tool_def in tools.tools] + available_wrapped_names = [f"mcp_{name}_{tool_def.name}" for tool_def in tools.tools] for tool_def in tools.tools: - if enabled_tools and tool_def.name not in enabled_tools: - logger.debug("MCP: skipping tool '{}' from server '{}' (not in enabledTools)", tool_def.name, name) + wrapped_name = f"mcp_{name}_{tool_def.name}" + if ( + not allow_all_tools + and tool_def.name not in enabled_tools + and wrapped_name not in enabled_tools + ): + logger.debug( + "MCP: skipping tool '{}' from server '{}' (not in enabledTools)", + wrapped_name, + name, + ) continue wrapper = MCPToolWrapper(session, name, tool_def, tool_timeout=cfg.tool_timeout) registry.register(wrapper) logger.debug("MCP: registered tool '{}' from server '{}'", wrapper.name, name) registered_count += 1 + if enabled_tools: + if tool_def.name in enabled_tools: + matched_enabled_tools.add(tool_def.name) + if wrapped_name in enabled_tools: + matched_enabled_tools.add(wrapped_name) + + if enabled_tools and not allow_all_tools: + unmatched_enabled_tools = sorted(enabled_tools - matched_enabled_tools) + if unmatched_enabled_tools: + logger.warning( + "MCP server '{}': enabledTools entries not found: {}. Available raw names: {}. " + "Available wrapped names: {}", + name, + ", ".join(unmatched_enabled_tools), + ", ".join(available_raw_names) or "(none)", + ", ".join(available_wrapped_names) or "(none)", + ) logger.info("MCP server '{}': connected, {} tools registered", name, registered_count) except Exception as e: diff --git a/nanobot/config/schema.py b/nanobot/config/schema.py index aa3e676..033fb63 100644 --- a/nanobot/config/schema.py +++ b/nanobot/config/schema.py @@ -140,7 +140,7 @@ class MCPServerConfig(Base): url: str = "" # HTTP/SSE: endpoint URL headers: dict[str, str] = Field(default_factory=dict) # HTTP/SSE: custom headers tool_timeout: int = 30 # seconds before a tool call is cancelled - enabled_tools: list[str] = Field(default_factory=list) # Only register these tools; empty = all tools + enabled_tools: list[str] = Field(default_factory=lambda: ["*"]) # Only register these tools; accepts raw MCP names or wrapped mcp__ names; ["*"] = all tools; [] = no tools class ToolsConfig(Base): """Tools configuration.""" diff --git a/tests/test_mcp_tool.py b/tests/test_mcp_tool.py index bf68425..d014f58 100644 --- a/tests/test_mcp_tool.py +++ b/tests/test_mcp_tool.py @@ -1,12 +1,15 @@ from __future__ import annotations import asyncio +from contextlib import AsyncExitStack, asynccontextmanager import sys from types import ModuleType, SimpleNamespace import pytest -from nanobot.agent.tools.mcp import MCPToolWrapper +from nanobot.agent.tools.mcp import MCPToolWrapper, connect_mcp_servers +from nanobot.agent.tools.registry import ToolRegistry +from nanobot.config.schema import MCPServerConfig class _FakeTextContent: @@ -14,12 +17,63 @@ class _FakeTextContent: self.text = text +@pytest.fixture +def fake_mcp_runtime() -> dict[str, object | None]: + return {"session": None} + + @pytest.fixture(autouse=True) -def _fake_mcp_module(monkeypatch: pytest.MonkeyPatch) -> None: +def _fake_mcp_module( + monkeypatch: pytest.MonkeyPatch, fake_mcp_runtime: dict[str, object | None] +) -> None: mod = ModuleType("mcp") mod.types = SimpleNamespace(TextContent=_FakeTextContent) + + class _FakeStdioServerParameters: + def __init__(self, command: str, args: list[str], env: dict | None = None) -> None: + self.command = command + self.args = args + self.env = env + + class _FakeClientSession: + def __init__(self, _read: object, _write: object) -> None: + self._session = fake_mcp_runtime["session"] + + async def __aenter__(self) -> object: + return self._session + + async def __aexit__(self, exc_type, exc, tb) -> bool: + return False + + @asynccontextmanager + async def _fake_stdio_client(_params: object): + yield object(), object() + + @asynccontextmanager + async def _fake_sse_client(_url: str, httpx_client_factory=None): + yield object(), object() + + @asynccontextmanager + async def _fake_streamable_http_client(_url: str, http_client=None): + yield object(), object(), object() + + mod.ClientSession = _FakeClientSession + mod.StdioServerParameters = _FakeStdioServerParameters monkeypatch.setitem(sys.modules, "mcp", mod) + client_mod = ModuleType("mcp.client") + stdio_mod = ModuleType("mcp.client.stdio") + stdio_mod.stdio_client = _fake_stdio_client + sse_mod = ModuleType("mcp.client.sse") + sse_mod.sse_client = _fake_sse_client + streamable_http_mod = ModuleType("mcp.client.streamable_http") + streamable_http_mod.streamable_http_client = _fake_streamable_http_client + + monkeypatch.setitem(sys.modules, "mcp.client", client_mod) + monkeypatch.setitem(sys.modules, "mcp.client.stdio", stdio_mod) + monkeypatch.setitem(sys.modules, "mcp.client.sse", sse_mod) + monkeypatch.setitem(sys.modules, "mcp.client.streamable_http", streamable_http_mod) + def _make_wrapper(session: object, *, timeout: float = 0.1) -> MCPToolWrapper: tool_def = SimpleNamespace( @@ -97,3 +151,132 @@ async def test_execute_handles_generic_exception() -> None: result = await wrapper.execute() assert result == "(MCP tool call failed: RuntimeError)" + + +def _make_tool_def(name: str) -> SimpleNamespace: + return SimpleNamespace( + name=name, + description=f"{name} tool", + inputSchema={"type": "object", "properties": {}}, + ) + + +def _make_fake_session(tool_names: list[str]) -> SimpleNamespace: + async def initialize() -> None: + return None + + async def list_tools() -> SimpleNamespace: + return SimpleNamespace(tools=[_make_tool_def(name) for name in tool_names]) + + return SimpleNamespace(initialize=initialize, list_tools=list_tools) + + +@pytest.mark.asyncio +async def test_connect_mcp_servers_enabled_tools_supports_raw_names( + fake_mcp_runtime: dict[str, object | None], +) -> None: + fake_mcp_runtime["session"] = _make_fake_session(["demo", "other"]) + registry = ToolRegistry() + stack = AsyncExitStack() + await stack.__aenter__() + try: + await connect_mcp_servers( + {"test": MCPServerConfig(command="fake", enabled_tools=["demo"])}, + registry, + stack, + ) + finally: + await stack.aclose() + + assert registry.tool_names == ["mcp_test_demo"] + + +@pytest.mark.asyncio +async def test_connect_mcp_servers_enabled_tools_defaults_to_all( + fake_mcp_runtime: dict[str, object | None], +) -> None: + fake_mcp_runtime["session"] = _make_fake_session(["demo", "other"]) + registry = ToolRegistry() + stack = AsyncExitStack() + await stack.__aenter__() + try: + await connect_mcp_servers( + {"test": MCPServerConfig(command="fake")}, + registry, + stack, + ) + finally: + await stack.aclose() + + assert registry.tool_names == ["mcp_test_demo", "mcp_test_other"] + + +@pytest.mark.asyncio +async def test_connect_mcp_servers_enabled_tools_supports_wrapped_names( + fake_mcp_runtime: dict[str, object | None], +) -> None: + fake_mcp_runtime["session"] = _make_fake_session(["demo", "other"]) + registry = ToolRegistry() + stack = AsyncExitStack() + await stack.__aenter__() + try: + await connect_mcp_servers( + {"test": MCPServerConfig(command="fake", enabled_tools=["mcp_test_demo"])}, + registry, + stack, + ) + finally: + await stack.aclose() + + assert registry.tool_names == ["mcp_test_demo"] + + +@pytest.mark.asyncio +async def test_connect_mcp_servers_enabled_tools_empty_list_registers_none( + fake_mcp_runtime: dict[str, object | None], +) -> None: + fake_mcp_runtime["session"] = _make_fake_session(["demo", "other"]) + registry = ToolRegistry() + stack = AsyncExitStack() + await stack.__aenter__() + try: + await connect_mcp_servers( + {"test": MCPServerConfig(command="fake", enabled_tools=[])}, + registry, + stack, + ) + finally: + await stack.aclose() + + assert registry.tool_names == [] + + +@pytest.mark.asyncio +async def test_connect_mcp_servers_enabled_tools_warns_on_unknown_entries( + fake_mcp_runtime: dict[str, object | None], monkeypatch: pytest.MonkeyPatch +) -> None: + fake_mcp_runtime["session"] = _make_fake_session(["demo"]) + registry = ToolRegistry() + warnings: list[str] = [] + + def _warning(message: str, *args: object) -> None: + warnings.append(message.format(*args)) + + monkeypatch.setattr("nanobot.agent.tools.mcp.logger.warning", _warning) + + stack = AsyncExitStack() + await stack.__aenter__() + try: + await connect_mcp_servers( + {"test": MCPServerConfig(command="fake", enabled_tools=["unknown"])}, + registry, + stack, + ) + finally: + await stack.aclose() + + assert registry.tool_names == [] + assert warnings + assert "enabledTools entries not found: unknown" in warnings[-1] + assert "Available raw names: demo" in warnings[-1] + assert "Available wrapped names: mcp_test_demo" in warnings[-1] From a2acacd8f2a2395438954877062499bfb424e16a Mon Sep 17 00:00:00 2001 From: chengyongru <2755839590@qq.com> Date: Sat, 14 Mar 2026 18:14:35 +0800 Subject: [PATCH 09/10] fix: add exception handling to prevent agent loop crash --- nanobot/agent/loop.py | 3 +++ nanobot/cli/commands.py | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.py index e05a73e..ed28a9e 100644 --- a/nanobot/agent/loop.py +++ b/nanobot/agent/loop.py @@ -258,6 +258,9 @@ class AgentLoop: msg = await asyncio.wait_for(self.bus.consume_inbound(), timeout=1.0) except asyncio.TimeoutError: continue + except Exception as e: + logger.warning("Error consuming inbound message: {}, continuing...", e) + continue cmd = msg.content.strip().lower() if cmd == "/stop": diff --git a/nanobot/cli/commands.py b/nanobot/cli/commands.py index d8aa411..685c1be 100644 --- a/nanobot/cli/commands.py +++ b/nanobot/cli/commands.py @@ -564,6 +564,10 @@ def gateway( ) except KeyboardInterrupt: console.print("\nShutting down...") + except Exception: + import traceback + console.print("\n[red]Error: Gateway crashed unexpectedly[/red]") + console.print(traceback.format_exc()) finally: await agent.close_mcp() heartbeat.stop() From 61f0923c66a12980d4e6420ba318ceac54276046 Mon Sep 17 00:00:00 2001 From: Xubin Ren Date: Sat, 14 Mar 2026 10:45:37 +0000 Subject: [PATCH 10/10] fix(telegram): include restart in help text --- nanobot/channels/telegram.py | 1 + tests/test_telegram_channel.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/nanobot/channels/telegram.py b/nanobot/channels/telegram.py index 9ffc208..a5942da 100644 --- a/nanobot/channels/telegram.py +++ b/nanobot/channels/telegram.py @@ -453,6 +453,7 @@ class TelegramChannel(BaseChannel): "🐈 nanobot commands:\n" "/new — Start a new conversation\n" "/stop — Stop the current task\n" + "/restart — Restart the bot\n" "/help — Show available commands" ) diff --git a/tests/test_telegram_channel.py b/tests/test_telegram_channel.py index 70feef5..c96f5e4 100644 --- a/tests/test_telegram_channel.py +++ b/tests/test_telegram_channel.py @@ -597,3 +597,19 @@ async def test_forward_command_does_not_inject_reply_context() -> None: assert len(handled) == 1 assert handled[0]["content"] == "/new" + + +@pytest.mark.asyncio +async def test_on_help_includes_restart_command() -> None: + channel = TelegramChannel( + TelegramConfig(enabled=True, token="123:abc", allow_from=["*"], group_policy="open"), + MessageBus(), + ) + update = _make_telegram_update(text="/help", chat_type="private") + update.message.reply_text = AsyncMock() + + await channel._on_help(update, None) + + update.message.reply_text.assert_awaited_once() + help_text = update.message.reply_text.await_args.args[0] + assert "/restart" in help_text