protocol-droid 0.4.0

A Model Context Protocol toolkit that speaks every published spec revision, with one canonical IR behind many per-revision codecs.
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
//! `AcpMethod` — every method the ACP v1.25.0 schema names.
//!
//! # Why this is a flat enum, not a `MethodTable` trait
//!
//! The protocol-neutral core (MCP and A2A) lives in
//! [`crate::domain::jsonrpc`]. The method table is the
//! per-protocol translation layer between the wire and the
//! application handler. A flat enum over a `MethodTable` trait
//! is `match`-friendly for the dispatcher, `serde`-friendly for
//! the wire, and the same shape every per-protocol module
//! uses. Mirroring that shape here keeps the dispatcher
//! generic.
//!
//! # Why the wire names are slash-separated
//!
//! ACP's `meta.json` (vendored at
//! `crates/protocol-droid-schema/spec/acp/v1.25.0/meta.json`)
//! names methods as `session/prompt`, `session/cancel`, and so
//! on. The slash is part of the wire name, not a separator
//! this crate invented; a peer that receives a method without
//! the slash is a wire violation. The `as_wire_name` and
//! `FromStr` impls keep the slash visible at every boundary.

use core::fmt;
use core::str::FromStr;

use serde::{Deserialize, Serialize};

/// A method on the ACP wire. Carries the direction (agent-side
/// or client-side) and the slash-separated wire name.
///
/// The wire form is the JSON-RPC 2.0 `method` field, not a
/// path. The slash is part of the name.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "direction", content = "name")]
pub enum AcpMethod {
    /// A method the editor (client) calls on the agent.
    /// An agent that supports a method answers the call.
    /// The wire name is the slash-separated form from
    /// `meta.json::agentMethods`.
    Agent(AgentMethod),
    /// A method the agent calls on the editor (client).
    /// An editor that supports a method answers the call.
    /// The wire name is the slash-separated form from
    /// `meta.json::clientMethods`.
    Client(ClientMethod),
    /// A JSON-RPC standard method, prefixed `$/`. Only
    /// `$/cancel_request` is named in the v1 schema.
    Protocol(ProtocolMethod),
}

/// The 13 methods an ACP agent publishes, as named in
/// `meta.json::agentMethods`. The wire names are the
/// slash-separated form.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AgentMethod {
    /// `initialize` — the editor's first call, advertising
    /// its protocol version and capabilities. The agent
    /// answers with its own version and capabilities.
    Initialize,
    /// `authenticate` — proves the editor's identity after
    /// `initialize` has negotiated an auth method.
    Authenticate,
    /// `session/new` — opens a new session.
    SessionNew,
    /// `session/load` — resumes a session from a checkpoint.
    SessionLoad,
    /// `session/set_mode` — switches the session's mode
    /// (e.g. ask vs. plan).
    SessionSetMode,
    /// `session/set_config_option` — sets a per-session
    /// configuration value the agent advertised in
    /// `initialize`.
    SessionSetConfigOption,
    /// `session/prompt` — sends a user turn. The agent
    /// streams `session/update` notifications back through
    /// the editor.
    SessionPrompt,
    /// `session/cancel` — cancels an in-flight `session/prompt`.
    SessionCancel,
    /// `session/list` — enumerates sessions.
    SessionList,
    /// `session/delete` — removes a session.
    SessionDelete,
    /// `session/resume` — resumes a previously-closed
    /// session.
    SessionResume,
    /// `session/close` — closes a session without deleting
    /// it.
    SessionClose,
    /// `logout` — terminates the auth session.
    Logout,
}

/// The 11 methods an ACP editor (client) publishes.
///
/// The names are the slash-separated form
/// `meta.json::clientMethods` lists. The agent
/// calls these during a session to ask the editor
/// for input, render updates, drive the editor's
/// filesystem, drive the editor's terminal, or run
/// an elicitation flow.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ClientMethod {
    /// `session/request_permission` — the agent asks the
    /// editor's user whether a tool call may proceed.
    SessionRequestPermission,
    /// `session/update` — the agent streams a turn update
    /// (a chunk of text, a tool call, a status change) to
    /// the editor.
    SessionUpdate,
    /// `fs/write_text_file` — the agent asks the editor
    /// to write a file. The editor decides whether to
    /// prompt the user, write silently, or refuse.
    FsWriteTextFile,
    /// `fs/read_text_file` — the agent asks the editor
    /// to read a file. The editor decides what to return
    /// (a `fs/read_text_file` request may hit an
    /// editor-side virtual file system, not the disk).
    FsReadTextFile,
    /// `terminal/create` — the agent asks the editor to
    /// allocate a terminal. The editor returns an
    /// identifier the agent uses for follow-up calls.
    TerminalCreate,
    /// `terminal/output` — the agent reads captured
    /// terminal output.
    TerminalOutput,
    /// `terminal/release` — the agent releases a terminal
    /// it no longer needs.
    TerminalRelease,
    /// `terminal/wait_for_exit` — the agent waits for a
    /// terminal command to exit.
    TerminalWaitForExit,
    /// `terminal/kill` — the agent sends `SIGTERM` to a
    /// running terminal command.
    TerminalKill,
    /// `elicitation/create` — the agent starts a
    /// structured elicitation (e.g. "pick one of these
    /// three options") in the editor.
    ElicitationCreate,
    /// `elicitation/complete` — the agent signals the
    /// elicitation is done; the editor's user has either
    /// answered or dismissed.
    ElicitationComplete,
}

