protocol-droid 0.5.2

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
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
//! This module's tests.
//!
//! Out of the module so that reading `registration` means reading the types,
//! the ports, and the use case rather than the tests underneath all three.

use std::assert_matches;
use std::future::Future;
use std::sync::atomic::{AtomicU32, Ordering};
use std::task::{Context, Poll, Waker};

use super::{
    Available, ClientIdentity, ClientMetadata, ClientMetadataError, DynamicRegistrar,
    MetadataDocumentLoader, RegistrarError, RegistrationError, RegistrationPlan, acquire_client,
    plan_registration,
};
use crate::domain::ProtocolVersion;
use crate::domain::mcp::authorization::{AuthorizationModel, authorization_model};
use crate::domain::oauth::registration::{ClientId, ClientIdError, RegisteredClient};

const AS: &str = "https://auth.example.com";
const OTHER_AS: &str = "https://other-auth.example.com";
const URL: &str = "https://client.example.com/metadata.json";

/// The revisions whose authorization spec differs in a way this module
/// acts on. A bare `AuthorizationModel` would let a test pick a revision by
/// accident and assert something true of all of them.
const EARLY: ProtocolVersion = ProtocolVersion::V2024_11_05;
const DYNAMIC_ONLY: ProtocolVersion = ProtocolVersion::V2025_03_26;
const BOTH: ProtocolVersion = ProtocolVersion::V2025_11_25;
const DOCUMENT_FIRST: ProtocolVersion = ProtocolVersion::V2026_07_28;

fn model(version: ProtocolVersion) -> AuthorizationModel {
    authorization_model(version)
}

