deribit-mcp 1.0.0

MCP (Model Context Protocol) server for Deribit trading platform
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
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
//! Resource registry, `deribit://` URI parsing, and read dispatch.
//!
//! Resources are partitioned by lifetime:
//!
//! - [`static_`] — refresh-on-read resources backed by `deribit-http`.
//! - [`live`] — WebSocket-backed subscribable resources, populated in
//!   v0.3 per ADR-0006.
//!
//! v0.1-07 shipped the URI parser, catalogue, and dispatch surface.
//! v0.1-12 wired the two static reads:
//! `deribit://currencies` → `static_::read_currencies`,
//! `deribit://instruments/{currency}` → `static_::read_instruments`.
//! v0.3-02 wires `deribit://book/{instrument}` through the
//! [`LiveRegistry`] when a [`SubscriptionProvider`] is attached.
//! `deribit://ticker/{instrument}` (v0.3-03) and
//! `deribit://trades/{instrument}` (v0.3-04) are accepted by the
//! parser but `read()` still returns
//! [`AdapterError::Internal`] until those issues land. Without a
//! `SubscriptionProvider`, even `Book` falls back to
//! `AdapterError::Internal { reason: "live subscription provider
//! not configured" }`.

use std::sync::Arc;
use std::time::Duration;

use rmcp::model::{Annotated, RawResource, RawResourceTemplate, Resource, ResourceTemplate};

use crate::context::AdapterContext;
use crate::error::AdapterError;

pub mod live;
pub mod static_;

use live::{BookSnapshot, LiveRegistry, SubscriptionProvider, TickerSnapshot, TradeUpdate};

/// Strongly-typed `deribit://` URI variants.
///
/// The parser accepts every documented template; serving lives
/// behind [`ResourceRegistry::read`]. As of v0.3-02 `Book` is
/// served through the [`LiveRegistry`]; the remaining live URIs
/// (`Ticker`, `Trades`) still return [`AdapterError::Internal`]
/// until v0.3-03 / v0.3-04 wire them.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ResourceUri {
    /// `deribit://currencies` — the currency catalogue (static).
    Currencies,
    /// `deribit://instruments/{currency}` — instruments for a currency
    /// (static).
    Instruments {
        /// Currency symbol, upper-case (`BTC`, `ETH`, …).
        currency: String,
    },
    /// `deribit://book/{instrument}` — order book (live, v0.3+).
    Book {
        /// Instrument name (`BTC-PERPETUAL`, …).
        instrument: String,
    },
    /// `deribit://ticker/{instrument}` — ticker (live, v0.3+).
    Ticker {
        /// Instrument name.
        instrument: String,
    },
    /// `deribit://trades/{instrument}` — last trades (live, v0.3+).
    Trades {
        /// Instrument name.
        instrument: String,
    },
}

impl ResourceUri {
    /// Render back to the canonical `deribit://...` string form.
    #[must_use]
    pub fn to_uri(&self) -> String {
        match self {
            Self::Currencies => "deribit://currencies".to_string(),
            Self::Instruments { currency } => format!("deribit://instruments/{currency}"),
            Self::Book { instrument } => format!("deribit://book/{instrument}"),
            Self::Ticker { instrument } => format!("deribit://ticker/{instrument}"),
            Self::Trades { instrument } => format!("deribit://trades/{instrument}"),
        }
    }
}

/// `deribit://` URI scheme.
const SCHEME: &str = "deribit://";

