arabica-cli 0.7.0

Arabica: a terminal coding agent with a fullscreen TUI, ACP support for Zed, and one-shot runs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
//! MCP tools supplied by an ACP client for one session.

use std::collections::{HashMap, HashSet};
use std::path::Path;

use agent_client_protocol::schema::v1::McpServer;
use arabica_model::{ContentBlock, ToolDefinition, ToolResultItem};
use arabica_protocol::{RunId, ToolInteractionKind};
use arabica_runner::{
    RunnerEnvironment, RunnerError, RunnerOutput, ToolExecutionRequest, ToolExecutionResult,
    truncate_output,
};
use rmcp::model::CallToolRequestParams;
use rmcp::service::RunningService;
use rmcp::transport::{StreamableHttpClientTransport, TokioChildProcess};
use rmcp::{RoleClient, ServiceExt};

type Client = RunningService<RoleClient, ()>;

#[derive(Debug)]
struct RoutedTool {
    server: usize,
    original_name: String,
    server_id: String,
}

/// Connections and tool names are scoped to a single ACP session. Nothing in
/// this type is shared with another workspace or persisted with credentials.
#[derive(Debug, Default)]
pub struct McpTools {
    clients: Vec<Client>,
    routes: HashMap<String, RoutedTool>,
    definitions: Vec<ToolDefinition>,
    server_ids: std::collections::BTreeSet<String>,
}

/// One server's live connection plus its advertised tools, between
/// transport setup and route registration.
struct Connected {
    name: String,
    client: Client,
    tools: Vec<rmcp::model::Tool>,
}

/// Bring one server up and discover its tools. Error messages carry the
/// server name and the failure, never environment values or header values.
async fn connect_server(server: McpServer, cwd: &Path) -> Result<Connected, String> {
    let (name, client) = match server {
        McpServer::Stdio(config) => {
            if !config.command.is_absolute() {
                return Err(format!(
                    "MCP server '{}' command must be absolute",
                    config.name
                ));
            }
            let mut command = tokio::process::Command::new(&config.command);
            command
                .args(&config.args)
                .current_dir(cwd)
                .kill_on_drop(true)
                .env_clear();
            // Only the environment explicitly supplied with the server may
            // contain MCP credentials. Do not leak the agent's provider
            // key to a server process.
            for key in ["PATH", "HOME", "TMPDIR", "LANG", "LC_ALL", "SYSTEMROOT"] {
                if let Some(value) = std::env::var_os(key) {
                    command.env(key, value);
                }
            }
            for variable in &config.env {
                command.env(&variable.name, &variable.value);
            }
            let transport = TokioChildProcess::new(command).map_err(|error| {
                format!("MCP server '{}' could not start: {error}", config.name)
            })?;
            let client =
                tokio::time::timeout(std::time::Duration::from_secs(15), ().serve(transport))
                    .await
                    .map_err(|_| format!("MCP server '{}' initialization timed out", config.name))?
                    .map_err(|error| {
                        format!(
                            "MCP server '{}' initialization failed: {error}",
                            config.name
                        )
                    })?;
            (config.name, client)
        }
        McpServer::Http(config) => {
            let mut headers = reqwest::header::HeaderMap::new();
            for header in &config.headers {
                let key = reqwest::header::HeaderName::from_bytes(header.name.as_bytes()).map_err(
                    |_| {
                        format!(
                            "MCP server '{}' has an invalid HTTP header name",
                            config.name
                        )
                    },
                )?;
                let value =
                    reqwest::header::HeaderValue::from_str(&header.value).map_err(|_| {
                        format!(
                            "MCP server '{}' has an invalid HTTP header value",
                            config.name
                        )
                    })?;
                headers.insert(key, value);
            }
            let http_client = reqwest::Client::builder()
                .default_headers(headers)
                .build()
                .map_err(|error| {
                    format!("MCP server '{}' HTTP client failed: {error}", config.name)
                })?;
            let transport = StreamableHttpClientTransport::with_client(
                http_client,
                rmcp::transport::streamable_http_client::StreamableHttpClientTransportConfig::with_uri(config.url.as_str()),
            );
            let client =
                tokio::time::timeout(std::time::Duration::from_secs(15), ().serve(transport))
                    .await
                    .map_err(|_| format!("MCP server '{}' initialization timed out", config.name))?
                    .map_err(|error| {
                        format!(
                            "MCP server '{}' initialization failed: {error}",
                            config.name
                        )
                    })?;
            (config.name, client)
        }
        McpServer::Sse(config) => {
            return Err(format!(
                "MCP server '{}' uses unsupported SSE transport",
                config.name
            ));
        }
        _ => return Err("unsupported MCP transport".to_owned()),
    };
    if name.is_empty() {
        return Err("MCP server name must not be empty".to_owned());
    }
    let tools = tokio::time::timeout(std::time::Duration::from_secs(15), client.list_all_tools())
        .await
        .map_err(|_| format!("MCP server '{name}' tool discovery timed out"))?
        .map_err(|error| format!("MCP server '{name}' tool discovery failed: {error}"))?;
    Ok(Connected {
        name,
        client,
        tools,
    })
}

