Skip to content

Commit 47fd818

Browse files
authored
fix: Standardize error types to conform to JSON-RPC (#20)
We previously used `serde_json::error::Error` as the associated type for the FromStr trait. It has now been updated to return a consistent error type that aligns with the MCP specification.
1 parent 71e63e5 commit 47fd818

File tree

6 files changed

+46
-26
lines changed

6 files changed

+46
-26
lines changed

examples/mcp_client_handle_message.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rust_mcp_schema::schema_utils::*;
22
use rust_mcp_schema::*;
33
use std::str::FromStr;
44

5-
type AppError = serde_json::error::Error;
5+
type AppError = JsonrpcErrorError;
66

77
const SAMPLE_PAYLOAD: &str = r#"
88
{

examples/mcp_server_handle_message.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rust_mcp_schema::schema_utils::*;
22
use rust_mcp_schema::*;
33
use std::str::FromStr;
44

5-
type AppError = serde_json::error::Error;
5+
type AppError = JsonrpcErrorError;
66

77
const SAMPLE_PAYLOAD: &str = r#"
88
{

src/generated_schema/2024_11_05/mcp_schema.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
///
77
/// Generated from : <https://github.com/modelcontextprotocol/specification.git>
88
/// Hash : 63e1dbb75456b359b9ed8b27d21f4ac68cbb753e
9-
/// Generated at : 2025-02-14 07:43:44
9+
/// Generated at : 2025-02-14 08:18:40
1010
/// ----------------------------------------------------------------------------
1111
///
1212
/// MCP Protocol Version

src/generated_schema/2024_11_05/schema_utils.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl Display for ClientJsonrpcRequest {
8989
}
9090

9191
impl FromStr for ClientJsonrpcRequest {
92-
type Err = serde_json::error::Error;
92+
type Err = JsonrpcErrorError;
9393

9494
/// Parses a JSON-RPC request from a string.
9595
///
@@ -101,7 +101,7 @@ impl FromStr for ClientJsonrpcRequest {
101101
///
102102
/// # Returns
103103
/// * `Ok(ClientJsonrpcRequest)` if parsing is successful.
104-
/// * `Err(serde_json::error::Error)` if the string is not valid JSON.
104+
/// * `Err(JsonrpcErrorError)` if the string is not valid JSON.
105105
///
106106
/// # Example
107107
/// ```
@@ -114,6 +114,7 @@ impl FromStr for ClientJsonrpcRequest {
114114
/// ```
115115
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
116116
serde_json::from_str(s)
117+
.map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
117118
}
118119
}
119120

@@ -210,10 +211,11 @@ impl Display for ClientJsonrpcNotification {
210211
}
211212

212213
impl FromStr for ClientJsonrpcNotification {
213-
type Err = serde_json::error::Error;
214+
type Err = JsonrpcErrorError;
214215

215216
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
216217
serde_json::from_str(s)
218+
.map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
217219
}
218220
}
219221

@@ -297,10 +299,11 @@ impl Display for ClientJsonrpcResponse {
297299
}
298300

299301
impl FromStr for ClientJsonrpcResponse {
300-
type Err = serde_json::error::Error;
302+
type Err = JsonrpcErrorError;
301303

302304
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
303305
serde_json::from_str(s)
306+
.map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
304307
}
305308
}
306309
//*******************************//
@@ -349,10 +352,11 @@ impl<'de> serde::Deserialize<'de> for ResultFromClient {
349352
//*******************************//
350353

351354
impl FromStr for ClientMessage {
352-
type Err = serde_json::error::Error;
355+
type Err = JsonrpcErrorError;
353356

354357
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
355358
serde_json::from_str(s)
359+
.map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
356360
}
357361
}
358362

@@ -382,10 +386,11 @@ pub enum ServerMessage {
382386
}
383387

384388
impl FromStr for ServerMessage {
385-
type Err = serde_json::error::Error;
389+
type Err = JsonrpcErrorError;
386390

387391
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
388392
serde_json::from_str(s)
393+
.map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
389394
}
390395
}
391396

@@ -436,10 +441,11 @@ impl Display for ServerJsonrpcRequest {
436441
}
437442

438443
impl FromStr for ServerJsonrpcRequest {
439-
type Err = serde_json::error::Error;
444+
type Err = JsonrpcErrorError;
440445

441446
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
442447
serde_json::from_str(s)
448+
.map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
443449
}
444450
}
445451
//*************************//
@@ -535,10 +541,11 @@ impl Display for ServerJsonrpcNotification {
535541
}
536542

537543
impl FromStr for ServerJsonrpcNotification {
538-
type Err = serde_json::error::Error;
544+
type Err = JsonrpcErrorError;
539545

540546
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
541547
serde_json::from_str(s)
548+
.map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
542549
}
543550
}
544551
//*******************************//
@@ -621,10 +628,11 @@ impl Display for ServerJsonrpcResponse {
621628
}
622629

623630
impl FromStr for ServerJsonrpcResponse {
624-
type Err = serde_json::error::Error;
631+
type Err = JsonrpcErrorError;
625632

626633
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
627634
serde_json::from_str(s)
635+
.map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
628636
}
629637
}
630638
//*******************************//
@@ -690,10 +698,11 @@ impl Display for JsonrpcError {
690698
}
691699

692700
impl FromStr for JsonrpcError {
693-
type Err = serde_json::error::Error;
701+
type Err = JsonrpcErrorError;
694702

695703
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
696704
serde_json::from_str(s)
705+
.map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
697706
}
698707
}
699708