/// Parse a `deribit://...` URI into [`ResourceUri`].
///
/// # Errors
///
/// Returns [`AdapterError::Validation`] for any input that does not
/// match a documented template.
pub fn parse_resource_uri(s: &str) -> Result<ResourceUri, AdapterError> {
    let rest = s
        .strip_prefix(SCHEME)
        .ok_or_else(|| AdapterError::validation("uri", format!("not a `{SCHEME}` URI: {s}")))?;
    if rest.is_empty() {
        return Err(AdapterError::validation("uri", "empty resource path"));
    }
    let mut segments = rest.splitn(2, '/');
    let head = segments
        .next()
        .ok_or_else(|| AdapterError::validation("uri", "missing resource head"))?;
    // Treat a trailing-slash tail (`deribit://instruments/`) as
    // "missing", not as an empty currency/instrument segment, so the
    // error reported is the documented `field: "uri"` shape.
    let tail = segments.next().filter(|s| !s.is_empty());

    match (head, tail) {
        ("currencies", None) => Ok(ResourceUri::Currencies),
        ("currencies", Some(_)) => Err(AdapterError::validation(
            "uri",
            "`deribit://currencies` takes no path",
        )),
        ("instruments", Some(currency)) => {
            let currency = parse_currency(currency)?;
            Ok(ResourceUri::Instruments { currency })
        }
        ("instruments", None) => Err(AdapterError::validation(
            "uri",
            "`deribit://instruments/{currency}` requires a currency",
        )),
        ("book", Some(instrument)) => {
            let instrument = parse_instrument_name(instrument)?;
            Ok(ResourceUri::Book { instrument })
        }
        ("ticker", Some(instrument)) => {
            let instrument = parse_instrument_name(instrument)?;
            Ok(ResourceUri::Ticker { instrument })
        }
        ("trades", Some(instrument)) => {
            let instrument = parse_instrument_name(instrument)?;
            Ok(ResourceUri::Trades { instrument })
        }
        ("book" | "ticker" | "trades", None) => Err(AdapterError::validation(
            "uri",
            format!("`deribit://{head}/{{instrument}}` requires an instrument"),
        )),
        (other, _) => Err(AdapterError::validation(
            "uri",
            format!("unknown resource head: `{other}`"),
        )),
    }
}

/// Validate a Deribit currency segment (`BTC`, `ETH`, …).
///
/// Deribit currency symbols are short ASCII upper-case identifiers.
/// We accept 1..=8 chars of `[A-Z0-9_]` to match what the upstream
/// `deribit-http` crate sees in practice; the upstream call enforces
/// the canonical set.
fn parse_currency(s: &str) -> Result<String, AdapterError> {
    if s.is_empty() || s.len() > 8 {
        return Err(AdapterError::validation(
            "currency",
            format!("expected 1..=8 chars, got {} for `{s}`", s.len()),
        ));
    }
    if !s
        .chars()
        .all(|c| c.is_ascii_uppercase() || c.is_ascii_digit() || c == '_')
    {
        return Err(AdapterError::validation(
            "currency",
            format!("expected `[A-Z0-9_]`, got `{s}`"),
        ));
    }
    Ok(s.to_string())
}

/// Validate an instrument name segment (`BTC-PERPETUAL`,
/// `BTC-31MAY24-50000-C`, …).
///
/// Deribit instrument names are dash-separated ASCII upper-case tokens.
/// Light-touch validation: 1..=64 chars of `[A-Z0-9_-]`. The upstream
/// HTTP / WebSocket call enforces semantic shape.
fn parse_instrument_name(s: &str) -> Result<String, AdapterError> {
    if s.is_empty() || s.len() > 64 {
        return Err(AdapterError::validation(
            "instrument",
            format!("expected 1..=64 chars, got {} for `{s}`", s.len()),
        ));
    }
    if !s
        .chars()
        .all(|c| c.is_ascii_uppercase() || c.is_ascii_digit() || c == '-' || c == '_')
    {
        return Err(AdapterError::validation(
            "instrument",
            format!("expected `[A-Z0-9_-]`, got `{s}`"),
        ));
    }
    Ok(s.to_string())
}

/// Snapshot of `resources/list` and `resources/templates/list`
/// produced by the registry.
#[derive(Debug, Default, Clone)]
pub struct ResourceList {
    /// Concrete resources (e.g. `deribit://currencies`).
    pub resources: Vec<Resource>,
    /// URI templates (e.g. `deribit://book/{instrument}`).
    pub templates: Vec<ResourceTemplate>,
}

/// Body returned by [`ResourceRegistry::read`].
///
/// v0.1-07 ships only [`Self::Json`] (JSON payloads from
/// `deribit-http`). The variant set is closed; new transports add
/// new variants.
#[derive(Debug, Clone, PartialEq)]
pub enum ResourceContent {
    /// JSON body produced by an upstream HTTP read. The MIME type
    /// surfaced to MCP is `application/json`.
    Json(serde_json::Value),
}

