Skip to content
Open
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
56 changes: 55 additions & 1 deletion crates/rmcp/src/model/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ pub enum PromptMessageContent {
image: ImageContent,
},
/// Embedded server-side resource
Resource { resource: EmbeddedResource },
Resource {
#[serde(flatten)]
resource: EmbeddedResource,
},
/// A link to a resource that can be fetched separately
ResourceLink {
#[serde(flatten)]
Expand Down Expand Up @@ -321,6 +324,57 @@ mod tests {
assert!(json.contains("\"name\":\"test.txt\""));
}

#[test]
fn test_prompt_message_resource_serialization_is_flat() {
// Regression test: PromptMessageContent::Resource must serialize to
// the spec-compliant flat shape `{ "type": "resource", "resource": { "uri", "mimeType", "text" } }`
// and NOT the double-nested shape `{ "type": "resource", "resource": { "resource": {...} } }`.
// See: https://modelcontextprotocol.io/specification/2025-06-18/server/prompts
let message = PromptMessage::new_resource(
PromptMessageRole::User,
"alc://packages/sc/narrative".to_string(),
Some("text/markdown".to_string()),
Some("# Hello".to_string()),
None,
None,
None,
);

let value: serde_json::Value = serde_json::to_value(&message).unwrap();

// Drill into content
let content = value.get("content").expect("content present");
assert_eq!(
content.get("type").and_then(|v| v.as_str()),
Some("resource")
);

let resource = content
.get("resource")
.expect("resource field present at content level");

// Spec-compliant: resource.uri / resource.mimeType / resource.text MUST be flat
assert_eq!(
resource.get("uri").and_then(|v| v.as_str()),
Some("alc://packages/sc/narrative"),
"expected flat resource.uri, got: {resource:#?}"
);
assert_eq!(
resource.get("mimeType").and_then(|v| v.as_str()),
Some("text/markdown")
);
assert_eq!(
resource.get("text").and_then(|v| v.as_str()),
Some("# Hello")
);

// Regression guard: content.resource MUST NOT contain a nested `resource` key.
assert!(
resource.get("resource").is_none(),
"double-nested resource detected (regression): {resource:#?}"
);
}

#[test]
fn test_prompt_message_content_resource_link_deserialization() {
let json = r#"{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,35 +140,6 @@
]
},
"Annotated2": {
"type": "object",
"properties": {
"_meta": {
"description": "Optional protocol-level metadata for this content block",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"annotations": {
"anyOf": [
{
"$ref": "#/definitions/Annotations"
},
{
"type": "null"
}
]
},
"resource": {
"$ref": "#/definitions/ResourceContents"
}
},
"required": [
"resource"
]
},
"Annotated3": {
"description": "Represents a resource in the extension with metadata",
"type": "object",
"properties": {
Expand Down Expand Up @@ -244,7 +215,7 @@
"name"
]
},
"Annotated4": {
"Annotated3": {
"type": "object",
"properties": {
"annotations": {
Expand Down Expand Up @@ -1475,7 +1446,7 @@
"resourceTemplates": {
"type": "array",
"items": {
"$ref": "#/definitions/Annotated4"
"$ref": "#/definitions/Annotated3"
}
}
},
Expand All @@ -1502,7 +1473,7 @@
"resources": {
"type": "array",
"items": {
"$ref": "#/definitions/Annotated3"
"$ref": "#/definitions/Annotated2"
}
}
},
Expand Down Expand Up @@ -2142,8 +2113,26 @@
"description": "Embedded server-side resource",
"type": "object",
"properties": {
"_meta": {
"description": "Optional protocol-level metadata for this content block",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"annotations": {
"anyOf": [
{
"$ref": "#/definitions/Annotations"
},
{
"type": "null"
}
]
},
"resource": {
"$ref": "#/definitions/Annotated2"
"$ref": "#/definitions/ResourceContents"
},
"type": {
"type": "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,35 +140,6 @@
]
},
"Annotated2": {
"type": "object",
"properties": {
"_meta": {
"description": "Optional protocol-level metadata for this content block",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"annotations": {
"anyOf": [
{
"$ref": "#/definitions/Annotations"
},
{
"type": "null"
}
]
},
"resource": {
"$ref": "#/definitions/ResourceContents"
}
},
"required": [
"resource"
]
},
"Annotated3": {
"description": "Represents a resource in the extension with metadata",
"type": "object",
"properties": {
Expand Down Expand Up @@ -244,7 +215,7 @@
"name"
]
},
"Annotated4": {
"Annotated3": {
"type": "object",
"properties": {
"annotations": {
Expand Down Expand Up @@ -1475,7 +1446,7 @@
"resourceTemplates": {
"type": "array",
"items": {
"$ref": "#/definitions/Annotated4"
"$ref": "#/definitions/Annotated3"
}
}
},
Expand All @@ -1502,7 +1473,7 @@
"resources": {
"type": "array",
"items": {
"$ref": "#/definitions/Annotated3"
"$ref": "#/definitions/Annotated2"
}
}
},
Expand Down Expand Up @@ -2142,8 +2113,26 @@
"description": "Embedded server-side resource",
"type": "object",
"properties": {
"_meta": {
"description": "Optional protocol-level metadata for this content block",
"type": [
"object",
"null"
],
"additionalProperties": true
},
"annotations": {
"anyOf": [
{
"$ref": "#/definitions/Annotations"
},
{
"type": "null"
}
]
},
"resource": {
"$ref": "#/definitions/Annotated2"
"$ref": "#/definitions/ResourceContents"
},
"type": {
"type": "string",
Expand Down