Skip to content

Commit 0b8f622

Browse files
authored
feat: new cunstructor and conversion utilities for call tool operations (#41)
* introduce CallToolError * implement new conversion functions * fix format
1 parent 7b8672d commit 0b8f622

File tree

4 files changed

+146
-2
lines changed

4 files changed

+146
-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-28 17:52:11
9+
/// Generated at : 2025-02-28 18:09:15
1010
/// ----------------------------------------------------------------------------
1111
///
1212
/// MCP Protocol Version

src/generated_schema/2024_11_05/schema_utils.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,78 @@ impl std::error::Error for CallToolError {
14181418
}
14191419
}
14201420

1421+
/// Conversion of `CallToolError` into a `CallToolResult` with an error.
1422+
impl From<CallToolError> for CallToolResult {
1423+
fn from(value: CallToolError) -> Self {
1424+
// Convert `CallToolError` to a `CallToolResult` by using the `with_error` method
1425+
CallToolResult::with_error(value)
1426+
}
1427+
}
1428+
1429+
impl CallToolResult {
1430+
/// Create a `CallToolResult` with an error, containing an error message in the content
1431+
pub fn with_error(error: CallToolError) -> Self {
1432+
Self {
1433+
content: vec![CallToolResultContentItem::TextContent(TextContent::new(
1434+
None,
1435+
error.to_string(), // Convert the error to a string and wrap it in TextContent
1436+
))],
1437+
is_error: Some(true), // Indicate that the result is an error
1438+
meta: None,
1439+
}
1440+
}
1441+
1442+
/// Create a `CallToolResult` containing text content and optional annotations
1443+
pub fn text_content(text_content: String, text_annotations: Option<TextContentAnnotations>) -> Self {
1444+
Self {
1445+
content: vec![TextContent::new(text_annotations, text_content).into()],
1446+
is_error: None,
1447+
meta: None,
1448+
}
1449+
}
1450+
1451+
/// Create a `CallToolResult` containing image content, with data, MIME type, and optional annotations
1452+
pub fn image_content(data: String, mime_type: String, annotations: Option<ImageContentAnnotations>) -> Self {
1453+
Self {
1454+
content: vec![ImageContent::new(annotations, data, mime_type).into()],
1455+
is_error: None,
1456+
meta: None,
1457+
}
1458+
}
1459+
1460+
/// Create a `CallToolResult` containing an embedded resource, with optional annotations
1461+
pub fn embedded_resource(resource: EmbeddedResourceResource, annotations: Option<EmbeddedResourceAnnotations>) -> Self {
1462+
Self {
1463+
content: vec![EmbeddedResource::new(annotations, resource).into()],
1464+
is_error: None,
1465+
meta: None,
1466+
}
1467+
}
1468+
1469+
// Add metadata to the `CallToolResult`, allowing additional context or information to be included
1470+
pub fn with_meta(mut self, meta: Option<serde_json::Map<String, Value>>) -> Self {
1471+
self.meta = meta;
1472+
self
1473+
}
1474+
}
1475+
1476+
impl CallToolResultContentItem {
1477+
/// Create a `CallToolResultContentItem` with text content and optional annotations
1478+
pub fn text_content(test_content: String, annotations: Option<TextContentAnnotations>) -> Self {
1479+
TextContent::new(annotations, test_content).into()
1480+
}
1481+
1482+
/// Create a `CallToolResultContentItem` with image content, with data, MIME type, and optional annotations
1483+
pub fn image_content(data: String, mime_type: String, annotations: Option<ImageContentAnnotations>) -> Self {
1484+
ImageContent::new(annotations, data, mime_type).into()
1485+
}
1486+
1487+
/// Create a `CallToolResultContentItem` with an embedded resource, with optional annotations
1488+
pub fn embedded_resource(resource: EmbeddedResourceResource, annotations: Option<EmbeddedResourceAnnotations>) -> Self {
1489+
EmbeddedResource::new(annotations, resource).into()
1490+
}
1491+
}
1492+
14211493
/// BEGIN AUTO GENERATED
14221494
impl ::serde::Serialize for ClientJsonrpcRequest {
14231495
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-28 17:52:11
9+
/// Generated at : 2025-02-28 18:09:15
1010
/// ----------------------------------------------------------------------------
1111
///
1212
/// MCP Protocol Version

src/generated_schema/draft/schema_utils.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,78 @@ impl std::error::Error for CallToolError {
14181418
}
14191419
}
14201420