impl McpTools {
    /// Stable compound identities use original names, never flattened wire names.
    pub fn context_identities(
        &self,
    ) -> std::collections::BTreeMap<String, arabica_protocol::ContextIdentity> {
        let mut identities: std::collections::BTreeMap<_, _> = self
            .definitions
            .iter()
            .filter_map(|tool| {
                self.routes.get(&tool.name).map(|route| {
                    (
                        tool.name.clone(),
                        arabica_runtime::mcp_context_identity(
                            &route.server_id,
                            tool,
                            &route.original_name,
                        ),
                    )
                })
            })
            .collect();
        for server_id in &self.server_ids {
            let id = arabica_runtime::mcp_server_context_id(server_id);
            identities.insert(
                id.clone(),
                arabica_protocol::ContextIdentity {
                    id,
                    kind: arabica_protocol::ContextSourceKind::Mcp,
                    version: None,
                    display_name: server_id.clone(),
                    server_id: Some(server_id.clone()),
                    tool_id: None,
                },
            );
        }
        identities
    }

    pub async fn connect(servers: Vec<McpServer>, cwd: &Path) -> Result<Self, String> {
        let mut result = Self::default();
        let mut names = HashSet::new();
        for server in servers {
            let connected = connect_server(server, cwd).await?;
            result.register(connected, &mut names, "session")?;
        }
        Ok(result)
    }

    /// Connect as many servers as possible, collecting one diagnostic per
    /// failure: the shape terminal surfaces want, where one broken server
    /// must not take the session down. [`Self::connect`] keeps the strict
    /// all-or-nothing contract the ACP client relies on.
    pub async fn connect_lenient(servers: Vec<McpServer>, cwd: &Path) -> (Self, Vec<String>) {
        let mut result = Self::default();
        let mut names = HashSet::new();
        let mut diagnostics = Vec::new();
        for server in servers {
            match connect_server(server, cwd).await {
                Ok(connected) => {
                    if let Err(error) = result.register(connected, &mut names, "configured") {
                        diagnostics.push(error);
                    }
                }
                Err(error) => diagnostics.push(error),
            }
        }
        (result, diagnostics)
    }

    /// Wire one live server's tools into the route table.
    fn register(
        &mut self,
        connected: Connected,
        names: &mut HashSet<String>,
        origin: &str,
    ) -> Result<(), String> {
        let Connected {
            name,
            client,
            tools,
        } = connected;
        if names.contains(&name) {
            return Err(format!("duplicate MCP server name '{name}'"));
        }
        let server_index = self.clients.len();
        // Validate the whole server before publishing any of its routes.
        let mut routes = HashMap::new();
        let mut definitions = Vec::new();
        for tool in tools {
            let exposed_name = format!("mcp__{}__{}", safe_name(&name), safe_name(&tool.name));
            if tool.name.is_empty() || exposed_name.len() > 64 {
                return Err(format!(
                    "MCP server '{name}' exposed a tool name that cannot be advertised to the model"
                ));
            }
            if self.routes.contains_key(&exposed_name) || routes.contains_key(&exposed_name) {
                return Err(format!("MCP tool name collision at '{exposed_name}'"));
            }
            routes.insert(
                exposed_name.clone(),
                RoutedTool {
                    server: server_index,
                    original_name: tool.name.to_string(),
                    server_id: format!("{origin}:{name}"),
                },
            );
            definitions.push(ToolDefinition {
                name: exposed_name,
                description: format!(
                    "MCP server '{name}': {}",
                    tool.description.as_deref().unwrap_or("No description")
                ),
                input_schema: serde_json::Value::Object((*tool.input_schema).clone()),
                strict: None,
            });
        }
        names.insert(name.clone());
        self.routes.extend(routes);
        self.definitions.extend(definitions);
        self.server_ids.insert(format!("{origin}:{name}"));
        self.clients.push(client);
        Ok(())
    }

