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
//! Represents a request from an MCP client
use super::{JSONRPC_VERSION, Message, ProgressToken};
use serde::{Deserialize, Serialize};
use std::fmt;
use std::fmt::{Debug, Formatter};
#[cfg(feature = "server")]
use crate::Context;
#[cfg(feature = "http-server")]
use {crate::auth::Claims, http::HeaderMap, std::sync::Arc};
#[cfg(feature = "tasks")]
use crate::types::RelatedTaskMetadata;
#[cfg(feature = "server")]
pub use from_request::FromRequest;
pub use request_id::RequestId;
#[cfg(feature = "server")]
mod from_request;
mod request_id;
/// A request in the JSON-RPC protocol.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Request {
/// JSON-RPC protocol version.
///
/// > **Note:** always 2.0.
pub jsonrpc: String,
/// Request identifier. Must be a string or number and unique within the session.
pub id: RequestId,
/// Name of the method to invoke.
pub method: String,
/// Optional parameters for the method.
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<serde_json::Value>,
/// Current MCP Session ID
#[serde(skip)]
pub session_id: Option<uuid::Uuid>,
/// HTTP headers
#[serde(skip)]
#[cfg(feature = "http-server")]
pub headers: HeaderMap,
/// Authentication and Authorization claims attached to this request by
/// the HTTP engine. Type-erased so any engine can supply its own
/// [`Claims`]-implementing type.
#[serde(skip)]
#[cfg(feature = "http-server")]
pub claims: Option<Arc<dyn Claims>>,
}
/// Provides metadata related to the request that provides additional protocol-level information.
///
/// > **Note:** This class contains properties that are used by the Model Context Protocol
/// > for features like progress tracking and other protocol-specific capabilities.
#[derive(Default, Clone, Deserialize, Serialize)]
pub struct RequestParamsMeta {
/// An opaque token that will be attached to any subsequent progress notifications.
///
/// > **Note:** The receiver is not obligated to provide these notifications.
#[serde(rename = "progressToken", skip_serializing_if = "Option::is_none")]
pub progress_token: Option<ProgressToken>,
/// Represents metadata for associating messages with a task.
///
/// > **Note:** Include this in the _meta field under the key `io.modelcontextprotocol/related-task`.
#[serde(
rename = "io.modelcontextprotocol/related-task",
skip_serializing_if = "Option::is_none"
)]
#[cfg(feature = "tasks")]
pub(crate) task: Option<RelatedTaskMetadata>,
/// MCP request context
#[serde(skip)]
#[cfg(feature = "server")]
pub(crate) context: Option<Context>,
}
impl Debug for RequestParamsMeta {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("RequestParamsMeta")
.field("progress_token", &self.progress_token)
.finish()
}
}
impl From<Request> for Message {
#[inline]
fn from(request: Request) -> Self {
Self::Request(request)
}
}
impl RequestParamsMeta {
/// Creates a new [`RequestParamsMeta`] with [`ProgressToken`] for a specific [`RequestId`]
pub fn new(id: &RequestId) -> Self {
Self {
progress_token: Some(ProgressToken::from(id)),
#[cfg(feature = "tasks")]
task: None,
#[cfg(feature = "server")]
context: None,
}
}
}
impl Request {
/// Creates a new [`Request`]
pub fn new<T: Serialize>(
id: Option<RequestId>,
method: impl Into<String>,
params: Option<T>,
) -> Self {
Self {
jsonrpc: JSONRPC_VERSION.into(),
session_id: None,
id: id.unwrap_or_default(),
method: method.into(),
params: params.and_then(|p| serde_json::to_value(p).ok()),
#[cfg(feature = "http-server")]
headers: HeaderMap::with_capacity(8),
#[cfg(feature = "http-server")]
claims: None,
}
}
/// Returns request's id if it's specified, otherwise returns default value
///
/// Default: `(no id)`
pub fn id(&self) -> RequestId {
self.id.clone()
}
/// Returns the full id (session_id?/request_id)
pub fn full_id(&self) -> RequestId {
let id = self.id.clone();
if let Some(session_id) = self.session_id {
id.concat(RequestId::Uuid(session_id))
} else {
id
}
}
/// Returns [`Request`] params metadata
pub fn meta(&self) -> Option<RequestParamsMeta> {
self.params
.as_ref()?
.get("_meta")
.cloned()
.and_then(|meta| serde_json::from_value(meta).ok())
}
}
#[cfg(test)]
mod tests {}