arabica-cli 0.13.1

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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
//! 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, ClientCapabilities, ClientConfig, Implementation, ProtocolVersion,
};
use rmcp::service::RunningService;
use rmcp::transport::{StreamableHttpClientTransport, TokioChildProcess};
use rmcp::{ClientLifecycleMode, ClientServiceExt, RoleClient};

pub mod auth;
pub mod commands;
pub mod credentials;
pub mod oauth;

use auth::{AuthEnvironment, McpAuth, Origin};

type Client = RunningService<RoleClient, ClientConfig>;

// Stdio: the SDK's ten-second probe deadline plus fifteen seconds for a
// silent legacy server to complete its fallback handshake. HTTP: the whole
// modern probe, or a legacy probe and handshake.
const CONNECTION_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(25);

fn client_config() -> ClientConfig {
    // Advertise only implemented input capabilities. Tool execution and
    // state-only MRTR continuations do not require optional capabilities.
    ClientConfig::new(
        ClientCapabilities::default(),
        Implementation::new("arabica", env!("CARGO_PKG_VERSION")),
    )
}

/// Set once at startup by `arabica acp --mcp-protocol modern`.
static MODERN_ONLY: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

/// Refuse the legacy `initialize` lifecycle for every MCP server in this
/// process: a server that is not MCP 2026-07-28 fails to connect.
pub fn require_modern_protocol() {
    MODERN_ONLY.store(true, std::sync::atomic::Ordering::Relaxed);
}

fn modern_only() -> bool {
    MODERN_ONLY.load(std::sync::atomic::Ordering::Relaxed)
}

fn modern_lifecycle() -> ClientLifecycleMode {
    ClientLifecycleMode::Discover {
        preferred_versions: vec![ProtocolVersion::V_2026_07_28],
    }
}

fn stdio_lifecycle() -> ClientLifecycleMode {
    if modern_only() {
        return modern_lifecycle();
    }
    // A legacy stdio server may ignore `server/discover` entirely, so the
    // SDK's probe deadline is the only evidence it can give.
    ClientLifecycleMode::Auto {
        preferred_versions: vec![ProtocolVersion::V_2026_07_28],
        legacy_version: Some(ProtocolVersion::LATEST_WITH_INITIALIZE),
    }
}

/// Whether a failed modern probe proves the server is legacy: a correlated
/// JSON-RPC rejection of `server/discover` that is not a modern-era error.
/// Timeouts, transport failures, and HTTP 401/403 prove nothing.
fn is_legacy_evidence(error: &rmcp::service::ClientInitializeError) -> bool {
    use rmcp::model::ErrorCode;
    matches!(
        error,
        rmcp::service::ClientInitializeError::JsonRpcError(data)
            if !matches!(
                data.code,
                ErrorCode::MISSING_REQUIRED_CLIENT_CAPABILITY
                    | ErrorCode::HEADER_MISMATCH
                    | ErrorCode::UNSUPPORTED_PROTOCOL_VERSION
            )
    )
}