/// A JSON-RPC standard method, prefixed `$/`. The v1 schema
/// names only `$/cancel_request`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProtocolMethod {
    /// `$/cancel_request` — a peer cancels an in-flight
    /// request by id. The peer that sent the request
    /// discards it (or returns a "cancelled" error if the
    /// response already left when the cancel arrived).
    CancelRequest,
}

impl AgentMethod {
    /// The wire name as it appears in a JSON-RPC envelope.
    /// The slash is part of the name; the peer is
    /// required to send the slashes too.
    pub const fn as_wire_name(self) -> &'static str {
        match self {
            Self::Initialize => "initialize",
            Self::Authenticate => "authenticate",
            Self::SessionNew => "session/new",
            Self::SessionLoad => "session/load",
            Self::SessionSetMode => "session/set_mode",
            Self::SessionSetConfigOption => "session/set_config_option",
            Self::SessionPrompt => "session/prompt",
            Self::SessionCancel => "session/cancel",
            Self::SessionList => "session/list",
            Self::SessionDelete => "session/delete",
            Self::SessionResume => "session/resume",
            Self::SessionClose => "session/close",
            Self::Logout => "logout",
        }
    }
}

impl ClientMethod {
    /// The wire name as it appears in a JSON-RPC envelope.
    pub const fn as_wire_name(self) -> &'static str {
        match self {
            Self::SessionRequestPermission => "session/request_permission",
            Self::SessionUpdate => "session/update",
            Self::FsWriteTextFile => "fs/write_text_file",
            Self::FsReadTextFile => "fs/read_text_file",
            Self::TerminalCreate => "terminal/create",
            Self::TerminalOutput => "terminal/output",
            Self::TerminalRelease => "terminal/release",
            Self::TerminalWaitForExit => "terminal/wait_for_exit",
            Self::TerminalKill => "terminal/kill",
            Self::ElicitationCreate => "elicitation/create",
            Self::ElicitationComplete => "elicitation/complete",
        }
    }
}

impl ProtocolMethod {
    /// The wire name, including the `$/` prefix.
    pub const fn as_wire_name(self) -> &'static str {
        match self {
            Self::CancelRequest => "$/cancel_request",
        }
    }
}

impl AcpMethod {
    /// The wire name as it appears in a JSON-RPC envelope.
    /// The direction is implied by the variant, not part
    /// of the wire name.
    pub const fn as_wire_name(self) -> &'static str {
        match self {
            Self::Agent(method) => method.as_wire_name(),
            Self::Client(method) => method.as_wire_name(),
            Self::Protocol(method) => method.as_wire_name(),
        }
    }
}

impl fmt::Display for AcpMethod {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_wire_name())
    }
}

/// The error returned by [`AcpMethod::from_str`].
///
/// The `meta.json`-listed set is exhaustive for v1.25.0,
/// so every well-formed wire name parses; an unknown
/// name is a wire violation or a future method.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AcpMethodParseError {
    input: String,
}

impl fmt::Display for AcpMethodParseError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "ACP method {:?} is not in the v1.25.0 method table",
            self.input
        )
    }
}

impl std::error::Error for AcpMethodParseError {}

impl FromStr for AcpMethod {
    type Err = AcpMethodParseError;

