agentyk 0.1.1

Compose agents from values and run them: turn loop, event log, capabilities, MCP, multi-provider LLM drivers.
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
//! The HTTP MCP transport against a real socket: handshake, auth header,
//! session id, SSE responses, and tools reaching a turn.
#![cfg(all(feature = "mcp", feature = "http"))]

use std::sync::Arc;

use agentyk::{
    Agent, McpAuthProvider, McpCapability, McpClient, McpProtocolMode, McpServer, ModelSpec,
    Result, SimDriver, SimTurn, StaticBearer,
};
use async_trait::async_trait;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
use tokio::sync::Mutex;

/// A canned MCP server: answers each POST from a script, and keeps the raw
/// requests so a test can assert on headers and bodies.
struct FakeMcpServer {
    url: String,
    requests: Arc<Mutex<Vec<String>>>,
    _handle: tokio::task::JoinHandle<()>,
}

struct FakeReply {
    status: &'static str,
    content_type: &'static str,
    body: String,
    session_id: Option<&'static str>,
}

impl FakeReply {
    fn ok(content_type: &'static str, body: String) -> Self {
        Self {
            status: "200 OK",
            content_type,
            body,
            session_id: None,
        }
    }

    fn error(status: &'static str, body: impl Into<String>) -> Self {
        Self {
            status,
            content_type: "application/json",
            body: body.into(),
            session_id: None,
        }
    }

    fn session(mut self, session_id: &'static str) -> Self {
        self.session_id = Some(session_id);
        self
    }
}

impl FakeMcpServer {
    async fn serving(replies: Vec<FakeReply>) -> Self {
        let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
        let port = listener.local_addr().expect("addr").port();
        let requests = Arc::new(Mutex::new(Vec::new()));
        let seen = requests.clone();

        let handle = tokio::spawn(async move {
            let mut replies = replies.into_iter();
            loop {
                let Ok((mut socket, _)) = listener.accept().await else {
                    return;
                };
                let mut buffer = Vec::new();
                loop {
                    let mut chunk = [0u8; 4096];
                    let Ok(read) = socket.read(&mut chunk).await else {
                        return;
                    };
                    if read == 0 {
                        break;
                    }
                    buffer.extend_from_slice(&chunk[..read]);
                    let text = String::from_utf8_lossy(&buffer);
                    if let Some((head, rest)) = text.split_once("\r\n\r\n") {
                        let length: usize = head
                            .lines()
                            .find_map(|line| {
                                line.strip_prefix("Content-Length: ")
                                    .or_else(|| line.strip_prefix("content-length: "))
                            })
                            .and_then(|value| value.trim().parse().ok())
                            .unwrap_or(0);
                        if rest.len() >= length {
                            break;
                        }
                    }
                }
                seen.lock()
                    .await
                    .push(String::from_utf8_lossy(&buffer).into_owned());

                let reply = replies
                    .next()
                    .unwrap_or_else(|| FakeReply::ok("application/json", "{}".into()));
                let session_header = reply
                    .session_id
                    .map(|id| format!("Mcp-Session-Id: {id}\r\n"))
                    .unwrap_or_default();
                let response = format!(
                    "HTTP/1.1 {}\r\nContent-Type: {}\r\n{session_header}Content-Length: {}\r\nConnection: close\r\n\r\n{}",
                    reply.status,
                    reply.content_type,
                    reply.body.len(),
                    reply.body,
                );
                let _ = socket.write_all(response.as_bytes()).await;
                let _ = socket.flush().await;
            }
        });

        Self {
            url: format!("http://127.0.0.1:{port}/mcp"),
            requests,
            _handle: handle,
        }
    }

    async fn requests(&self) -> Vec<String> {
        self.requests.lock().await.clone()
    }
}

fn envelope(id: u8, result: serde_json::Value) -> String {
    serde_json::json!({"jsonrpc": "2.0", "id": id, "result": result}).to_string()
}