/// The error types these tests answer, collected so `?` works and a failure
/// prints the engine's own account of what it refused.
#[derive(Debug, thiserror::Error)]
enum Failure {
    #[error(transparent)]
    Id(#[from] ClientIdError),
    #[error(transparent)]
    Metadata(#[from] ClientMetadataError),
    #[error(transparent)]
    Registration(#[from] RegistrationError),
    #[error("a fake port pended, which none of them should")]
    Pended,
}

impl From<RegistrarError> for Failure {
    fn from(error: RegistrarError) -> Self {
        Self::Registration(RegistrationError::DynamicFailed(error))
    }
}

/// Drives a future that is expected never to pend, and says so when it
/// does.
///
/// `#[tokio::test]` is not available here: this module is gated on `oauth`,
/// and the tokio macros come in with `http`. Pulling a runtime in to test
/// fakes that return immediately would be a heavier dependency than the
/// thing under test. `Waker::noop` is correct for a future that resolves on
/// its first poll, and returning `None` on `Pending` is what makes that
/// assumption checkable rather than merely stated.
fn now<T>(future: impl Future<Output = T>) -> Option<T> {
    let mut future = std::pin::pin!(future);
    let waker = Waker::noop();
    match future.as_mut().poll(&mut Context::from_waker(waker)) {
        Poll::Ready(output) => Some(output),
        Poll::Pending => None,
    }
}

fn metadata() -> Result<ClientMetadata, Failure> {
    Ok(
        ClientMetadata::new(vec!["https://client.example.com/callback".to_owned()])?
            .with_client_name("Example"),
    )
}

fn document_identity() -> Result<ClientIdentity, Failure> {
    Ok(ClientIdentity::from_document(ClientId::metadata_url(URL)?))
}

/// A dynamic registrar that always succeeds, counting its calls so a test
/// can prove it was *not* reached.
struct Dynamic {
    calls: AtomicU32,
}

impl DynamicRegistrar for Dynamic {
    // Not `async`: this fake resolves on its first poll, so `async` here
    // would be an async block with no await point in it.
    fn register_dynamic(
        &self,
        issuer: &str,
        _metadata: &ClientMetadata,
    ) -> impl Future<Output = Result<RegisteredClient, RegistrarError>> + Send {
        self.calls.fetch_add(1, Ordering::Relaxed);
        std::future::ready(
            RegisteredClient::dynamic(issuer, ClientId::opaque("dynamic-1"), None)
                .map_err(Into::into),
        )
    }
}

/// A document loader that answers with whatever it was built to answer.
struct Loader {
    outcome: Result<(), RegistrarError>,
    calls: AtomicU32,
}

impl Loader {
    fn serving() -> Self {
        Self {
            outcome: Ok(()),
            calls: AtomicU32::new(0),
        }
    }

    fn failing(error: RegistrarError) -> Self {
        Self {
            outcome: Err(error),
            calls: AtomicU32::new(0),
        }
    }
}

impl MetadataDocumentLoader for Loader {
    fn fetch_metadata_document(
        &self,
        issuer: &str,
        client_id: &ClientId,
    ) -> impl Future<Output = Result<RegisteredClient, RegistrarError>> + Send {
        self.calls.fetch_add(1, Ordering::Relaxed);
        std::future::ready(match &self.outcome {
            Ok(()) => Ok(RegisteredClient::from_metadata_document(
                issuer,
                client_id.clone(),
            )),
            Err(failure) => Err(failure.clone()),
        })
    }
}

// --- what each revision allows -----------------------------------------

/// `2024-11-05` has no authorization entry at all, so a client that
/// registers here is doing something the revision never asked for. Proceeding
/// quietly is how that goes unnoticed.
#[test]
fn a_revision_with_no_authorization_framework_refuses_to_register() -> Result<(), Failure> {
    assert_matches!(
        plan_registration(
            &model(EARLY),
            AS,
            &ClientIdentity::from_metadata(metadata()?),
            None,
            Available::both(),
        ),
        Err(RegistrationError::NoMechanism)
    );
    Ok(())
}

/// `2025-03-26` names RFC 7591 and nothing else. A client holding only a
/// document has nothing it can use, and saying so beats silently
/// registering a second identity.
#[test]
fn a_document_is_not_usable_where_the_revision_never_named_one() -> Result<(), Failure> {
    assert_matches!(
        plan_registration(
            &model(DYNAMIC_ONLY),
            AS,
            &document_identity()?,
            None,
            Available::both(),
        ),
        Err(RegistrationError::NoUsableMechanism)
    );
    Ok(())
}

#[test]
fn a_revision_naming_only_dynamic_registers_dynamically() -> Result<(), Failure> {
    let plan = plan_registration(
        &model(DYNAMIC_ONLY),
        AS,
        &ClientIdentity::from_metadata(metadata()?),
        None,
        Available::dynamic_only(),
    )?;

    assert_matches!(plan, RegistrationPlan::RegisterDynamic { .. });
    Ok(())
}

/// The ordering the whole module exists for. `2026-07-28` changelog §4
/// deprecates RFC 7591 "in favor of" documents, so a client with both must
/// lead with the document — a client that leads with the deprecated
/// mechanism against a server supporting both is provoking a rejection.
#[test]
fn a_document_is_preferred_even_when_the_client_can_also_register() -> Result<(), Failure> {
    let identity = document_identity()?.with_metadata_fallback(metadata()?);

    let plan = plan_registration(
        &model(DOCUMENT_FIRST),
        AS,
        &identity,
        None,
        Available::both(),
    )?;

    assert_matches!(plan, RegistrationPlan::FetchDocument { .. });
    Ok(())
}

/// `2025-11-25` changelog §8 recommends documents without deprecating RFC
/// 7591, so the same ordering applies a revision earlier.
#[test]
fn the_preference_predates_the_deprecation() -> Result<(), Failure> {
    let identity = document_identity()?.with_metadata_fallback(metadata()?);

    let plan = plan_registration(&model(BOTH), AS, &identity, None, Available::both())?;

    assert_matches!(plan, RegistrationPlan::FetchDocument { .. });
    Ok(())
}

/// A deployment that wired no document loader must not be handed a plan to
/// fetch one.
#[test]
fn a_document_needs_a_loader_to_fetch_it_with() -> Result<(), Failure> {
    let identity = document_identity()?;

    assert_matches!(
        plan_registration(
            &model(DOCUMENT_FIRST),
            AS,
            &identity,
            None,
            Available::dynamic_only(),
        ),
        Err(RegistrationError::NoUsableMechanism)
    );
    Ok(())
}

// --- the MUST that has to be caught before anything is sent ------------

/// `2026-07-28` §8 makes `application_type` a MUST for dynamic
/// registration. Catching it at plan time is the difference between a clear
/// refusal and a registered client the server will not honour.
#[test]
fn the_required_application_type_is_refused_before_anything_is_sent() -> Result<(), Failure> {
    assert_matches!(
        plan_registration(
            &model(DOCUMENT_FIRST),
            AS,
            &ClientIdentity::from_metadata(metadata()?),
            None,
            Available::dynamic_only(),
        ),
        Err(RegistrationError::ApplicationTypeRequired)
    );
    Ok(())
}

#[test]
fn supplying_the_application_type_satisfies_the_requirement() -> Result<(), Failure> {
    let identity = ClientIdentity::from_metadata(metadata()?.with_application_type("native"));

    assert_matches!(
        plan_registration(
            &model(DOCUMENT_FIRST),
            AS,
            &identity,
            None,
            Available::dynamic_only(),
        ),
        Ok(RegistrationPlan::RegisterDynamic { .. })
    );
    Ok(())
}

/// `2026-07-28` deprecates RFC 7591 rather than removing it, so a server
/// implementing only RFC 7591 is not misconfigured and a client must still
/// be able to use it.
#[test]
fn the_deprecated_mechanism_is_still_available() {
    assert!(
        model(DOCUMENT_FIRST).client_registration.supports_dynamic(),
        "2026-07-28 still permits RFC 7591"
    );
}

// --- stored credentials ------------------------------------------------

#[test]
fn a_stored_credential_from_this_issuer_is_reused() -> Result<(), Failure> {
    let stored = RegisteredClient::dynamic(AS, ClientId::opaque("stored"), None)?;

    let plan = plan_registration(
        &model(DOCUMENT_FIRST),
        AS,
        &document_identity()?,
        Some(&stored),
        Available::both(),
    )?;

    assert_matches!(plan, RegistrationPlan::Reuse(ref client) if client == &stored);
    Ok(())
}

/// `2026-07-28` binds a credential to its issuer so a credential minted by
/// one server cannot be presented to another. A stored credential from
/// elsewhere has to be refused, not quietly reused.
#[test]
fn a_credential_from_another_issuer_is_refused_where_binding_is_required() -> Result<(), Failure> {
    let stored = RegisteredClient::dynamic(OTHER_AS, ClientId::opaque("stored"), None)?;

    assert_matches!(
        plan_registration(
            &model(DOCUMENT_FIRST),
            AS,
            &document_identity()?,
            Some(&stored),
            Available::both(),
        ),
        Err(RegistrationError::IssuerBound)
    );
    Ok(())
}

/// Older revisions do not require binding, so the same stored credential
/// is reused. It still carries the issuer it was issued by, so nothing
/// downstream can mistake it for the current server's.
#[test]
fn a_credential_from_another_issuer_is_reused_where_binding_is_not_required() -> Result<(), Failure>
{
    let stored = RegisteredClient::dynamic(OTHER_AS, ClientId::opaque("stored"), None)?;

    let plan = plan_registration(
        &model(DYNAMIC_ONLY),
        AS,
        &ClientIdentity::from_metadata(metadata()?),
        Some(&stored),
        Available::dynamic_only(),
    )?;

    assert_matches!(plan, RegistrationPlan::Reuse(ref client) if client.is_issued_by(OTHER_AS));
    Ok(())
}

// --- executing the plan ------------------------------------------------

#[test]
fn a_served_document_is_used_without_registering() -> Result<(), Failure> {
    let dynamic = Dynamic {
        calls: AtomicU32::new(0),
    };
    let loader = Loader::serving();

    let client = match now(acquire_client(
        &model(DOCUMENT_FIRST),
        AS,
        &document_identity()?,
        None,
        Some(&dynamic),
        Some(&loader),
    )) {
        Some(result) => result?,
        None => return Err(Failure::Pended),
    };

    assert_eq!(client.client_id().as_str(), URL);
    assert_eq!(loader.calls.load(Ordering::Relaxed), 1);
    assert_eq!(
        dynamic.calls.load(Ordering::Relaxed),
        0,
        "the document path registered anyway"
    );
    Ok(())
}

/// `2026-07-28` keeps RFC 7591 for servers that do not implement documents,
/// and a document that does not resolve is exactly that server.
#[test]
fn an_unreachable_document_falls_back_to_dynamic_registration() -> Result<(), Failure> {
    let dynamic = Dynamic {
        calls: AtomicU32::new(0),
    };
    let loader = Loader::failing(RegistrarError::Unavailable("connection refused".into()));
    let identity = document_identity()?.with_metadata_fallback(metadata()?);

    let client = match now(acquire_client(
        &model(DOCUMENT_FIRST),
        AS,
        &identity,
        None,
        Some(&dynamic),
        Some(&loader),
    )) {
        Some(result) => result?,
        None => return Err(Failure::Pended),
    };

    assert_eq!(client.client_id().as_str(), "dynamic-1");
    assert_eq!(dynamic.calls.load(Ordering::Relaxed), 1);
    Ok(())
}

/// The distinction that matters. A document that could not be *fetched* is a
/// server without documents; a document that was fetched and does not claim
/// the URL it came from is a forgery, and falling back from it would let an
/// impersonated client quietly register a fresh identity instead of
/// surfacing the problem.
#[test]
fn a_document_that_is_present_but_wrong_never_falls_back() -> Result<(), Failure> {
    let dynamic = Dynamic {
        calls: AtomicU32::new(0),
    };
    let loader = Loader::failing(RegistrarError::Document(ClientIdError::DocumentMismatch));
    let identity = document_identity()?.with_metadata_fallback(metadata()?);

    let Some(outcome) = now(acquire_client(
        &model(DOCUMENT_FIRST),
        AS,
        &identity,
        None,
        Some(&dynamic),
        Some(&loader),
    )) else {
        return Err(Failure::Pended);
    };

    assert_matches!(outcome, Err(RegistrationError::DocumentUnavailable(_)));
    assert_eq!(
        dynamic.calls.load(Ordering::Relaxed),
        0,
        "a forged document silently became a fresh registration"
    );
    Ok(())
}

/// With no metadata to fall back on, an unreachable document is just a
/// failure.
#[test]
fn an_unreachable_document_with_no_fallback_is_an_error() -> Result<(), Failure> {
    let dynamic = Dynamic {
        calls: AtomicU32::new(0),
    };
    let loader = Loader::failing(RegistrarError::Unavailable("connection refused".into()));

    let Some(outcome) = now(acquire_client(
        &model(DOCUMENT_FIRST),
        AS,
        &document_identity()?,
        None,
        Some(&dynamic),
        Some(&loader),
    )) else {
        return Err(Failure::Pended);
    };

    assert_matches!(outcome, Err(RegistrationError::DocumentUnavailable(_)));
    assert_eq!(dynamic.calls.load(Ordering::Relaxed), 0);
    Ok(())
}

#[test]
fn a_stored_credential_is_returned_without_any_port_being_called() -> Result<(), Failure> {
    let dynamic = Dynamic {
        calls: AtomicU32::new(0),
    };
    let loader = Loader::serving();
    let stored = RegisteredClient::dynamic(AS, ClientId::opaque("stored"), None)?;

    let client = match now(acquire_client(
        &model(DOCUMENT_FIRST),
        AS,
        &document_identity()?,
        Some(&stored),
        Some(&dynamic),
        Some(&loader),
    )) {
        Some(result) => result?,
        None => return Err(Failure::Pended),
    };

    assert_eq!(&client, &stored);
    assert_eq!(loader.calls.load(Ordering::Relaxed), 0);
    assert_eq!(dynamic.calls.load(Ordering::Relaxed), 0);
    Ok(())
}

// --- the metadata itself ----------------------------------------------

/// RFC 7591 §2 makes `redirect_uris` the one required member.
#[test]
fn metadata_without_redirect_uris_is_refused() {
    assert!(ClientMetadata::new(Vec::new()).is_err());
}

/// A bare path is what a half-filled configuration produces, and a server
/// would reject it.
#[test]
fn metadata_with_a_relative_redirect_uri_is_refused() {
    assert!(ClientMetadata::new(vec!["/callback".to_owned()]).is_err());
}

#[test]
fn a_loopback_redirect_is_allowed() -> Result<(), Failure> {
    // RFC 8252 §7.3 makes the native-app loopback redirect legal, so an
    // https-only rule would refuse a legitimate client.
    let metadata = ClientMetadata::new(vec!["http://127.0.0.1:8080/callback".to_owned()])?;

    assert_eq!(metadata.redirect_uris().len(), 1);
    Ok(())
}

/// The named fields are the ones this crate gates on, so the passthrough
/// must not be able to set one — otherwise a document could claim redirect
/// URIs no constructor ever checked.
#[test]
fn the_passthrough_cannot_overwrite_a_modelled_field() -> Result<(), Failure> {
    let metadata = metadata()?;

    assert_matches!(
        metadata.with_field("redirect_uris", serde_json::json!(["https://evil.example.com"])),
        Err(ClientMetadataError::ReservedField(ref name)) if name == "redirect_uris"
    );
    Ok(())
}

/// `2025-11-25` and later name `client_uri`, `icon_uri` and `title`. A
/// client that publishes one and has it dropped at a struct boundary has
/// registered something other than what it meant to.
#[test]
fn an_unmodelled_field_reaches_the_wire() -> Result<(), Failure> {
    let metadata = metadata()?
        .with_field("client_uri", "https://client.example.com")?
        .with_field("title", "Example Client")?;

    let document = serde_json::to_value(&metadata)
        .map_err(|_| Failure::Registration(RegistrationError::NoMechanism))?;

    assert_eq!(
        document.get("client_uri"),
        Some(&serde_json::json!("https://client.example.com"))
    );
    assert_eq!(
        document.get("title"),
        Some(&serde_json::json!("Example Client"))
    );
    Ok(())
}

/// The named fields and the passthrough coexist on the wire without either
/// shadowing the other.
#[test]
fn modelled_and_extra_fields_coexist() -> Result<(), Failure> {
    let metadata = metadata()?
        .with_application_type("native")
        .with_field("title", "Example")?;

    let document = serde_json::to_value(&metadata)
        .map_err(|_| Failure::Registration(RegistrationError::NoMechanism))?;

    assert_eq!(
        document.get("application_type"),
        Some(&serde_json::json!("native"))
    );
    assert_eq!(
        document.get("client_name"),
        Some(&serde_json::json!("Example"))
    );
    assert_eq!(document.get("title"), Some(&serde_json::json!("Example")));
    assert_eq!(
        document.get("redirect_uris"),
        Some(&serde_json::json!(["https://client.example.com/callback"]))
    );
    Ok(())
}