#[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,
    origin: Origin,
    environment: &AuthEnvironment,
) -> 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(
                CONNECTION_TIMEOUT,
                client_config().serve_with_lifecycle(transport, stdio_lifecycle()),
            )
            .await
            .map_err(|_| format!("MCP server '{}' connection timed out", config.name))?
            .map_err(|error| format!("MCP server '{}' connection failed: {error}", config.name))?;
            (config.name, client)
        }
        McpServer::Http(config) => {
            let auth = McpAuth::from_meta(&config.name, config.meta.as_ref())?;
            let mut headers = HashMap::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
                        )
                    },
                )?;
                if matches!(
                    key.as_str(),
                    "accept"
                        | "content-type"
                        | "mcp-session-id"
                        | "last-event-id"
                        | "mcp-protocol-version"
                        | "mcp-method"
                        | "mcp-name"
                ) || key.as_str().starts_with("mcp-param-")
                {
                    return Err(format!(
                        "MCP server '{}' cannot override a protocol-owned HTTP header",
                        config.name
                    ));
                }
                if auth.is_some() && key == reqwest::header::AUTHORIZATION {
                    return Err(format!(
                        "MCP server '{}' cannot combine an Authorization header with dynamic authentication",
                        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);
            }
            // Keep the SDK's body-preserving, same-origin redirect policy so
            // configured credentials cannot be forwarded to another origin.
            let transport_config = rmcp::transport::streamable_http_client::StreamableHttpClientTransportConfig::with_uri(config.url.as_str())
                .custom_headers(headers);
            // Probe strictly, then fall back only on the server's own legacy
            // evidence. Unlike the SDK's automatic mode, a slow `server/discover`
            // is not treated as legacy: an HTTP legacy server always answers.
            // Each attempt gets a fresh transport.
            macro_rules! serve_http {
                ($transport:expr) => {
                    async {
                        match client_config()
                            .serve_with_lifecycle($transport, modern_lifecycle())
                            .await
                        {
                            Err(error) if !modern_only() && is_legacy_evidence(&error) => {
                                client_config()
                                    .with_protocol_version(ProtocolVersion::LATEST_WITH_INITIALIZE)
                                    .serve_with_lifecycle(
                                        $transport,
                                        ClientLifecycleMode::Initialize,
                                    )
                                    .await
                            }
                            result => result,
                        }
                    }
                };
            }
            let client = match &auth {
                None => {
                    tokio::time::timeout(
                        CONNECTION_TIMEOUT,
                        serve_http!(StreamableHttpClientTransport::from_config(
                            transport_config.clone()
                        )),
                    )
                    .await
                }
                Some(auth) => {
                    let source =
                        auth::source_for(auth, &config.name, &config.url, origin, environment)
                            .await?;
                    let credentials = std::sync::Arc::new(credentials::Credentials::new(source));
                    // Resolve the first credential before the lifecycle
                    // starts, so a slow provider fails on its own deadline
                    // instead of consuming the connection's.
                    credentials
                        .current()
                        .await
                        .map_err(|error| format!("MCP server '{}': {error}", config.name))?;
                    tokio::time::timeout(
                        CONNECTION_TIMEOUT,
                        serve_http!(StreamableHttpClientTransport::with_client(
                            credentials::CredentialedClient::new(credentials.clone()),
                            transport_config.clone(),
                        )),
                    )
                    .await
                }
            }
            .map_err(|_| format!("MCP server '{}' connection timed out", config.name))?
            .map_err(|error| format!("MCP server '{}' connection 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 = if client
        .peer_info()
        .is_some_and(|info| info.capabilities.tools.is_some())
    {
        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}"))?
    } else {
        // A valid server may expose only resources or prompts. Do not send an
        // undeclared tools/list request or fail the entire ACP session.
        Vec::new()
    };
    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,
        environment: &AuthEnvironment,
    ) -> Result<Self, String> {
        let mut result = Self::default();
        let mut names = HashSet::new();
        for server in servers {
            let connected = connect_server(server, cwd, Origin::Client, environment).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,
        environment: &AuthEnvironment,
    ) -> (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, Origin::Configured, environment).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())?;
    let environment = AuthEnvironment {
        arabica_home: Some(arabica_home.to_path_buf()),
        broker: None,
    };
    Ok(McpTools::connect_lenient(servers, cwd, &environment).await)
}

/// How a failed `tools/call` ended.
#[derive(Debug, PartialEq, Eq)]
enum CallFailure {
    /// The server answered with a refusal; the call did not run.
    Rejected(String),
    /// No definitive answer (timeout, lost connection, server error); the
    /// call may have run.
    Unknown(String),
}

const MAX_REASON_LENGTH: usize = 300;

fn bounded(text: &str) -> String {
    text.chars().take(MAX_REASON_LENGTH).collect()
}

