protocol-droid 0.3.0

A Model Context Protocol toolkit that speaks every published spec revision, with one canonical IR behind many per-revision codecs.
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
//! A server that pushes while the client is idle.
//!
//! A Streamable HTTP server may send a message at any time on the stream a
//! `GET` opens — `notifications/tools/list_changed`, a sampling request, a log
//! line. The question here is whether a client that has said nothing since its
//! handshake is still told.
//!
//! Nothing here uses a double. The upstream is a real [`HttpClient`] against a
//! real server over a real socket, because the behaviour under test lives in
//! the client's transport and in the loop that waits on it — and a hand-written
//! peer proved the bridge's routing worked while saying nothing about either.
//!
//! ## Why there are two push tests
//!
//! One test cannot tell "the push never left the server" apart from "the push
//! arrived and nobody had a stream to put it on". Those are different bugs with
//! different fixes, so the upstream here is *held* until the test says the
//! client is reading ([`a_push_reaches_a_client_that_has_said_nothing`]) and is
//! *not* held in the second ([`a_push_that_lands_before_the_client_reads_is_not_lost`]),
//! which is where the distinction actually bites.

#![cfg(feature = "http")]

use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;

use axum::Router;
use axum::body::Body;
use axum::extract::State;
use axum::response::Response;
use axum::routing::post;
use serde_json::{Value, json};
use tokio::sync::Notify;

use protocol_droid::adapters::primary::transport::http::{
    HttpBridge, SecurityHeaders, ServerConfig, SessionStore,
};
use protocol_droid::adapters::secondary::codec::result::WireToolResults;
use protocol_droid::adapters::secondary::transport::http::{HttpClient, HttpClientConfig};
use protocol_droid::application::bridge::{
    BridgePeer, Identity, OpenedUpstream, Upstream, UpstreamError,
};
use protocol_droid::application::client::{ClientConfig, handshake};
use protocol_droid::application::session::IdSource;
use protocol_droid::domain::session::SessionId;
use protocol_droid::domain::{DegradePolicy, Leg, SupportedVersions, TransportKind};

/// A deterministic id source, so a session id in a failure is reproducible.
struct Counting(std::sync::atomic::AtomicU64);

impl IdSource for Counting {
    fn next_session_id(&self) -> SessionId {
        SessionId::new(self.0.fetch_add(1, Ordering::SeqCst) + 1)
    }
}

/// Hex, because `SessionId` is hex: a server answering anything else is telling
/// the client it has no session worth naming.
const UPSTREAM_SESSION: &str = "0f0e0d0c0b0a0908";

/// How long a test waits for a frame before calling the bridge broken.
///
/// Short on purpose: the bridge relays on its own task, so a frame that is
/// going to arrive has arrived within milliseconds. A long budget only turns a
/// failing assertion into a slow build for everyone waiting on it.
const PATIENCE: Duration = Duration::from_secs(5);

/// Whether the bridge opened the upstream's standing stream, within `PATIENCE`.
///
/// A poll rather than a single read. The bridge opens that stream from a task
/// spawned when the client's handshake was answered, so "the handshake returned"
/// and "the GET left" are two different moments with nothing ordering them —
/// reading the counter once would be a race that passes most of the time.
async fn wait_for_open(state: &Server) -> bool {
    let deadline = tokio::time::Instant::now() + PATIENCE;
    loop {
        if state.opened.load(Ordering::SeqCst) > 0 {
            return true;
        }
        if tokio::time::Instant::now() >= deadline {
            return false;
        }
        tokio::time::sleep(Duration::from_millis(10)).await;
    }
}

/// How the upstream server should behave about when it pushes.
struct Server {
    /// How many times the standing stream was opened. The direct evidence of
    /// whether the bridge asked to listen at all, which is the first thing to
    /// establish when a push goes missing.
    opened: AtomicUsize,
    /// When set, the stream waits for this before answering.
    release: Option<Arc<Notify>>,
}