    /// Absorb another set of connections behind this one, re-basing its
    /// server indices; used to combine client-supplied servers with
    /// user-config ones.
    pub fn extend(&mut self, other: McpTools) -> Result<(), String> {
        if !self.server_ids.is_disjoint(&other.server_ids) {
            return Err("duplicate MCP server identity".to_owned());
        }
        if let Some(name) = other
            .routes
            .keys()
            .filter(|name| self.routes.contains_key(*name))
            .min()
        {
            return Err(format!("MCP tool name collision at '{name}'"));
        }
        let offset = self.clients.len();
        for (exposed_name, route) in other.routes {
            if self.routes.contains_key(&exposed_name) {
                return Err(format!("MCP tool name collision at '{exposed_name}'"));
            }
            self.routes.insert(
                exposed_name.clone(),
                RoutedTool {
                    server: route.server + offset,
                    original_name: route.original_name,
                    server_id: route.server_id,
                },
            );
        }
        self.server_ids.extend(other.server_ids);
        self.definitions.extend(other.definitions);
        self.clients.extend(other.clients);
        Ok(())
    }

    pub fn definitions(&self) -> &[ToolDefinition] {
        &self.definitions
    }

    pub fn contains(&self, name: &str) -> bool {
        self.routes.contains_key(name)
    }
}

/// A server declaration's display name, whatever transport it uses.
pub fn server_name(server: &McpServer) -> &str {
    match server {
        McpServer::Stdio(config) => &config.name,
        McpServer::Http(config) => &config.name,
        _ => "",
    }
}

/// Connect the MCP servers declared in the user config file
/// (`$ARABICA_HOME/config.toml`), for terminal chat and `-p`. A malformed
/// config is an error; a server that cannot come up becomes a diagnostic
/// and is skipped. Diagnostics name the server and the failure, never the
/// values of its environment variables or headers.
pub async fn connect_configured(
    cwd: &Path,
    arabica_home: &Path,
) -> Result<(McpTools, Vec<String>), String> {
    let servers =
        crate::config::user_config_mcp(arabica_home).map_err(|error| error.to_string())?;
    Ok(McpTools::connect_lenient(servers, cwd).await)
}

fn safe_name(value: &str) -> String {
    value
        .chars()
        .map(|ch| {
            if ch.is_ascii_alphanumeric() || ch == '_' {
                ch
            } else {
                '_'
            }
        })
        .collect()
}

impl RunnerEnvironment for McpTools {
    fn classify(&self, _call: &arabica_model::ToolCallItem) -> ToolInteractionKind {
        // Server annotations are untrusted hints. Treat all MCP calls as
        // generic and require ACP approval through the default permission rule.
        ToolInteractionKind::Generic
    }

    async fn execute(
        &mut self,
        request: ToolExecutionRequest,
    ) -> Result<ToolExecutionResult, RunnerError> {
        let route = self
            .routes
            .get(&request.call.name)
            .ok_or_else(|| RunnerError::new("unknown MCP tool"))?;
        let arguments = request
            .call
            .arguments
            .as_object()
            .cloned()
            .ok_or_else(|| RunnerError::new("MCP tool arguments must be an object"))?;
        let params =
            CallToolRequestParams::new(route.original_name.clone()).with_arguments(arguments);
        let result = self.clients[route.server]
            .call_tool(params)
            .await
            .map_err(|error| RunnerError::new(format!("MCP tool call failed: {error}")))?;
        let serialized = serde_json::to_string(&result).map_err(|error| {
            RunnerError::new(format!("MCP tool result could not be serialized: {error}"))
        })?;
        let content = truncate_output(serialized);
        Ok(ToolExecutionResult {
            result: ToolResultItem {
                id: None,
                call_id: request.call.call_id,
                name: Some(request.call.name),
                content: vec![ContentBlock::text(content.clone())],
                is_error: result.is_error.unwrap_or(false),
            },
            output: vec![RunnerOutput::Stdout(content)],
        })
    }

