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
use serde_json::Value;
use std::collections::HashMap;
/// Information about a connected MCP client.
#[derive(Debug, Clone, Default)]
pub struct ClientInfo {
/// Client name.
pub name: String,
/// Client version.
pub version: String,
}
/// Context for an MCP request.
#[derive(Debug, Clone)]
pub struct RequestContext {
/// Connected client info, if available.
pub client_info: Option<ClientInfo>,
/// JSON-RPC request ID.
pub request_id: Value,
/// Whether the connection has been initialized.
pub initialized: bool,
/// Arbitrary key-value metadata.
pub metadata: HashMap<String, Value>,
}
impl RequestContext {
/// Create a new request context with the given ID.
pub fn new(request_id: Value) -> Self {
Self {
client_info: None,
request_id,
initialized: false,
metadata: HashMap::new(),
}
}
/// Set the client info.
pub fn with_client_info(mut self, info: ClientInfo) -> Self {
self.client_info = Some(info);
self
}
/// Mark this context as initialized.
pub fn set_initialized(&mut self) {
self.initialized = true;
}
/// Get a metadata value by key.
pub fn get_metadata(&self, key: &str) -> Option<&Value> {
self.metadata.get(key)
}
/// Set a metadata key-value pair.
pub fn set_metadata(&mut self, key: String, value: Value) {
self.metadata.insert(key, value);
}
}