async fn upstream_post(body: axum::body::Bytes) -> Response {
    let Ok(Value::Object(request)) = serde_json::from_slice::<Value>(&body) else {
        return Response::builder()
            .status(400)
            .body(Body::empty())
            .unwrap_or_default();
    };
    let id = request.get("id").cloned().unwrap_or(Value::Null);
    let result = match request.get("method").and_then(Value::as_str) {
        Some("initialize") => json!({
            "protocolVersion": "2025-11-25",
            "capabilities": {"tools": {}},
            "serverInfo": {"name": "far", "version": "1"}
        }),
        Some("tools/list") => {
            json!({"tools": [{"name": "echo", "inputSchema": {"type": "object"}}]})
        }
        _ => Value::Null,
    };
    let reply = json!({"jsonrpc": "2.0", "id": id, "result": result});
    // A stream, because that is the shape the specification prefers and the one
    // a client has to be able to read — and the header has to say so, or the
    // client is right to refuse to read SSE out of a body labelled JSON.
    Response::builder()
        .header("content-type", "text/event-stream")
        .header("mcp-session-id", UPSTREAM_SESSION)
        .body(Body::from(format!("event: message\ndata: {reply}\n\n")))
        .unwrap_or_default()
}

/// The standing stream: one unsolicited notification, then the peer goes quiet.
async fn upstream_get(State(state): axum::extract::State<Arc<Server>>) -> Response {
    state.opened.fetch_add(1, Ordering::SeqCst);
    if let Some(release) = &state.release {
        release.notified().await;
    }
    let notice = json!({
        "jsonrpc": "2.0",
        "method": "notifications/message",
        "params": {"level": "info", "data": "pushed while you were quiet"}
    });
    let frames = format!("event: message\ndata: {notice}\n\n");
    Response::builder()
        .header("content-type", "text/event-stream")
        .body(Body::from(frames))
        .unwrap_or_default()
}

async fn spawn_upstream(state: Arc<Server>) -> Result<String, Box<dyn std::error::Error>> {
    // One `route` call, with both methods chained onto it. Two calls at the
    // same path do not merge: the second replaces the first, so the POST route
    // would silently vanish and every request would get a 405 with an empty
    // body — which reads downstream as "the peer sent something that was not
    // JSON".
    let app = Router::new().route(
        "/mcp",
        post(upstream_post)
            .get(upstream_get)
            .with_state(Arc::clone(&state)),
    );
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?;
    let addr = listener.local_addr()?;
    tokio::spawn(async move {
        let _ = axum::serve(listener, app).await;
    });
    Ok(format!("http://{addr}/mcp"))
}

/// The production shape, spelled out: dial a real client, run the real
/// handshake, hand the bridge the leg and the peer together.
///
/// The same two steps the CLI's `ConfiguredUpstream` takes, because a test that
/// wired the bridge up some other way would be testing a bridge nobody runs.
struct HttpUpstream {
    url: String,
}

impl Upstream for HttpUpstream {
    async fn open(&self) -> Result<OpenedUpstream, UpstreamError> {
        let config = HttpClientConfig::new(&self.url).map_err(wire)?;
        let client = HttpClient::new(config).map_err(wire)?;
        let versions = SupportedVersions::all().map_err(wire)?;
        let speaking =
            ClientConfig::speaking(versions.as_slice().to_vec(), TransportKind::StreamableHttp)
                .map_err(wire)?;
        let session = handshake(&client, &speaking).await.map_err(wire)?;
        let leg = Leg::new(session.version, session.profile);
        Ok(OpenedUpstream::new(
            leg,
            Arc::new(client) as Arc<dyn BridgePeer>,
        ))
    }
}

fn wire(error: impl std::fmt::Display) -> UpstreamError {
    let text = error.to_string();
    // The reason a session failed to start is otherwise dropped by the
    // bridge's task, which leaves "the upstream could not open" and "the
    // upstream opened and said nothing" looking identical from the outside.
    UpstreamError::Unreachable(text)
}