    async fn cancel(&mut self, _run_id: &RunId) -> Result<bool, RunnerError> {
        Ok(false)
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;
    use std::sync::atomic::{AtomicBool, Ordering};

    use agent_client_protocol::schema::v1::{HttpHeader, McpServerHttp};
    use arabica_model::ToolCallItem;
    use axum::Json;
    use axum::extract::State;
    use axum::http::{HeaderMap, StatusCode};
    use axum::response::{IntoResponse, Response};
    use axum::routing::post;
    use serde_json::{Value, json};

    use super::*;

    async fn mcp_http(
        State(saw_header): State<Arc<AtomicBool>>,
        headers: HeaderMap,
        Json(request): Json<Value>,
    ) -> Response {
        if headers
            .get("x-test-token")
            .and_then(|value| value.to_str().ok())
            == Some("expected")
        {
            saw_header.store(true, Ordering::SeqCst);
        }
        let Some(id) = request.get("id") else {
            return StatusCode::ACCEPTED.into_response();
        };
        let result = match request["method"].as_str() {
            Some("initialize") => json!({
                "protocolVersion": request["params"]["protocolVersion"],
                "capabilities": {"tools": {}},
                "serverInfo": {"name": "http-fixture", "version": "1.0"}
            }),
            Some("tools/list") => json!({"tools": [{
                "name": "echo", "description": "Echo one value",
                "inputSchema": {"type": "object", "properties": {"value": {"type": "string"}}}
            }]}),
            Some("tools/call") => json!({"content": [{
                "type": "text", "text": request["params"]["arguments"]["value"]
            }]}),
            _ => return StatusCode::NOT_FOUND.into_response(),
        };
        Json(json!({"jsonrpc": "2.0", "id": id, "result": result})).into_response()
    }

    #[tokio::test]
    async fn a_broken_server_becomes_a_diagnostic_not_a_failure() {
        let (tools, diagnostics) = McpTools::connect_lenient(
            vec![McpServer::Sse(
                agent_client_protocol::schema::v1::McpServerSse::new(
                    "dead",
                    "http://127.0.0.1:1/sse",
                ),
            )],
            Path::new("/"),
        )
        .await;
        assert!(tools.definitions().is_empty());
        assert_eq!(diagnostics.len(), 1);
        assert!(
            diagnostics[0].contains("unsupported SSE transport"),
            "{diagnostics:?}"
        );
    }

    #[tokio::test]
    async fn extend_merges_tools_from_two_servers() {
        let saw_header = Arc::new(AtomicBool::new(false));
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind");
        let address = listener.local_addr().expect("address");
        let app = axum::Router::new()
            .route("/mcp", post(mcp_http))
            .with_state(saw_header.clone());
        let server = tokio::spawn(async move { axum::serve(listener, app).await.expect("serve") });
        let config = McpServerHttp::new("remote", format!("http://{address}/mcp"));
        let (extra, diagnostics) =
            McpTools::connect_lenient(vec![McpServer::Http(config)], Path::new("/")).await;
        assert!(diagnostics.is_empty());
        // The base holds nothing; extending must re-base server indices so
        // the merged route table still executes against the right client.
        let mut merged = McpTools::default();
        merged.extend(extra).expect("merge succeeds");
        assert_eq!(merged.definitions()[0].name, "mcp__remote__echo");
        let response = merged
            .execute(ToolExecutionRequest {
                run_id: RunId::new("run-1"),
                call: ToolCallItem {
                    id: None,
                    call_id: "call-1".to_owned(),
                    name: "mcp__remote__echo".to_owned(),
                    arguments: json!({"value": "merged"}),
                    provider_state: None,
                },
            })
            .await
            .expect("merged tool call succeeds");
        assert!(
            response
                .output
                .iter()
                .any(|item| matches!(item, RunnerOutput::Stdout(text) if text.contains("merged")))
        );
        server.abort();
    }

    #[tokio::test]
    async fn client_supplied_http_server_uses_headers_and_executes_discovered_tool() {
        let saw_header = Arc::new(AtomicBool::new(false));
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind");
        let address = listener.local_addr().expect("address");
        let app = axum::Router::new()
            .route("/mcp", post(mcp_http))
            .with_state(saw_header.clone());
        let server = tokio::spawn(async move { axum::serve(listener, app).await.expect("serve") });
        let config = McpServerHttp::new("remote", format!("http://{address}/mcp"))
            .headers(vec![HttpHeader::new("x-test-token", "expected")]);
        let mut tools = McpTools::connect(vec![McpServer::Http(config)], Path::new("/"))
            .await
            .expect("HTTP MCP connects");
        assert_eq!(tools.definitions()[0].name, "mcp__remote__echo");
        let response = tools
            .execute(ToolExecutionRequest {
                run_id: RunId::new("run-1"),
                call: ToolCallItem {
                    id: None,
                    call_id: "call-1".to_owned(),
                    name: "mcp__remote__echo".to_owned(),
                    arguments: json!({"value": "hello"}),
                    provider_state: None,
                },
            })
            .await
            .expect("tool call succeeds");
        assert!(!response.result.is_error);
        assert!(
            response
                .output
                .iter()
                .any(|item| matches!(item, RunnerOutput::Stdout(text) if text.contains("hello")))
        );
        assert!(saw_header.load(Ordering::SeqCst));
        server.abort();
    }
}