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
//! Per-connection context threaded through every method handler (#2054).
//!
//! Why: `session.attach` needs a way to push server-initiated JSON-RPC
//! notifications (session lifecycle/events) back to the SAME connection
//! that called it, asynchronously, after the initial response has already
//! been sent. Most handlers (`ping`, `health`, `session.list`, …) have no
//! use for this, but the [`Router`](super::router::Router) dispatch path
//! must thread it uniformly so a handful of methods aren't a special case
//! with a different call signature.
//! What: [`ConnectionContext`] carries a `connection_id` (identifies which
//! connection/subscriber this call came from — one per STDIO process
//! lifetime, one per HTTP `POST /rpc` call) and a [`NotifySender`] the
//! connection's transport drains and writes to the wire. Over STDIO, the
//! transport loop keeps one long-lived receiver for the whole process and
//! interleaves notifications with request/response lines on stdout. Over
//! HTTP, each `POST /rpc` call gets a throwaway channel — any queued
//! notification vanishes once the response is written, because HTTP's real
//! streaming path for `session.attach` is the dedicated `GET
//! /sessions/{id}/events` SSE route (see `crate::serve::http`), not this
//! channel. A handler that spawns a background forwarder using `notify`
//! (like `session.attach`) must treat a failed `send` as "the connection is
//! gone" and stop.
//! Test: `context::tests::connection_context_is_constructible`.
use Value;
use mpsc;
use Uuid;
/// Channel used to push a fully-formed JSON-RPC wire object (a `Response`
/// serialised to `Value`, or a server-initiated notification) to the
/// connection's outbound stream, asynchronously, outside the normal
/// request/response cycle.
///
/// Why: `serde_json::Value` (rather than `trusty_mcp::Response`) is
/// the channel item type because a server-initiated *notification*
/// (`{"jsonrpc":"2.0","method":"session.event","params":{...}}`) has no
/// `result`/`error` field — it isn't a `Response` at all — so the channel
/// must be able to carry either shape. The transport writes whatever
/// arrives verbatim as one NDJSON line.
/// What: `mpsc::UnboundedSender<Value>`. Unbounded because session events
/// are rare relative to request/response traffic and a bounded channel
/// would require a backpressure policy (drop? block the emitter?) that no
/// current requirement calls for.
pub type NotifySender = UnboundedSender;
/// Per-connection state passed to every JSON-RPC method handler.
///
/// Why: threading this uniformly (rather than adding a second handler trait
/// just for `session.attach`) keeps the dispatch path — and every future
/// method — on one call signature. See the module docs for the STDIO vs.
/// HTTP distinction.
/// What: `connection_id` is a fresh `Uuid` per STDIO process invocation or
/// per HTTP `POST /rpc` call; `notify` is that connection's outbound
/// notification sink.
/// Test: `context::tests::connection_context_is_constructible`.