/// Registry of MCP resources the server exposes.
#[derive(Clone)]
pub struct ResourceRegistry {
    list: ResourceList,
    live: LiveRegistry,
    /// Optional provider for live subscriptions. `None` until the
    /// upstream `deribit-websocket` provider is configured (default
    /// for v0.1 / anonymous contexts); reads on live URIs return
    /// `AdapterError::Internal` when missing.
    provider: Option<Arc<dyn SubscriptionProvider>>,
}

impl std::fmt::Debug for ResourceRegistry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ResourceRegistry")
            .field("list", &self.list)
            .field("live", &self.live)
            .field(
                "provider",
                &self.provider.as_ref().map(|_| "<dyn SubscriptionProvider>"),
            )
            .finish()
    }
}

impl Default for ResourceRegistry {
    fn default() -> Self {
        Self {
            list: ResourceList::default(),
            live: LiveRegistry::new(),
            provider: None,
        }
    }
}

/// Per-call deadline waiting for the first frame on a fresh
/// subscription. Bounded so a stalled upstream cannot turn a
/// `resources/read` into an unbounded await.
const FIRST_FRAME_TIMEOUT: Duration = Duration::from_secs(5);

impl ResourceRegistry {
    /// Construct an empty registry.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Replace the live-subscription provider. The real upstream
    /// `deribit-websocket` provider is wired by the binary
    /// startup path (v0.3-02 / -03 / -04); tests pass a stub
    /// implementation.
    #[must_use]
    pub fn with_subscription_provider(mut self, provider: Arc<dyn SubscriptionProvider>) -> Self {
        self.provider = Some(provider);
        self
    }

    /// Build the v0.1 catalogue.
    ///
    /// Concrete entries: `deribit://currencies`. The
    /// per-currency `deribit://instruments/{currency}` resources are
    /// only knowable after a live `get_currencies` call (resolved in
    /// v0.1-12); they live as a template until then.
    ///
    /// Templates: `deribit://instruments/{currency}`,
    /// `deribit://book/{instrument}`,
    /// `deribit://ticker/{instrument}`,
    /// `deribit://trades/{instrument}`. The last three are accepted
    /// by the parser but `read()` returns
    /// [`AdapterError::Validation`] until v0.3 wires the live
    /// transport.
    #[must_use]
    pub fn build() -> Self {
        let mut list = ResourceList::default();
        list.resources.push(make_resource(
            "deribit://currencies",
            "Deribit currency catalogue",
            "Static list of Deribit currency symbols and metadata.",
        ));
        list.templates.push(make_template(
            "deribit://instruments/{currency}",
            "Deribit instruments by currency",
            "Static list of instruments for a given currency.",
        ));
        list.templates.push(make_template(
            "deribit://book/{instrument}",
            "Deribit order book (live)",
            "Order book snapshots from the `book.<instrument>.raw` \
             channel. Read returns the latest decoded BookSnapshot \
             when a SubscriptionProvider is configured; otherwise \
             AdapterError::Internal.",
        ));
        list.templates.push(make_template(
            "deribit://ticker/{instrument}",
            "Deribit ticker (live)",
            "Throttled ticker snapshots from the \
             `ticker.<instrument>.100ms` channel. Read returns the \
             latest TickerSnapshot when a SubscriptionProvider is \
             configured; otherwise AdapterError::Internal.",
        ));
        list.templates.push(make_template(
            "deribit://trades/{instrument}",
            "Deribit last trades (live)",
            "Trade events from the `trades.<instrument>.raw` channel. \
             Read returns the most recent N TradeUpdate values \
             (newest first) when a SubscriptionProvider is configured; \
             otherwise AdapterError::Internal.",
        ));
        Self {
            list,
            live: LiveRegistry::new(),
            provider: None,
        }
    }

    /// Snapshot the registered resources.
    #[must_use]
    pub fn resources(&self) -> Vec<Resource> {
        self.list.resources.clone()
    }

    /// Snapshot the registered resource templates.
    #[must_use]
    pub fn templates(&self) -> Vec<ResourceTemplate> {
        self.list.templates.clone()
    }

