Skip to content

Commit 83734bf

Browse files
authored
feat: enhance CallToolError API and Introduce is_initialized_notification to (#85)
Relevant Client Message Types
1 parent 0e98fca commit 83734bf

File tree

13 files changed

+879
-318
lines changed

13 files changed

+879
-318
lines changed

Cargo.lock

Lines changed: 36 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ edition = "2021"
1414
path = "src/rust-mcp-schema.rs"
1515

1616
[dependencies]
17-
serde = { version = "1.0.219", features = ["derive"] }
18-
serde_json = { version = "1.0.140" }
17+
serde = { version = "1.0", features = ["derive"] }
18+
serde_json = { version = "1.0.143" }
1919

2020

2121
[dev-dependencies]

rust-toolchain.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[toolchain]
2+
channel = "1.88.0"
3+
components = ["rustfmt", "clippy"]

scripts/run_test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
COMMON_FEATURES=("schema_utils")
55

66
# schema versions features (tested one at a time)
7-
SCHEMA_VERSION_FEATURES=("2025_06_18", "2025_03_26", "2024_11_05", "draft")
7+
SCHEMA_VERSION_FEATURES=("2025_06_18", "2025_03_26", "2024_11_05") #// TODO: add the "draft" tests back
88

99
# space-separated string
1010
COMMON_FEATURES_STR="${COMMON_FEATURES[*]}"

src/generated_schema/2024_11_05/mcp_schema.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/// modify or extend the implementations as needed, but please do so at your own risk.
66
///
77
/// Generated from : <https://github.com/modelcontextprotocol/specification.git>
8-
/// Hash : 0695a497eb50a804fc0e88c18a93a21a675d6b3e
9-
/// Generated at : 2025-07-27 15:33:20
8+
/// Hash : a470342d05c345b580642821605b9c885bad237b
9+
/// Generated at : 2025-08-29 19:12:22
1010
/// ----------------------------------------------------------------------------
1111
///
1212
/// MCP Protocol Version

src/generated_schema/2024_11_05/schema_utils.rs

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,11 @@ impl ClientMessage {
236236
pub fn is_initialize_request(&self) -> bool {
237237
matches!(self, Self::Request(request) if request.request.is_initialize_request())
238238
}
239+
240+
/// Returns `true` if the message is an `InitializedNotification`
241+
pub fn is_initialized_notification(&self) -> bool {
242+
matches!(self, Self::Notification(notofication) if notofication.notification.is_initialized_notification())
243+
}
239244
}
240245

241246
impl From<ClientJsonrpcNotification> for ClientMessage {
@@ -519,7 +524,7 @@ impl TryFrom<NotificationFromClient> for ClientNotification {
519524
}
520525

521526
impl NotificationFromClient {
522-
/// Checks if the current notification is an `InitializedNotification` from the client, indicating that the client has been initialized.
527+
/// Returns `true` if the message is an `InitializedNotification`
523528
pub fn is_initialized_notification(&self) -> bool {
524529
matches!(
525530
self,
@@ -1318,10 +1323,15 @@ pub enum MessageFromClient {
13181323
}
13191324

13201325
impl MessageFromClient {
1321-
/// Returns `true` if the request is an `InitializeRequest`.
1326+
/// Returns `true` if the message is an `InitializeRequest`.
13221327
pub fn is_initialize_request(&self) -> bool {
13231328
matches!(self, Self::RequestFromClient(request) if request.is_initialize_request())
13241329
}
1330+
1331+
/// Returns `true` if the message is an `InitializedNotification`
1332+
pub fn is_initialized_notification(&self) -> bool {
1333+
matches!(self, Self::NotificationFromClient(notofication) if notofication.is_initialized_notification())
1334+
}
13251335
}
13261336

13271337
impl From<RequestFromClient> for MessageFromClient {
@@ -1450,12 +1460,60 @@ impl CallToolError {
14501460
}
14511461

14521462
/// Specific constructor to create a `CallToolError` for an `UnknownTool` error.
1453-
pub fn unknown_tool(tool_name: String) -> Self {
1463+
pub fn unknown_tool(tool_name: impl Into<String>) -> Self {
14541464
// Create a `CallToolError` from an `UnknownTool` error (wrapped in a `Box`).
1455-
CallToolError(Box::new(UnknownTool(tool_name)))
1465+
CallToolError(Box::new(UnknownTool(tool_name.into())))
1466+
}
1467+
1468+
pub fn invalid_arguments(tool_name: impl Into<String>, message: Option<impl Into<String>>) -> Self {
1469+
let tool_name = tool_name.into();
1470+
let message = message.map(|m| m.into());
1471+
1472+
let full_message = match message {
1473+
Some(msg) => format!("Invalid arguments for tool '{tool_name}': {msg}" ),
1474+
None => format!("Invalid arguments for tool '{tool_name}'"),
1475+
};
1476+
Self::from_message(full_message)
1477+
}
1478+
1479+
/// Creates a new `CallToolError` from a string message.
1480+
///
1481+
/// This is useful for generating ad-hoc or one-off errors without defining a custom error type.
1482+
/// Internally, it wraps the string in a lightweight error type that implements the `Error` trait.
1483+
///
1484+
/// # Examples
1485+
///
1486+
/// ```
1487+
/// let err = CallToolError::from_message("Something went wrong");
1488+
/// println!("{:?}", err);
1489+
/// ```
1490+
///
1491+
/// # Parameters
1492+
///
1493+
/// - `message`: Any type that can be converted into a `String` (e.g., `&str` or `String`)
1494+
///
1495+
/// # Returns
1496+
///
1497+
/// A `CallToolError` wrapping a dynamic error created from the provided message.
1498+
pub fn from_message(message: impl Into<String>) -> Self {
1499+
struct MsgError(String);
1500+
impl std::fmt::Debug for MsgError {
1501+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1502+
write!(f, "{}", self.0)
1503+
}
1504+
}
1505+
impl std::fmt::Display for MsgError {
1506+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1507+
write!(f, "{}", self.0)
1508+
}
1509+
}
1510+
impl std::error::Error for MsgError {}
1511+
1512+
CallToolError::new(MsgError(message.into()))
14561513
}
14571514
}
14581515

1516+
14591517
/// Converts a `CallToolError` into a `RpcError`.
14601518
///
14611519
/// The conversion creates an internal error variant of `RpcError`
@@ -1508,8 +1566,6 @@ impl<T: Into<String>> From<T> for TextContent {
15081566
}
15091567
}
15101568

1511-
1512-
15131569
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
15141570
#[serde(untagged)]
15151571
#[allow(clippy::large_enum_variant)]
@@ -1568,8 +1624,6 @@ impl Display for ClientMessages {
15681624
}
15691625
}
15701626

1571-
1572-
15731627
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
15741628
#[serde(untagged)]
15751629
#[allow(clippy::large_enum_variant)]
@@ -1628,8 +1682,6 @@ impl Display for ServerMessages {
16281682
}
16291683
}
16301684

1631-
1632-
16331685
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
16341686
#[serde(untagged)]
16351687
#[allow(clippy::large_enum_variant)]

src/generated_schema/2025_03_26/mcp_schema.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/// modify or extend the implementations as needed, but please do so at your own risk.
66
///
77
/// Generated from : <https://github.com/modelcontextprotocol/specification.git>
8-
/// Hash : 0695a497eb50a804fc0e88c18a93a21a675d6b3e
9-
/// Generated at : 2025-07-27 15:33:21
8+
/// Hash : a470342d05c345b580642821605b9c885bad237b
9+
/// Generated at : 2025-08-29 19:12:23
1010
/// ----------------------------------------------------------------------------
1111
///
1212
/// MCP Protocol Version

0 commit comments

Comments
 (0)