use std::collections::HashMap; use std::io; use std::sync::Arc; use std::sync::atomic::AtomicBool; use std::sync::atomic::Ordering; use akode_config::AppToolApproval; use akode_config::Constrained; use akode_config::types::ApprovalsReviewer; use akode_exec_server::FileSystemSandboxContext; use akode_protocol::mcp::McpServerInfo; use akode_protocol::models::PermissionProfile; use akode_protocol::protocol::AskForApproval; use akode_rmcp_client::InProcessTransportFactory; use akode_rmcp_client::RmcpClient; use akode_utils_absolute_path::AbsolutePathBuf; use akode_utils_path_uri::PathUri; use futures::FutureExt; use pretty_assertions::assert_eq; use rmcp::model::JsonObject; use rmcp::model::Tool; use tokio::io::DuplexStream; use super::McpBinding; use super::McpPermissionSource; use super::PreparedMcpCall; use crate::McpServerPermissions; use crate::binding_clients::McpBindingClients; use crate::client_tool_catalog::ClientToolCatalog; use crate::connection_manager::McpConnectionSet; use crate::rmcp_client::ManagedClient; use crate::server::McpServerMetadata; use crate::server::McpServerOrigin; use crate::tools::ToolInfo; const SERVER_NAME: &str = "docs"; const TOOL_NAME: &str = "search"; struct TestInProcessTransportFactory; impl InProcessTransportFactory for TestInProcessTransportFactory { fn open(&self) -> futures::future::BoxFuture<'static, io::Result> { async { let (client_stream, _server_stream) = tokio::io::duplex(1); Ok(client_stream) } .boxed() } } struct TestStep { step: Arc, client: Arc, tool_catalog: Arc, } async fn test_step( label: &str, approval_mode: AppToolApproval, supports_sandbox_state_meta: bool, ) -> TestStep { let tool = ToolInfo { server_name: SERVER_NAME.to_string(), supports_parallel_tool_calls: false, server_origin: None, callable_name: TOOL_NAME.to_string(), callable_namespace: SERVER_NAME.to_string(), namespace_description: None, tool: Tool::new( TOOL_NAME.to_string(), format!("{label} catalog"), Arc::new(JsonObject::default()), ), plugin_display_names: Vec::new(), }; let client = Arc::new( RmcpClient::new_in_process_client(Arc::new(TestInProcessTransportFactory)) .await .expect("create in-process MCP client"), ); let tool_catalog = Arc::new(ClientToolCatalog::new( vec![tool.clone()], /*meta*/ None, )); let managed_client = Arc::new(ManagedClient { connection_sharing: Default::default(), shared_elicitation: None, _auth_change_notifications: None, client: Arc::clone(&client), server_info: McpServerInfo { name: label.to_string(), title: Some(format!("{label} server")), version: "1.0.0".to_string(), description: None, icons: None, website_url: None, }, tool_catalog: Arc::clone(&tool_catalog), tool_timeout: None, server_instructions: None, server_supports_sandbox_state_meta_capability: supports_sandbox_state_meta, client_supports_network_policy_meta_capability: false, }); let clients = Arc::new(McpBindingClients::new(HashMap::from([( SERVER_NAME.to_string(), Arc::clone(&managed_client), )]))); let connections = Arc::new(McpConnectionSet::empty(/*prefix_mcp_tool_names*/ true)); let mut config = crate::mcp::tests::test_mcp_config(std::env::temp_dir()); config.host_cwd = config.host_cwd.join(label); config.environment_cwds.insert( akode_config::DEFAULT_MCP_SERVER_ENVIRONMENT_ID.to_string(), akode_utils_path_uri::PathUri::from_abs_path(&config.host_cwd.join("executor")), ); let server_permissions = if label == "old" { config.approval_policy = Constrained::allow_any(AskForApproval::Never); config.permission_profile = PermissionProfile::Disabled; let mut sandbox = FileSystemSandboxContext::from_permission_profile( PermissionProfile::read_only(), PathUri::from_abs_path(&config.host_cwd.join("executor")), ); sandbox.user_home_dir = Some(PathUri::from_abs_path(&config.host_cwd.join("home"))); sandbox.temporary_directories = Some(vec![PathUri::from_abs_path(&config.host_cwd.join("tmp"))]); McpServerPermissions::Environment(Arc::new(sandbox)) } else { config.approvals_reviewer = ApprovalsReviewer::AutoReview; McpServerPermissions::Thread }; config .server_permissions .insert(SERVER_NAME.to_string(), server_permissions); config .environment_use_mxc .insert(format!("{label}-environment"), label == "new"); let config = Arc::new(config); let prepared = PreparedMcpCall::new( Arc::clone(&connections), managed_client, Arc::clone(&config), tool_catalog.read(Arc::new).await, tool.clone(), McpServerMetadata { environment_id: format!("{label}-environment"), pollutes_memory: label == "old", origin: Some(McpServerOrigin::StreamableHttp(format!( "https://{label}.example" ))), supports_parallel_tool_calls: false, default_tools_approval_mode: Some(approval_mode), tool_approval_modes: HashMap::new(), }, Some(format!("{label}-plugin")), label == "old", ) .expect("test call should have a permission source"); let calls = HashMap::from([((SERVER_NAME.to_string(), TOOL_NAME.to_string()), prepared)]); TestStep { step: Arc::new(McpBinding::new( connections, clients, config, /*plugins_available*/ false, vec![tool], calls, )), client, tool_catalog, } } #[tokio::test] async fn prepared_call_keeps_captured_connection_and_authority_after_refresh() -> anyhow::Result<()> { let old = test_step( "old", AppToolApproval::Prompt, /*supports_sandbox_state_meta*/ true, ) .await; let old_call = old .step .prepare_call(SERVER_NAME, TOOL_NAME) .expect("old step should prepare the advertised tool"); let old_connections = Arc::downgrade(&old.step.connections); let new = test_step( "new", AppToolApproval::Approve, /*supports_sandbox_state_meta*/ false, ) .await; let new_call = new .step .prepare_call(SERVER_NAME, TOOL_NAME) .expect("new step should prepare the advertised tool"); assert_eq!( ( old.step.tools()[0].tool.description.as_deref(), old_call.tool_info().tool.description.as_deref(), old_call.server_origin(), old_call.server_environment_id(), old_call.server_pollutes_memory(), old_call.tool_approval_mode(), old_call.plugin_id(), old_call.is_selected_plugin_server(), old_call .server_supports_sandbox_state_meta_capability() .await?, ), ( Some("old catalog"), Some("old catalog"), Some("https://old.example"), "old-environment", true, AppToolApproval::Prompt, Some("old-plugin"), true, true, ) ); assert_eq!( ( new.step.tools()[0].tool.description.as_deref(), new_call.tool_info().tool.description.as_deref(), new_call.server_environment_id(), new_call.tool_approval_mode(), ), ( Some("new catalog"), Some("new catalog"), "new-environment", AppToolApproval::Approve, ) ); assert!(Arc::ptr_eq(&old_call.client.client, &old.client)); assert!(!Arc::ptr_eq(&old.client, &new.client)); let McpPermissionSource::Environment { sandbox, .. } = old_call.permission_source() else { anyhow::bail!("expected the old call to keep its environment permissions"); }; let old_root = AbsolutePathBuf::current_dir()?.join("old"); assert_eq!( ( &sandbox.cwd, &sandbox.user_home_dir, &sandbox.temporary_directories ), ( &PathUri::from_abs_path(&old_root.join("executor")), &Some(PathUri::from_abs_path(&old_root.join("home"))), &Some(vec![PathUri::from_abs_path(&old_root.join("tmp"))]), ), ); let McpPermissionSource::Thread { host_cwd, .. } = new_call.permission_source() else { anyhow::bail!("expected thread permissions"); }; assert_eq!(host_cwd, &AbsolutePathBuf::current_dir()?.join("new")); assert_eq!( ( old_call.config().approval_policy.value(), old_call.permission_profile(), old_call.config().approvals_reviewer, old_call.config().environment_use_mxc.get("old-environment"), ), ( AskForApproval::Never, &PermissionProfile::read_only(), ApprovalsReviewer::User, Some(&false), ) ); assert_eq!( ( new_call.config().approval_policy.value(), new_call.config().approvals_reviewer, new_call.config().environment_use_mxc.get("new-environment"), ), ( AskForApproval::OnRequest, ApprovalsReviewer::AutoReview, Some(&true) ) ); drop(old.step); assert!( old_connections.upgrade().is_some(), "the prepared call should keep its captured connection set alive" ); drop(old_call); assert!( old_connections.upgrade().is_none(), "the captured connection set should be released with the prepared call" ); Ok(()) } #[tokio::test] async fn prepared_call_does_not_reroute_after_captured_connection_closes() { let old = test_step( "old", AppToolApproval::Prompt, /*supports_sandbox_state_meta*/ true, ) .await; let old_call = old .step .prepare_call(SERVER_NAME, TOOL_NAME) .expect("old step should prepare the advertised tool"); let new = test_step( "new", AppToolApproval::Approve, /*supports_sandbox_state_meta*/ false, ) .await; assert!(!Arc::ptr_eq(&old.client, &new.client)); old.client.shutdown().await; let error = old_call .call( Some(serde_json::json!({"query": "codex"})), /*meta*/ None, /*timeout*/ None, ) .await .expect_err("a call bound to a closed connection must fail"); assert!( format!("{error:#}").contains("MCP client is shut down"), "the prepared call should fail on its captured client: {error:#}" ); } #[tokio::test] async fn prepared_call_is_rejected_after_catalog_refresh() { let step = test_step( "old", AppToolApproval::Prompt, /*supports_sandbox_state_meta*/ true, ) .await; let prepared = step .step .prepare_call(SERVER_NAME, TOOL_NAME) .expect("step should prepare the advertised tool"); let mut changed_tools = step.step.tools().to_vec(); changed_tools[0].tool.description = Some("changed".into()); step.tool_catalog.replace_for_test(changed_tools).await; let error = prepared .call( Some(serde_json::json!({"query": "codex"})), /*meta*/ None, /*timeout*/ None, ) .await .expect_err("a call from an older catalog must be rejected"); assert!( format!("{error:#}").contains("catalog changed"), "unexpected error: {error:#}" ); } #[tokio::test] async fn stale_prepared_call_does_not_run_preparation() { let step = test_step( "old", AppToolApproval::Prompt, /*supports_sandbox_state_meta*/ true, ) .await; let prepared = step .step .prepare_call(SERVER_NAME, TOOL_NAME) .expect("step should prepare the advertised tool"); let mut changed_tools = step.step.tools().to_vec(); changed_tools[0].tool.description = Some("changed".into()); step.tool_catalog.replace_for_test(changed_tools).await; let prepared_side_effect_ran = Arc::new(AtomicBool::new(false)); let marker = Arc::clone(&prepared_side_effect_ran); prepared .call_with_preparation(/*requested_timeout*/ None, || async move { marker.store(true, Ordering::SeqCst); Ok((None, None)) }) .await .expect_err("a call from an older catalog must be rejected"); assert!(!prepared_side_effect_ran.load(Ordering::SeqCst)); } #[tokio::test] async fn sandbox_executable_requires_local_stdio_and_a_full_cli() -> anyhow::Result<()> { #[cfg(unix)] use std::os::unix::ffi::OsStrExt; let step = test_step( "old", AppToolApproval::Approve, /*supports_sandbox_state_meta*/ true, ) .await; let call = step .step .prepare_call(SERVER_NAME, TOOL_NAME) .expect("prepared call"); // Use a probe-capable CLI so remote/HTTP exclusions cannot pass merely // because the supplied executable lacks the sandbox capability. let executable = akode_utils_cargo_bin::cargo_bin("akode")?; for (server, runtime_environment, path, expected) in [ ( serde_json::json!({"command": "mcp"}), "local", Some(executable.clone()), Some(AbsolutePathBuf::from_absolute_path_checked(&executable)?), ), // The Rust test harness accepts `sandbox --help`, but must not be // advertised as a sandbox launcher: it lacks `--sandbox-state-json`. ( serde_json::json!({"command": "mcp"}), "local", Some(std::env::current_exe()?), None, ), (serde_json::json!({"command": "mcp"}), "local", None, None), #[cfg(unix)] ( serde_json::json!({"command": "mcp"}), "local", Some(std::ffi::OsStr::from_bytes(b"/non-utf8-\xff/codex").into()), None, ), ( serde_json::json!({"command": "mcp"}), "local", Some("codex".into()), None, ), ( serde_json::json!({"command": "mcp", "environment_id": "remote"}), "remote", Some(executable.clone()), None, ), ( serde_json::json!({"command": "mcp"}), "remote", Some(executable.clone()), None, ), ( serde_json::json!({"url": "http://127.0.0.1:8080/mcp"}), "local", Some(executable), None, ), ] { let mut call = call.clone(); let config = Arc::make_mut(&mut call.config); config.codex_self_exe = path; let mut catalog = crate::ResolvedMcpCatalog::builder(); catalog.register(crate::McpServerRegistration::from_config( SERVER_NAME.to_owned(), serde_json::from_value(server)?, )); config.mcp_server_catalog = catalog.build(); call.server_metadata.environment_id = runtime_environment.to_owned(); assert_eq!(call.sandbox_codex_executable().await, expected); let state = crate::SandboxState { permission_profile: PermissionProfile::default(), codex_linux_sandbox_exe: None, sandbox_cwd: akode_utils_path_uri::PathUri::from_host_native_path( std::env::current_dir()?, )?, use_legacy_landlock: false, use_mxc: false, }; let expected_state = serde_json::to_value(&state)?; let mut metadata = Some(serde_json::json!({ "unrelated": "retained", "codex/sandbox-state": {"stale": true}, "codex/sandbox-state-meta": {"different": true}, "codex/sandbox-launcher": {"codexExecutable": "must-not-survive"}, })); call.augment_sandbox_metadata(&mut metadata, state).await?; let mut expected_metadata = serde_json::json!({ "unrelated": "retained", "codex/sandbox-state": expected_state, "codex/sandbox-state-meta": expected_state, }); if let Some(executable) = expected { expected_metadata["codex/sandbox-launcher"] = serde_json::json!({"codexExecutable": executable}); } assert_eq!(metadata, Some(expected_metadata)); } Ok(()) }