    /// Snapshot of the full `resources/list` + templates payload.
    #[must_use]
    pub fn list(&self) -> ResourceList {
        self.list.clone()
    }

    /// Whether the registry has any entries or templates.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.list.resources.is_empty() && self.list.templates.is_empty()
    }

    /// Read a resource by its parsed URI.
    ///
    /// v0.1-12 routes static URIs (`Currencies`, `Instruments`) to
    /// [`static_::read_currencies`] / [`static_::read_instruments`].
    /// Live URIs (`Book`, `Ticker`, `Trades`) return a structured
    /// [`AdapterError::Internal`] with `reason: "live resources land
    /// in v0.3"` so the LLM sees a stable error shape — the live
    /// transport ships in v0.3 (ADR-0006).
    ///
    /// # Errors
    ///
    /// Static reads surface whatever upstream HTTP failure the call
    /// produces (network, rate-limit, API). Live reads return
    /// [`AdapterError::Internal`] until v0.3 ships.
    pub async fn read(
        &self,
        ctx: &AdapterContext,
        uri: &ResourceUri,
    ) -> Result<ResourceContent, AdapterError> {
        match uri {
            ResourceUri::Currencies => {
                Ok(ResourceContent::Json(static_::read_currencies(ctx).await?))
            }
            ResourceUri::Instruments { currency } => Ok(ResourceContent::Json(
                static_::read_instruments(ctx, currency).await?,
            )),
            ResourceUri::Book { instrument } => {
                let value = self.read_live(uri).await?;
                let book = BookSnapshot::from_value(instrument, &value)?;
                Ok(ResourceContent::Json(serde_json::to_value(&book)?))
            }
            ResourceUri::Ticker { instrument } => {
                let value = self.read_live(uri).await?;
                let ticker = TickerSnapshot::from_value(instrument, &value)?;
                Ok(ResourceContent::Json(serde_json::to_value(&ticker)?))
            }
            ResourceUri::Trades { .. } => {
                // Subscribe (so the reader task is running) and
                // hand back the rolling history of decoded
                // trades. Each upstream frame is an array of
                // trade objects; we flatten across frames and
                // sort by timestamp newest-first so the LLM
                // sees a deterministic chronological list,
                // capped at `HISTORY_CAPACITY`.
                use tokio::sync::broadcast::error::RecvError;
                let provider = self.provider.as_ref().ok_or_else(|| {
                    AdapterError::internal("live subscription provider not configured")
                })?;
                let handle = self.live.subscribe(provider.as_ref(), uri).await?;
                let mut updates = handle.updates();
                if handle.latest().await.is_none() {
                    match tokio::time::timeout(FIRST_FRAME_TIMEOUT, updates.recv()).await {
                        Ok(Ok(())) | Ok(Err(RecvError::Lagged(_))) => {}
                        Ok(Err(RecvError::Closed)) => {
                            return Err(AdapterError::internal(
                                "live subscription closed before producing a frame",
                            ));
                        }
                        Err(_elapsed) => {
                            return Err(AdapterError::internal(
                                "live subscription did not produce a frame in time",
                            ));
                        }
                    }
                }
                let mut trades: Vec<TradeUpdate> = Vec::new();
                for frame in handle.history().await {
                    // Propagate decode errors rather than silently
                    // dropping a malformed frame — an upstream
                    // contract change should surface, not vanish.
                    let mut decoded = TradeUpdate::batch_from_value(&frame)?;
                    trades.append(&mut decoded);
                }
                trades.sort_by_key(|t| std::cmp::Reverse(t.timestamp));
                trades.truncate(live::HISTORY_CAPACITY);
                Ok(ResourceContent::Json(serde_json::to_value(&trades)?))
            }
        }
    }

    /// Subscribe to a live URI and return the latest cached
    /// snapshot, waiting up to [`FIRST_FRAME_TIMEOUT`] on a fresh
    /// subscription. The returned `SubscriptionHandle` is dropped at
    /// the end of the call — the underlying entry stays open as long
    /// as any other subscriber holds a handle, otherwise the
    /// refcount returns to zero and the upstream channel closes.
    async fn read_live(&self, uri: &ResourceUri) -> Result<serde_json::Value, AdapterError> {
        use tokio::sync::broadcast::error::RecvError;

        let provider = self
            .provider
            .as_ref()
            .ok_or_else(|| AdapterError::internal("live subscription provider not configured"))?;
        let handle = self.live.subscribe(provider.as_ref(), uri).await?;
        // Subscribe to the broadcast BEFORE checking `latest`. If
        // the first frame arrives between an early `latest` check
        // and the `updates()` subscription, the broadcast receiver
        // would miss its signal (no backlog) and we would time out
        // spuriously.
        let mut updates = handle.updates();
        if let Some(snapshot) = handle.latest().await {
            return Ok(snapshot);
        }
        match tokio::time::timeout(FIRST_FRAME_TIMEOUT, updates.recv()).await {
            Ok(Ok(())) => handle
                .latest()
                .await
                .ok_or_else(|| AdapterError::internal("update fired without snapshot")),
            Ok(Err(RecvError::Lagged(_))) => handle
                .latest()
                .await
                .ok_or_else(|| AdapterError::internal("broadcast lagged before first frame")),
            Ok(Err(RecvError::Closed)) => Err(AdapterError::internal(
                "live subscription closed before producing a frame",
            )),
            Err(_elapsed) => Err(AdapterError::internal(
                "live subscription did not produce a frame in time",
            )),
        }
    }
}

