| 1 | //! In-TUI MCP manager command parser. |
| 2 | |
| 3 | use codewhale_command_contract::facets::CommandPresentationContext; |
| 4 | use codewhale_command_contract::handler::{CommandCapabilities, CommandContexts, CommandHandler}; |
| 5 | use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand}; |
| 6 | |
| 7 | use crate::commands::CommandResult; |
| 8 | use crate::tui::app::{AppAction, McpUiAction}; |
| 9 | |
| 10 | const GITHUB_MCP_URL: &str = "https://api.githubcopilot.com/mcp/"; |
| 11 | const CHROME_DEVTOOLS_MCP_PACKAGE: &str = "chrome-devtools-mcp@1.7.0"; |
| 12 | const PLAYWRIGHT_MCP_PACKAGE: &str = "@playwright/mcp@0.0.79"; |
| 13 | const PLAYWRIGHT_MCP_SOURCE: &str = "https://github.com/microsoft/playwright-mcp"; |
| 14 | const CONTAINER_USE_SOURCE: &str = "https://github.com/dagger/container-use"; |
| 15 | |
| 16 | pub(in crate::commands) const COMMAND_INFO: CommandInfo = CommandInfo { |
| 17 | name: "mcp", |
| 18 | aliases: &["mcps"], |
| 19 | usage: "/mcp [init|review|approve <digest>|revoke|import|import approve <name>|import decline <name>|recommendations|add recommended <id>|add stdio <name> <command> [args...]|add http <name> <url>|enable <name>|disable <name>|remove <name>|retry <name>|doctor|validate|restart|reload]", |
| 20 | description_key: "cmd_mcp_description", |
| 21 | }; |
| 22 | |
| 23 | pub(in crate::commands) struct McpCmd; |
| 24 | |
| 25 | impl RegisterCommand<CommandResult> for McpCmd { |
| 26 | fn info() -> &'static CommandInfo { |
| 27 | &COMMAND_INFO |
| 28 | } |
| 29 | |
| 30 | fn handler() -> CommandHandler<CommandResult> { |
| 31 | CommandHandler::Contextual { |
| 32 | capabilities: CommandCapabilities::PRESENTATION, |
| 33 | handler: mcp_contextual, |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | fn mcp_contextual(contexts: CommandContexts<'_>, args: Option<&str>) -> CommandResult { |
| 39 | let mut parts = contexts.into_parts(); |
| 40 | let Some(presentation) = parts.presentation.as_deref_mut() else { |
| 41 | return CommandResult::error("Command capability unavailable: presentation"); |
| 42 | }; |
| 43 | mcp(presentation, args) |
| 44 | } |
| 45 | |
| 46 | fn mcp(presentation: &mut dyn CommandPresentationContext, args: Option<&str>) -> CommandResult { |
| 47 | let raw = args.unwrap_or("").trim(); |
| 48 | if raw.is_empty() { |
| 49 | return CommandResult::action(AppAction::OpenExtensions { |
| 50 | tab: crate::tui::views::extensions::ExtensionsTab::Mcp, |
| 51 | }); |
| 52 | } |
| 53 | if raw.eq_ignore_ascii_case("status") || raw.eq_ignore_ascii_case("list") { |
| 54 | return CommandResult::action(AppAction::Mcp(McpUiAction::Show)); |
| 55 | } |
| 56 | |
| 57 | let mut parts = raw.split_whitespace(); |
| 58 | let action = parts.next().unwrap_or("").to_ascii_lowercase(); |
| 59 | match action.as_str() { |
| 60 | "init" => CommandResult::action(AppAction::Mcp(McpUiAction::Init { |
| 61 | force: parts.any(|part| part == "--force" || part == "-f"), |
| 62 | })), |
| 63 | "review" => CommandResult::action(AppAction::Mcp(McpUiAction::ProjectReview)), |
| 64 | "approve" => match parts.next() { |
| 65 | Some(digest) => CommandResult::action(AppAction::Mcp(McpUiAction::ProjectApprove { |
| 66 | digest: digest.to_string(), |
| 67 | })), |
| 68 | None => CommandResult::error("Usage: /mcp approve <digest>"), |
| 69 | }, |
| 70 | "revoke" => CommandResult::action(AppAction::Mcp(McpUiAction::ProjectRevoke)), |
| 71 | "recommend" | "recommended" | "recommendations" => { |
| 72 | CommandResult::message(recommended_mcp_text(presentation)) |
| 73 | } |
| 74 | "add" => parse_add(presentation, parts.collect()), |
| 75 | "enable" => match parse_name(parts.next(), "Usage: /mcp enable <name>") { |
| 76 | Ok(name) => CommandResult::action(AppAction::Mcp(McpUiAction::Enable { name })), |
| 77 | Err(msg) => CommandResult::error(msg), |
| 78 | }, |
| 79 | "disable" => match parse_name(parts.next(), "Usage: /mcp disable <name>") { |
| 80 | Ok(name) => CommandResult::action(AppAction::Mcp(McpUiAction::Disable { name })), |
| 81 | Err(msg) => CommandResult::error(msg), |
| 82 | }, |
| 83 | "remove" | "rm" => match parse_name(parts.next(), "Usage: /mcp remove <name>") { |
| 84 | Ok(name) => CommandResult::action(AppAction::Mcp(McpUiAction::Remove { name })), |
| 85 | Err(msg) => CommandResult::error(msg), |
| 86 | }, |
| 87 | "login" => match parse_name(parts.next(), "Usage: /mcp login <name> [--scope scope]") { |
| 88 | Ok(name) => CommandResult::action(AppAction::Mcp(McpUiAction::Login { |
| 89 | name, |
| 90 | scopes: parse_scopes(parts.collect()), |
| 91 | })), |
| 92 | Err(msg) => CommandResult::error(msg), |
| 93 | }, |
| 94 | "logout" => match parse_name(parts.next(), "Usage: /mcp logout <name>") { |
| 95 | Ok(name) => CommandResult::action(AppAction::Mcp(McpUiAction::Logout { name })), |
| 96 | Err(msg) => CommandResult::error(msg), |
| 97 | }, |
| 98 | "retry" => match parse_name(parts.next(), "Usage: /mcp retry <name>") { |
| 99 | Ok(name) => CommandResult::action(AppAction::Mcp(McpUiAction::Retry { name })), |
| 100 | Err(msg) => CommandResult::error(msg), |
| 101 | }, |
| 102 | "import" | "marketplace" | "sources" => { |
| 103 | let sub = parts.next().unwrap_or("").to_ascii_lowercase(); |
| 104 | match sub.as_str() { |
| 105 | "" | "list" | "status" => { |
| 106 | CommandResult::action(AppAction::Mcp(McpUiAction::ImportList)) |
| 107 | } |
| 108 | "approve" | "add" => { |
| 109 | match parse_name(parts.next(), "Usage: /mcp import approve <name>") { |
| 110 | Ok(name) => { |
| 111 | CommandResult::action(AppAction::Mcp(McpUiAction::ImportApprove { |
| 112 | name, |
| 113 | })) |
| 114 | } |
| 115 | Err(msg) => CommandResult::error(msg), |
| 116 | } |
| 117 | } |
| 118 | "decline" | "deny" | "reject" => { |
| 119 | match parse_name(parts.next(), "Usage: /mcp import decline <name>") { |
| 120 | Ok(name) => { |
| 121 | CommandResult::action(AppAction::Mcp(McpUiAction::ImportDecline { |
| 122 | name, |
| 123 | })) |
| 124 | } |
| 125 | Err(msg) => CommandResult::error(msg), |
| 126 | } |
| 127 | } |
| 128 | _ => { |
| 129 | CommandResult::error("Usage: /mcp import [list|approve <name>|decline <name>]") |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | "validate" | "doctor" => match parts.next() { |
| 134 | Some(name) => CommandResult::action(AppAction::Mcp(McpUiAction::Diagnose { |
| 135 | name: name.to_string(), |
| 136 | })), |
| 137 | None => CommandResult::action(AppAction::Mcp(McpUiAction::Validate)), |
| 138 | }, |
| 139 | "reload" | "reconnect" | "restart" => { |
| 140 | CommandResult::action(AppAction::Mcp(McpUiAction::Reload)) |
| 141 | } |
| 142 | _ => CommandResult::error( |
| 143 | "Usage: /mcp [init|review|approve <digest>|revoke|import|recommendations|add recommended <id>|add stdio <name> <command> [args...]|add http <name> <url>|enable <name>|disable <name>|remove <name>|login <name>|logout <name>|retry <name>|doctor|validate|restart|reload]", |
| 144 | ), |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | fn parse_name(name: Option<&str>, usage: &str) -> Result<String, String> { |
| 149 | match name { |
| 150 | Some(name) if !name.trim().is_empty() => Ok(name.to_string()), |
| 151 | _ => Err(usage.to_string()), |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | fn parse_add(presentation: &mut dyn CommandPresentationContext, parts: Vec<&str>) -> CommandResult { |
| 156 | parse_add_for_platform(presentation, parts, cfg!(windows)) |
| 157 | } |
| 158 | |
| 159 | fn parse_add_for_platform( |
| 160 | presentation: &mut dyn CommandPresentationContext, |
| 161 | parts: Vec<&str>, |
| 162 | windows: bool, |
| 163 | ) -> CommandResult { |
| 164 | if parts |
| 165 | .first() |
| 166 | .is_some_and(|part| part.eq_ignore_ascii_case("recommended")) |
| 167 | { |
| 168 | return match parts.as_slice() { |
| 169 | [_, id] if id.eq_ignore_ascii_case("hugging-face") || id.eq_ignore_ascii_case("hf") => { |
| 170 | CommandResult::action(AppAction::Mcp(McpUiAction::AddHttp { |
| 171 | name: "hugging-face".to_string(), |
| 172 | url: "https://huggingface.co/mcp".to_string(), |
| 173 | transport: None, |
| 174 | })) |
| 175 | } |
| 176 | [_, id] if id.eq_ignore_ascii_case("github") || id.eq_ignore_ascii_case("gh") => { |
| 177 | CommandResult::action(AppAction::Mcp(McpUiAction::AddHttp { |
| 178 | name: "github".to_string(), |
| 179 | url: GITHUB_MCP_URL.to_string(), |
| 180 | transport: None, |
| 181 | })) |
| 182 | } |
| 183 | [_, id] |
| 184 | if id.eq_ignore_ascii_case("chrome-devtools") |
| 185 | || id.eq_ignore_ascii_case("chrome") => |
| 186 | { |
| 187 | CommandResult::action(AppAction::Mcp(McpUiAction::AddStdio { |
| 188 | name: "chrome-devtools".to_string(), |
| 189 | command: recommended_npx_command_for(windows).to_string(), |
| 190 | args: vec!["-y".to_string(), CHROME_DEVTOOLS_MCP_PACKAGE.to_string()], |
| 191 | })) |
| 192 | } |
| 193 | [_, id] if id.eq_ignore_ascii_case("playwright") => { |
| 194 | CommandResult::action(AppAction::Mcp(McpUiAction::AddStdio { |
| 195 | name: "playwright".to_string(), |
| 196 | command: recommended_npx_command_for(windows).to_string(), |
| 197 | args: vec![ |
| 198 | "-y".to_string(), |
| 199 | PLAYWRIGHT_MCP_PACKAGE.to_string(), |
| 200 | "--isolated".to_string(), |
| 201 | ], |
| 202 | })) |
| 203 | } |
| 204 | [_, id] |
| 205 | if id.eq_ignore_ascii_case("container-use") |
| 206 | || id.eq_ignore_ascii_case("container") => |
| 207 | { |
| 208 | CommandResult::action(AppAction::Mcp(McpUiAction::AddStdio { |
| 209 | name: "container-use".to_string(), |
| 210 | command: "container-use".to_string(), |
| 211 | args: vec!["stdio".to_string()], |
| 212 | })) |
| 213 | } |
| 214 | [_, _] => CommandResult::error(mcp_unknown_id(presentation)), |
| 215 | _ => CommandResult::error("Usage: /mcp add recommended <id>"), |
| 216 | }; |
| 217 | } |
| 218 | if parts.len() < 3 { |
| 219 | return CommandResult::error( |
| 220 | "Usage: /mcp add stdio <name> <command> [args...] OR /mcp add http <name> <url>", |
| 221 | ); |
| 222 | } |
| 223 | match parts[0].to_ascii_lowercase().as_str() { |
| 224 | "stdio" => CommandResult::action(AppAction::Mcp(McpUiAction::AddStdio { |
| 225 | name: parts[1].to_string(), |
| 226 | command: parts[2].to_string(), |
| 227 | args: parts[3..].iter().map(|s| (*s).to_string()).collect(), |
| 228 | })), |
| 229 | "http" => CommandResult::action(AppAction::Mcp(McpUiAction::AddHttp { |
| 230 | name: parts[1].to_string(), |
| 231 | url: parts[2].to_string(), |
| 232 | transport: None, |
| 233 | })), |
| 234 | "sse" => CommandResult::action(AppAction::Mcp(McpUiAction::AddHttp { |
| 235 | name: parts[1].to_string(), |
| 236 | url: parts[2].to_string(), |
| 237 | transport: Some("sse".to_string()), |
| 238 | })), |
| 239 | _ => CommandResult::error( |
| 240 | "Usage: /mcp add stdio <name> <command> [args...] OR /mcp add http <name> <url>", |
| 241 | ), |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /// Localized unknown-recommendation error through the presentation facet (D3). |
| 246 | fn mcp_unknown_id(presentation: &mut dyn CommandPresentationContext) -> String { |
| 247 | presentation |
| 248 | .translate( |
| 249 | "mcp_recommended_unknown_id", |
| 250 | &[("recommendations_command", "/mcp recommendations")], |
| 251 | ) |
| 252 | .unwrap_or_else(|_| { |
| 253 | "Unknown MCP suggestion. Run /mcp recommendations to see the list.".to_string() |
| 254 | }) |
| 255 | } |
| 256 | |
| 257 | fn recommended_mcp_text(presentation: &mut dyn CommandPresentationContext) -> String { |
| 258 | let heading = presentation |
| 259 | .translate("mcp_recommendations_heading", &[]) |
| 260 | .unwrap_or_else(|_| "Suggested MCP servers (nothing installs automatically)".to_string()); |
| 261 | let safety = presentation |
| 262 | .translate( |
| 263 | "mcp_recommendations_safety", |
| 264 | &[("restart_command", "/mcp restart")], |
| 265 | ) |
| 266 | .unwrap_or_else(|_| "Viewing this list adds or enables nothing.".to_string()); |
| 267 | let github = presentation |
| 268 | .translate( |
| 269 | "mcp_recommendation_github", |
| 270 | &[ |
| 271 | ("endpoint", GITHUB_MCP_URL), |
| 272 | ("login_command", "/mcp login github"), |
| 273 | ("add_command", "/mcp add recommended github"), |
| 274 | ], |
| 275 | ) |
| 276 | .unwrap_or_else(|_| { |
| 277 | format!( |
| 278 | "• github — GitHub's official remote MCP endpoint\n endpoint: {GITHUB_MCP_URL}" |
| 279 | ) |
| 280 | }); |
| 281 | let chrome = presentation |
| 282 | .translate( |
| 283 | "mcp_recommendation_chrome", |
| 284 | &[ |
| 285 | ("package", CHROME_DEVTOOLS_MCP_PACKAGE), |
| 286 | ("launcher", "npx/npx.cmd"), |
| 287 | ("restart_command", "/mcp restart"), |
| 288 | ("add_command", "/mcp add recommended chrome-devtools"), |
| 289 | ], |
| 290 | ) |
| 291 | .unwrap_or_else(|_| { |
| 292 | format!("• chrome-devtools — official Chrome DevTools MCP via pinned npm package\n package: {CHROME_DEVTOOLS_MCP_PACKAGE}") |
| 293 | }); |
| 294 | let playwright = presentation |
| 295 | .translate( |
| 296 | "mcp_recommendation_playwright", |
| 297 | &[ |
| 298 | ("package", PLAYWRIGHT_MCP_PACKAGE), |
| 299 | ("source", PLAYWRIGHT_MCP_SOURCE), |
| 300 | ("launcher", "npx/npx.cmd"), |
| 301 | ("restart_command", "/mcp restart"), |
| 302 | ("add_command", "/mcp add recommended playwright"), |
| 303 | ], |
| 304 | ) |
| 305 | .unwrap_or_else(|_| { |
| 306 | format!("• playwright — Microsoft's official Playwright MCP via pinned npm package\n package: {PLAYWRIGHT_MCP_PACKAGE}") |
| 307 | }); |
| 308 | let container_use = presentation |
| 309 | .translate( |
| 310 | "mcp_recommendation_container_use", |
| 311 | &[ |
| 312 | ("source", CONTAINER_USE_SOURCE), |
| 313 | ("restart_command", "/mcp restart"), |
| 314 | ("add_command", "/mcp add recommended container-use"), |
| 315 | ], |
| 316 | ) |
| 317 | .unwrap_or_else(|_| format!("• container-use — Dagger's experimental container-use MCP\n source: {CONTAINER_USE_SOURCE}")); |
| 318 | format!( |
| 319 | "{heading}\n\ |
| 320 | {safety}\n\ |
| 321 | \n\ |
| 322 | • hugging-face — remote Hugging Face MCP endpoint\n\ |
| 323 | provenance: bundled Codewhale recommendation\n\ |
| 324 | add explicitly: /mcp add recommended hugging-face\n\ |
| 325 | then inspect: /mcp doctor · reload all configured servers: /mcp restart\n\ |
| 326 | \n\ |
| 327 | {github}\n\ |
| 328 | \n\ |
| 329 | {chrome}\n\ |
| 330 | \n\ |
| 331 | {playwright}\n\ |
| 332 | \n\ |
| 333 | {container_use}\n\ |
| 334 | \n\ |
| 335 | External sources (~/.claude.json, .mcp.json, marketplace manifests):\n\ |
| 336 | /mcp import — list candidates with provenance (keyboard/mouse status)\n\ |
| 337 | /mcp import approve <name> — create managed connector after consent\n\ |
| 338 | /mcp import decline <name> — durable decline until source content changes\n\ |
| 339 | enabled=false is a hard block and will never import. Nothing is auto-imported." |
| 340 | ) |
| 341 | } |
| 342 | |
| 343 | fn recommended_npx_command_for(windows: bool) -> &'static str { |
| 344 | if windows { "npx.cmd" } else { "npx" } |
| 345 | } |
| 346 | |
| 347 | fn parse_scopes(parts: Vec<&str>) -> Vec<String> { |
| 348 | let mut scopes = Vec::new(); |
| 349 | let mut iter = parts.into_iter(); |
| 350 | while let Some(part) = iter.next() { |
| 351 | if part == "--scope" { |
| 352 | let Some(value) = iter.next() else { |
| 353 | continue; |
| 354 | }; |
| 355 | for scope in value.split(',') { |
| 356 | let scope = scope.trim(); |
| 357 | if !scope.is_empty() { |
| 358 | scopes.push(scope.to_string()); |
| 359 | } |
| 360 | } |
| 361 | continue; |
| 362 | } |
| 363 | let value = part.strip_prefix("--scope="); |
| 364 | let Some(value) = value else { |
| 365 | for scope in part.split(',') { |
| 366 | let scope = scope.trim(); |
| 367 | if !scope.is_empty() { |
| 368 | scopes.push(scope.to_string()); |
| 369 | } |
| 370 | } |
| 371 | continue; |
| 372 | }; |
| 373 | for scope in value.split(',') { |
| 374 | let scope = scope.trim(); |
| 375 | if !scope.is_empty() { |
| 376 | scopes.push(scope.to_string()); |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | scopes |
| 381 | } |
| 382 | |
| 383 | #[cfg(test)] |
| 384 | mod tests { |
| 385 | use super::*; |
| 386 | |
| 387 | struct FakePresentation; |
| 388 | impl CommandPresentationContext for FakePresentation { |
| 389 | fn translate(&self, key: &str, r: &[(&str, &str)]) -> Result<String, String> { |
| 390 | let mut out = match key { |
| 391 | "mcp_recommended_unknown_id" => { |
| 392 | "Unknown MCP suggestion. Run {recommendations_command} to see the list.".to_string() |
| 393 | } |
| 394 | "mcp_recommendations_heading" => { |
| 395 | "Suggested MCP servers (nothing installs automatically)".to_string() |
| 396 | } |
| 397 | "mcp_recommendations_safety" => { |
| 398 | "Looking adds nothing. Adding writes config only — review it before {restart_command} connects anything." |
| 399 | .to_string() |
| 400 | } |
| 401 | "mcp_recommendation_github" => { |
| 402 | "• github — GitHub's official remote MCP endpoint\n endpoint: {endpoint}\n auth is separate: use {login_command} only when the server advertises OAuth;\n otherwise configure a least-privilege PAT outside command history.\n add: {add_command}".to_string() |
| 403 | } |
| 404 | "mcp_recommendation_chrome" => { |
| 405 | "• chrome-devtools — official Chrome DevTools MCP via pinned npm package\n package: {package} ({launcher})\n it can inspect/control Chrome and read authenticated pages.\n add: {add_command}".to_string() |
| 406 | } |
| 407 | "mcp_recommendation_playwright" => { |
| 408 | "• playwright — Microsoft's official Playwright MCP via pinned npm package\n package: {package} ({launcher})\n source: {source}\n --isolated starts a fresh browser profile. It can browse/control pages and read authenticated pages.\n add: {add_command}".to_string() |
| 409 | } |
| 410 | "mcp_recommendation_container_use" => { |
| 411 | "• container-use — Dagger's experimental container-use MCP\n command: container-use stdio\n source: {source}\n requires the separately installed container-use binary; Codewhale never downloads or installs this binary.\n add: {add_command}".to_string() |
| 412 | } |
| 413 | _ => return Err("unknown translation key".to_string()), |
| 414 | }; |
| 415 | for (name, value) in r { |
| 416 | out = out.replace(&format!("{{{name}}}"), value); |
| 417 | } |
| 418 | Ok(out) |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | #[test] |
| 423 | fn parses_add_and_validate() { |
| 424 | let add = mcp( |
| 425 | &mut FakePresentation, |
| 426 | Some("add stdio local node server.js"), |
| 427 | ); |
| 428 | assert!(matches!( |
| 429 | add.action, |
| 430 | Some(AppAction::Mcp(McpUiAction::AddStdio { name, command, args })) |
| 431 | if name == "local" && command == "node" && args == vec!["server.js".to_string()] |
| 432 | )); |
| 433 | |
| 434 | let validate = mcp(&mut FakePresentation, Some("validate")); |
| 435 | assert!(matches!( |
| 436 | validate.action, |
| 437 | Some(AppAction::Mcp(McpUiAction::Validate)) |
| 438 | )); |
| 439 | |
| 440 | let doctor = mcp(&mut FakePresentation, Some("doctor")); |
| 441 | assert!(matches!( |
| 442 | doctor.action, |
| 443 | Some(AppAction::Mcp(McpUiAction::Validate)) |
| 444 | )); |
| 445 | for command in ["validate github", "doctor github"] { |
| 446 | assert!(matches!( |
| 447 | mcp(&mut FakePresentation, Some(command)).action, |
| 448 | Some(AppAction::Mcp(McpUiAction::Diagnose { name })) if name == "github" |
| 449 | )); |
| 450 | } |
| 451 | let restart = mcp(&mut FakePresentation, Some("restart")); |
| 452 | assert!(matches!( |
| 453 | restart.action, |
| 454 | Some(AppAction::Mcp(McpUiAction::Reload)) |
| 455 | )); |
| 456 | |
| 457 | let recommended = mcp(&mut FakePresentation, Some("recommendations")) |
| 458 | .message |
| 459 | .expect("recommendations text"); |
| 460 | assert!(recommended.contains("nothing installs automatically")); |
| 461 | assert!(recommended.contains("provenance:")); |
| 462 | assert!(recommended.contains("https://api.githubcopilot.com/mcp/")); |
| 463 | assert!(recommended.contains("chrome-devtools-mcp@1.7.0")); |
| 464 | assert!(recommended.contains("@playwright/mcp@0.0.79")); |
| 465 | assert!(recommended.contains("https://github.com/microsoft/playwright-mcp")); |
| 466 | assert!(recommended.contains("https://github.com/dagger/container-use")); |
| 467 | assert!(!recommended.contains("https://github.com/trycua/cua")); |
| 468 | assert!(!recommended.to_ascii_lowercase().contains("cua-driver")); |
| 469 | assert!(recommended.contains("least-privilege PAT outside command history")); |
| 470 | assert!(recommended.contains("read authenticated pages")); |
| 471 | |
| 472 | let unknown = mcp(&mut FakePresentation, Some("add recommended unknown")) |
| 473 | .message |
| 474 | .expect("localized unknown recommendation error"); |
| 475 | assert!(unknown.contains("Unknown MCP suggestion"), "{unknown}"); |
| 476 | |
| 477 | let add_recommended = mcp(&mut FakePresentation, Some("add recommended hugging-face")); |
| 478 | assert!(matches!( |
| 479 | add_recommended.action, |
| 480 | Some(AppAction::Mcp(McpUiAction::AddHttp { name, url, transport: None })) |
| 481 | if name == "hugging-face" && url == "https://huggingface.co/mcp" |
| 482 | )); |
| 483 | |
| 484 | let add_github = mcp(&mut FakePresentation, Some("add recommended github")); |
| 485 | assert!(matches!( |
| 486 | add_github.action, |
| 487 | Some(AppAction::Mcp(McpUiAction::AddHttp { name, url, transport: None })) |
| 488 | if name == "github" && url == GITHUB_MCP_URL |
| 489 | )); |
| 490 | |
| 491 | let add_chrome = mcp( |
| 492 | &mut FakePresentation, |
| 493 | Some("add recommended chrome-devtools"), |
| 494 | ); |
| 495 | assert!(matches!( |
| 496 | add_chrome.action, |
| 497 | Some(AppAction::Mcp(McpUiAction::AddStdio { name, command, args })) |
| 498 | if name == "chrome-devtools" |
| 499 | && command == recommended_npx_command_for(cfg!(windows)) |
| 500 | && args == vec!["-y".to_string(), CHROME_DEVTOOLS_MCP_PACKAGE.to_string()] |
| 501 | )); |
| 502 | |
| 503 | let add_playwright = mcp(&mut FakePresentation, Some("add recommended playwright")); |
| 504 | assert!(matches!( |
| 505 | add_playwright.action, |
| 506 | Some(AppAction::Mcp(McpUiAction::AddStdio { name, command, args })) |
| 507 | if name == "playwright" |
| 508 | && command == recommended_npx_command_for(cfg!(windows)) |
| 509 | && args == vec![ |
| 510 | "-y".to_string(), |
| 511 | PLAYWRIGHT_MCP_PACKAGE.to_string(), |
| 512 | "--isolated".to_string(), |
| 513 | ] |
| 514 | )); |
| 515 | |
| 516 | let add_container = mcp(&mut FakePresentation, Some("add recommended container-use")); |
| 517 | assert!(matches!( |
| 518 | add_container.action, |
| 519 | Some(AppAction::Mcp(McpUiAction::AddStdio { name, command, args })) |
| 520 | if name == "container-use" |
| 521 | && command == "container-use" |
| 522 | && args == vec!["stdio".to_string()] |
| 523 | )); |
| 524 | |
| 525 | let add_cua = mcp(&mut FakePresentation, Some("add recommended cua")); |
| 526 | assert!( |
| 527 | add_cua.is_error, |
| 528 | "Cua is not a Codewhale computer-use recommendation" |
| 529 | ); |
| 530 | assert!( |
| 531 | add_cua |
| 532 | .message |
| 533 | .as_deref() |
| 534 | .is_some_and(|message| message.contains("Unknown MCP suggestion")), |
| 535 | "{:?}", |
| 536 | add_cua.message |
| 537 | ); |
| 538 | |
| 539 | let import_list = mcp(&mut FakePresentation, Some("import")); |
| 540 | assert!(matches!( |
| 541 | import_list.action, |
| 542 | Some(AppAction::Mcp(McpUiAction::ImportList)) |
| 543 | )); |
| 544 | let import_approve = mcp(&mut FakePresentation, Some("import approve local-tools")); |
| 545 | assert!(matches!( |
| 546 | import_approve.action, |
| 547 | Some(AppAction::Mcp(McpUiAction::ImportApprove { name })) |
| 548 | if name == "local-tools" |
| 549 | )); |
| 550 | let import_decline = mcp(&mut FakePresentation, Some("import decline local-tools")); |
| 551 | assert!(matches!( |
| 552 | import_decline.action, |
| 553 | Some(AppAction::Mcp(McpUiAction::ImportDecline { name })) |
| 554 | if name == "local-tools" |
| 555 | )); |
| 556 | let marketplace = mcp(&mut FakePresentation, Some("marketplace")); |
| 557 | assert!(matches!( |
| 558 | marketplace.action, |
| 559 | Some(AppAction::Mcp(McpUiAction::ImportList)) |
| 560 | )); |
| 561 | |
| 562 | let login = mcp( |
| 563 | &mut FakePresentation, |
| 564 | Some("login remote --scope tools/read,tools/write"), |
| 565 | ); |
| 566 | assert!(matches!( |
| 567 | login.action, |
| 568 | Some(AppAction::Mcp(McpUiAction::Login { name, scopes })) |
| 569 | if name == "remote" |
| 570 | && scopes == vec!["tools/read".to_string(), "tools/write".to_string()] |
| 571 | )); |
| 572 | |
| 573 | let retry = mcp(&mut FakePresentation, Some("retry remote")); |
| 574 | assert!(matches!( |
| 575 | retry.action, |
| 576 | Some(AppAction::Mcp(McpUiAction::Retry { name })) if name == "remote" |
| 577 | )); |
| 578 | } |
| 579 | |
| 580 | #[test] |
| 581 | fn recommended_chrome_launcher_is_native_on_unix_and_windows() { |
| 582 | assert_eq!(recommended_npx_command_for(false), "npx"); |
| 583 | assert_eq!(recommended_npx_command_for(true), "npx.cmd"); |
| 584 | |
| 585 | let windows = parse_add_for_platform( |
| 586 | &mut FakePresentation, |
| 587 | vec!["recommended", "playwright"], |
| 588 | true, |
| 589 | ); |
| 590 | assert!(matches!( |
| 591 | windows.action, |
| 592 | Some(AppAction::Mcp(McpUiAction::AddStdio { name, command, args })) |
| 593 | if name == "playwright" |
| 594 | && command == "npx.cmd" |
| 595 | && args == vec![ |
| 596 | "-y".to_string(), |
| 597 | PLAYWRIGHT_MCP_PACKAGE.to_string(), |
| 598 | "--isolated".to_string(), |
| 599 | ] |
| 600 | )); |
| 601 | } |
| 602 | |
| 603 | #[test] |
| 604 | fn recommendations_state_execution_and_install_boundaries() { |
| 605 | let text = recommended_mcp_text(&mut FakePresentation); |
| 606 | assert!(text.contains("nothing installs automatically")); |
| 607 | assert!(text.contains("Suggested MCP servers")); |
| 608 | assert!(text.contains("never downloads or")); |
| 609 | assert!(text.contains("installs this binary")); |
| 610 | assert!(text.contains("experimental")); |
| 611 | assert!(text.contains("--isolated")); |
| 612 | assert!(!text.contains("operating-system permissions")); |
| 613 | assert!(!text.to_ascii_lowercase().contains("cua")); |
| 614 | } |
| 615 | |
| 616 | #[test] |
| 617 | fn handler_is_contextual_and_requests_presentation_facet() { |
| 618 | let CommandHandler::Contextual { |
| 619 | capabilities, |
| 620 | handler, |
| 621 | } = McpCmd::handler() |
| 622 | else { |
| 623 | panic!("mcp must be contextual"); |
| 624 | }; |
| 625 | assert_eq!(capabilities, CommandCapabilities::PRESENTATION); |
| 626 | let missing = handler(CommandContexts::empty(), Some("list")); |
| 627 | assert!(missing.is_error); |
| 628 | assert_eq!( |
| 629 | missing.message.as_deref(), |
| 630 | Some("Error: Command capability unavailable: presentation") |
| 631 | ); |
| 632 | assert_eq!(McpCmd::info().description_key, "cmd_mcp_description"); |
| 633 | assert_eq!(McpCmd::info().aliases, &["mcps"]); |
| 634 | } |
| 635 | } |
| 636 |