Skip to content

Commit 7b8672d

Browse files
authored
feat: introduce CallToolError (#39)
1 parent 12b86f3 commit 7b8672d

File tree

4 files changed

+102
-2
lines changed

4 files changed

+102
-2
lines changed

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 : bb1446ff1810a0df57989d78366d626d2c01b9d7
9-
/// Generated at : 2025-02-22 14:26:53
9+
/// Generated at : 2025-02-28 17:52:11
1010
/// ----------------------------------------------------------------------------
1111
///
1212
/// MCP Protocol Version

src/generated_schema/2024_11_05/schema_utils.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,56 @@ impl FromMessage<MessageFromClient> for ClientMessage {
13681368
}
13691369
}
13701370

1371+
/// A custom error type `UnknownTool` that wraps a `String`.
1372+
/// This can be used as the error type in the result of a `CallToolRequest` when a non-existent or unimplemented tool is called.
1373+
#[derive(Debug)]
1374+
pub struct UnknownTool(pub String);
1375+
1376+
// Implement `Display` for `UnknownTool` to format the error message.
1377+
impl core::fmt::Display for UnknownTool {
1378+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1379+
// The formatted string will display "Unknown tool: <tool_name>"
1380+
write!(f, "Unknown tool: {}", self.0)
1381+
}
1382+
}
1383+
1384+
// Implement the `Error` trait for `UnknownTool`, making it a valid error type.
1385+
impl std::error::Error for UnknownTool {}
1386+
1387+
/// A specific error type that can hold any kind of error and is used to
1388+
/// encapsulate various error scenarios when a `CallToolRequest` fails.
1389+
#[derive(Debug)]
1390+
pub struct CallToolError(pub Box<dyn std::error::Error>);
1391+
1392+
// Implement methods for `CallToolError` to handle different error types.
1393+
impl CallToolError {
1394+
/// Constructor to create a new `CallToolError` from a generic error.
1395+
pub fn new<E: std::error::Error + 'static>(err: E) -> Self {
1396+
// Box the error to fit inside the `CallToolError` struct
1397+
CallToolError(Box::new(err))
1398+
}
1399+
1400+
/// Specific constructor to create a `CallToolError` for an `UnknownTool` error.
1401+
pub fn unknown_tool(tool_name: String) -> Self {
1402+
// Create a `CallToolError` from an `UnknownTool` error (wrapped in a `Box`).
1403+
CallToolError(Box::new(UnknownTool(tool_name)))
1404+
}
1405+
}
1406+
1407+
// Implement `Display` for `CallToolError` to provide a user-friendly error message.
1408+
impl core::fmt::Display for CallToolError {
1409+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1410+
write!(f, "{}", self.0)
1411+
}
1412+
}
1413+
1414+
// Implement `Error` for `CallToolError` to propagate the source of the error.
1415+
impl std::error::Error for CallToolError {
1416+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
1417+
self.0.source()
1418+
}
1419+
}
1420+
13711421
/// BEGIN AUTO GENERATED
13721422
impl ::serde::Serialize for ClientJsonrpcRequest {
13731423
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>

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 : bb1446ff1810a0df57989d78366d626d2c01b9d7
9-
/// Generated at : 2025-02-22 14:26:53
9+
/// Generated at : 2025-02-28 17:52:11
1010
/// ----------------------------------------------------------------------------
1111
///
1212
/// MCP Protocol Version

src/generated_schema/draft/schema_utils.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,56 @@ impl FromMessage<MessageFromClient> for ClientMessage {
13681368
}
13691369
}
13701370

1371+
/// A custom error type `UnknownTool` that wraps a `String`.
1372+
/// This can be used as the error type in the result of a `CallToolRequest` when a non-existent or unimplemented tool is called.
1373+
#[derive(Debug)]
1374+
pub struct UnknownTool(pub String);
1375+
1376+
// Implement `Display` for `UnknownTool` to format the error message.
1377+
impl core::fmt::Display for UnknownTool {
1378+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1379+
// The formatted string will display "Unknown tool: <tool_name>"
1380+
write!(f, "Unknown tool: {}", self.0)
1381+
}
1382+
}
1383+
1384+
// Implement the `Error` trait for `UnknownTool`, making it a valid error type.
1385+
impl std::error::Error for UnknownTool {}
1386+
1387+
/// A specific error type that can hold any kind of error and is used to
1388+
/// encapsulate various error scenarios when a `CallToolRequest` fails.
1389+
#[derive(Debug)]
1390+
pub struct CallToolError(pub Box<dyn std::error::Error>);
1391+
1392+
// Implement methods for `CallToolError` to handle different error types.
1393+
impl CallToolError {
1394+
/// Constructor to create a new `CallToolError` from a generic error.
1395+
pub fn new<E: std::error::Error + 'static>(err: E) -> Self {
1396+
// Box the error to fit inside the `CallToolError` struct
1397+
CallToolError(Box::new(err))
1398+
}
1399+
1400+
/// Specific constructor to create a `CallToolError` for an `UnknownTool` error.
1401+
pub fn unknown_tool(tool_name: String) -> Self {
1402+
// Create a `CallToolError` from an `UnknownTool` error (wrapped in a `Box`).
1403+
CallToolError(Box::new(UnknownTool(tool_name)))
1404+
}
1405+
}
1406+
1407+
// Implement `Display` for `CallToolError` to provide a user-friendly error message.
1408+
impl core::fmt::Display for CallToolError {
1409+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1410+
write!(f, "{}", self.0)
1411+
}
1412+
}
1413+
1414+
// Implement `Error` for `CallToolError` to propagate the source of the error.
1415+
impl std::error::Error for CallToolError {
1416+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
1417+
self.0.source()
1418+
}
1419+
}
1420+
13711421
/// BEGIN AUTO GENERATED
13721422
impl ::serde::Serialize for ClientJsonrpcRequest {
13731423
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>

0 commit comments

Comments
 (0)