fn make_resource(uri: &'static str, name: &'static str, description: &'static str) -> Resource {
    let raw = RawResource {
        uri: uri.to_string(),
        name: name.to_string(),
        title: None,
        description: Some(description.to_string()),
        mime_type: Some("application/json".to_string()),
        size: None,
        icons: None,
        meta: None,
    };
    Annotated {
        raw,
        annotations: None,
    }
}

fn make_template(
    template: &'static str,
    name: &'static str,
    description: &'static str,
) -> ResourceTemplate {
    let raw = RawResourceTemplate {
        uri_template: template.to_string(),
        name: name.to_string(),
        title: None,
        description: Some(description.to_string()),
        mime_type: Some("application/json".to_string()),
        icons: None,
    };
    Annotated {
        raw,
        annotations: None,
    }
}

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

    #[test]
    fn parses_currencies() {
        assert_eq!(
            parse_resource_uri("deribit://currencies").unwrap(),
            ResourceUri::Currencies
        );
    }

    #[test]
    fn parses_instruments_with_currency() {
        assert_eq!(
            parse_resource_uri("deribit://instruments/BTC").unwrap(),
            ResourceUri::Instruments {
                currency: "BTC".to_string()
            }
        );
    }

    #[test]
    fn parses_book_template() {
        assert_eq!(
            parse_resource_uri("deribit://book/BTC-PERPETUAL").unwrap(),
            ResourceUri::Book {
                instrument: "BTC-PERPETUAL".to_string()
            }
        );
    }

    #[test]
    fn parses_ticker_and_trades() {
        assert!(matches!(
            parse_resource_uri("deribit://ticker/ETH-PERPETUAL").unwrap(),
            ResourceUri::Ticker { .. }
        ));
        assert!(matches!(
            parse_resource_uri("deribit://trades/BTC-31MAY24-50000-C").unwrap(),
            ResourceUri::Trades { .. }
        ));
    }

    #[test]
    fn rejects_non_deribit_scheme() {
        let err = parse_resource_uri("foo://bar").unwrap_err();
        match err {
            AdapterError::Validation { field, .. } => assert_eq!(field, "uri"),
            other => panic!("unexpected: {other:?}"),
        }
    }

    #[test]
    fn rejects_currencies_with_path() {
        let err = parse_resource_uri("deribit://currencies/extra").unwrap_err();
        assert!(matches!(err, AdapterError::Validation { .. }));
    }

    #[test]
    fn rejects_instruments_without_currency() {
        let err = parse_resource_uri("deribit://instruments/").unwrap_err();
        assert!(matches!(err, AdapterError::Validation { .. }));
    }

    #[test]
    fn rejects_unknown_head() {
        let err = parse_resource_uri("deribit://options/BTC").unwrap_err();
        assert!(matches!(err, AdapterError::Validation { .. }));
    }

    #[test]
    fn rejects_lowercase_currency() {
        let err = parse_resource_uri("deribit://instruments/btc").unwrap_err();
        match err {
            AdapterError::Validation { field, .. } => assert_eq!(field, "currency"),
            other => panic!("unexpected: {other:?}"),
        }
    }

    #[test]
    fn rejects_overlong_instrument() {
        let long = "X".repeat(65);
        let uri = format!("deribit://book/{long}");
        let err = parse_resource_uri(&uri).unwrap_err();
        match err {
            AdapterError::Validation { field, .. } => assert_eq!(field, "instrument"),
            other => panic!("unexpected: {other:?}"),
        }
    }

    #[test]
    fn round_trip_to_uri() {
        for original in [
            "deribit://currencies",
            "deribit://instruments/BTC",
            "deribit://book/BTC-PERPETUAL",
            "deribit://ticker/ETH-PERPETUAL",
            "deribit://trades/BTC-31MAY24-50000-C",
        ] {
            let parsed = parse_resource_uri(original).unwrap();
            assert_eq!(parsed.to_uri(), original);
        }
    }

    #[test]
    fn registry_build_lists_static_currency_entry() {
        let r = ResourceRegistry::build();
        assert_eq!(r.resources().len(), 1);
        assert_eq!(r.resources()[0].raw.uri, "deribit://currencies");
    }

    #[test]
    fn registry_build_lists_four_templates() {
        let r = ResourceRegistry::build();
        let templates = r.templates();
        assert_eq!(templates.len(), 4);
        let uris: Vec<&str> = templates
            .iter()
            .map(|t| t.raw.uri_template.as_str())
            .collect();
        assert!(uris.contains(&"deribit://instruments/{currency}"));
        assert!(uris.contains(&"deribit://book/{instrument}"));
        assert!(uris.contains(&"deribit://ticker/{instrument}"));
        assert!(uris.contains(&"deribit://trades/{instrument}"));
    }

    fn ctx() -> AdapterContext {
        use crate::config::{Config, LogFormat, OrderTransport, Transport};
        use std::net::SocketAddr;
        use std::sync::Arc;
        let cfg = Config {
            endpoint: "https://test.deribit.com".to_string(),
            client_id: None,
            client_secret: None,
            allow_trading: false,
            max_order_usd: None,
            transport: Transport::Stdio,
            http_listen: SocketAddr::from(([127, 0, 0, 1], 8723)),
            http_bearer_token: None,
            log_format: LogFormat::Text,
            order_transport: OrderTransport::Http,
        };
        AdapterContext::new(Arc::new(cfg)).expect("ctx")
    }

    #[tokio::test]
    async fn read_live_book_uses_provider_and_returns_snapshot() {
        use crate::resources::live::{SubscriptionProvider, SubscriptionStream};
        use std::future::Future;
        use std::pin::Pin;
        use std::sync::Arc;
        use std::sync::atomic::{AtomicU64, Ordering};

        /// Counts every `subscribe()` call so the test can pin the
        /// "open exactly once across multiple reads" semantics.
        struct CountingProvider {
            opened: Arc<AtomicU64>,
        }
        impl SubscriptionProvider for CountingProvider {
            fn subscribe(
                &self,
                _uri: ResourceUri,
            ) -> Pin<Box<dyn Future<Output = Result<SubscriptionStream, AdapterError>> + Send + '_>>
            {
                self.opened.fetch_add(1, Ordering::AcqRel);
                Box::pin(async move {
                    let frame = serde_json::json!({
                        "bids": [[50_000.0, 1.0], [49_999.0, 2.0]],
                        "asks": [[50_001.0, 1.5]],
                        "change_id": 42_u64,
                        "timestamp": 1_700_000_000_000_i64,
                    });
                    let stream = futures_util::stream::iter(vec![Ok::<_, AdapterError>(frame)]);
                    Ok(Box::pin(stream) as SubscriptionStream)
                })
            }
        }

        let opened = Arc::new(AtomicU64::new(0));
        let provider = Arc::new(CountingProvider {
            opened: opened.clone(),
        });
        let registry = ResourceRegistry::build().with_subscription_provider(provider);
        let uri = ResourceUri::Book {
            instrument: "BTC-PERPETUAL".to_string(),
        };

        let content = registry.read(&ctx(), &uri).await.expect("ok");
        match content {
            ResourceContent::Json(value) => {
                assert_eq!(
                    value.get("instrument").and_then(|v| v.as_str()),
                    Some("BTC-PERPETUAL")
                );
                assert_eq!(value.get("change_id").and_then(|v| v.as_u64()), Some(42));
                assert!(value.get("bids").and_then(|v| v.as_array()).is_some());
            }
        }

        // Second read should hit the cached entry. The first read
        // dropped its `SubscriptionHandle`, so the entry's refcount
        // returned to zero and the deferred-cleanup task may have
        // already removed it. To pin the semantics deterministically,
        // assert the provider was opened *at most twice* — once
        // for the first read, optionally once if the cleanup
        // raced — and never more than that (no per-read leak).
        let _ = registry.read(&ctx(), &uri).await.expect("ok");
        let opens = opened.load(Ordering::Acquire);
        assert!(
            (1..=2).contains(&opens),
            "expected provider opens in 1..=2, got {opens}"
        );
    }

    #[tokio::test]
    async fn read_live_book_without_provider_returns_internal() {
        let registry = ResourceRegistry::build();
        let uri = ResourceUri::Book {
            instrument: "BTC-PERPETUAL".to_string(),
        };
        let err = registry.read(&ctx(), &uri).await.unwrap_err();
        assert!(matches!(err, AdapterError::Internal { .. }));
    }

    #[tokio::test]
    async fn read_live_trades_uses_provider_and_returns_chronological_history() {
        use crate::resources::live::{SubscriptionProvider, SubscriptionStream};
        use std::future::Future;
        use std::pin::Pin;
        use std::sync::Arc;

        struct StubProvider;
        impl SubscriptionProvider for StubProvider {
            fn subscribe(
                &self,
                _uri: ResourceUri,
            ) -> Pin<Box<dyn Future<Output = Result<SubscriptionStream, AdapterError>> + Send + '_>>
            {
                Box::pin(async move {
                    // Two frames, each carrying a small batch.
                    let frames = vec![
                        Ok::<_, AdapterError>(serde_json::json!([
                            {
                                "direction": "buy",
                                "price": 50_001.0,
                                "amount": 1.0,
                                "trade_id": "t1",
                                "timestamp": 1_700_000_000_001_i64
                            }
                        ])),
                        Ok::<_, AdapterError>(serde_json::json!([
                            {
                                "direction": "sell",
                                "price": 50_002.0,
                                "amount": 0.5,
                                "trade_id": "t2",
                                "timestamp": 1_700_000_000_002_i64,
                                "liquidation": "M",
                                "tick_direction": 1_i64,
                                "mark_price": 50_001.5,
                                "index_price": 50_010.0
                            }
                        ])),
                    ];
                    let stream = futures_util::stream::iter(frames);
                    Ok(Box::pin(stream) as SubscriptionStream)
                })
            }
        }

        let registry = ResourceRegistry::build().with_subscription_provider(Arc::new(StubProvider));
        let uri = ResourceUri::Trades {
            instrument: "BTC-PERPETUAL".to_string(),
        };
        // Give the reader task a beat to drain both frames.
        let _ = registry.read(&ctx(), &uri).await.expect("first ok");
        tokio::time::sleep(std::time::Duration::from_millis(20)).await;
        let content = registry.read(&ctx(), &uri).await.expect("second ok");
        let ResourceContent::Json(value) = content;
        let trades = value.as_array().expect("array");
        let ids: Vec<&str> = trades
            .iter()
            .filter_map(|t| t.get("trade_id").and_then(|v| v.as_str()))
            .collect();
        assert_eq!(
            ids,
            vec!["t2", "t1"],
            "expected newest-first ordering; got {ids:?}"
        );
        let t2 = &trades[0];
        assert_eq!(
            t2.get("liquidation").and_then(|v| v.as_str()),
            Some("M"),
            "t2 should carry the liquidation marker"
        );
        assert_eq!(t2.get("tick_direction").and_then(|v| v.as_i64()), Some(1));
        assert_eq!(
            t2.get("mark_price").and_then(|v| v.as_f64()),
            Some(50_001.5)
        );
        assert_eq!(
            t2.get("index_price").and_then(|v| v.as_f64()),
            Some(50_010.0)
        );
    }

    #[tokio::test]
    async fn read_live_ticker_uses_provider_and_returns_snapshot_with_greeks() {
        use crate::resources::live::{SubscriptionProvider, SubscriptionStream};
        use std::future::Future;
        use std::pin::Pin;
        use std::sync::Arc;

        struct StubProvider;
        impl SubscriptionProvider for StubProvider {
            fn subscribe(
                &self,
                _uri: ResourceUri,
            ) -> Pin<Box<dyn Future<Output = Result<SubscriptionStream, AdapterError>> + Send + '_>>
            {
                Box::pin(async move {
                    let frame = serde_json::json!({
                        "mark_price": 50_000.5,
                        "index_price": 50_010.0,
                        "best_bid_price": 50_000.0,
                        "best_ask_price": 50_001.0,
                        "last_price": 49_999.5,
                        "mark_iv": 65.0,
                        "greeks": {
                            "delta": 0.55,
                            "gamma": 0.0001,
                            "vega": 12.3,
                            "theta": -0.4,
                            "rho": 0.05
                        },
                        "timestamp": 1_700_000_000_000_i64,
                    });
                    let stream = futures_util::stream::iter(vec![Ok::<_, AdapterError>(frame)]);
                    Ok(Box::pin(stream) as SubscriptionStream)
                })
            }
        }

        let registry = ResourceRegistry::build().with_subscription_provider(Arc::new(StubProvider));
        let uri = ResourceUri::Ticker {
            instrument: "BTC-31MAY24-50000-C".to_string(),
        };
        let content = registry.read(&ctx(), &uri).await.expect("ok");
        let ResourceContent::Json(value) = content;
        assert_eq!(
            value.get("instrument").and_then(|v| v.as_str()),
            Some("BTC-31MAY24-50000-C")
        );
        assert_eq!(
            value.get("mark_price").and_then(|v| v.as_f64()),
            Some(50_000.5)
        );
        assert_eq!(value.get("delta").and_then(|v| v.as_f64()), Some(0.55));
        assert_eq!(value.get("gamma").and_then(|v| v.as_f64()), Some(0.0001));
        assert_eq!(value.get("vega").and_then(|v| v.as_f64()), Some(12.3));
    }

    #[tokio::test]
    async fn read_live_ticker_perp_omits_greeks() {
        use crate::resources::live::{SubscriptionProvider, SubscriptionStream};
        use std::future::Future;
        use std::pin::Pin;
        use std::sync::Arc;

        struct StubProvider;
        impl SubscriptionProvider for StubProvider {
            fn subscribe(
                &self,
                _uri: ResourceUri,
            ) -> Pin<Box<dyn Future<Output = Result<SubscriptionStream, AdapterError>> + Send + '_>>
            {
                Box::pin(async move {
                    let frame = serde_json::json!({
                        "mark_price": 50_000.5,
                        "best_bid_price": 50_000.0,
                        "best_ask_price": 50_001.0,
                        "last_price": 49_999.5,
                        "timestamp": 1_700_000_000_000_i64,
                    });
                    let stream = futures_util::stream::iter(vec![Ok::<_, AdapterError>(frame)]);
                    Ok(Box::pin(stream) as SubscriptionStream)
                })
            }
        }

        let registry = ResourceRegistry::build().with_subscription_provider(Arc::new(StubProvider));
        let uri = ResourceUri::Ticker {
            instrument: "BTC-PERPETUAL".to_string(),
        };
        let content = registry.read(&ctx(), &uri).await.expect("ok");
        let ResourceContent::Json(value) = content;
        // `skip_serializing_if = "Option::is_none"` keeps absent
        // fields out of the JSON payload entirely.
        assert!(value.get("delta").is_none());
        assert!(value.get("gamma").is_none());
        assert!(value.get("vega").is_none());
        assert!(value.get("mark_iv").is_none());
    }
}