Skip to main content

brainwires_agent_network/
connection.rs

1use serde_json::Value;
2use std::collections::HashMap;
3
4/// Information about a connected MCP client.
5#[derive(Debug, Clone, Default)]
6pub struct ClientInfo {
7    /// Client name.
8    pub name: String,
9    /// Client version.
10    pub version: String,
11}
12
13/// Context for an MCP request.
14#[derive(Debug, Clone)]
15pub struct RequestContext {
16    /// Connected client info, if available.
17    pub client_info: Option<ClientInfo>,
18    /// JSON-RPC request ID.
19    pub request_id: Value,
20    /// Whether the connection has been initialized.
21    pub initialized: bool,
22    /// Arbitrary key-value metadata.
23    pub metadata: HashMap<String, Value>,
24}
25
26impl RequestContext {
27    /// Create a new request context with the given ID.
28    pub fn new(request_id: Value) -> Self {
29        Self {
30            client_info: None,
31            request_id,
32            initialized: false,
33            metadata: HashMap::new(),
34        }
35    }
36
37    /// Set the client info.
38    pub fn with_client_info(mut self, info: ClientInfo) -> Self {
39        self.client_info = Some(info);
40        self
41    }
42
43    /// Mark this context as initialized.
44    pub fn set_initialized(&mut self) {
45        self.initialized = true;
46    }
47
48    /// Get a metadata value by key.
49    pub fn get_metadata(&self, key: &str) -> Option<&Value> {
50        self.metadata.get(key)
51    }
52
53    /// Set a metadata key-value pair.
54    pub fn set_metadata(&mut self, key: String, value: Value) {
55        self.metadata.insert(key, value);
56    }
57}