From 83d1c15344c9cc406db75c517c4a0edca869a4df Mon Sep 17 00:00:00 2001 From: Ali Hashemi Date: Fri, 28 Feb 2025 17:54:20 -0400 Subject: [PATCH] introduce CallToolError --- src/generated_schema/2024_11_05/mcp_schema.rs | 2 +- .../2024_11_05/schema_utils.rs | 50 +++++++++++++++++++ src/generated_schema/draft/mcp_schema.rs | 2 +- src/generated_schema/draft/schema_utils.rs | 50 +++++++++++++++++++ 4 files changed, 102 insertions(+), 2 deletions(-) diff --git a/src/generated_schema/2024_11_05/mcp_schema.rs b/src/generated_schema/2024_11_05/mcp_schema.rs index f0d7778..d14859e 100644 --- a/src/generated_schema/2024_11_05/mcp_schema.rs +++ b/src/generated_schema/2024_11_05/mcp_schema.rs @@ -6,7 +6,7 @@ /// /// Generated from : /// Hash : bb1446ff1810a0df57989d78366d626d2c01b9d7 -/// Generated at : 2025-02-22 14:26:53 +/// Generated at : 2025-02-28 17:52:11 /// ---------------------------------------------------------------------------- /// /// MCP Protocol Version diff --git a/src/generated_schema/2024_11_05/schema_utils.rs b/src/generated_schema/2024_11_05/schema_utils.rs index a80c2a1..61900ff 100644 --- a/src/generated_schema/2024_11_05/schema_utils.rs +++ b/src/generated_schema/2024_11_05/schema_utils.rs @@ -1368,6 +1368,56 @@ impl FromMessage for ClientMessage { } } +/// A custom error type `UnknownTool` that wraps a `String`. +/// This can be used as the error type in the result of a `CallToolRequest` when a non-existent or unimplemented tool is called. +#[derive(Debug)] +pub struct UnknownTool(pub String); + +// Implement `Display` for `UnknownTool` to format the error message. +impl core::fmt::Display for UnknownTool { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + // The formatted string will display "Unknown tool: " + write!(f, "Unknown tool: {}", self.0) + } +} + +// Implement the `Error` trait for `UnknownTool`, making it a valid error type. +impl std::error::Error for UnknownTool {} + +/// A specific error type that can hold any kind of error and is used to +/// encapsulate various error scenarios when a `CallToolRequest` fails. +#[derive(Debug)] +pub struct CallToolError(pub Box); + +// Implement methods for `CallToolError` to handle different error types. +impl CallToolError { + /// Constructor to create a new `CallToolError` from a generic error. + pub fn new(err: E) -> Self { + // Box the error to fit inside the `CallToolError` struct + CallToolError(Box::new(err)) + } + + /// Specific constructor to create a `CallToolError` for an `UnknownTool` error. + pub fn unknown_tool(tool_name: String) -> Self { + // Create a `CallToolError` from an `UnknownTool` error (wrapped in a `Box`). + CallToolError(Box::new(UnknownTool(tool_name))) + } +} + +// Implement `Display` for `CallToolError` to provide a user-friendly error message. +impl core::fmt::Display for CallToolError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "{}", self.0) + } +} + +// Implement `Error` for `CallToolError` to propagate the source of the error. +impl std::error::Error for CallToolError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + self.0.source() + } +} + /// BEGIN AUTO GENERATED impl ::serde::Serialize for ClientJsonrpcRequest { fn serialize(&self, serializer: S) -> std::result::Result diff --git a/src/generated_schema/draft/mcp_schema.rs b/src/generated_schema/draft/mcp_schema.rs index 56d371c..025e6cb 100644 --- a/src/generated_schema/draft/mcp_schema.rs +++ b/src/generated_schema/draft/mcp_schema.rs @@ -6,7 +6,7 @@ /// /// Generated from : /// Hash : bb1446ff1810a0df57989d78366d626d2c01b9d7 -/// Generated at : 2025-02-22 14:26:53 +/// Generated at : 2025-02-28 17:52:11 /// ---------------------------------------------------------------------------- /// /// MCP Protocol Version diff --git a/src/generated_schema/draft/schema_utils.rs b/src/generated_schema/draft/schema_utils.rs index d4c50b4..cc66f5f 100644 --- a/src/generated_schema/draft/schema_utils.rs +++ b/src/generated_schema/draft/schema_utils.rs @@ -1368,6 +1368,56 @@ impl FromMessage for ClientMessage { } } +/// A custom error type `UnknownTool` that wraps a `String`. +/// This can be used as the error type in the result of a `CallToolRequest` when a non-existent or unimplemented tool is called. +#[derive(Debug)] +pub struct UnknownTool(pub String); + +// Implement `Display` for `UnknownTool` to format the error message. +impl core::fmt::Display for UnknownTool { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + // The formatted string will display "Unknown tool: " + write!(f, "Unknown tool: {}", self.0) + } +} + +// Implement the `Error` trait for `UnknownTool`, making it a valid error type. +impl std::error::Error for UnknownTool {} + +/// A specific error type that can hold any kind of error and is used to +/// encapsulate various error scenarios when a `CallToolRequest` fails. +#[derive(Debug)] +pub struct CallToolError(pub Box); + +// Implement methods for `CallToolError` to handle different error types. +impl CallToolError { + /// Constructor to create a new `CallToolError` from a generic error. + pub fn new(err: E) -> Self { + // Box the error to fit inside the `CallToolError` struct + CallToolError(Box::new(err)) + } + + /// Specific constructor to create a `CallToolError` for an `UnknownTool` error. + pub fn unknown_tool(tool_name: String) -> Self { + // Create a `CallToolError` from an `UnknownTool` error (wrapped in a `Box`). + CallToolError(Box::new(UnknownTool(tool_name))) + } +} + +// Implement `Display` for `CallToolError` to provide a user-friendly error message. +impl core::fmt::Display for CallToolError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "{}", self.0) + } +} + +// Implement `Error` for `CallToolError` to propagate the source of the error. +impl std::error::Error for CallToolError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + self.0.source() + } +} + /// BEGIN AUTO GENERATED impl ::serde::Serialize for ClientJsonrpcRequest { fn serialize(&self, serializer: S) -> std::result::Result