Skip to content

Commit 7ead1e6

Browse files
committed
fix notification method name bug
1 parent 149c7f6 commit 7ead1e6

File tree

4 files changed

+80
-4
lines changed

4 files changed

+80
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mcp-sdk-rs"
3-
version = "0.1.2"
3+
version = "0.1.3"
44
edition = "2021"
55
description = "An SDK for building Model Context Protocol (MCP) clients and servers in Rust"
66
license = "MIT"

src/client/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ impl Client {
303303
method: &str,
304304
params: Option<serde_json::Value>,
305305
) -> Result<(), Error> {
306-
let notification = Notification::new(method, params);
306+
let path = format!("notifications/{method}");
307+
let notification = Notification::new(path, params);
307308
self.sender
308309
.send(Message::Notification(notification))
309310
.map_err(|_| Error::Transport("failed to send notification message".to_string()))

src/protocol.rs

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,53 +24,95 @@ pub const SUPPORTED_PROTOCOL_VERSIONS: &[&str] = &[LATEST_PROTOCOL_VERSION, "202
2424
pub const JSONRPC_VERSION: &str = "2.0";
2525

2626
/// A unique identifier for a request
27+
///
28+
/// This enum represents the possible types of request identifiers in the MCP protocol.
29+
/// It can be either a string or a number, as per JSON-RPC 2.0 specification.
2730
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
2831
#[serde(untagged)]
2932
pub enum RequestId {
33+
/// String representation of the request ID
3034
String(String),
35+
/// Numeric representation of the request ID
3136
Number(i64),
3237
}
3338

3439
/// Base JSON-RPC request structure
40+
///
41+
/// This struct represents a JSON-RPC request in the MCP protocol.
42+
/// It includes the JSON-RPC version, method name, optional parameters, and a unique identifier.
3543
#[derive(Debug, Clone, Serialize, Deserialize)]
3644
pub struct Request {
45+
/// JSON-RPC version (always "2.0")
3746
pub jsonrpc: String,
47+
/// Name of the method to be invoked
3848
pub method: String,
49+
/// Optional parameters for the method
3950
#[serde(skip_serializing_if = "Option::is_none")]
4051
pub params: Option<Value>,
52+
/// Unique identifier for the request
4153
pub id: RequestId,
4254
}
4355

4456
/// Base JSON-RPC notification structure
57+
///
58+
/// This struct represents a JSON-RPC notification in the MCP protocol.
59+
/// It is similar to a request but does not include an id field, as it does not expect a response.
4560
#[derive(Debug, Clone, Serialize, Deserialize)]
4661
pub struct Notification {
62+
/// JSON-RPC version (always "2.0")
4763
pub jsonrpc: String,
64+
/// Name of the method to be invoked
4865
pub method: String,
66+
/// Optional parameters for the method
4967
#[serde(skip_serializing_if = "Option::is_none")]
5068
pub params: Option<Value>,
5169
}
5270

5371
/// Base JSON-RPC response structure
72+
///
73+
/// This struct represents a JSON-RPC response in the MCP protocol.
74+
/// It includes the JSON-RPC version, the id of the request it's responding to,
75+
/// and either a result or an error field.
5476
#[derive(Debug, Clone, Serialize, Deserialize)]
5577
pub struct Response {
78+
/// JSON-RPC version (always "2.0")
5679
pub jsonrpc: String,
80+
/// ID of the request this response corresponds to
5781
pub id: RequestId,
82+
/// The result of a successful request (null if there was an error)
5883
#[serde(skip_serializing_if = "Option::is_none")]
5984
pub result: Option<Value>,
85+
/// The error object if the request failed (null if the request was successful)
6086
#[serde(skip_serializing_if = "Option::is_none")]
6187
pub error: Option<ResponseError>,
6288
}
6389

6490
/// JSON-RPC error object
91+
///
92+
/// This struct represents an error that occurred during the processing of a JSON-RPC request.
6593
#[derive(Debug, Clone, Serialize, Deserialize)]
6694
pub struct ResponseError {
95+
/// The error code
6796
pub code: i32,
97+
/// A short description of the error
6898
pub message: String,
99+
/// Additional information about the error
69100
#[serde(skip_serializing_if = "Option::is_none")]
70101
pub data: Option<Value>,
71102
}
72103

73104
impl Request {
105+
/// Creates a new Request instance
106+
///
107+
/// # Arguments
108+
///
109+
/// * `method` - The name of the method to be invoked
110+
/// * `params` - Optional parameters for the method
111+
/// * `id` - Unique identifier for the request
112+
///
113+
/// # Returns
114+
///
115+
/// A new Request instance
74116
pub fn new(method: impl Into<String>, params: Option<Value>, id: RequestId) -> Self {
75117
Self {
76118
jsonrpc: crate::JSONRPC_VERSION.to_string(),
@@ -82,6 +124,16 @@ impl Request {
82124
}
83125

84126
impl Notification {
127+
/// Creates a new Notification instance
128+
///
129+
/// # Arguments
130+
///
131+
/// * `method` - The name of the method to be invoked
132+
/// * `params` - Optional parameters for the method
133+
///
134+
/// # Returns
135+
///
136+
/// A new Notification instance
85137
pub fn new(method: impl Into<String>, params: Option<Value>) -> Self {
86138
Self {
87139
jsonrpc: crate::JSONRPC_VERSION.to_string(),
@@ -92,6 +144,16 @@ impl Notification {
92144
}
93145

94146
impl Response {
147+
/// Creates a new successful Response instance
148+
///
149+
/// # Arguments
150+
///
151+
/// * `id` - The id of the request this response corresponds to
152+
/// * `result` - The result of the successful request
153+
///
154+
/// # Returns
155+
///
156+
/// A new Response instance representing a successful result
95157
pub fn success(id: RequestId, result: Option<Value>) -> Self {
96158
Self {
97159
jsonrpc: crate::JSONRPC_VERSION.to_string(),
@@ -101,6 +163,16 @@ impl Response {
101163
}
102164
}
103165

166+
/// Creates a new error Response instance
167+
///
168+
/// # Arguments
169+
///
170+
/// * `id` - The id of the request this response corresponds to
171+
/// * `error` - The error that occurred during request processing
172+
///
173+
/// # Returns
174+
///
175+
/// A new Response instance representing an error
104176
pub fn error(id: RequestId, error: ResponseError) -> Self {
105177
Self {
106178
jsonrpc: crate::JSONRPC_VERSION.to_string(),
@@ -148,6 +220,9 @@ impl From<Error> for ResponseError {
148220
}
149221

150222
impl fmt::Display for RequestId {
223+
/// Provides a string representation of the RequestId
224+
///
225+
/// This implementation allows RequestId to be easily printed or converted to a string.
151226
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152227
match self {
153228
RequestId::String(s) => write!(f, "{}", s),
@@ -228,4 +303,4 @@ mod tests {
228303
assert!(SUPPORTED_PROTOCOL_VERSIONS.contains(&LATEST_PROTOCOL_VERSION));
229304
assert_eq!(JSONRPC_VERSION, "2.0");
230305
}
231-
}
306+
}

0 commit comments

Comments
 (0)