@@ -1386,9 +1395,10 @@ impl Display for JsonrpcErrorError {
13861395
}
13871396
}
13881397
impl FromStr for JsonrpcErrorError {
1389-
type Err = serde_json::error::Error;
1398+
type Err = JsonrpcErrorError;
13901399
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
13911400
serde_json::from_str(s)
1401+
.map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
13921402
}
13931403
}
13941404
/// Constructs a new JsonrpcError using the provided arguments.

src/generated_schema/draft/mcp_schema.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
///
77
/// Generated from : <https://github.com/modelcontextprotocol/specification.git>
88
/// Hash : 63e1dbb75456b359b9ed8b27d21f4ac68cbb753e
9-
/// Generated at : 2025-02-14 07:43:47
9+
/// Generated at : 2025-02-14 08:18:40
1010
/// ----------------------------------------------------------------------------
1111
///
1212
/// MCP Protocol Version

src/generated_schema/draft/schema_utils.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl Display for ClientJsonrpcRequest {
8989
}
9090

9191
impl FromStr for ClientJsonrpcRequest {
92-
type Err = serde_json::error::Error;
92+
type Err = JsonrpcErrorError;
9393

9494
/// Parses a JSON-RPC request from a string.
9595
///
@@ -101,7 +101,7 @@ impl FromStr for ClientJsonrpcRequest {
101101
///
102102
/// # Returns
103103
/// * `Ok(ClientJsonrpcRequest)` if parsing is successful.
104-
/// * `Err(serde_json::error::Error)` if the string is not valid JSON.
104+
/// * `Err(JsonrpcErrorError)` if the string is not valid JSON.
105105
///
106106
/// # Example
107107
/// ```
@@ -114,6 +114,7 @@ impl FromStr for ClientJsonrpcRequest {
114114
/// ```
115115
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
116116
serde_json::from_str(s)
117+
.map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
117118
}
118119
}
119120

@@ -210,10 +211,11 @@ impl Display for ClientJsonrpcNotification {
210211
}
211212

212213
impl FromStr for ClientJsonrpcNotification {
213-
type Err = serde_json::error::Error;
214+
type Err = JsonrpcErrorError;
214215

215216
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
216217
serde_json::from_str(s)
218+
.map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
217219
}
218220
}
219221

@@ -297,10 +299,11 @@ impl Display for ClientJsonrpcResponse {
297299
}
298300

299301
impl FromStr for ClientJsonrpcResponse {
300-
type Err = serde_json::error::Error;
302+
type Err = JsonrpcErrorError;
301303

302304
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
303305
serde_json::from_str(s)
306+
.map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
304307
}
305308
}
306309
//*******************************//
@@ -349,10 +352,11 @@ impl<'de> serde::Deserialize<'de> for ResultFromClient {
349352
//*******************************//
350353

351354
impl FromStr for ClientMessage {
352-
type Err = serde_json::error::Error;
355+
type Err = JsonrpcErrorError;
353356

354357
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
355358
serde_json::from_str(s)
359+
.map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
356360
}
357361
}
358362

@@ -382,10 +386,11 @@ pub enum ServerMessage {
382386
}
383387

384388
impl FromStr for ServerMessage {
385-
type Err = serde_json::error::Error;
389+
type Err = JsonrpcErrorError;
386390

387391
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
388392
serde_json::from_str(s)
393+
.map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
389394
}
390395
}
391396

@@ -436,10 +441,11 @@ impl Display for ServerJsonrpcRequest {
436441
}
437442

438443
impl FromStr for ServerJsonrpcRequest {
439-
type Err = serde_json::error::Error;
444+
type Err = JsonrpcErrorError;
440445

441446
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
442447
serde_json::from_str(s)
448+
.map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
443449
}
444450
}
445451
//*************************//
@@ -535,10 +541,11 @@ impl Display for ServerJsonrpcNotification {
535541
}
536542

537543
impl FromStr for ServerJsonrpcNotification {
538-
type Err = serde_json::error::Error;
544+
type Err = JsonrpcErrorError;
539545

540546
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
541547
serde_json::from_str(s)
548+
.map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
542549
}
543550
}
544551
//*******************************//
@@ -621,10 +628,11 @@ impl Display for ServerJsonrpcResponse {
621628
}
622629

623630
impl FromStr for ServerJsonrpcResponse {
624-
type Err = serde_json::error::Error;
631+
type Err = JsonrpcErrorError;
625632

626633
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
627634
serde_json::from_str(s)
635+
.map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
628636
}
629637
}
630638
//*******************************//
@@ -690,10 +698,11 @@ impl Display for JsonrpcError {
690698
}
691699

692700
impl FromStr for JsonrpcError {
693-
type Err = serde_json::error::Error;
701+
type Err = JsonrpcErrorError;
694702

695703
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
696704
serde_json::from_str(s)
705+
.map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
697706
}
698707
}
699708

@@ -1380,9 +1389,10 @@ impl Display for JsonrpcErrorError {
13801389
}
13811390
}
13821391
impl FromStr for JsonrpcErrorError {
1383-
type Err = serde_json::error::Error;
1392+
type Err = JsonrpcErrorError;
13841393
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
13851394
serde_json::from_str(s)
1395+
.map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
13861396
}
13871397
}
13881398
/// Constructs a new JsonrpcError using the provided arguments.

0 commit comments

Comments
 (0)