feat(app): features screen with tool switches and MCP servers; v2 drops host file and screen tools
- 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>
This commit is contained in:
135
app/src/features.rs
Normal file
135
app/src/features.rs
Normal file
@@ -0,0 +1,135 @@
|
||||
//! The brain's features and MCP servers (`app/src/admin.spec.md`, "Features and MCP servers"):
|
||||
//! what the app writes into the brain's settings, and the MCP servers' environment values, which
|
||||
//! are secrets and reach the worker only through its environment.
|
||||
|
||||
use serde_json::{json, Map, Value};
|
||||
|
||||
use crate::secrets::{Secrets, SecretsError};
|
||||
use crate::settings::{FeatureSettings, McpServerSettings};
|
||||
|
||||
/// The worker's environment variable with the MCP servers' environment values
|
||||
/// (`voice_worker/brain.py`, `MCP_ENV_VAR`).
|
||||
pub const MCP_ENV_VAR: &str = "JARVIS_MCP_ENV";
|
||||
|
||||
/// The brain's built-in tools behind each feature switch (`jarvis/tools/registry.py`).
|
||||
pub const FEATURE_TOOLS: &[(&str, &[&str])] = &[
|
||||
("weather", &["getWeather"]),
|
||||
("webSearch", &["webSearch", "fetchWebPage"]),
|
||||
("meals", &["logMeal", "fetchMeals", "deleteMeal"]),
|
||||
];
|
||||
|
||||
/// The built-in tools of the features that are off.
|
||||
pub fn disabled_tools(f: &FeatureSettings) -> Vec<&'static str> {
|
||||
let on = |key: &str| match key {
|
||||
"weather" => f.weather,
|
||||
"webSearch" => f.web_search,
|
||||
_ => f.meals,
|
||||
};
|
||||
FEATURE_TOOLS.iter().filter(|(k, _)| !on(k)).flat_map(|(_, tools)| tools.iter().copied()).collect()
|
||||
}
|
||||
|
||||
/// The brain's `mcps`: the enabled servers, without their environment values.
|
||||
pub fn brain_mcps(servers: &[McpServerSettings]) -> Value {
|
||||
Value::Object(servers.iter().filter(|s| s.enabled)
|
||||
.map(|s| (s.name.clone(), json!({"transport": "stdio", "command": s.command, "args": s.args})))
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// Where one environment value of a server is kept in the secrets.
|
||||
pub fn secret_name(server: &str, var: &str) -> String {
|
||||
format!("mcp:{server}:{var}")
|
||||
}
|
||||
|
||||
/// `{server: {NAME: value}}` for the enabled servers, with the values that are stored.
|
||||
pub fn mcp_env(secrets: &Secrets, servers: &[McpServerSettings]) -> Result<Value, SecretsError> {
|
||||
let mut out = Map::new();
|
||||
for s in servers.iter().filter(|s| s.enabled) {
|
||||
let mut vars = Map::new();
|
||||
for var in &s.env {
|
||||
if let Some(v) = secrets.get(&secret_name(&s.name, var))? {
|
||||
vars.insert(var.clone(), Value::String(v));
|
||||
}
|
||||
}
|
||||
if !vars.is_empty() {
|
||||
out.insert(s.name.clone(), Value::Object(vars));
|
||||
}
|
||||
}
|
||||
Ok(Value::Object(out))
|
||||
}
|
||||
|
||||
/// Which environment values are stored: `{server: {NAME: true|false}}` (never the values).
|
||||
pub fn env_status(secrets: &Secrets, servers: &[McpServerSettings]) -> Result<Value, SecretsError> {
|
||||
let names = secrets.names()?;
|
||||
Ok(Value::Object(servers.iter().map(|s| {
|
||||
let vars = s.env.iter().map(|v| (v.clone(), Value::Bool(names.contains(&secret_name(&s.name, v))))).collect();
|
||||
(s.name.clone(), Value::Object(vars))
|
||||
}).collect()))
|
||||
}
|
||||
|
||||
/// Remove the stored values of servers and names no longer listed.
|
||||
pub fn prune(secrets: &Secrets, servers: &[McpServerSettings]) -> Result<(), SecretsError> {
|
||||
let keep: Vec<String> = servers.iter().flat_map(|s| s.env.iter().map(|v| secret_name(&s.name, v))).collect();
|
||||
for name in secrets.names()? {
|
||||
if name.starts_with("mcp:") && !keep.contains(&name) {
|
||||
secrets.remove(&name)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::datadir::temp_dir;
|
||||
use crate::settings::parse_settings;
|
||||
|
||||
fn servers(v: Value) -> Vec<McpServerSettings> {
|
||||
parse_settings(&json!({"mcpServers": v})).unwrap().mcp_servers
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_feature_tools_are_the_brains_tool_names() {
|
||||
let registry = std::fs::read_to_string(
|
||||
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../src/jarvis/tools/registry.py")).unwrap();
|
||||
for (_, tools) in FEATURE_TOOLS {
|
||||
for t in *tools {
|
||||
assert!(registry.contains(&format!("\"{t}\":")), "{t} is not a built-in tool");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn features_that_are_off_turn_their_tools_off() {
|
||||
let all_on = FeatureSettings { weather: true, web_search: true, meals: true };
|
||||
assert!(disabled_tools(&all_on).is_empty());
|
||||
let off = disabled_tools(&FeatureSettings { weather: false, web_search: true, meals: false });
|
||||
assert_eq!(off, vec!["getWeather", "logMeal", "fetchMeals", "deleteMeal"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_enabled_servers_reach_the_brain_and_their_values_only_through_the_environment() {
|
||||
let s = servers(json!([
|
||||
{"name": "brave", "command": "npx", "args": ["-y", "brave"], "env": ["BRAVE_API_KEY", "UNSET"]},
|
||||
{"name": "off", "command": "uvx", "env": ["K"], "enabled": false}]));
|
||||
assert_eq!(brain_mcps(&s), json!({"brave": {"transport": "stdio", "command": "npx", "args": ["-y", "brave"]}}));
|
||||
let secrets = Secrets::new(&temp_dir("mcpenv").join("secrets.dat"));
|
||||
secrets.set(&secret_name("brave", "BRAVE_API_KEY"), "key-1").unwrap();
|
||||
secrets.set(&secret_name("off", "K"), "key-2").unwrap();
|
||||
assert_eq!(mcp_env(&secrets, &s).unwrap(), json!({"brave": {"BRAVE_API_KEY": "key-1"}}));
|
||||
assert_eq!(env_status(&secrets, &s).unwrap(),
|
||||
json!({"brave": {"BRAVE_API_KEY": true, "UNSET": false}, "off": {"K": true}}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn values_of_removed_servers_and_names_are_deleted() {
|
||||
let secrets = Secrets::new(&temp_dir("mcpprune").join("secrets.dat"));
|
||||
secrets.set(crate::secrets::DISCORD_TOKEN, "tok").unwrap();
|
||||
for (srv, var) in [("a", "K1"), ("a", "K2"), ("b", "K")] {
|
||||
secrets.set(&secret_name(srv, var), "v").unwrap();
|
||||
}
|
||||
prune(&secrets, &servers(json!([{"name": "a", "command": "x", "env": ["K1"]}]))).unwrap();
|
||||
let mut left = secrets.names().unwrap();
|
||||
left.sort();
|
||||
assert_eq!(left, vec![crate::secrets::DISCORD_TOKEN.to_string(), secret_name("a", "K1")]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user