a2a-rust 0.1.0

Rust SDK for the A2A (Agent-to-Agent) protocol
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
#![cfg(feature = "client")]

use std::collections::BTreeMap;
use std::time::Duration;

use futures_util::StreamExt;
use serde_json::json;
use tokio::time::sleep;
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, Request, Respond, ResponseTemplate};

use a2a_rust::A2AError;
use a2a_rust::client::{A2AClient, AgentCardDiscovery, AgentCardDiscoveryConfig};
use a2a_rust::types::{
    AgentCapabilities, AgentCard, AgentInterface, Message, Part, Role, SendMessageRequest,
    SendMessageResponse, StreamResponse, SubscribeToTaskRequest, Task, TaskState, TaskStatus,
    TaskStatusUpdateEvent,
};

#[tokio::test]
async fn discovery_refetches_after_ttl_expiry() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/.well-known/agent-card.json"))
        .respond_with(ResponseTemplate::new(200).set_body_json(agent_card(
            vec![interface("/rpc", "JSONRPC")],
            capabilities(false, false),
        )))
        .mount(&server)
        .await;

    let discovery = AgentCardDiscovery::with_config(AgentCardDiscoveryConfig {
        ttl: Duration::from_millis(20),
    });

    discovery
        .discover(&server.uri())
        .await
        .expect("first discovery should succeed");
    discovery
        .discover(&server.uri())
        .await
        .expect("cached discovery should succeed");
    sleep(Duration::from_millis(30)).await;
    discovery
        .discover(&server.uri())
        .await
        .expect("discovery after ttl should refetch");

    let requests = server
        .received_requests()
        .await
        .expect("received requests should be available");
    let discovery_hits = requests
        .iter()
        .filter(|request| request.url.path() == "/.well-known/agent-card.json")
        .count();

    assert_eq!(discovery_hits, 2);
}

#[tokio::test]
async fn client_uses_first_supported_interface_from_agent_card() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/.well-known/agent-card.json"))
        .respond_with(ResponseTemplate::new(200).set_body_json(agent_card(
            vec![interface("/", "HTTP+JSON"), interface("/rpc", "JSONRPC")],
            capabilities(false, false),
        )))
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/message:send"))
        .respond_with(
            ResponseTemplate::new(200).set_body_json(send_message_response("rest-first", None)),
        )
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/rpc"))
        .respond_with(ResponseTemplate::new(500))
        .mount(&server)
        .await;

    let client = A2AClient::new(&server.uri()).expect("client should build");
    let response = client
        .send_message(user_message_request(None))
        .await
        .expect("rest-first transport should succeed");

    match response {
        SendMessageResponse::Message(message) => {
            assert_eq!(message.parts[0].text.as_deref(), Some("rest-first"));
        }
        SendMessageResponse::Task(_) => panic!("expected message response"),
    }

    let requests = server
        .received_requests()
        .await
        .expect("received requests should be available");
    assert!(
        requests
            .iter()
            .any(|request| request.url.path() == "/message:send"),
        "rest endpoint should have been called first"
    );
    assert!(
        !requests.iter().any(|request| request.url.path() == "/rpc"),
        "jsonrpc endpoint should not be used when HTTP+JSON is listed first"
    );
}

#[tokio::test]
async fn client_rejects_jsonrpc_response_with_mismatched_id() {
    let server = MockServer::start().await;
    mount_jsonrpc_discovery(&server).await;
    Mock::given(method("POST"))
        .and(path("/rpc"))
        .respond_with(JsonRpcEnvelopeResponder {
            jsonrpc: "2.0",
            mismatched_id: true,
        })
        .mount(&server)
        .await;

    let client = A2AClient::new(&server.uri()).expect("client should build");
    let error = client
        .send_message(user_message_request(None))
        .await
        .expect_err("mismatched response ids should fail");

    match error {
        A2AError::InvalidAgentResponse(detail) => {
            assert!(detail.contains("response id did not match request id"));
        }
        other => panic!("expected invalid agent response, got {other}"),
    }
}

#[tokio::test]
async fn client_rejects_jsonrpc_response_with_invalid_version() {
    let server = MockServer::start().await;
    mount_jsonrpc_discovery(&server).await;
    Mock::given(method("POST"))
        .and(path("/rpc"))
        .respond_with(JsonRpcEnvelopeResponder {
            jsonrpc: "1.0",
            mismatched_id: false,
        })
        .mount(&server)
        .await;

    let client = A2AClient::new(&server.uri()).expect("client should build");
    let error = client
        .send_message(user_message_request(None))
        .await
        .expect_err("invalid jsonrpc versions should fail");

    match error {
        A2AError::InvalidAgentResponse(detail) => {
            assert!(detail.contains("jsonrpc must be \"2.0\""));
        }
        other => panic!("expected invalid agent response, got {other}"),
    }
}