/// A bridge in front of a real HTTP upstream, bound and serving.
async fn running_bridge(state: Arc<Server>) -> Result<String, Box<dyn std::error::Error>> {
    let url = spawn_upstream(Arc::clone(&state)).await?;
    let ids: Arc<dyn IdSource + Send + Sync> =
        Arc::new(Counting(std::sync::atomic::AtomicU64::new(0)));
    let sessions: Arc<SessionStore<_>> = Arc::new(SessionStore::new(ids));
    let server = ServerConfig::new()?.with_security(SecurityHeaders::default());

    let bridge = HttpBridge::new(
        server,
        sessions,
        HttpUpstream { url },
        Identity::new("push-test", "0.0.0"),
        DegradePolicy::default(),
        WireToolResults,
    )?;
    let router = bridge.into_router()?;
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?;
    let addr = listener.local_addr()?;
    tokio::spawn(async move {
        let _ = axum::serve(listener, router).await;
    });
    Ok(format!("http://{addr}/mcp"))
}

/// Opens the client's event stream and reads from it.
///
/// Incremental, because the stream is not supposed to end: a downstream client
/// holds it for the life of its session, so reading it to completion waits for
/// a close that never comes. Chunks are decoded as they arrive, since a frame
/// can straddle two of them.
struct Reading {
    response: reqwest::Response,
    pending: String,
}

impl Reading {
    async fn open(
        client: &reqwest::Client,
        url: &str,
        session: &str,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        let response = client
            .get(url)
            .header("accept", "text/event-stream")
            .header("mcp-session-id", session)
            .send()
            .await?;
        if !response.status().is_success() {
            return Err(format!("the stream answered {}", response.status()).into());
        }
        Ok(Self {
            response,
            pending: String::new(),
        })
    }

    /// Every frame that arrives within `budget`, looking for `needle`.
    ///
    /// One deadline for the whole scan rather than one per frame. A per-frame
    /// deadline multiplied by the number of frames is a test that takes most of
    /// a minute to fail, which is long enough that nobody re-runs it and short
    /// enough that everyone assumes it is slow rather than stuck.
    ///
    /// Returns every frame it saw — a failure has to show what *did* arrive, or
    /// "it did not work" and "it never got that far" look the same.
    async fn await_frame(
        &mut self,
        needle: &str,
        budget: Duration,
    ) -> Result<Option<Vec<String>>, Box<dyn std::error::Error>> {
        let deadline = tokio::time::Instant::now() + budget;
        let mut seen: Vec<String> = Vec::new();
        loop {
            let arrived = tokio::time::timeout_at(deadline, self.response.chunk()).await;
            let Ok(Ok(Some(bytes))) = arrived else {
                return Ok(if seen.iter().any(|f| f.contains(needle)) {
                    Some(seen)
                } else {
                    None
                });
            };
            self.pending.push_str(&String::from_utf8_lossy(&bytes));
            let ready: Vec<String> = self
                .pending
                .lines()
                .filter_map(|line| line.strip_prefix("data:"))
                .map(|data| data.trim().to_owned())
                .collect();
            if !ready.is_empty() {
                seen.extend(ready);
                // Only the tail can hold half a line; keeping the rest would
                // re-scan every frame the stream has already delivered.
                let cut = self.pending.rfind('\n').map_or(0, |at| at + 1);
                self.pending = self.pending.split_off(cut);
            }
        }
    }
}

fn initialize(id: i64) -> String {
    json!({
        "jsonrpc": "2.0",
        "id": id,
        "method": "initialize",
        "params": {
            "protocolVersion": "2025-11-25",
            "capabilities": {},
            "clientInfo": {"name": "t", "version": "1"}
        }
    })
    .to_string()
}

/// Completes the handshake and hands back the session and an open stream.
async fn connected(
    url: &str,
) -> Result<(reqwest::Client, String, Reading), Box<dyn std::error::Error>> {
    let client = reqwest::Client::new();
    let opened = client
        .post(url)
        .header("content-type", "application/json")
        .header("accept", "application/json, text/event-stream")
        .body(initialize(1))
        .send()
        .await?;
    if !opened.status().is_success() {
        return Err(format!("the handshake answered {}", opened.status()).into());
    }
    let session = opened
        .headers()
        .get("mcp-session-id")
        .and_then(|value| value.to_str().ok())
        .ok_or("no session id came back")?
        .to_owned();
    let reading = Reading::open(&client, url, &session).await?;
    Ok((client, session, reading))
}

