forked from agentclientprotocol/agent-client-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.rs
More file actions
2219 lines (2032 loc) · 73.7 KB
/
agent.rs
File metadata and controls
2219 lines (2032 loc) · 73.7 KB
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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Methods and notifications the agent handles/receives.
//!
//! This module defines the Agent trait and all associated types for implementing
//! an AI coding agent that follows the Agent Client Protocol (ACP).
use std::{path::PathBuf, sync::Arc};
use derive_more::{Display, From};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::value::RawValue;
use crate::ext::ExtRequest;
use crate::{ClientCapabilities, ContentBlock, ExtNotification, ProtocolVersion, SessionId};
// Initialize
/// Request parameters for the initialize method.
///
/// Sent by the client to establish connection and negotiate capabilities.
///
/// See protocol docs: [Initialization](https://agentclientprotocol.com/protocol/initialization)
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[schemars(extend("x-side" = "agent", "x-method" = INITIALIZE_METHOD_NAME))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct InitializeRequest {
/// The latest protocol version supported by the client.
pub protocol_version: ProtocolVersion,
/// Capabilities supported by the client.
#[serde(default)]
pub client_capabilities: ClientCapabilities,
/// Information about the Client name and version sent to the Agent.
///
/// Note: in future versions of the protocol, this will be required.
#[serde(skip_serializing_if = "Option::is_none")]
pub client_info: Option<Implementation>,
/// Extension point for implementations
#[serde(skip_serializing_if = "Option::is_none", rename = "_meta")]
pub meta: Option<serde_json::Value>,
}
impl InitializeRequest {
#[must_use]
pub fn new(protocol_version: ProtocolVersion) -> Self {
Self {
protocol_version,
client_capabilities: ClientCapabilities::default(),
client_info: None,
meta: None,
}
}
/// Capabilities supported by the client.
#[must_use]
pub fn client_capabilities(mut self, client_capabilities: ClientCapabilities) -> Self {
self.client_capabilities = client_capabilities;
self
}
/// Information about the Client name and version sent to the Agent.
#[must_use]
pub fn client_info(mut self, client_info: Implementation) -> Self {
self.client_info = Some(client_info);
self
}
/// Extension point for implementations
#[must_use]
pub fn meta(mut self, meta: serde_json::Value) -> Self {
self.meta = Some(meta);
self
}
}
/// Response to the `initialize` method.
///
/// Contains the negotiated protocol version and agent capabilities.
///
/// See protocol docs: [Initialization](https://agentclientprotocol.com/protocol/initialization)
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[schemars(extend("x-side" = "agent", "x-method" = INITIALIZE_METHOD_NAME))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct InitializeResponse {
/// The protocol version the client specified if supported by the agent,
/// or the latest protocol version supported by the agent.
///
/// The client should disconnect, if it doesn't support this version.
pub protocol_version: ProtocolVersion,
/// Capabilities supported by the agent.
#[serde(default)]
pub agent_capabilities: AgentCapabilities,
/// Authentication methods supported by the agent.
#[serde(default)]
pub auth_methods: Vec<AuthMethod>,
/// Information about the Agent name and version sent to the Client.
///
/// Note: in future versions of the protocol, this will be required.
#[serde(skip_serializing_if = "Option::is_none")]
pub agent_info: Option<Implementation>,
/// Extension point for implementations
#[serde(skip_serializing_if = "Option::is_none", rename = "_meta")]
pub meta: Option<serde_json::Value>,
}
impl InitializeResponse {
#[must_use]
pub fn new(protocol_version: ProtocolVersion) -> Self {
Self {
protocol_version,
agent_capabilities: AgentCapabilities::default(),
auth_methods: vec![],
agent_info: None,
meta: None,
}
}
/// Capabilities supported by the agent.
#[must_use]
pub fn agent_capabilities(mut self, agent_capabilities: AgentCapabilities) -> Self {
self.agent_capabilities = agent_capabilities;
self
}
/// Authentication methods supported by the agent.
#[must_use]
pub fn auth_methods(mut self, auth_methods: Vec<AuthMethod>) -> Self {
self.auth_methods = auth_methods;
self
}
/// Information about the Agent name and version sent to the Client.
#[must_use]
pub fn agent_info(mut self, agent_info: Implementation) -> Self {
self.agent_info = Some(agent_info);
self
}
/// Extension point for implementations
#[must_use]
pub fn meta(mut self, meta: serde_json::Value) -> Self {
self.meta = Some(meta);
self
}
}
/// Metadata about the implementation of the client or agent.
/// Describes the name and version of an MCP implementation, with an optional
/// title for UI representation.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Implementation {
/// Intended for programmatic or logical use, but can be used as a display
/// name fallback if title isn’t present.
pub name: String,
/// Intended for UI and end-user contexts — optimized to be human-readable
/// and easily understood.
///
/// If not provided, the name should be used for display.
pub title: Option<String>,
/// Version of the implementation. Can be displayed to the user or used
/// for debugging or metrics purposes. (e.g. "1.0.0").
pub version: String,
/// Extension point for implementations
#[serde(skip_serializing_if = "Option::is_none", rename = "_meta")]
pub meta: Option<serde_json::Value>,
}
impl Implementation {
pub fn new(name: impl Into<String>, version: impl Into<String>) -> Self {
Self {
name: name.into(),
title: None,
version: version.into(),
meta: None,
}
}
/// Intended for UI and end-user contexts — optimized to be human-readable
/// and easily understood.
///
/// If not provided, the name should be used for display.
#[must_use]
pub fn title(mut self, title: impl Into<String>) -> Self {
self.title = Some(title.into());
self
}
/// Extension point for implementations
#[must_use]
pub fn meta(mut self, meta: serde_json::Value) -> Self {
self.meta = Some(meta);
self
}
}
// Authentication
/// Request parameters for the authenticate method.
///
/// Specifies which authentication method to use.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[schemars(extend("x-side" = "agent", "x-method" = AUTHENTICATE_METHOD_NAME))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AuthenticateRequest {
/// The ID of the authentication method to use.
/// Must be one of the methods advertised in the initialize response.
pub method_id: AuthMethodId,
/// Extension point for implementations
#[serde(skip_serializing_if = "Option::is_none", rename = "_meta")]
pub meta: Option<serde_json::Value>,
}
impl AuthenticateRequest {
#[must_use]
pub fn new(method_id: impl Into<AuthMethodId>) -> Self {
Self {
method_id: method_id.into(),
meta: None,
}
}
#[must_use]
pub fn meta(mut self, meta: serde_json::Value) -> Self {
self.meta = Some(meta);
self
}
}
/// Response to the `authenticate` method.
#[derive(Default, Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
#[schemars(extend("x-side" = "agent", "x-method" = AUTHENTICATE_METHOD_NAME))]
#[non_exhaustive]
pub struct AuthenticateResponse {
/// Extension point for implementations
#[serde(skip_serializing_if = "Option::is_none", rename = "_meta")]
pub meta: Option<serde_json::Value>,
}
impl AuthenticateResponse {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn meta(mut self, meta: serde_json::Value) -> Self {
self.meta = Some(meta);
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Hash, Display, From)]
#[serde(transparent)]
#[from(Arc<str>, String, &'static str)]
#[non_exhaustive]
pub struct AuthMethodId(pub Arc<str>);
impl AuthMethodId {
pub fn new(id: impl Into<Arc<str>>) -> Self {
Self(id.into())
}
}
/// Describes an available authentication method.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AuthMethod {
/// Unique identifier for this authentication method.
pub id: AuthMethodId,
/// Human-readable name of the authentication method.
pub name: String,
/// Optional description providing more details about this authentication method.
pub description: Option<String>,
/// Extension point for implementations
#[serde(skip_serializing_if = "Option::is_none", rename = "_meta")]
pub meta: Option<serde_json::Value>,
}
impl AuthMethod {
pub fn new(id: impl Into<AuthMethodId>, name: impl Into<String>) -> Self {
Self {
id: id.into(),
name: name.into(),
description: None,
meta: None,
}
}
/// Optional description providing more details about this authentication method.
#[must_use]
pub fn description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
/// Extension point for implementations
#[must_use]
pub fn meta(mut self, meta: serde_json::Value) -> Self {
self.meta = Some(meta);
self
}
}
// New session
/// Request parameters for creating a new session.
///
/// See protocol docs: [Creating a Session](https://agentclientprotocol.com/protocol/session-setup#creating-a-session)
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[schemars(extend("x-side" = "agent", "x-method" = SESSION_NEW_METHOD_NAME))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct NewSessionRequest {
/// The working directory for this session. Must be an absolute path.
pub cwd: PathBuf,
/// List of MCP (Model Context Protocol) servers the agent should connect to.
pub mcp_servers: Vec<McpServer>,
/// Extension point for implementations
#[serde(skip_serializing_if = "Option::is_none", rename = "_meta")]
pub meta: Option<serde_json::Value>,
}
impl NewSessionRequest {
pub fn new(cwd: impl Into<PathBuf>) -> Self {
Self {
cwd: cwd.into(),
mcp_servers: vec![],
meta: None,
}
}
/// List of MCP (Model Context Protocol) servers the agent should connect to.
#[must_use]
pub fn mcp_servers(mut self, mcp_servers: Vec<McpServer>) -> Self {
self.mcp_servers = mcp_servers;
self
}
/// Extension point for implementations
#[must_use]
pub fn meta(mut self, meta: serde_json::Value) -> Self {
self.meta = Some(meta);
self
}
}
/// Response from creating a new session.
///
/// See protocol docs: [Creating a Session](https://agentclientprotocol.com/protocol/session-setup#creating-a-session)
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[schemars(extend("x-side" = "agent", "x-method" = SESSION_NEW_METHOD_NAME))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct NewSessionResponse {
/// Unique identifier for the created session.
///
/// Used in all subsequent requests for this conversation.
pub session_id: SessionId,
/// Initial mode state if supported by the Agent
///
/// See protocol docs: [Session Modes](https://agentclientprotocol.com/protocol/session-modes)
#[serde(skip_serializing_if = "Option::is_none")]
pub modes: Option<SessionModeState>,
/// **UNSTABLE**
///
/// This capability is not part of the spec yet, and may be removed or changed at any point.
///
/// Initial model state if supported by the Agent
#[cfg(feature = "unstable_session_model")]
#[serde(skip_serializing_if = "Option::is_none")]
pub models: Option<SessionModelState>,
/// Extension point for implementations
#[serde(skip_serializing_if = "Option::is_none", rename = "_meta")]
pub meta: Option<serde_json::Value>,
}
impl NewSessionResponse {
#[must_use]
pub fn new(session_id: impl Into<SessionId>) -> Self {
Self {
session_id: session_id.into(),
modes: None,
#[cfg(feature = "unstable_session_model")]
models: None,
meta: None,
}
}
/// Initial mode state if supported by the Agent
///
/// See protocol docs: [Session Modes](https://agentclientprotocol.com/protocol/session-modes)
#[must_use]
pub fn modes(mut self, modes: SessionModeState) -> Self {
self.modes = Some(modes);
self
}
/// **UNSTABLE**
///
/// This capability is not part of the spec yet, and may be removed or changed at any point.
///
/// Initial model state if supported by the Agent
#[cfg(feature = "unstable_session_model")]
#[must_use]
pub fn models(mut self, models: SessionModelState) -> Self {
self.models = Some(models);
self
}
/// Extension point for implementations
#[must_use]
pub fn meta(mut self, meta: serde_json::Value) -> Self {
self.meta = Some(meta);
self
}
}
// Load session
/// Request parameters for loading an existing session.
///
/// Only available if the Agent supports the `loadSession` capability.
///
/// See protocol docs: [Loading Sessions](https://agentclientprotocol.com/protocol/session-setup#loading-sessions)
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[schemars(extend("x-side" = "agent", "x-method" = SESSION_LOAD_METHOD_NAME))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct LoadSessionRequest {
/// List of MCP servers to connect to for this session.
pub mcp_servers: Vec<McpServer>,
/// The working directory for this session.
pub cwd: PathBuf,
/// The ID of the session to load.
pub session_id: SessionId,
/// Extension point for implementations
#[serde(skip_serializing_if = "Option::is_none", rename = "_meta")]
pub meta: Option<serde_json::Value>,
}
impl LoadSessionRequest {
pub fn new(session_id: impl Into<SessionId>, cwd: impl Into<PathBuf>) -> Self {
Self {
mcp_servers: vec![],
cwd: cwd.into(),
session_id: session_id.into(),
meta: None,
}
}
/// List of MCP servers to connect to for this session.
#[must_use]
pub fn mcp_servers(mut self, mcp_servers: Vec<McpServer>) -> Self {
self.mcp_servers = mcp_servers;
self
}
/// Extension point for implementations
#[must_use]
pub fn meta(mut self, meta: serde_json::Value) -> Self {
self.meta = Some(meta);
self
}
}
/// Response from loading an existing session.
#[derive(Default, Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[schemars(extend("x-side" = "agent", "x-method" = SESSION_LOAD_METHOD_NAME))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct LoadSessionResponse {
/// Initial mode state if supported by the Agent
///
/// See protocol docs: [Session Modes](https://agentclientprotocol.com/protocol/session-modes)
#[serde(default, skip_serializing_if = "Option::is_none")]
pub modes: Option<SessionModeState>,
/// **UNSTABLE**
///
/// This capability is not part of the spec yet, and may be removed or changed at any point.
///
/// Initial model state if supported by the Agent
#[cfg(feature = "unstable_session_model")]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub models: Option<SessionModelState>,
/// Extension point for implementations
#[serde(skip_serializing_if = "Option::is_none", rename = "_meta")]
pub meta: Option<serde_json::Value>,
}
impl LoadSessionResponse {
#[must_use]
pub fn new() -> Self {
Self::default()
}
/// Initial mode state if supported by the Agent
///
/// See protocol docs: [Session Modes](https://agentclientprotocol.com/protocol/session-modes)
#[must_use]
pub fn modes(mut self, modes: SessionModeState) -> Self {
self.modes = Some(modes);
self
}
/// **UNSTABLE**
///
/// This capability is not part of the spec yet, and may be removed or changed at any point.
///
/// Initial model state if supported by the Agent
#[cfg(feature = "unstable_session_model")]
#[must_use]
pub fn models(mut self, models: SessionModelState) -> Self {
self.models = Some(models);
self
}
/// Extension point for implementations
#[must_use]
pub fn meta(mut self, meta: serde_json::Value) -> Self {
self.meta = Some(meta);
self
}
}
// Fork session
/// **UNSTABLE**
///
/// This capability is not part of the spec yet, and may be removed or changed at any point.
///
/// Request parameters for forking an existing session.
///
/// Creates a new session based on the context of an existing one, allowing
/// operations like generating summaries without affecting the original session's history.
///
/// Only available if the Agent supports the `session.fork` capability.
#[cfg(feature = "unstable_session_fork")]
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[schemars(extend("x-side" = "agent", "x-method" = SESSION_FORK_METHOD_NAME))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ForkSessionRequest {
/// The ID of the session to fork.
pub session_id: SessionId,
/// Extension point for implementations
#[serde(skip_serializing_if = "Option::is_none", rename = "_meta")]
pub meta: Option<serde_json::Value>,
}
#[cfg(feature = "unstable_session_fork")]
impl ForkSessionRequest {
pub fn new(session_id: impl Into<SessionId>) -> Self {
Self {
session_id: session_id.into(),
meta: None,
}
}
/// Extension point for implementations
#[must_use]
pub fn meta(mut self, meta: serde_json::Value) -> Self {
self.meta = Some(meta);
self
}
}
/// **UNSTABLE**
///
/// This capability is not part of the spec yet, and may be removed or changed at any point.
///
/// Response from forking an existing session.
#[cfg(feature = "unstable_session_fork")]
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[schemars(extend("x-side" = "agent", "x-method" = SESSION_FORK_METHOD_NAME))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ForkSessionResponse {
/// Unique identifier for the newly created forked session.
pub session_id: SessionId,
/// Initial mode state if supported by the Agent
///
/// See protocol docs: [Session Modes](https://agentclientprotocol.com/protocol/session-modes)
#[serde(skip_serializing_if = "Option::is_none")]
pub modes: Option<SessionModeState>,
/// **UNSTABLE**
///
/// This capability is not part of the spec yet, and may be removed or changed at any point.
///
/// Initial model state if supported by the Agent
#[cfg(feature = "unstable_session_model")]
#[serde(skip_serializing_if = "Option::is_none")]
pub models: Option<SessionModelState>,
/// Extension point for implementations
#[serde(skip_serializing_if = "Option::is_none", rename = "_meta")]
pub meta: Option<serde_json::Value>,
}
#[cfg(feature = "unstable_session_fork")]
impl ForkSessionResponse {
#[must_use]
pub fn new(session_id: impl Into<SessionId>) -> Self {
Self {
session_id: session_id.into(),
modes: None,
#[cfg(feature = "unstable_session_model")]
models: None,
meta: None,
}
}
/// Initial mode state if supported by the Agent
///
/// See protocol docs: [Session Modes](https://agentclientprotocol.com/protocol/session-modes)
#[must_use]
pub fn modes(mut self, modes: SessionModeState) -> Self {
self.modes = Some(modes);
self
}
/// **UNSTABLE**
///
/// This capability is not part of the spec yet, and may be removed or changed at any point.
///
/// Initial model state if supported by the Agent
#[cfg(feature = "unstable_session_model")]
#[must_use]
pub fn models(mut self, models: SessionModelState) -> Self {
self.models = Some(models);
self
}
/// Extension point for implementations
#[must_use]
pub fn meta(mut self, meta: serde_json::Value) -> Self {
self.meta = Some(meta);
self
}
}
// List sessions
/// **UNSTABLE**
///
/// This capability is not part of the spec yet, and may be removed or changed at any point.
///
/// Request parameters for listing existing sessions.
///
/// Only available if the Agent supports the `listSessions` capability.
#[cfg(feature = "unstable_session_list")]
#[derive(Default, Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[schemars(extend("x-side" = "agent", "x-method" = SESSION_LIST_METHOD_NAME))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ListSessionsRequest {
/// Filter sessions by working directory. Must be an absolute path.
#[serde(skip_serializing_if = "Option::is_none")]
pub cwd: Option<PathBuf>,
/// Opaque cursor token from a previous response's nextCursor field for cursor-based pagination
#[serde(skip_serializing_if = "Option::is_none")]
pub cursor: Option<String>,
/// Extension point for implementations
#[serde(skip_serializing_if = "Option::is_none", rename = "_meta")]
pub meta: Option<serde_json::Value>,
}
#[cfg(feature = "unstable_session_list")]
impl ListSessionsRequest {
#[must_use]
pub fn new() -> Self {
Self::default()
}
/// Filter sessions by working directory. Must be an absolute path.
#[must_use]
pub fn cwd(mut self, cwd: impl Into<PathBuf>) -> Self {
self.cwd = Some(cwd.into());
self
}
/// Opaque cursor token from a previous response's nextCursor field for cursor-based pagination
#[must_use]
pub fn cursor(mut self, cursor: impl Into<String>) -> Self {
self.cursor = Some(cursor.into());
self
}
/// Extension point for implementations
#[must_use]
pub fn meta(mut self, meta: serde_json::Value) -> Self {
self.meta = Some(meta);
self
}
}
/// **UNSTABLE**
///
/// This capability is not part of the spec yet, and may be removed or changed at any point.
///
/// Response from listing sessions.
#[cfg(feature = "unstable_session_list")]
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[schemars(extend("x-side" = "agent", "x-method" = SESSION_LIST_METHOD_NAME))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ListSessionsResponse {
/// Array of session information objects
pub sessions: Vec<SessionInfo>,
/// Opaque cursor token. If present, pass this in the next request's cursor parameter
/// to fetch the next page. If absent, there are no more results.
#[serde(skip_serializing_if = "Option::is_none")]
pub next_cursor: Option<String>,
/// Extension point for implementations
#[serde(skip_serializing_if = "Option::is_none", rename = "_meta")]
pub meta: Option<serde_json::Value>,
}
#[cfg(feature = "unstable_session_list")]
impl ListSessionsResponse {
#[must_use]
pub fn new(sessions: Vec<SessionInfo>) -> Self {
Self {
sessions,
next_cursor: None,
meta: None,
}
}
#[must_use]
pub fn next_cursor(mut self, next_cursor: impl Into<String>) -> Self {
self.next_cursor = Some(next_cursor.into());
self
}
/// Extension point for implementations
#[must_use]
pub fn meta(mut self, meta: serde_json::Value) -> Self {
self.meta = Some(meta);
self
}
}
/// **UNSTABLE**
///
/// This capability is not part of the spec yet, and may be removed or changed at any point.
///
/// Information about a session returned by session/list
#[cfg(feature = "unstable_session_list")]
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct SessionInfo {
/// Unique identifier for the session
pub session_id: SessionId,
/// The working directory for this session. Must be an absolute path.
pub cwd: PathBuf,
/// Human-readable title for the session
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
/// ISO 8601 timestamp of last activity
#[serde(skip_serializing_if = "Option::is_none")]
pub updated_at: Option<String>,
/// Extension point for implementations
#[serde(skip_serializing_if = "Option::is_none", rename = "_meta")]
pub meta: Option<serde_json::Value>,
}
#[cfg(feature = "unstable_session_list")]
impl SessionInfo {
pub fn new(session_id: impl Into<SessionId>, cwd: impl Into<PathBuf>) -> Self {
Self {
session_id: session_id.into(),
cwd: cwd.into(),
title: None,
updated_at: None,
meta: None,
}
}
/// Human-readable title for the session
#[must_use]
pub fn title(mut self, title: impl Into<String>) -> Self {
self.title = Some(title.into());
self
}
/// ISO 8601 timestamp of last activity
#[must_use]
pub fn updated_at(mut self, updated_at: impl Into<String>) -> Self {
self.updated_at = Some(updated_at.into());
self
}
/// Extension point for implementations
#[must_use]
pub fn meta(mut self, meta: serde_json::Value) -> Self {
self.meta = Some(meta);
self
}
}
// Session modes
/// The set of modes and the one currently active.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct SessionModeState {
/// The current mode the Agent is in.
pub current_mode_id: SessionModeId,
/// The set of modes that the Agent can operate in
pub available_modes: Vec<SessionMode>,
/// Extension point for implementations
#[serde(skip_serializing_if = "Option::is_none", rename = "_meta")]
pub meta: Option<serde_json::Value>,
}
impl SessionModeState {
#[must_use]
pub fn new(
current_mode_id: impl Into<SessionModeId>,
available_modes: Vec<SessionMode>,
) -> Self {
Self {
current_mode_id: current_mode_id.into(),
available_modes,
meta: None,
}
}
/// Extension point for implementations
#[must_use]
pub fn meta(mut self, meta: serde_json::Value) -> Self {
self.meta = Some(meta);
self
}
}
/// A mode the agent can operate in.
///
/// See protocol docs: [Session Modes](https://agentclientprotocol.com/protocol/session-modes)
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct SessionMode {
pub id: SessionModeId,
pub name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
/// Extension point for implementations
#[serde(skip_serializing_if = "Option::is_none", rename = "_meta")]
pub meta: Option<serde_json::Value>,
}
impl SessionMode {
pub fn new(id: impl Into<SessionModeId>, name: impl Into<String>) -> Self {
Self {
id: id.into(),
name: name.into(),
description: None,
meta: None,
}
}
#[must_use]
pub fn description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
/// Extension point for implementations
#[must_use]
pub fn meta(mut self, meta: serde_json::Value) -> Self {
self.meta = Some(meta);
self
}
}
/// Unique identifier for a Session Mode.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Hash, From, Display)]
#[serde(transparent)]
#[from(Arc<str>, String, &'static str)]
#[non_exhaustive]
pub struct SessionModeId(pub Arc<str>);
impl SessionModeId {
pub fn new(id: impl Into<Arc<str>>) -> Self {
Self(id.into())
}
}
/// Request parameters for setting a session mode.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[schemars(extend("x-side" = "agent", "x-method" = SESSION_SET_MODE_METHOD_NAME))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct SetSessionModeRequest {
/// The ID of the session to set the mode for.
pub session_id: SessionId,
/// The ID of the mode to set.
pub mode_id: SessionModeId,
/// Extension point for implementations
#[serde(skip_serializing_if = "Option::is_none", rename = "_meta")]
pub meta: Option<serde_json::Value>,
}
impl SetSessionModeRequest {
#[must_use]
pub fn new(session_id: impl Into<SessionId>, mode_id: impl Into<SessionModeId>) -> Self {
Self {
session_id: session_id.into(),
mode_id: mode_id.into(),
meta: None,
}
}
#[must_use]
pub fn meta(mut self, meta: serde_json::Value) -> Self {
self.meta = Some(meta);
self
}
}
/// Response to `session/set_mode` method.
#[derive(Default, Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[schemars(extend("x-side" = "agent", "x-method" = SESSION_SET_MODE_METHOD_NAME))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct SetSessionModeResponse {
#[serde(skip_serializing_if = "Option::is_none", rename = "_meta")]
pub meta: Option<serde_json::Value>,
}
impl SetSessionModeResponse {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn meta(mut self, meta: serde_json::Value) -> Self {
self.meta = Some(meta);
self
}
}
// MCP
/// Configuration for connecting to an MCP (Model Context Protocol) server.
///
/// MCP servers provide tools and context that the agent can use when
/// processing prompts.
///
/// See protocol docs: [MCP Servers](https://agentclientprotocol.com/protocol/session-setup#mcp-servers)
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[serde(tag = "type", rename_all = "snake_case")]
#[schemars(extend("discriminator" = {"propertyName": "type"}))]
#[non_exhaustive]
pub enum McpServer {
/// HTTP transport configuration
///
/// Only available when the Agent capabilities indicate `mcp_capabilities.http` is `true`.
Http(McpServerHttp),
/// SSE transport configuration
///
/// Only available when the Agent capabilities indicate `mcp_capabilities.sse` is `true`.
Sse(McpServerSse),
/// Stdio transport configuration
///
/// All Agents MUST support this transport.
#[serde(untagged)]
Stdio(McpServerStdio),
}
/// HTTP transport configuration for MCP.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct McpServerHttp {
/// Human-readable name identifying this MCP server.
pub name: String,
/// URL to the MCP server.
pub url: String,
/// HTTP headers to set when making requests to the MCP server.
pub headers: Vec<HttpHeader>,
/// Extension point for implementations
#[serde(skip_serializing_if = "Option::is_none", rename = "_meta")]
pub meta: Option<serde_json::Value>,
}
impl McpServerHttp {
pub fn new(name: impl Into<String>, url: impl Into<String>) -> Self {
Self {
name: name.into(),
url: url.into(),