/// initialize → (notification) → tools/list → tools/call.
async fn scripted_server() -> FakeMcpServer {
    FakeMcpServer::serving(vec![
        FakeReply::ok(
            "application/json",
            envelope(1, serde_json::json!({"protocolVersion": "2025-06-18"})),
        )
        .session("session-42"),
        FakeReply::ok("application/json", String::from("{}")).session("session-42"),
        FakeReply::ok(
            "application/json",
            envelope(
                2,
                serde_json::json!({"tools": [{
                    "name": "search_issues",
                    "description": "Search issues.",
                    "inputSchema": {"type": "object", "properties": {"q": {"type": "string"}}}
                }]}),
            ),
        )
        .session("session-42"),
        // The call answers over SSE, which the transport must handle just as
        // well as a JSON body.
        FakeReply::ok(
            "text/event-stream",
            format!(
                "event: message\ndata: {}\n\n",
                envelope(
                    3,
                    serde_json::json!({"content": [{"type": "text", "text": "3 open issues"}]})
                )
            ),
        )
        .session("session-42"),
    ])
    .await
}

#[tokio::test]
async fn a_remote_server_contributes_its_tools_to_a_turn() -> Result<()> {
    let server = scripted_server().await;
    let agent = Agent::builder()
        .model(ModelSpec::llmsim())
        .driver(SimDriver::new([
            SimTurn::tool_call("search_issues", serde_json::json!({"q": "open"})),
            SimTurn::text("3 open issues"),
        ]))
        .capability(McpCapability::new(
            McpServer::http("github", &server.url).protocol_mode(McpProtocolMode::Stateful),
        ))
        .build()?;

    let mut session = agent.session();
    let turn = session.run("how many open issues?").await?;
    assert_eq!(turn.response, "3 open issues");

    let tool_output = session
        .events()
        .await?
        .into_iter()
        .find_map(|event| match event.data {
            agentyk::EventData::ToolCompleted { output, .. } => Some(output),
            _ => None,
        })
        .expect("tool.completed");
    assert_eq!(tool_output, "3 open issues", "the SSE response was read");
    Ok(())
}

#[tokio::test]
async fn the_session_id_from_initialize_is_sent_on_later_requests() -> Result<()> {
    let server = scripted_server().await;
    let agent = Agent::builder()
        .model(ModelSpec::llmsim())
        .driver(SimDriver::new([SimTurn::text("done")]))
        .capability(McpCapability::new(
            McpServer::http("github", &server.url).protocol_mode(McpProtocolMode::Stateful),
        ))
        .build()?;
    agent.session().run("list the tools").await?;

    let requests = server.requests().await;
    assert!(requests.len() >= 3, "handshake then tools/list");
    assert!(
        !requests[0].contains("Mcp-Session-Id"),
        "nothing to send before initialize answered"
    );
    assert!(
        requests[1..]
            .iter()
            .all(|request| request.contains("mcp-session-id: session-42")
                || request.contains("Mcp-Session-Id: session-42")),
        "every later request must carry the session: {requests:?}"
    );
    Ok(())
}

#[tokio::test]
async fn an_auth_provider_is_asked_for_every_request() -> Result<()> {
    /// Returns a different token each call, which is what an expiring
    /// credential looks like from the client's side.
    struct Rotating(std::sync::atomic::AtomicUsize);

    #[async_trait]
    impl McpAuthProvider for Rotating {
        async fn authorization(&self, server: &str) -> Result<Option<String>> {
            let nth = self.0.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            Ok(Some(format!("Bearer {server}-{nth}")))
        }
    }

    let server = scripted_server().await;
    let agent = Agent::builder()
        .model(ModelSpec::llmsim())
        .driver(SimDriver::new([SimTurn::text("done")]))
        .capability(
            McpCapability::new(
                McpServer::http("github", &server.url).protocol_mode(McpProtocolMode::Stateful),
            )
            .auth(Rotating(std::sync::atomic::AtomicUsize::new(0))),
        )
        .build()?;
    agent.session().run("list the tools").await?;

    let requests = server.requests().await;
    assert!(
        requests[0].contains("authorization: Bearer github-0"),
        "{}",
        requests[0]
    );
    assert!(
        requests[1].contains("authorization: Bearer github-1"),
        "a fresh token per request, not one captured at connect: {}",
        requests[1]
    );
    Ok(())
}