#[tokio::test]
async fn a_push_reaches_a_client_that_has_said_nothing() -> Result<(), Box<dyn std::error::Error>> {
    let release = Arc::new(Notify::new());
    let state = Arc::new(Server {
        opened: AtomicUsize::new(0),
        release: Some(Arc::clone(&release)),
    });
    let url = running_bridge(Arc::clone(&state)).await?;

    // Handshake, then open the stream — and say nothing else. The client is now
    // idle, which is the whole question: if delivery needs a client frame to
    // trigger it, this is the assertion that catches it.
    let (_client, _session, mut reading) = connected(&url).await?;

    // Only now does the server push, so nothing here is a race.
    release.notify_one();

    // Waited for rather than read once. The bridge opens the upstream's stream
    // from a spawned task, so "the handshake returned" and "the GET left" are
    // two different moments and nothing orders them. Reading the counter once
    // would make this a race that passes most of the time.
    if !wait_for_open(&state).await {
        return Err("the bridge never opened the upstream's standing stream".into());
    }

    // Not "the next frame": the handshake reply is already on this stream, and
    // the notification the server volunteered upstream is a second frame behind
    // it. Reading one frame and asserting on it would be asserting on the
    // handshake however the bridge behaved.
    let _ = reading
        .await_frame("notifications/message", PATIENCE)
        .await?
        .ok_or("no frame carried it")?;
    Ok(())
}

#[tokio::test]
async fn a_push_that_lands_before_the_client_reads_is_not_lost()
-> Result<(), Box<dyn std::error::Error>> {
    let state = Arc::new(Server {
        opened: AtomicUsize::new(0),
        release: None,
    });
    let url = running_bridge(Arc::clone(&state)).await?;

    let client = reqwest::Client::new();
    let opened = client
        .post(&url)
        .header("content-type", "application/json")
        .header("accept", "application/json, text/event-stream")
        .body(initialize(1))
        .send()
        .await?;
    let session = opened
        .headers()
        .get("mcp-session-id")
        .and_then(|value| value.to_str().ok())
        .ok_or("no session id came back")?
        .to_owned();

    // The push is not held back, so it very probably lands before the `GET`
    // below registers a stream. A client opening its stream slightly late is
    // normal, not exotic, and a frame written to a sink that did not exist yet
    // is exactly the way this would be lost.
    tokio::time::sleep(Duration::from_millis(150)).await;

    if !wait_for_open(&state).await {
        return Err("the bridge never opened the upstream's standing stream".into());
    }

    let mut reading = Reading::open(&client, &url, &session).await?;
    reading
        .await_frame("notifications/message", PATIENCE)
        .await?
        .ok_or("no frame carried the notification")?;
    Ok(())
}

#[tokio::test]
async fn a_request_still_crosses_to_the_upstream() -> Result<(), Box<dyn std::error::Error>> {
    // The push path is only worth having if the ordinary path still works, and
    // changing how the loop waits is exactly the kind of thing that can cost it
    // a request.
    let state = Arc::new(Server {
        opened: AtomicUsize::new(0),
        release: None,
    });
    let url = running_bridge(Arc::clone(&state)).await?;
    let (client, session, mut reading) = connected(&url).await?;

    client
        .post(&url)
        .header("content-type", "application/json")
        .header("accept", "application/json, text/event-stream")
        .header("mcp-session-id", &session)
        .body(json!({"jsonrpc": "2.0", "id": 2, "method": "tools/list", "params": {}}).to_string())
        .send()
        .await?;

    // The upstream volunteers a notification, so the reply may not be first.
    reading
        .await_frame("\"echo\"", PATIENCE)
        .await?
        .ok_or("no frame carried the tool list")?;
    Ok(())
}