#[tokio::test]
async fn client_parses_sse_streams_with_lf_frame_delimiters() {
    let server = MockServer::start().await;
    mount_http_json_discovery(&server, true).await;
    Mock::given(method("POST"))
        .and(path("/message:stream"))
        .respond_with(ResponseTemplate::new(200).set_body_raw(
            sse_body(
                "\n\n",
                &[StreamResponse::Message(agent_message_response(
                    "lf-stream",
                    None,
                ))],
            ),
            "text/event-stream",
        ))
        .mount(&server)
        .await;

    let client = A2AClient::new(&server.uri()).expect("client should build");
    let items = client
        .send_streaming_message(user_message_request(None))
        .await
        .expect("stream should succeed")
        .collect::<Vec<_>>()
        .await;

    assert_eq!(items.len(), 1);
    match &items[0] {
        Ok(StreamResponse::Message(message)) => {
            assert_eq!(message.parts[0].text.as_deref(), Some("lf-stream"));
        }
        other => panic!("expected message stream response, got {other:?}"),
    }
}

#[tokio::test]
async fn client_parses_sse_streams_with_crlf_frame_delimiters() {
    let server = MockServer::start().await;
    mount_http_json_discovery(&server, true).await;
    Mock::given(method("GET"))
        .and(path("/tasks/task-1:subscribe"))
        .respond_with(ResponseTemplate::new(200).set_body_raw(
            sse_body(
                "\r\n\r\n",
                &[
                    StreamResponse::Task(task("task-1")),
                    StreamResponse::StatusUpdate(TaskStatusUpdateEvent {
                        task_id: "task-1".to_owned(),
                        context_id: "ctx-1".to_owned(),
                        status: TaskStatus {
                            state: TaskState::Completed,
                            message: None,
                            timestamp: Some("2026-03-13T08:31:00Z".to_owned()),
                        },
                        metadata: None,
                    }),
                ],
            ),
            "text/event-stream",
        ))
        .mount(&server)
        .await;

    let client = A2AClient::new(&server.uri()).expect("client should build");
    let items = client
        .subscribe_to_task(SubscribeToTaskRequest {
            id: "task-1".to_owned(),
            tenant: None,
        })
        .await
        .expect("subscribe stream should succeed")
        .collect::<Vec<_>>()
        .await;

    assert_eq!(items.len(), 2);
    match &items[0] {
        Ok(StreamResponse::Task(task)) => assert_eq!(task.id, "task-1"),
        other => panic!("expected task event first, got {other:?}"),
    }
    match &items[1] {
        Ok(StreamResponse::StatusUpdate(update)) => {
            assert_eq!(update.status.state, TaskState::Completed);
        }
        other => panic!("expected status update second, got {other:?}"),
    }
}

#[tokio::test]
async fn client_maps_http_problem_details_to_a2a_error() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/.well-known/agent-card.json"))
        .respond_with(ResponseTemplate::new(200).set_body_json(agent_card(
            vec![interface("/", "HTTP+JSON")],
            capabilities(false, false),
        )))
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/message:send"))
        .respond_with(ResponseTemplate::new(400).set_body_json(json!({
            "type": "https://a2a-protocol.org/errors/extension-support-required",
            "title": "Extension support required",
            "status": 400,
            "detail": "missing required extensions: https://example.com/ext/required",
            "reason": "EXTENSION_SUPPORT_REQUIRED",
            "domain": "a2a-protocol.org",
        })))
        .mount(&server)
        .await;

    let client = A2AClient::new(&server.uri()).expect("client should build");
    let error = client
        .send_message(user_message_request(None))
        .await
        .expect_err("problem details should map to an A2A error");

    match error {
        A2AError::ExtensionSupportRequired(detail) => {
            assert!(detail.contains("missing required extensions"));
        }
        other => panic!("expected extension-support-required, got {other}"),
    }
}

#[tokio::test]
async fn client_rejects_agent_cards_without_a_supported_protocol_version() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/.well-known/agent-card.json"))
        .respond_with(ResponseTemplate::new(200).set_body_json(agent_card(
            vec![interface_with_version("/rpc", "JSONRPC", "0.9")],
            capabilities(false, false),
        )))
        .mount(&server)
        .await;

    let client = A2AClient::new(&server.uri()).expect("client should build");
    let error = client
        .send_message(user_message_request(None))
        .await
        .expect_err("unsupported interface versions should fail");

    match error {
        A2AError::VersionNotSupported(detail) => {
            assert!(detail.contains("1.0"));
            assert!(detail.contains("0.9"));
        }
        other => panic!("expected version-not-supported, got {other}"),
    }
}

async fn mount_jsonrpc_discovery(server: &MockServer) {
    Mock::given(method("GET"))
        .and(path("/.well-known/agent-card.json"))
        .respond_with(ResponseTemplate::new(200).set_body_json(agent_card(
            vec![interface("/rpc", "JSONRPC")],
            capabilities(false, false),
        )))
        .mount(server)
        .await;
}

