|
| 1 | +use codex_core::MCP_SANDBOX_STATE_NOTIFICATION; |
| 2 | +use codex_core::SandboxState; |
| 3 | +use codex_core::protocol::SandboxPolicy; |
| 4 | +use rmcp::ClientHandler; |
| 5 | +use rmcp::ErrorData as McpError; |
| 6 | +use rmcp::RoleClient; |
| 7 | +use rmcp::Service; |
| 8 | +use rmcp::model::ClientCapabilities; |
| 9 | +use rmcp::model::ClientInfo; |
| 10 | +use rmcp::model::CreateElicitationRequestParam; |
| 11 | +use rmcp::model::CreateElicitationResult; |
| 12 | +use rmcp::model::CustomClientNotification; |
| 13 | +use rmcp::model::ElicitationAction; |
| 14 | +use rmcp::service::RunningService; |
| 15 | +use rmcp::transport::ConfigureCommandExt; |
| 16 | +use rmcp::transport::TokioChildProcess; |
| 17 | +use serde_json::json; |
| 18 | +use std::collections::HashSet; |
| 19 | +use std::path::Path; |
| 20 | +use std::path::PathBuf; |
| 21 | +use std::process::Stdio; |
| 22 | +use std::sync::Arc; |
| 23 | +use std::sync::Mutex; |
| 24 | +use tokio::process::Command; |
| 25 | + |
| 26 | +pub fn create_transport<P>(codex_home: P) -> anyhow::Result<TokioChildProcess> |
| 27 | +where |
| 28 | + P: AsRef<Path>, |
| 29 | +{ |
| 30 | + let mcp_executable = assert_cmd::Command::cargo_bin("codex-exec-mcp-server")?; |
| 31 | + let execve_wrapper = assert_cmd::Command::cargo_bin("codex-execve-wrapper")?; |
| 32 | + let bash = Path::new(env!("CARGO_MANIFEST_DIR")) |
| 33 | + .join("..") |
| 34 | + .join("..") |
| 35 | + .join("tests") |
| 36 | + .join("suite") |
| 37 | + .join("bash"); |
| 38 | + |
| 39 | + let transport = |
| 40 | + TokioChildProcess::new(Command::new(mcp_executable.get_program()).configure(|cmd| { |
| 41 | + cmd.arg("--bash").arg(bash); |
| 42 | + cmd.arg("--execve").arg(execve_wrapper.get_program()); |
| 43 | + cmd.env("CODEX_HOME", codex_home.as_ref()); |
| 44 | + |
| 45 | + // Important: pipe stdio so rmcp can speak JSON-RPC over stdin/stdout |
| 46 | + cmd.stdin(Stdio::piped()); |
| 47 | + cmd.stdout(Stdio::piped()); |
| 48 | + |
| 49 | + // Optional but very helpful while debugging: |
| 50 | + cmd.stderr(Stdio::inherit()); |
| 51 | + }))?; |
| 52 | + |
| 53 | + Ok(transport) |
| 54 | +} |
| 55 | + |
| 56 | +pub async fn write_default_execpolicy<P>(policy: &str, codex_home: P) -> anyhow::Result<()> |
| 57 | +where |
| 58 | + P: AsRef<Path>, |
| 59 | +{ |
| 60 | + let policy_dir = codex_home.as_ref().join("policy"); |
| 61 | + tokio::fs::create_dir_all(&policy_dir).await?; |
| 62 | + tokio::fs::write(policy_dir.join("default.codexpolicy"), policy).await?; |
| 63 | + Ok(()) |
| 64 | +} |
| 65 | + |
| 66 | +pub async fn notify_readable_sandbox<P, S>( |
| 67 | + sandbox_cwd: P, |
| 68 | + codex_linux_sandbox_exe: Option<PathBuf>, |
| 69 | + service: &RunningService<RoleClient, S>, |
| 70 | +) -> anyhow::Result<()> |
| 71 | +where |
| 72 | + P: AsRef<Path>, |
| 73 | + S: Service<RoleClient> + ClientHandler, |
| 74 | +{ |
| 75 | + let sandbox_state = SandboxState { |
| 76 | + sandbox_policy: SandboxPolicy::ReadOnly, |
| 77 | + codex_linux_sandbox_exe, |
| 78 | + sandbox_cwd: sandbox_cwd.as_ref().to_path_buf(), |
| 79 | + }; |
| 80 | + send_sandbox_notification(sandbox_state, service).await |
| 81 | +} |
| 82 | + |
| 83 | +pub async fn notify_writable_sandbox_only_one_folder<P, S>( |
| 84 | + writable_folder: P, |
| 85 | + codex_linux_sandbox_exe: Option<PathBuf>, |
| 86 | + service: &RunningService<RoleClient, S>, |
| 87 | +) -> anyhow::Result<()> |
| 88 | +where |
| 89 | + P: AsRef<Path>, |
| 90 | + S: Service<RoleClient> + ClientHandler, |
| 91 | +{ |
| 92 | + let sandbox_state = SandboxState { |
| 93 | + sandbox_policy: SandboxPolicy::WorkspaceWrite { |
| 94 | + // Note that sandbox_cwd will already be included as a writable root |
| 95 | + // when the sandbox policy is expanded. |
| 96 | + writable_roots: vec![], |
| 97 | + network_access: false, |
| 98 | + // Disable writes to temp dir because this is a test, so |
| 99 | + // writable_folder is likely also under /tmp and we want to be |
| 100 | + // strict about what is writable. |
| 101 | + exclude_tmpdir_env_var: true, |
| 102 | + exclude_slash_tmp: true, |
| 103 | + }, |
| 104 | + codex_linux_sandbox_exe, |
| 105 | + sandbox_cwd: writable_folder.as_ref().to_path_buf(), |
| 106 | + }; |
| 107 | + send_sandbox_notification(sandbox_state, service).await |
| 108 | +} |
| 109 | + |
| 110 | +async fn send_sandbox_notification<S>( |
| 111 | + sandbox_state: SandboxState, |
| 112 | + service: &RunningService<RoleClient, S>, |
| 113 | +) -> anyhow::Result<()> |
| 114 | +where |
| 115 | + S: Service<RoleClient> + ClientHandler, |
| 116 | +{ |
| 117 | + let sandbox_state_notification = CustomClientNotification::new( |
| 118 | + MCP_SANDBOX_STATE_NOTIFICATION, |
| 119 | + Some(serde_json::to_value(sandbox_state)?), |
| 120 | + ); |
| 121 | + service |
| 122 | + .send_notification(sandbox_state_notification.into()) |
| 123 | + .await?; |
| 124 | + Ok(()) |
| 125 | +} |
| 126 | + |
| 127 | +pub struct InteractiveClient { |
| 128 | + pub elicitations_to_accept: HashSet<String>, |
| 129 | + pub elicitation_requests: Arc<Mutex<Vec<CreateElicitationRequestParam>>>, |
| 130 | +} |
| 131 | + |
| 132 | +impl ClientHandler for InteractiveClient { |
| 133 | + fn get_info(&self) -> ClientInfo { |
| 134 | + let capabilities = ClientCapabilities::builder().enable_elicitation().build(); |
| 135 | + ClientInfo { |
| 136 | + capabilities, |
| 137 | + ..Default::default() |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + fn create_elicitation( |
| 142 | + &self, |
| 143 | + request: CreateElicitationRequestParam, |
| 144 | + _context: rmcp::service::RequestContext<RoleClient>, |
| 145 | + ) -> impl std::future::Future<Output = Result<CreateElicitationResult, McpError>> + Send + '_ |
| 146 | + { |
| 147 | + self.elicitation_requests |
| 148 | + .lock() |
| 149 | + .unwrap() |
| 150 | + .push(request.clone()); |
| 151 | + |
| 152 | + let accept = self.elicitations_to_accept.contains(&request.message); |
| 153 | + async move { |
| 154 | + if accept { |
| 155 | + Ok(CreateElicitationResult { |
| 156 | + action: ElicitationAction::Accept, |
| 157 | + content: Some(json!({ "approve": true })), |
| 158 | + }) |
| 159 | + } else { |
| 160 | + Ok(CreateElicitationResult { |
| 161 | + action: ElicitationAction::Decline, |
| 162 | + content: None, |
| 163 | + }) |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments