Skip to content

feat: standardize error types to conform to JSON-RPC #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/mcp_client_handle_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rust_mcp_schema::schema_utils::*;
use rust_mcp_schema::*;
use std::str::FromStr;

type AppError = serde_json::error::Error;
type AppError = JsonrpcErrorError;

const SAMPLE_PAYLOAD: &str = r#"
{
Expand Down
2 changes: 1 addition & 1 deletion examples/mcp_server_handle_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rust_mcp_schema::schema_utils::*;
use rust_mcp_schema::*;
use std::str::FromStr;

type AppError = serde_json::error::Error;
type AppError = JsonrpcErrorError;

const SAMPLE_PAYLOAD: &str = r#"
{
Expand Down
2 changes: 1 addition & 1 deletion src/generated_schema/2024_11_05/mcp_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
///
/// Generated from : <https://github.com/modelcontextprotocol/specification.git>
/// Hash : 63e1dbb75456b359b9ed8b27d21f4ac68cbb753e
/// Generated at : 2025-02-14 07:43:44
/// Generated at : 2025-02-14 08:18:40
/// ----------------------------------------------------------------------------
///
/// MCP Protocol Version
Expand Down
32 changes: 21 additions & 11 deletions src/generated_schema/2024_11_05/schema_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Display for ClientJsonrpcRequest {
}

impl FromStr for ClientJsonrpcRequest {
type Err = serde_json::error::Error;
type Err = JsonrpcErrorError;

/// Parses a JSON-RPC request from a string.
///
Expand All @@ -101,7 +101,7 @@ impl FromStr for ClientJsonrpcRequest {
///
/// # Returns
/// * `Ok(ClientJsonrpcRequest)` if parsing is successful.
/// * `Err(serde_json::error::Error)` if the string is not valid JSON.
/// * `Err(JsonrpcErrorError)` if the string is not valid JSON.
///
/// # Example
/// ```
Expand All @@ -114,6 +114,7 @@ impl FromStr for ClientJsonrpcRequest {
/// ```
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
serde_json::from_str(s)
.map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
}
}

Expand Down Expand Up @@ -210,10 +211,11 @@ impl Display for ClientJsonrpcNotification {
}

impl FromStr for ClientJsonrpcNotification {
type Err = serde_json::error::Error;
type Err = JsonrpcErrorError;

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

Expand Down Expand Up @@ -297,10 +299,11 @@ impl Display for ClientJsonrpcResponse {
}

impl FromStr for ClientJsonrpcResponse {
type Err = serde_json::error::Error;
type Err = JsonrpcErrorError;

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

impl FromStr for ClientMessage {
type Err = serde_json::error::Error;
type Err = JsonrpcErrorError;

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

Expand Down Expand Up @@ -382,10 +386,11 @@ pub enum ServerMessage {
}

impl FromStr for ServerMessage {
type Err = serde_json::error::Error;
type Err = JsonrpcErrorError;

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

Expand Down Expand Up @@ -436,10 +441,11 @@ impl Display for ServerJsonrpcRequest {
}

impl FromStr for ServerJsonrpcRequest {
type Err = serde_json::error::Error;
type Err = JsonrpcErrorError;

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

impl FromStr for ServerJsonrpcNotification {
type Err = serde_json::error::Error;
type Err = JsonrpcErrorError;

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

impl FromStr for ServerJsonrpcResponse {
type Err = serde_json::error::Error;
type Err = JsonrpcErrorError;

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

impl FromStr for JsonrpcError {
type Err = serde_json::error::Error;
type Err = JsonrpcErrorError;

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

Expand Down Expand Up @@ -1386,9 +1395,10 @@ impl Display for JsonrpcErrorError {
}
}
impl FromStr for JsonrpcErrorError {
type Err = serde_json::error::Error;
type Err = JsonrpcErrorError;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
serde_json::from_str(s)
.map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
}
}
/// Constructs a new JsonrpcError using the provided arguments.
Expand Down
2 changes: 1 addition & 1 deletion src/generated_schema/draft/mcp_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
///
/// Generated from : <https://github.com/modelcontextprotocol/specification.git>
/// Hash : 63e1dbb75456b359b9ed8b27d21f4ac68cbb753e
/// Generated at : 2025-02-14 07:43:47
/// Generated at : 2025-02-14 08:18:40
/// ----------------------------------------------------------------------------
///
/// MCP Protocol Version
Expand Down
32 changes: 21 additions & 11 deletions src/generated_schema/draft/schema_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Display for ClientJsonrpcRequest {
}

impl FromStr for ClientJsonrpcRequest {
type Err = serde_json::error::Error;
type Err = JsonrpcErrorError;

/// Parses a JSON-RPC request from a string.
///
Expand All @@ -101,7 +101,7 @@ impl FromStr for ClientJsonrpcRequest {
///
/// # Returns
/// * `Ok(ClientJsonrpcRequest)` if parsing is successful.
/// * `Err(serde_json::error::Error)` if the string is not valid JSON.
/// * `Err(JsonrpcErrorError)` if the string is not valid JSON.
///
/// # Example
/// ```
Expand All @@ -114,6 +114,7 @@ impl FromStr for ClientJsonrpcRequest {
/// ```
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
serde_json::from_str(s)
.map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
}
}

Expand Down Expand Up @@ -210,10 +211,11 @@ impl Display for ClientJsonrpcNotification {
}

impl FromStr for ClientJsonrpcNotification {
type Err = serde_json::error::Error;
type Err = JsonrpcErrorError;

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

Expand Down Expand Up @@ -297,10 +299,11 @@ impl Display for ClientJsonrpcResponse {
}

impl FromStr for ClientJsonrpcResponse {
type Err = serde_json::error::Error;
type Err = JsonrpcErrorError;

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

impl FromStr for ClientMessage {
type Err = serde_json::error::Error;
type Err = JsonrpcErrorError;

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

Expand Down Expand Up @@ -382,10 +386,11 @@ pub enum ServerMessage {
}

impl FromStr for ServerMessage {
type Err = serde_json::error::Error;
type Err = JsonrpcErrorError;

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

Expand Down Expand Up @@ -436,10 +441,11 @@ impl Display for ServerJsonrpcRequest {
}

impl FromStr for ServerJsonrpcRequest {
type Err = serde_json::error::Error;
type Err = JsonrpcErrorError;

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

impl FromStr for ServerJsonrpcNotification {
type Err = serde_json::error::Error;
type Err = JsonrpcErrorError;

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

impl FromStr for ServerJsonrpcResponse {
type Err = serde_json::error::Error;
type Err = JsonrpcErrorError;

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

impl FromStr for JsonrpcError {
type Err = serde_json::error::Error;
type Err = JsonrpcErrorError;

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

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