incurs 0.6.0

A declarative CLI framework for Rust with typed commands, agent discovery, HTTP, and MCP
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
//! Runtime adapter for MCP servers loaded from an Agent Plugin directory.

use std::collections::{BTreeMap, HashMap};
use std::ffi::{OsStr, OsString};
use std::path::{Path, PathBuf};

use crate::agent_plugin::loader::{
    AgentPluginMcpServer, AgentPluginMcpTransport, AgentPluginStdioMcpServer, LoadedAgentPlugin,
};
use crate::cli::Cli;
use crate::mcp::McpRemoteOptions;

/// Runtime policy used while connecting a loaded Agent Plugin.
#[derive(Debug, Clone)]
pub struct AgentPluginRuntimeOptions {
    /// Base subprocess environment before plugin values and reserved variables are applied.
    pub base_environment: BTreeMap<OsString, OsString>,
    /// Exact MCP protocol standards accepted by the client.
    pub standards: incurs_mcp_protocol::McpStandardSet,
}

impl Default for AgentPluginRuntimeOptions {
    fn default() -> Self {
        Self {
            base_environment: std::env::vars_os().collect(),
            standards: incurs_mcp_protocol::McpStandardSet::default(),
        }
    }
}

/// Connection result for one independently started Agent Plugin MCP server.
#[derive(Debug, Clone)]
pub struct AgentPluginMcpServerStatus {
    /// Server key from `mcp.json`.
    pub name: String,
    /// Configured MCP transport.
    pub transport: AgentPluginMcpTransport,
    /// Number of tools projected when the connection succeeded.
    pub tool_count: usize,
    /// Connection, authentication, or handshake failure when the server was skipped.
    pub error: Option<String>,
}

/// Connected Agent Plugin tool surface and per-server connection results.
#[derive(Clone)]
pub struct ConnectedAgentPlugin {
    /// Namespaced non-CLI invocation surface for every connected MCP server.
    pub catalog: crate::tool::ToolCatalog,
    /// Stable server connection results in `mcp.json` key order.
    pub servers: Vec<AgentPluginMcpServerStatus>,
}