async fn mount_http_json_discovery(server: &MockServer, streaming: bool) {
    Mock::given(method("GET"))
        .and(path("/.well-known/agent-card.json"))
        .respond_with(ResponseTemplate::new(200).set_body_json(agent_card(
            vec![interface("/", "HTTP+JSON")],
            capabilities(streaming, false),
        )))
        .mount(server)
        .await;
}

fn agent_card(interfaces: Vec<AgentInterface>, capabilities: AgentCapabilities) -> AgentCard {
    AgentCard {
        name: "Wiremock Agent".to_owned(),
        description: "Client wiremock test agent".to_owned(),
        supported_interfaces: interfaces,
        provider: None,
        version: "0.1.0".to_owned(),
        documentation_url: None,
        capabilities,
        security_schemes: BTreeMap::new(),
        security_requirements: Vec::new(),
        default_input_modes: vec!["text/plain".to_owned()],
        default_output_modes: vec!["text/plain".to_owned()],
        skills: Vec::new(),
        signatures: Vec::new(),
        icon_url: None,
    }
}

fn interface(url: &str, protocol_binding: &str) -> AgentInterface {
    interface_with_version(url, protocol_binding, "1.0")
}

fn interface_with_version(
    url: &str,
    protocol_binding: &str,
    protocol_version: &str,
) -> AgentInterface {
    AgentInterface {
        url: url.to_owned(),
        protocol_binding: protocol_binding.to_owned(),
        tenant: None,
        protocol_version: protocol_version.to_owned(),
    }
}

fn capabilities(streaming: bool, push_notifications: bool) -> AgentCapabilities {
    AgentCapabilities {
        streaming: Some(streaming),
        push_notifications: Some(push_notifications),
        extensions: Vec::new(),
        extended_agent_card: Some(true),
    }
}

fn user_message_request(tenant: Option<String>) -> SendMessageRequest {
    SendMessageRequest {
        message: Message {
            message_id: "msg-1".to_owned(),
            context_id: Some("ctx-1".to_owned()),
            task_id: None,
            role: Role::User,
            parts: vec![Part {
                text: Some("ping".to_owned()),
                raw: None,
                url: None,
                data: None,
                metadata: None,
                filename: None,
                media_type: None,
            }],
            metadata: None,
            extensions: Vec::new(),
            reference_task_ids: Vec::new(),
        },
        configuration: None,
        metadata: None,
        tenant,
    }
}

fn agent_message_response(text: &str, tenant: Option<String>) -> Message {
    Message {
        message_id: "msg-agent-1".to_owned(),
        context_id: Some("ctx-1".to_owned()),
        task_id: None,
        role: Role::Agent,
        parts: vec![Part {
            text: Some(text.to_owned()),
            raw: None,
            url: None,
            data: None,
            metadata: None,
            filename: None,
            media_type: None,
        }],
        metadata: tenant.map(|tenant| {
            let mut metadata = serde_json::Map::new();
            metadata.insert("tenant".to_owned(), serde_json::Value::String(tenant));
            metadata
        }),
        extensions: Vec::new(),
        reference_task_ids: Vec::new(),
    }
}

fn send_message_response(text: &str, tenant: Option<String>) -> SendMessageResponse {
    SendMessageResponse::Message(agent_message_response(text, tenant))
}

fn task(id: &str) -> Task {
    Task {
        id: id.to_owned(),
        context_id: Some("ctx-1".to_owned()),
        status: TaskStatus {
            state: TaskState::Working,
            message: None,
            timestamp: Some("2026-03-13T08:30:00Z".to_owned()),
        },
        artifacts: Vec::new(),
        history: Vec::new(),
        metadata: None,
    }
}

fn sse_body(delimiter: &str, items: &[StreamResponse]) -> String {
    items
        .iter()
        .map(|item| {
            let payload = serde_json::to_string(item).expect("stream item should serialize");
            format!("data: {payload}{delimiter}")
        })
        .collect::<String>()
}

struct JsonRpcEnvelopeResponder {
    jsonrpc: &'static str,
    mismatched_id: bool,
}

impl Respond for JsonRpcEnvelopeResponder {
    fn respond(&self, request: &Request) -> ResponseTemplate {
        let request_body: serde_json::Value =
            serde_json::from_slice(&request.body).expect("request body should be valid json");
        let id = if self.mismatched_id {
            json!("wrong-id")
        } else {
            request_body
                .get("id")
                .cloned()
                .unwrap_or(serde_json::Value::Null)
        };

        ResponseTemplate::new(200).set_body_json(json!({
            "jsonrpc": self.jsonrpc,
            "result": serde_json::to_value(send_message_response("rpc", None))
                .expect("response should serialize"),
            "id": id,
        }))
    }
}