"""Turning tools off and the MCP servers the v2 brain connects (voice_worker.spec.md, "Tools"; reply.spec.md, "Tools and MCP"). Behaviours: a tool in ``disabled_tools`` is neither offered nor run, also through toolSearchTool; the v2 brain never offers v1's host file and screen tools; it connects the configured MCP servers with the environment the app passes outside the config file, and keeps that out of its own environment. """ from __future__ import annotations import dataclasses import json import sys import textwrap import time from pathlib import Path import pytest def _settings(tmp_path, monkeypatch, **extra): from jarvis.config import load_settings cfg = tmp_path / "config.json" cfg.write_text(json.dumps({"db_path": str(tmp_path / "jarvis.db"), **extra})) monkeypatch.setenv("JARVIS_CONFIG_PATH", str(cfg)) return load_settings() @pytest.mark.unit def test_disabled_tools_are_read_from_the_settings(tmp_path, monkeypatch): assert _settings(tmp_path, monkeypatch).disabled_tools == () s = _settings(tmp_path, monkeypatch, disabled_tools=["getWeather", "webSearch"]) assert set(s.disabled_tools) == {"getWeather", "webSearch"} @pytest.mark.unit def test_a_disabled_tool_is_not_offered(tmp_path, monkeypatch): from jarvis.reply.engine import _offered_builtins s = _settings(tmp_path, monkeypatch, disabled_tools=["getWeather"]) offered = _offered_builtins(s, chat_available=False) assert "getWeather" not in offered assert "webSearch" in offered @pytest.mark.unit def test_a_disabled_tool_does_not_run_even_when_the_model_names_it(tmp_path, monkeypatch): from jarvis.tools.registry import run_tool_with_retries monkeypatch.setenv("HOME", str(tmp_path)) monkeypatch.setenv("USERPROFILE", str(tmp_path)) s = _settings(tmp_path, monkeypatch, disabled_tools=["localFiles"]) r = run_tool_with_retries(None, s, "localFiles", {"operation": "write", "path": "x.txt", "content": "hi"}, "", "", "") assert r.success is False assert not (tmp_path / "x.txt").exists() @pytest.mark.unit def test_tool_search_never_surfaces_a_disabled_tool(tmp_path, monkeypatch): from jarvis.tools.base import ToolContext from jarvis.tools.builtin import tool_search # A router that picks every tool it is shown: what it is shown is the catalogue. monkeypatch.setattr(tool_search, "select_tools", lambda builtin_tools, **kw: list(builtin_tools)) s = _settings(tmp_path, monkeypatch, disabled_tools=["localFiles", "screenshot"]) ctx = ToolContext(db=None, cfg=s, system_prompt="", original_prompt="", redacted_text="", max_retries=0, user_print=lambda m: None) r = tool_search.ToolSearchTool().run({"query": "files on my computer"}, ctx) assert r.success names = {line.split(":", 1)[0].strip() for line in r.reply_text.splitlines() if ":" in line} assert "getWeather" in names assert not {"localFiles", "screenshot"} & names FAKE_SERVER = textwrap.dedent(""" import os from mcp.server.fastmcp import FastMCP app = FastMCP("fake") @app.tool() def secret_echo() -> str: \"\"\"Says the key it was started with.\"\"\" return "key=" + os.environ.get("FAKE_KEY", "") app.run() """) @pytest.mark.unit def test_the_v2_brain_offers_no_host_files_or_screen_and_connects_mcp_with_the_apps_env(tmp_path, monkeypatch, capfd): pytest.importorskip("mcp") # the real brain needs the jarvis dependencies (--brain) from jarvis.reply.engine import _offered_builtins from jarvis.tools import registry from voice_worker import brain as brain_mod server = tmp_path / "fake_server.py" server.write_text(FAKE_SERVER) _settings(tmp_path, monkeypatch, disabled_tools=["getWeather"], mcps={"fake": {"command": sys.executable, "args": [str(server)]}, "broken": {"command": str(tmp_path / "no-such-program")}}) monkeypatch.setenv(brain_mod.MCP_ENV_VAR, json.dumps({"fake": {"FAKE_KEY": "s3cret"}})) monkeypatch.setattr(registry, "_mcp_tools_cache", {}) b = brain_mod.JarvisBrain() # The secrets are taken out of the worker's environment, so no other child process inherits them. import os assert brain_mod.MCP_ENV_VAR not in os.environ offered = _offered_builtins(b.cfg, chat_available=False) assert not {"localFiles", "screenshot", "getWeather"} & set(offered) assert "webSearch" in offered deadline = time.monotonic() + 30 while "fake__secret_echo" not in registry.get_cached_mcp_tools() and time.monotonic() < deadline: time.sleep(0.1) assert "fake__secret_echo" in registry.get_cached_mcp_tools() r = registry.run_tool_with_retries(None, b.cfg, "fake__secret_echo", {}, "", "", "") assert r.success and "key=s3cret" in (r.reply_text or "") # Each server's outcome reaches the app's log without debug logging, never the secret. deadline = time.monotonic() + 10 err = "" while "broken" not in err and time.monotonic() < deadline: time.sleep(0.1) err += capfd.readouterr().err assert "MCP server fake: 1 tool(s)" in err assert "Warning: MCP server broken failed" in err assert "s3cret" not in err # The config file the app wrote never holds the secret. assert "s3cret" not in (tmp_path / "config.json").read_text() @pytest.mark.unit def test_a_slow_mcp_server_never_holds_up_a_turn_reading_the_tools(monkeypatch): """Discovery (which starts the servers) runs outside the cache lock: a turn reading the cached tools, and a new conversation's refresh, never wait for a slow server.""" import threading from jarvis.tools import registry release = threading.Event() calls = [] def slow_discover(cfg): calls.append(cfg) release.wait(10) return {}, {} monkeypatch.setattr(registry, "discover_mcp_tools", slow_discover) monkeypatch.setattr(registry, "_mcp_tools_cache", {}) monkeypatch.setattr(registry, "_mcp_config_cache", {}) t = threading.Thread(target=registry.initialize_mcp_tools, args=({"slow": {"command": "x"}}, False)) t.start() try: deadline = time.monotonic() + 5 while not calls and time.monotonic() < deadline: time.sleep(0.01) t0 = time.monotonic() assert registry.get_cached_mcp_tools() == {} assert registry.is_mcp_cache_initialized() # A new conversation's refresh starts in the background and returns at once. registry.refresh_mcp_tools_in_background() assert time.monotonic() - t0 < 1 finally: release.set() t.join(10) # Let the background refresh end while the fake discovery is still in place. assert registry._mcp_background_refresh.acquire(timeout=10) registry._mcp_background_refresh.release() assert len(calls) == 2 # the start's discovery and the refresh's @pytest.mark.unit def test_the_tts_child_never_inherits_the_mcp_secrets(monkeypatch): """The worker takes the app's MCP environment before it starts any child process (MeloTTS) and hands it to the brain.""" import os import voice_worker.__main__ as main_mod from voice_worker import brain as brain_mod seen = {} class Stop(Exception): pass class FakeTranscriber: def __init__(self, *a): pass def warm_up(self): pass class FakeMelo: def __init__(self, *a): seen["tts_env"] = brain_mod.MCP_ENV_VAR in os.environ class FakeBrain: def __init__(self, **kw): seen["brain_env"] = kw.get("mcp_env") raise Stop import voice_worker.stt_whisper as stt import voice_worker.tts_melo as melo import voice_worker.vad as vad monkeypatch.setattr(stt, "WhisperTranscriber", FakeTranscriber) monkeypatch.setattr(vad, "SileroVad", lambda: None) monkeypatch.setattr(melo, "MeloEngine", FakeMelo) monkeypatch.setattr(brain_mod, "JarvisBrain", FakeBrain) monkeypatch.setenv("JARVIS_VOICE_TOKEN", "t") monkeypatch.setenv(brain_mod.MCP_ENV_VAR, json.dumps({"fake": {"FAKE_KEY": "s3cret"}})) monkeypatch.setattr(sys, "argv", ["voice_worker", "--melo-python", "py", "--history", ":memory:", "--brain"]) with pytest.raises(Stop): main_mod.main() assert seen["tts_env"] is False assert seen["brain_env"] == {"fake": {"FAKE_KEY": "s3cret"}}