fn classify_call_failure(error: &rmcp::ServiceError) -> CallFailure {
    use rmcp::transport::streamable_http_client::StreamableHttpError;
    match error {
        rmcp::ServiceError::McpError(error) => CallFailure::Rejected(bounded(&format!(
            "the server returned JSON-RPC error {}: {}",
            error.code.0, error.message
        ))),
        rmcp::ServiceError::TransportSend(transport) => {
            let http = transport
                .error
                .downcast_ref::<StreamableHttpError<reqwest::Error>>();
            match http {
                // A credential that could not be resolved stops the request
                // before it is sent.
                Some(StreamableHttpError::Io(error))
                    if error
                        .get_ref()
                        .is_some_and(|inner| inner.is::<credentials::CredentialError>()) =>
                {
                    CallFailure::Rejected(bounded(&error.to_string()))
                }
                Some(StreamableHttpError::AuthRequired(_)) => {
                    CallFailure::Rejected("the server answered HTTP 401 Unauthorized".to_owned())
                }
                Some(StreamableHttpError::InsufficientScope(_)) => CallFailure::Rejected(
                    "the server answered HTTP 403 Forbidden (insufficient scope)".to_owned(),
                ),
                // rmcp reports other non-success statuses as "HTTP <status>: <body>".
                Some(StreamableHttpError::UnexpectedServerResponse(message))
                    if message.starts_with("HTTP 4") =>
                {
                    CallFailure::Rejected(bounded(&format!("the server answered {message}")))
                }
                Some(StreamableHttpError::Client(error))
                    if error
                        .status()
                        .is_some_and(|status| status.is_client_error()) =>
                {
                    CallFailure::Rejected(format!(
                        "the server answered HTTP {}",
                        error.status().expect("checked above")
                    ))
                }
                _ => CallFailure::Unknown(bounded(&transport.error.to_string())),
            }
        }
        other => CallFailure::Unknown(bounded(&other.to_string())),
    }
}

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 = match self.clients[route.server].call_tool(params).await {
            Ok(result) => result,
            Err(error) => match classify_call_failure(&error) {
                // The server refused the request, so the model may see the
                // failure and choose what to do next.
                CallFailure::Rejected(reason) => {
                    let content = format!("MCP tool call failed: {reason}");
                    return 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: true,
                        },
                        output: vec![RunnerOutput::Stderr(content)],
                    });
                }
                // The call may have run. Ending the turn keeps the model
                // loop from repeating a side effect it cannot see.
                CallFailure::Unknown(reason) => {
                    return Err(RunnerError::new(format!(
                        "MCP tool call outcome is unknown and it was not retried: {reason}"
                    )));
                }
            },
        };
        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)]
pub(crate) mod auth_tests;
#[cfg(test)]
mod stateless_http_tests;
#[cfg(test)]
mod stateless_stdio_tests;

#[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()
    }

    #[test]
    fn only_a_legacy_json_rpc_rejection_counts_as_legacy_evidence() {
        use rmcp::model::{ErrorCode, ErrorData};
        use rmcp::service::ClientInitializeError;
        let rejection =
            |code| ClientInitializeError::JsonRpcError(ErrorData::new(code, "no", None));
        assert!(is_legacy_evidence(&rejection(ErrorCode::METHOD_NOT_FOUND)));
        assert!(is_legacy_evidence(&rejection(ErrorCode::INVALID_REQUEST)));
        for modern in [
            ErrorCode::MISSING_REQUIRED_CLIENT_CAPABILITY,
            ErrorCode::HEADER_MISMATCH,
            ErrorCode::UNSUPPORTED_PROTOCOL_VERSION,
        ] {
            assert!(!is_legacy_evidence(&rejection(modern)));
        }
        assert!(!is_legacy_evidence(
            &ClientInitializeError::ConnectionClosed("lost".to_owned())
        ));
    }

    #[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("/"),
            &AuthEnvironment::default(),
        )
        .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("/"),
            &AuthEnvironment::default(),
        )
        .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("/"),
            &AuthEnvironment::default(),
        )
        .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();
    }
}