#[tokio::test]
async fn a_static_bearer_covers_the_simple_case() -> Result<()> {
    let server = scripted_server().await;
    let agent = Agent::builder()
        .model(ModelSpec::llmsim())
        .driver(SimDriver::new([SimTurn::text("done")]))
        .capability(
            McpCapability::new(
                McpServer::http("github", &server.url).protocol_mode(McpProtocolMode::Stateful),
            )
            .auth(StaticBearer::new("ghp_secret")),
        )
        .build()?;
    agent.session().run("list the tools").await?;

    assert!(server.requests().await[0].contains("authorization: Bearer ghp_secret"));
    Ok(())
}

#[tokio::test]
async fn extra_headers_are_sent_and_credentials_are_not_required() -> Result<()> {
    let server = scripted_server().await;
    let agent = Agent::builder()
        .model(ModelSpec::llmsim())
        .driver(SimDriver::new([SimTurn::text("done")]))
        .capability(McpCapability::new(
            McpServer::http("github", &server.url)
                .protocol_mode(McpProtocolMode::Stateful)
                .header("X-Tenant", "acme"),
        ))
        .build()?;
    agent.session().run("list the tools").await?;

    let first = server.requests().await[0].clone();
    assert!(first.contains("x-tenant: acme"), "{first}");
    assert!(!first.to_lowercase().contains("authorization:"));
    Ok(())
}

