Skip to content
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
21 changes: 10 additions & 11 deletions crates/rmcp/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl<'de> Deserialize<'de> for ProtocolVersion {
pub enum NumberOrString {
/// A numeric identifier
Number(u32),
/// A string identifier
/// A string identifier
String(Arc<str>),
}

Expand Down Expand Up @@ -1186,8 +1186,7 @@ pub type RootsListChangedNotification = NotificationNoParam<RootsListChangedNoti
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct CallToolResult {
/// The content returned by the tool (text, images, etc.)
#[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<Vec<Content>>,
pub content: Vec<Content>,
/// An optional JSON object that represents the structured result of the tool call
#[serde(skip_serializing_if = "Option::is_none")]
pub structured_content: Option<Value>,
Expand All @@ -1200,15 +1199,15 @@ impl CallToolResult {
/// Create a successful tool result with unstructured content
pub fn success(content: Vec<Content>) -> Self {
CallToolResult {
content: Some(content),
content,
structured_content: None,
is_error: Some(false),
}
}
/// Create an error tool result with unstructured content
pub fn error(content: Vec<Content>) -> Self {
CallToolResult {
content: Some(content),
content,
structured_content: None,
is_error: Some(true),
}
Expand All @@ -1229,7 +1228,7 @@ impl CallToolResult {
/// ```
pub fn structured(value: Value) -> Self {
CallToolResult {
content: Some(vec![Content::text(value.to_string())]),
content: vec![Content::text(value.to_string())],
structured_content: Some(value),
is_error: Some(false),
}
Expand All @@ -1254,7 +1253,7 @@ impl CallToolResult {
/// ```
pub fn structured_error(value: Value) -> Self {
CallToolResult {
content: Some(vec![Content::text(value.to_string())]),
content: vec![Content::text(value.to_string())],
structured_content: Some(value),
is_error: Some(true),
}
Expand All @@ -1270,10 +1269,10 @@ impl CallToolResult {
where
T: DeserializeOwned,
{
let raw_text = match (self.structured_content, &self.content) {
let raw_text = match (self.structured_content, &self.content.first()) {
(Some(value), _) => return serde_json::from_value(value),
(None, Some(contents)) => {
if let Some(text) = contents.first().and_then(|c| c.as_text()) {
if let Some(text) = contents.as_text() {
Copy link

Copilot AI Aug 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pattern match (None, Some(contents)) is unreachable because self.content is now a Vec<Content> (not Option<Vec<Content>>), so it can never be Some(contents). This should be (None, contents) and the condition should check if contents is not empty.

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI Aug 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable contents is now of type &Content (from first()) rather than &Vec<Content>, so calling contents.as_text() directly is incorrect. This should be accessing the first content item's text method.

Copilot uses AI. Check for mistakes.
let text = &text.text;
Some(text)
} else {
Expand Down Expand Up @@ -1308,13 +1307,13 @@ impl<'de> Deserialize<'de> for CallToolResult {

let helper = CallToolResultHelper::deserialize(deserializer)?;
let result = CallToolResult {
content: helper.content,
content: helper.content.unwrap_or_default(),
structured_content: helper.structured_content,
is_error: helper.is_error,
};

// Validate mutual exclusivity
if result.content.is_none() && result.structured_content.is_none() {
if result.content.is_empty() && result.structured_content.is_none() {
return Err(serde::de::Error::custom(
"CallToolResult must have either content or structured_content",
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,7 @@
"properties": {
"content": {
"description": "The content returned by the tool (text, images, etc.)",
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Annotated"
}
Expand All @@ -322,7 +319,10 @@
"structuredContent": {
"description": "An optional JSON object that represents the structured result of the tool call"
}
}
},
"required": [
"content"
]
},
"CancelledNotificationMethod": {
"type": "string",
Expand Down
14 changes: 7 additions & 7 deletions crates/rmcp/tests/test_structured_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ async fn test_structured_content_in_call_result() {

let result = CallToolResult::structured(structured_data.clone());

assert!(result.content.is_some());
assert!(!result.content.is_empty());
assert!(result.structured_content.is_some());

let contents = result.content.unwrap();
let contents = result.content;

assert_eq!(contents.len(), 1);

Expand All @@ -150,10 +150,10 @@ async fn test_structured_error_in_call_result() {

let result = CallToolResult::structured_error(error_data.clone());

assert!(result.content.is_some());
assert!(!result.content.is_empty());
assert!(result.structured_content.is_some());

let contents = result.content.unwrap();
let contents = result.content;

assert_eq!(contents.len(), 1);

Expand Down Expand Up @@ -217,10 +217,10 @@ async fn test_structured_return_conversion() {

// Tools which return structured content should also return a serialized version as
// Content::text for backwards compatibility.
assert!(call_result.content.is_some());
assert!(!call_result.content.is_empty());
assert!(call_result.structured_content.is_some());

let contents = call_result.content.unwrap();
let contents = call_result.content;

assert_eq!(contents.len(), 1);

Expand Down Expand Up @@ -278,5 +278,5 @@ async fn test_output_schema_requires_structured_content() {

// Verify it has structured_content and content
assert!(call_result.structured_content.is_some());
assert!(call_result.content.is_some());
assert!(!call_result.content.is_empty());
}
6 changes: 2 additions & 4 deletions crates/rmcp/tests/test_tool_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,7 @@ async fn test_optional_i64_field_with_null_input() -> anyhow::Result<()> {

let result_text = result
.content
.as_ref()
.and_then(|contents| contents.first())
.first()
.and_then(|content| content.raw.as_text())
.map(|text| text.text.as_str())
.expect("Expected text content");
Expand Down Expand Up @@ -330,8 +329,7 @@ async fn test_optional_i64_field_with_null_input() -> anyhow::Result<()> {

let some_result_text = some_result
.content
.as_ref()
.and_then(|contents| contents.first())
.first()
.and_then(|content| content.raw.as_text())
.map(|text| text.text.as_str())
.expect("Expected text content");
Expand Down
4 changes: 2 additions & 2 deletions examples/simple-chat-client/src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ impl ChatSession {
if result.is_error.is_some_and(|b| b) {
self.messages
.push(Message::user("tool call failed, mcp call error"));
} else if let Some(contents) = &result.content {
contents.iter().for_each(|content| {
} else {
result.content.iter().for_each(|content| {
if let Some(content_text) = content.as_text() {
let json_result = serde_json::from_str::<serde_json::Value>(
&content_text.text,
Expand Down