/// Fatal failure while preparing a loaded Agent Plugin runtime.
#[derive(Debug, thiserror::Error)]
pub enum AgentPluginRuntimeError {
    /// The client-managed data directory could not be prepared.
    #[error("failed to prepare Agent Plugin data directory: {0}")]
    Io(#[from] std::io::Error),
    /// Connected tools could not be assembled into a catalog.
    #[error("failed to build Agent Plugin tool catalog: {0}")]
    Catalog(#[from] crate::tool::ToolCatalogError),
}

/// Connects every valid MCP binding independently and returns one namespaced tool catalog.
pub async fn connect_agent_plugin(
    plugin: &LoadedAgentPlugin,
    options: &AgentPluginRuntimeOptions,
) -> Result<ConnectedAgentPlugin, AgentPluginRuntimeError> {
    std::fs::create_dir_all(&plugin.data_root)?;
    let remote = McpRemoteOptions {
        standards: options.standards.clone(),
        ..McpRemoteOptions::default()
    };
    let mut cli = Cli::create(plugin.manifest.name.clone());
    let mut servers = Vec::with_capacity(plugin.mcp_servers.len());
    for (name, server) in &plugin.mcp_servers {
        let transport = transport_kind(server);
        match connect_server(plugin, server, options, &remote).await {
            Ok(commands) => {
                let tool_count = commands.len();
                let mut group = Cli::create(name.clone());
                for (command, def) in commands {
                    group = group.command(command, def);
                }
                cli = cli.group(group);
                servers.push(AgentPluginMcpServerStatus {
                    name: name.clone(),
                    transport,
                    tool_count,
                    error: None,
                });
            }
            Err(error) => servers.push(AgentPluginMcpServerStatus {
                name: name.clone(),
                transport,
                tool_count: 0,
                error: Some(error),
            }),
        }
    }
    Ok(ConnectedAgentPlugin {
        catalog: cli.try_tool_catalog()?,
        servers,
    })
}

async fn connect_server(
    plugin: &LoadedAgentPlugin,
    server: &AgentPluginMcpServer,
    options: &AgentPluginRuntimeOptions,
    remote: &McpRemoteOptions,
) -> Result<BTreeMap<String, crate::command::CommandDef>, String> {
    match server {
        // A local server is spawned directly with exact argv and no shell.
        AgentPluginMcpServer::Stdio(server) => {
            let launch = prepare_stdio(
                &plugin.root,
                &plugin.data_root,
                server,
                &options.base_environment,
            )
            .map_err(|error| error.to_string())?;
            let mut command = tokio::process::Command::new(&launch.executable);
            command
                .args(&launch.args)
                .current_dir(&launch.cwd)
                .env_clear()
                .envs(&launch.environment);
            let transport = rmcp::transport::TokioChildProcess::new(command)
                .map_err(|error| error.to_string())?;
            crate::mcp::remote_commands_from_transport(transport, remote)
                .await
                .map_err(|error| error.to_string())
        }
        // A current remote server uses Streamable HTTP with configured visible headers.
        AgentPluginMcpServer::StreamableHttp(server) => {
            let headers = configured_headers(&server.headers)
                .map_err(|error| error.to_string())?
                .iter()
                .map(|(name, value)| (name.clone(), value.clone()))
                .collect::<HashMap<_, _>>();
            let config = rmcp::transport::streamable_http_client::StreamableHttpClientTransportConfig::with_uri(
                server.url.clone(),
            )
            .custom_headers(headers);
            let transport = rmcp::transport::StreamableHttpClientTransport::from_config(config);
            crate::mcp::remote_commands_from_transport(transport, remote)
                .await
                .map_err(|error| error.to_string())
        }
        // A legacy remote server uses the 2024-11-05 HTTP+SSE handshake.
        AgentPluginMcpServer::Sse(server) => {
            let url = url::Url::parse(&server.url).map_err(|error| error.to_string())?;
            let headers = configured_headers(&server.headers).map_err(|error| error.to_string())?;
            let transport = crate::agent_plugin_sse::LegacySseTransport::connect(url, headers)
                .await
                .map_err(|error| error.to_string())?;
            crate::mcp::remote_commands_from_transport(transport, remote)
                .await
                .map_err(|error| error.to_string())
        }
    }
}

fn transport_kind(server: &AgentPluginMcpServer) -> AgentPluginMcpTransport {
    match server {
        // Local subprocess binding.
        AgentPluginMcpServer::Stdio(_) => AgentPluginMcpTransport::Stdio,
        // Current remote HTTP binding.
        AgentPluginMcpServer::StreamableHttp(_) => AgentPluginMcpTransport::StreamableHttp,
        // Legacy remote HTTP+SSE binding.
        AgentPluginMcpServer::Sse(_) => AgentPluginMcpTransport::Sse,
    }
}

struct StdioLaunch {
    executable: OsString,
    args: Vec<String>,
    environment: BTreeMap<OsString, OsString>,
    cwd: PathBuf,
}

fn prepare_stdio(
    root: &Path,
    data: &Path,
    server: &AgentPluginStdioMcpServer,
    base_environment: &BTreeMap<OsString, OsString>,
) -> std::io::Result<StdioLaunch> {
    std::fs::create_dir_all(data)?;
    let cwd = if server.resolved_cwd.starts_with(data) {
        std::fs::create_dir_all(&server.resolved_cwd)?;
        let canonical_data = std::fs::canonicalize(data)?;
        let canonical_cwd = std::fs::canonicalize(&server.resolved_cwd)?;
        if !canonical_cwd.starts_with(canonical_data) {
            return Err(std::io::Error::other(
                "stdio cwd escapes the client-managed plugin data directory",
            ));
        }
        canonical_cwd
    } else {
        server.resolved_cwd.clone()
    };
    let root = root.to_string_lossy();
    let data = data.to_string_lossy();
    Ok(StdioLaunch {
        executable: server.resolved_command.as_ref().map_or_else(
            || OsString::from(&server.command),
            |path| path.as_os_str().to_owned(),
        ),
        args: server
            .args
            .iter()
            .map(|arg| expand(arg, &root, &data))
            .collect(),
        environment: resolve_environment(base_environment, &server.env, &root, &data),
        cwd,
    })
}

fn configured_headers(
    configured: &BTreeMap<String, String>,
) -> Result<http::HeaderMap, http::Error> {
    let mut headers = http::HeaderMap::with_capacity(configured.len());
    for (name, value) in configured {
        headers.insert(
            http::HeaderName::from_bytes(name.as_bytes())?,
            http::HeaderValue::from_str(value)?,
        );
    }
    Ok(headers)
}

fn expand(value: &str, root: &str, data: &str) -> String {
    const PLUGIN_ROOT: &str = "${PLUGIN_ROOT}";
    const PLUGIN_DATA: &str = "${PLUGIN_DATA}";

    let mut output = String::with_capacity(value.len());
    let mut rest = value;
    loop {
        let root_at = rest.find(PLUGIN_ROOT);
        let data_at = rest.find(PLUGIN_DATA);
        let next = match (root_at, data_at) {
            // The root placeholder appears before the data placeholder.
            (Some(root_at), Some(data_at)) if root_at <= data_at => {
                Some((root_at, PLUGIN_ROOT, root))
            }
            // The data placeholder appears before the root placeholder.
            (Some(_), Some(data_at)) => Some((data_at, PLUGIN_DATA, data)),
            // Only the root placeholder remains.
            (Some(root_at), None) => Some((root_at, PLUGIN_ROOT, root)),
            // Only the data placeholder remains.
            (None, Some(data_at)) => Some((data_at, PLUGIN_DATA, data)),
            // No recognized placeholders remain.
            (None, None) => None,
        };
        let Some((at, placeholder, replacement)) = next else {
            output.push_str(rest);
            break;
        };
        output.push_str(&rest[..at]);
        output.push_str(replacement);
        rest = &rest[at + placeholder.len()..];
    }
    output
}

fn resolve_environment(
    base: &BTreeMap<OsString, OsString>,
    configured: &BTreeMap<String, String>,
    root: &str,
    data: &str,
) -> BTreeMap<OsString, OsString> {
    let mut environment = base.clone();
    for (name, value) in configured {
        remove_environment_name(&mut environment, OsStr::new(name));
        environment.insert(
            OsString::from(name),
            OsString::from(expand(value, root, data)),
        );
    }
    for (name, value) in [("PLUGIN_ROOT", root), ("PLUGIN_DATA", data)] {
        remove_environment_name(&mut environment, OsStr::new(name));
        environment.insert(OsString::from(name), OsString::from(value));
    }
    environment
}

fn remove_environment_name(environment: &mut BTreeMap<OsString, OsString>, name: &OsStr) {
    environment.retain(|candidate, _| !same_environment_name(candidate, name));
}

#[cfg(windows)]
fn same_environment_name(left: &OsStr, right: &OsStr) -> bool {
    left.to_string_lossy()
        .eq_ignore_ascii_case(&right.to_string_lossy())
}

#[cfg(not(windows))]
fn same_environment_name(left: &OsStr, right: &OsStr) -> bool {
    left == right
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::agent_plugin::loader::AgentPluginStdioMcpServer;
    #[cfg(feature = "http")]
    use crate::agent_plugin::loader::{AgentPluginHttpMcpServer, AgentPluginManifest};

    #[cfg(feature = "http")]
    type LegacySseSender =
        tokio::sync::mpsc::Sender<Result<axum::response::sse::Event, std::convert::Infallible>>;

    #[cfg(feature = "http")]
    #[derive(Clone, Default)]
    struct LegacySseFixture {
        sender: std::sync::Arc<tokio::sync::Mutex<Option<LegacySseSender>>>,
    }

    #[cfg(feature = "http")]
    async fn legacy_sse_get(
        axum::extract::State(state): axum::extract::State<LegacySseFixture>,
    ) -> impl axum::response::IntoResponse {
        use futures::StreamExt;

        let (sender, receiver) = tokio::sync::mpsc::channel(16);
        *state.sender.lock().await = Some(sender);
        let endpoint = futures::stream::once(async {
            Ok(axum::response::sse::Event::default()
                .event("endpoint")
                .data("/messages"))
        });
        axum::response::sse::Sse::new(
            endpoint.chain(tokio_stream::wrappers::ReceiverStream::new(receiver)),
        )
    }

    #[cfg(feature = "http")]
    async fn legacy_sse_post(
        axum::extract::State(state): axum::extract::State<LegacySseFixture>,
        axum::Json(message): axum::Json<serde_json::Value>,
    ) -> axum::http::StatusCode {
        let Some(id) = message.get("id").cloned() else {
            return axum::http::StatusCode::ACCEPTED;
        };
        let result = match message.get("method").and_then(serde_json::Value::as_str) {
            // Legacy initialization response.
            Some("initialize") => serde_json::json!({
                "capabilities": { "tools": {} },
                "protocolVersion": "2024-11-05",
                "serverInfo": { "name": "legacy-fixture", "version": "1.0.0" }
            }),
            // One deterministic tool contract.
            Some("tools/list") => serde_json::json!({
                "tools": [{
                    "description": "Return pong",
                    "inputSchema": { "type": "object", "properties": {} },
                    "name": "ping"
                }]
            }),
            // One deterministic tool result.
            Some("tools/call") => serde_json::json!({
                "content": [{ "type": "text", "text": "{\"message\":\"pong\"}" }],
                "isError": false,
                "structuredContent": { "message": "pong" }
            }),
            // Unsupported requests receive a JSON-RPC method-not-found error.
            _ => {
                let response = serde_json::json!({
                    "error": { "code": -32601, "message": "Method not found" },
                    "id": id,
                    "jsonrpc": "2.0"
                });
                send_legacy_sse_message(&state, response).await;
                return axum::http::StatusCode::ACCEPTED;
            }
        };
        send_legacy_sse_message(
            &state,
            serde_json::json!({ "id": id, "jsonrpc": "2.0", "result": result }),
        )
        .await;
        axum::http::StatusCode::ACCEPTED
    }

    #[cfg(feature = "http")]
    async fn send_legacy_sse_message(state: &LegacySseFixture, message: serde_json::Value) {
        let sender = state.sender.lock().await.clone();
        if let Some(sender) = sender {
            let _ = sender
                .send(Ok(axum::response::sse::Event::default()
                    .event("message")
                    .data(message.to_string())))
                .await;
        }
    }

    struct TestDir(PathBuf);

    impl TestDir {
        fn new() -> Self {
            static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
            let path = std::env::temp_dir().join(format!(
                "incurs-agent-plugin-runtime-{}-{}",
                std::process::id(),
                NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
            ));
            std::fs::create_dir_all(&path).unwrap();
            Self(path)
        }
    }

    impl Drop for TestDir {
        fn drop(&mut self) {
            let _ = std::fs::remove_dir_all(&self.0);
        }
    }

    #[test]
    fn placeholder_expansion_is_global_and_non_recursive() {
        let root = "/plugins/${PLUGIN_DATA}/demo";
        let data = "/data/demo";

        assert_eq!(
            expand("${PLUGIN_ROOT}/a:${PLUGIN_DATA}/b:${OTHER}", root, data),
            "/plugins/${PLUGIN_DATA}/demo/a:/data/demo/b:${OTHER}"
        );
    }

    #[test]
    fn configured_environment_overlays_base_before_reserved_values() {
        let base = BTreeMap::from([
            (OsString::from("MODE"), OsString::from("base")),
            (OsString::from("PLUGIN_ROOT"), OsString::from("wrong")),
        ]);
        let configured = BTreeMap::from([
            ("MODE".to_string(), "plugin".to_string()),
            (
                "CONFIG".to_string(),
                "${PLUGIN_ROOT}/config:${UNKNOWN}".to_string(),
            ),
        ]);

        let environment = resolve_environment(&base, &configured, "/plugin", "/data");

        assert_eq!(environment[OsStr::new("MODE")], "plugin");
        assert_eq!(
            environment[OsStr::new("CONFIG")],
            "/plugin/config:${UNKNOWN}"
        );
        assert_eq!(environment[OsStr::new("PLUGIN_ROOT")], "/plugin");
        assert_eq!(environment[OsStr::new("PLUGIN_DATA")], "/data");
    }

    #[test]
    fn stdio_launch_creates_persistent_data_directories_without_removing_contents() {
        let temp = TestDir::new();
        let root = temp.0.join("plugin");
        let data = temp.0.join("data");
        std::fs::create_dir_all(&root).unwrap();
        std::fs::create_dir_all(&data).unwrap();
        std::fs::write(data.join("keep.txt"), "keep").unwrap();
        let server = AgentPluginStdioMcpServer {
            command: "demo".to_string(),
            resolved_command: None,
            args: vec!["${PLUGIN_DATA}/input".to_string()],
            resolved_args: vec![data.join("input").to_string_lossy().into_owned()],
            env: BTreeMap::from([("MODE".to_string(), "plugin".to_string())]),
            resolved_env: BTreeMap::from([("MODE".to_string(), "plugin".to_string())]),
            cwd: Some("${PLUGIN_DATA}/work".to_string()),
            resolved_cwd: data.join("work"),
        };

        let launch = prepare_stdio(
            &root,
            &data,
            &server,
            &BTreeMap::from([(OsString::from("MODE"), OsString::from("base"))]),
        )
        .unwrap();

        assert_eq!(launch.executable, OsString::from("demo"));
        assert_eq!(launch.args, server.resolved_args);
        assert_eq!(launch.environment[OsStr::new("MODE")], "plugin");
        assert_eq!(
            launch.environment[OsStr::new("PLUGIN_ROOT")],
            root.as_os_str()
        );
        assert_eq!(
            launch.environment[OsStr::new("PLUGIN_DATA")],
            data.as_os_str()
        );
        assert!(data.join("work").is_dir());
        assert_eq!(
            std::fs::read_to_string(data.join("keep.txt")).unwrap(),
            "keep"
        );
    }

    #[test]
    fn configured_headers_convert_without_changing_names_or_values() {
        let headers = configured_headers(&BTreeMap::from([
            ("X-Agent-Plugin".to_string(), "demo".to_string()),
            ("Authorization".to_string(), "visible value".to_string()),
        ]))
        .unwrap();

        assert_eq!(headers["x-agent-plugin"], "demo");
        assert_eq!(headers["authorization"], "visible value");
    }

    #[cfg(feature = "http")]
    #[tokio::test]
    async fn streamable_http_connection_and_failure_are_isolated_per_server() {
        struct Ping;

        #[async_trait::async_trait]
        impl crate::command::CommandHandler for Ping {
            async fn run(
                &self,
                _ctx: crate::command::CommandContext,
            ) -> crate::output::CommandResult {
                crate::output::CommandResult::Ok {
                    data: serde_json::json!({ "message": "pong" }),
                    cta: None,
                    exit_code: None,
                }
            }
        }

        let cli = Cli::create("fixture").command(
            "ping",
            crate::command::CommandDef::build("ping", Ping)
                .description("Return pong")
                .done(),
        );
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let app =
            axum::Router::new().route_service("/mcp", crate::mcp::http_service(&cli).unwrap());
        let server = tokio::spawn(async move {
            axum::serve(listener, app).await.unwrap();
        });
        let temp = TestDir::new();
        let root = temp.0.join("plugin");
        std::fs::create_dir_all(&root).unwrap();
        let plugin = LoadedAgentPlugin {
            root,
            data_root: temp.0.join("data"),
            manifest: AgentPluginManifest {
                schema: crate::agent_plugin::loader::AGENT_PLUGIN_SCHEMA.to_string(),
                name: "fixture".to_string(),
                version: None,
                description: None,
                author: None,
                homepage: None,
                repository: None,
                license: None,
                keywords: Vec::new(),
            },
            skills: Vec::new(),
            mcp_servers: BTreeMap::from([
                (
                    "bad".to_string(),
                    AgentPluginMcpServer::StreamableHttp(AgentPluginHttpMcpServer {
                        url: "http://127.0.0.1:1/mcp".to_string(),
                        headers: BTreeMap::new(),
                    }),
                ),
                (
                    "good".to_string(),
                    AgentPluginMcpServer::StreamableHttp(AgentPluginHttpMcpServer {
                        url: format!("http://{address}/mcp"),
                        headers: BTreeMap::new(),
                    }),
                ),
            ]),
            extensions: BTreeMap::new(),
        };

        let connected = connect_agent_plugin(&plugin, &AgentPluginRuntimeOptions::default())
            .await
            .unwrap();

        assert!(connected.servers[0].error.is_some());
        assert_eq!(connected.servers[1].error, None);
        assert_eq!(connected.servers[1].tool_count, 1);
        assert!(connected.catalog.get("good_ping").is_some());
        let outcome = connected
            .catalog
            .call(
                "good_ping",
                BTreeMap::new(),
                crate::tool::ToolCallOptions::isolated(),
            )
            .await;
        assert!(matches!(outcome, crate::tool::ToolCallOutcome::Ok { .. }));
        server.abort();
    }

    #[cfg(feature = "http")]
    #[tokio::test]
    async fn legacy_sse_connection_lists_and_calls_tools() {
        let state = LegacySseFixture::default();
        let app = axum::Router::new()
            .route("/sse", axum::routing::get(legacy_sse_get))
            .route("/messages", axum::routing::post(legacy_sse_post))
            .with_state(state);
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let server = tokio::spawn(async move {
            axum::serve(listener, app).await.unwrap();
        });
        let temp = TestDir::new();
        let root = temp.0.join("plugin");
        std::fs::create_dir_all(&root).unwrap();
        let plugin = LoadedAgentPlugin {
            root,
            data_root: temp.0.join("data"),
            manifest: AgentPluginManifest {
                schema: crate::agent_plugin::loader::AGENT_PLUGIN_SCHEMA.to_string(),
                name: "fixture".to_string(),
                version: None,
                description: None,
                author: None,
                homepage: None,
                repository: None,
                license: None,
                keywords: Vec::new(),
            },
            skills: Vec::new(),
            mcp_servers: BTreeMap::from([(
                "legacy".to_string(),
                AgentPluginMcpServer::Sse(AgentPluginHttpMcpServer {
                    url: format!("http://{address}/sse"),
                    headers: BTreeMap::new(),
                }),
            )]),
            extensions: BTreeMap::new(),
        };
        let options = AgentPluginRuntimeOptions {
            standards: incurs_mcp_protocol::McpStandardSet::legacy_only(),
            ..Default::default()
        };

        let connected = connect_agent_plugin(&plugin, &options).await.unwrap();

        assert_eq!(connected.servers[0].error, None);
        assert!(connected.catalog.get("legacy_ping").is_some());
        let outcome = connected
            .catalog
            .call(
                "legacy_ping",
                BTreeMap::new(),
                crate::tool::ToolCallOptions::isolated(),
            )
            .await;
        assert!(matches!(
            outcome,
            crate::tool::ToolCallOutcome::Ok { data, .. }
                if data == serde_json::json!({ "message": "pong" })
        ));
        server.abort();
    }
}