    fn from_str(input: &str) -> Result<Self, Self::Err> {
        if let Some(rest) = input.strip_prefix("$/") {
            return match rest {
                "cancel_request" => Ok(Self::Protocol(ProtocolMethod::CancelRequest)),
                _ => Err(AcpMethodParseError {
                    input: input.to_owned(),
                }),
            };
        }
        for method in [
            Self::Agent(AgentMethod::Initialize),
            Self::Agent(AgentMethod::Authenticate),
            Self::Agent(AgentMethod::SessionNew),
            Self::Agent(AgentMethod::SessionLoad),
            Self::Agent(AgentMethod::SessionSetMode),
            Self::Agent(AgentMethod::SessionSetConfigOption),
            Self::Agent(AgentMethod::SessionPrompt),
            Self::Agent(AgentMethod::SessionCancel),
            Self::Agent(AgentMethod::SessionList),
            Self::Agent(AgentMethod::SessionDelete),
            Self::Agent(AgentMethod::SessionResume),
            Self::Agent(AgentMethod::SessionClose),
            Self::Agent(AgentMethod::Logout),
        ] {
            if method.as_wire_name() == input {
                return Ok(method);
            }
        }
        for method in [
            Self::Client(ClientMethod::SessionRequestPermission),
            Self::Client(ClientMethod::SessionUpdate),
            Self::Client(ClientMethod::FsWriteTextFile),
            Self::Client(ClientMethod::FsReadTextFile),
            Self::Client(ClientMethod::TerminalCreate),
            Self::Client(ClientMethod::TerminalOutput),
            Self::Client(ClientMethod::TerminalRelease),
            Self::Client(ClientMethod::TerminalWaitForExit),
            Self::Client(ClientMethod::TerminalKill),
            Self::Client(ClientMethod::ElicitationCreate),
            Self::Client(ClientMethod::ElicitationComplete),
        ] {
            if method.as_wire_name() == input {
                return Ok(method);
            }
        }
        Err(AcpMethodParseError {
            input: input.to_owned(),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Every method named in `meta.json` round-trips
    /// through `as_wire_name` and `FromStr`. The wire
    /// names are the ones the spec publishes; the
    /// dispatcher matches on the typed enum, not on
    /// the string, so a typo in the wire name would
    /// only show up here.
    #[test]
    fn every_meta_method_round_trips_through_wire_name() -> Result<(), AcpMethodParseError> {
        let cases: &[(AcpMethod, &str)] = &[
            (AcpMethod::Agent(AgentMethod::Initialize), "initialize"),
            (AcpMethod::Agent(AgentMethod::Authenticate), "authenticate"),
            (AcpMethod::Agent(AgentMethod::SessionNew), "session/new"),
            (AcpMethod::Agent(AgentMethod::SessionLoad), "session/load"),
            (
                AcpMethod::Agent(AgentMethod::SessionSetMode),
                "session/set_mode",
            ),
            (
                AcpMethod::Agent(AgentMethod::SessionSetConfigOption),
                "session/set_config_option",
            ),
            (
                AcpMethod::Agent(AgentMethod::SessionPrompt),
                "session/prompt",
            ),
            (
                AcpMethod::Agent(AgentMethod::SessionCancel),
                "session/cancel",
            ),
            (AcpMethod::Agent(AgentMethod::SessionList), "session/list"),
            (
                AcpMethod::Agent(AgentMethod::SessionDelete),
                "session/delete",
            ),
            (
                AcpMethod::Agent(AgentMethod::SessionResume),
                "session/resume",
            ),
            (AcpMethod::Agent(AgentMethod::SessionClose), "session/close"),
            (AcpMethod::Agent(AgentMethod::Logout), "logout"),
            (
                AcpMethod::Client(ClientMethod::SessionRequestPermission),
                "session/request_permission",
            ),
            (
                AcpMethod::Client(ClientMethod::SessionUpdate),
                "session/update",
            ),
            (
                AcpMethod::Client(ClientMethod::FsWriteTextFile),
                "fs/write_text_file",
            ),
            (
                AcpMethod::Client(ClientMethod::FsReadTextFile),
                "fs/read_text_file",
            ),
            (
                AcpMethod::Client(ClientMethod::TerminalCreate),
                "terminal/create",
            ),
            (
                AcpMethod::Client(ClientMethod::TerminalOutput),
                "terminal/output",
            ),
            (
                AcpMethod::Client(ClientMethod::TerminalRelease),
                "terminal/release",
            ),
            (
                AcpMethod::Client(ClientMethod::TerminalWaitForExit),
                "terminal/wait_for_exit",
            ),
            (
                AcpMethod::Client(ClientMethod::TerminalKill),
                "terminal/kill",
            ),
            (
                AcpMethod::Client(ClientMethod::ElicitationCreate),
                "elicitation/create",
            ),
            (
                AcpMethod::Client(ClientMethod::ElicitationComplete),
                "elicitation/complete",
            ),
            (
                AcpMethod::Protocol(ProtocolMethod::CancelRequest),
                "$/cancel_request",
            ),
        ];
        assert_eq!(
            cases.len(),
            25,
            "every published ACP v1 method should appear"
        );
        for (method, expected_name) in cases {
            assert_eq!(method.as_wire_name(), *expected_name);
            let round_tripped: AcpMethod = expected_name.parse()?;
            assert_eq!(
                round_tripped, *method,
                "round-trip failed for {expected_name}"
            );
        }
        Ok(())
    }

    /// An unknown wire name is an error, not a silent
    /// fallback. A future method would either extend
    /// the enum or land in `Unknown { name }` if we
    /// ever add that variant; today, an unknown name
    /// is a wire violation.
    #[test]
    fn an_unknown_wire_name_is_an_error() {
        let Err(error) = "session/unicorn".parse::<AcpMethod>() else {
            return;
        };
        assert_eq!(error.input, "session/unicorn");
    }
}