#[tokio::test]
async fn auto_uses_the_stateless_protocol_and_caches_the_verdict() -> Result<()> {
    let server = FakeMcpServer::serving(vec![
        FakeReply::ok(
            "application/json",
            envelope(
                1,
                serde_json::json!({"tools": [{
                    "name": "search",
                    "description": "Search.",
                    "inputSchema": {"type": "object"}
                }]}),
            ),
        ),
        FakeReply::ok(
            "application/json",
            envelope(
                2,
                serde_json::json!({
                    "content": [{"type": "text", "text": "found"}],
                    "isError": false
                }),
            ),
        ),
    ])
    .await;
    let client = McpClient::connect_with_auth(
        &McpServer::http("stateless", &server.url),
        Some(Arc::new(StaticBearer::new("oauth-token"))),
    )
    .await?;

    assert_eq!(client.list_tools().await?[0].name, "search");
    assert_eq!(
        client
            .call_tool("search", serde_json::json!({"q": "mcp"}))
            .await?
            .content,
        "found"
    );

    let requests = server.requests().await;
    assert_eq!(requests.len(), 2, "no initialize round trip");
    for request in &requests {
        let lower = request.to_lowercase();
        assert!(lower.contains("mcp-protocol-version: 2026-07-28"));
        assert!(!lower.contains("mcp-session-id:"));
        assert!(!request.contains(r#""method":"initialize""#));
        assert!(request.contains("io.modelcontextprotocol/clientInfo"));
        assert!(request.contains(r#""name":"agentyk""#));
        assert!(
            request.contains(r#""io.modelcontextprotocol/protocolVersion":"2026-07-28""#),
            "without a handshake the version rides on every request: {request}"
        );
        assert!(
            request.contains(r#""io.modelcontextprotocol/clientCapabilities":{}"#),
            "we advertise no roots, sampling, or elicitation: {request}"
        );
        assert!(lower.contains("authorization: bearer oauth-token"));
    }
    assert!(
        requests[0]
            .to_lowercase()
            .contains("mcp-method: tools/list")
    );
    let call = requests[1].to_lowercase();
    assert!(call.contains("mcp-method: tools/call"));
    assert!(call.contains("mcp-name: search"));
    Ok(())
}

#[tokio::test]
async fn auto_falls_back_to_stateful_and_uses_the_server_version_and_session() -> Result<()> {
    let server = FakeMcpServer::serving(vec![
        FakeReply::error(
            "400 Bad Request",
            r#"{"error":{"message":"Unsupported protocol version"}}"#,
        ),
        FakeReply::ok(
            "application/json",
            envelope(0, serde_json::json!({"protocolVersion": "2025-03-26"})),
        )
        .session("legacy-session"),
        FakeReply::error("202 Accepted", ""),
        FakeReply::ok(
            "application/json",
            envelope(1, serde_json::json!({"tools": []})),
        ),
        FakeReply::ok(
            "application/json",
            envelope(2, serde_json::json!({"tools": []})),
        ),
    ])
    .await;
    let client = McpClient::connect(&McpServer::http("legacy", &server.url)).await?;

    assert!(client.list_tools().await?.is_empty());
    assert!(client.list_tools().await?.is_empty());

    let requests = server.requests().await;
    assert_eq!(requests.len(), 5);
    assert!(requests[0].contains(r#""method":"tools/list""#));
    assert!(
        requests[0]
            .to_lowercase()
            .contains("mcp-protocol-version: 2026-07-28")
    );
    assert!(requests[1].contains(r#""method":"initialize""#));
    assert!(requests[2].contains(r#""method":"notifications/initialized""#));
    for request in &requests[2..] {
        let lower = request.to_lowercase();
        assert!(lower.contains("mcp-protocol-version: 2025-03-26"));
        assert!(lower.contains("mcp-session-id: legacy-session"));
    }
    assert!(
        requests[3..]
            .iter()
            .all(|request| request.contains(r#""method":"tools/list""#)),
        "the cached negotiation skips another probe and handshake"
    );
    Ok(())
}

#[tokio::test]
async fn pinning_the_latest_protocol_surfaces_rejection_without_falling_back() -> Result<()> {
    let server = FakeMcpServer::serving(vec![FakeReply::error(
        "400 Bad Request",
        r#"{"error":{"message":"Unsupported protocol version"}}"#,
    )])
    .await;
    let client = McpClient::connect(
        &McpServer::http("latest-only", &server.url).protocol_mode(McpProtocolMode::Latest),
    )
    .await?;

    let error = client
        .list_tools()
        .await
        .expect_err("the latest protocol is pinned");
    assert!(error.to_string().contains("400 Bad Request"), "{error}");
    let requests = server.requests().await;
    assert_eq!(
        requests.len(),
        1,
        "a pinned stateless mode must not initialize"
    );
    assert!(
        requests[0]
            .to_lowercase()
            .contains("mcp-protocol-version: 2026-07-28")
    );
    Ok(())
}

/// A `2026-07-28` server that will not speak `2026-07-28` names what it does
/// speak in an `UnsupportedProtocolVersionError`. That code is allocated to
/// the spec, so only a modern server emits it — the client must retry on the
/// offered version rather than read the 400 as "wants a handshake".
#[tokio::test]
async fn a_version_error_retries_at_an_offered_version_without_a_handshake() -> Result<()> {
    let server = FakeMcpServer::serving(vec![
        FakeReply::error(
            "400 Bad Request",
            serde_json::json!({
                "jsonrpc": "2.0",
                "id": 1,
                "error": {
                    "code": -32022,
                    "message": "Unsupported protocol version",
                    "data": {"supported": ["2026-07-28"], "requested": "2026-07-28"},
                },
            })
            .to_string(),
        ),
        FakeReply::ok(
            "application/json",
            envelope(1, serde_json::json!({"tools": [{"name": "search"}]})),
        ),
    ])
    .await;
    let client = McpClient::connect(&McpServer::http("modern", &server.url)).await?;

    assert_eq!(client.list_tools().await?[0].name, "search");
    let requests = server.requests().await;
    assert_eq!(requests.len(), 2, "probe then retry, no handshake");
    assert!(
        !requests
            .iter()
            .any(|request| request.contains(r#""method":"initialize""#)),
        "a spec-allocated code proves the server is modern"
    );
    Ok(())
}

/// The same code with nothing we can speak has to fail, not fall back into a
/// handshake the server would also refuse.
#[tokio::test]
async fn a_version_error_with_no_common_version_is_reported() -> Result<()> {
    let server = FakeMcpServer::serving(vec![FakeReply::error(
        "400 Bad Request",
        serde_json::json!({
            "jsonrpc": "2.0",
            "id": 1,
            "error": {
                "code": -32022,
                "message": "Unsupported protocol version",
                "data": {"supported": ["3000-01-01"]},
            },
        })
        .to_string(),
    )])
    .await;
    let client = McpClient::connect(&McpServer::http("future", &server.url)).await?;

    let error = client.list_tools().await.expect_err("nothing in common");
    assert!(error.to_string().contains("3000-01-01"), "{error}");
    assert_eq!(server.requests().await.len(), 1, "no pointless handshake");
    Ok(())
}

/// `ttlMs` is required on `tools/list` from `2026-07-28`. A capability
/// resolves its tools every turn, so honouring it is the difference between
/// one round trip and one per turn.
#[tokio::test]
async fn a_fresh_tool_list_is_served_from_the_cache() -> Result<()> {
    let server = FakeMcpServer::serving(vec![
        FakeReply::ok(
            "application/json",
            envelope(
                1,
                serde_json::json!({
                    "resultType": "complete",
                    "tools": [{"name": "search"}],
                    "ttlMs": 3_600_000u64,
                    "cacheScope": "public",
                }),
            ),
        ),
        FakeReply::ok(
            "application/json",
            envelope(
                2,
                serde_json::json!({"tools": [{"name": "should-not-be-asked"}]}),
            ),
        ),
    ])
    .await;
    let client = McpClient::connect(
        &McpServer::http("cached", &server.url).protocol_mode(McpProtocolMode::Latest),
    )
    .await?;

    assert_eq!(client.list_tools().await?[0].name, "search");
    assert_eq!(
        client.list_tools().await?[0].name,
        "search",
        "the second call is answered from memory"
    );
    assert_eq!(server.requests().await.len(), 1, "one round trip, not two");
    Ok(())
}

/// Without a hint there is nothing to cache on, and re-listing every turn is
/// the only correct behaviour.
#[tokio::test]
async fn a_tool_list_with_no_freshness_hint_is_not_cached() -> Result<()> {
    let server = FakeMcpServer::serving(vec![
        FakeReply::ok(
            "application/json",
            envelope(1, serde_json::json!({"tools": [{"name": "search"}]})),
        ),
        FakeReply::ok(
            "application/json",
            envelope(2, serde_json::json!({"tools": [{"name": "search"}]})),
        ),
    ])
    .await;
    let client = McpClient::connect(
        &McpServer::http("uncached", &server.url).protocol_mode(McpProtocolMode::Latest),
    )
    .await?;

    client.list_tools().await?;
    client.list_tools().await?;
    assert_eq!(server.requests().await.len(), 2);
    Ok(())
}

/// A server that wants elicitation answered mid-call returns a result with no
/// `content` at all. Reading it as an ordinary result would tell the model the
/// tool succeeded and returned nothing.
#[tokio::test]
async fn a_tool_call_needing_input_fails_instead_of_returning_nothing() -> Result<()> {
    let server = FakeMcpServer::serving(vec![
        FakeReply::ok(
            "application/json",
            envelope(
                1,
                serde_json::json!({
                    "resultType": "complete",
                    "tools": [{"name": "deploy", "description": "Deploy."}],
                }),
            ),
        ),
        FakeReply::ok(
            "application/json",
            envelope(
                2,
                serde_json::json!({
                    "resultType": "input_required",
                    "inputRequests": [{
                        "method": "elicitation/create",
                        "params": {"message": "Which environment?"},
                    }],
                }),
            ),
        ),
    ])
    .await;
    let client = McpClient::connect(
        &McpServer::http("deployer", &server.url).protocol_mode(McpProtocolMode::Latest),
    )
    .await?;

    assert_eq!(client.list_tools().await?[0].name, "deploy");
    let error = client
        .call_tool("deploy", serde_json::json!({}))
        .await
        .expect_err("MRTR is not supported");
    assert!(error.to_string().contains("multi round-trip"), "{error}");
    Ok(())
}