- Settings `features` (weather, webSearch, meals) and `mcpServers` (name, command, args, env names, enabled), edited on the new 기능 screen. The tools of a feature that is off go into the brain's `disabled_tools`; only enabled MCP servers go into its `mcps`. - MCP environment values (API keys) live only in the encrypted secrets (`mcp:<server>:<NAME>`), shown as stored or not, masked in logs, removed with their server, and reach the worker only in `JARVIS_MCP_ENV`, which the brain takes out of its environment before giving each server its own. - `enabled_builtin_tools(cfg)` is the one list the router, toolSearchTool and the executor use, so a turned-off tool is neither offered nor run, also when the model names it. - The v2 brain never offers v1's `localFiles` (home folder read/write/delete) and `screenshot`: anyone in the voice or chat channels could ask for them. Screen viewing comes in M7. - The brain connects MCP servers on its own thread and logs each outcome without debug logging. - Stored secrets are masked in the log hub as soon as the data folder is found. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
133 lines
5.3 KiB
Python
133 lines
5.3 KiB
Python
"""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()
|