1421+
/// Conversion of `CallToolError` into a `CallToolResult` with an error.
1422+
impl From<CallToolError> for CallToolResult {
1423+
fn from(value: CallToolError) -> Self {
1424+
// Convert `CallToolError` to a `CallToolResult` by using the `with_error` method
1425+
CallToolResult::with_error(value)
1426+
}
1427+
}
1428+
1429+
impl CallToolResult {
1430+
/// Create a `CallToolResult` with an error, containing an error message in the content
1431+
pub fn with_error(error: CallToolError) -> Self {
1432+
Self {
1433+
content: vec![CallToolResultContentItem::TextContent(TextContent::new(
1434+
None,
1435+
error.to_string(), // Convert the error to a string and wrap it in TextContent
1436+
))],
1437+
is_error: Some(true), // Indicate that the result is an error
1438+
meta: None,
1439+
}
1440+
}
1441+
1442+
/// Create a `CallToolResult` containing text content and optional annotations
1443+
pub fn text_content(text_content: String, text_annotations: Option<TextContentAnnotations>) -> Self {
1444+
Self {
1445+
content: vec![TextContent::new(text_annotations, text_content).into()],
1446+
is_error: None,
1447+
meta: None,
1448+
}
1449+
}
1450+
1451+
/// Create a `CallToolResult` containing image content, with data, MIME type, and optional annotations
1452+
pub fn image_content(data: String, mime_type: String, annotations: Option<ImageContentAnnotations>) -> Self {
1453+
Self {
1454+
content: vec![ImageContent::new(annotations, data, mime_type).into()],
1455+
is_error: None,
1456+
meta: None,
1457+
}
1458+
}
1459+
1460+
/// Create a `CallToolResult` containing an embedded resource, with optional annotations
1461+
pub fn embedded_resource(resource: EmbeddedResourceResource, annotations: Option<EmbeddedResourceAnnotations>) -> Self {
1462+
Self {
1463+
content: vec![EmbeddedResource::new(annotations, resource).into()],
1464+
is_error: None,
1465+
meta: None,
1466+
}
1467+
}
1468+
1469+
// Add metadata to the `CallToolResult`, allowing additional context or information to be included
1470+
pub fn with_meta(mut self, meta: Option<serde_json::Map<String, Value>>) -> Self {
1471+
self.meta = meta;
1472+
self
1473+
}
1474+
}
1475+
1476+
impl CallToolResultContentItem {
1477+
/// Create a `CallToolResultContentItem` with text content and optional annotations
1478+
pub fn text_content(test_content: String, annotations: Option<TextContentAnnotations>) -> Self {
1479+
TextContent::new(annotations, test_content).into()
1480+
}
1481+
1482+
/// Create a `CallToolResultContentItem` with image content, with data, MIME type, and optional annotations
1483+
pub fn image_content(data: String, mime_type: String, annotations: Option<ImageContentAnnotations>) -> Self {
1484+
ImageContent::new(annotations, data, mime_type).into()
1485+
}
1486+
1487+
/// Create a `CallToolResultContentItem` with an embedded resource, with optional annotations
1488+
pub fn embedded_resource(resource: EmbeddedResourceResource, annotations: Option<EmbeddedResourceAnnotations>) -> Self {
1489+
EmbeddedResource::new(annotations, resource).into()
1490+
}
1491+
}
1492+
14211493
/// BEGIN AUTO GENERATED
14221494
impl ::serde::Serialize for ClientJsonrpcRequest {
14231495
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>

0 commit comments

Comments
 (0)