rust-sdk/crates/rmcp/tests/test_structured_output.rs
Jean-Marc Le Roux fbc7ab70ca
feat: Add support for Tool.outputSchema and CallToolResult.structuredContent (#316)
* feat: add output_schema field to Tool struct

- Add optional output_schema field to Tool struct for defining tool
output structure
- Update Tool::new() to initialize output_schema as None

* feat: add structured_content field to CallToolResult

- Add optional structured_content field for JSON object results
- Make content field optional to support either structured or
unstructured results
- Add CallToolResult::structured() and structured_error() constructor
methods

* feat: implement validation for mutually exclusive content/structuredContent

- Add validate() method to ensure content and structured_content are
mutually exclusive
- Implement custom Deserialize to enforce validation during
deserialization
- Update documentation to clarify the mutual exclusivity requirement

* feat: add output_schema support to #[tool] macro

- Add output_schema field to ToolAttribute and ResolvedToolAttribute
structs
- Implement automatic output schema generation from return types
- Support explicit output_schema attribute for manual specification
- Generate schemas for Result<T, E> where T is not CallToolResult
- Update tool generation to include output_schema in Tool struct

* feat: implement IntoCallToolResult for structured content

- Add Structured<T> wrapper type for explicit structured content
- Implement IntoCallToolResult for Structured<T> with JSON serialization
- Add support for Result<Structured<T>, E> conversions
- Enable tools to return structured content through the trait system

* fix: update simple-chat-client example for optional content field

- Handle Option<Vec<Content>> in CallToolResult.content
- Add proper unwrapping for the optional content field
- Fix compilation error in chat.rs

* fix: update examples and tests for optional content field

- Add output_schema field to Tool initialization in sampling_stdio
example
- Update test_tool_macros tests to handle Option<Vec<Content>>
- Use as_ref() before calling first() on optional content field

* feat: implement basic schema validation in conversion logic

- Add validate_against_schema function for basic type validation
- Add note that full JSON Schema validation requires dedicated library
- Document that actual validation should happen in tool handler

* feat: add structured output support for tools

- Add output_schema field to Tool struct for defining output JSON schemas
- Add structured_content field to CallToolResult (mutually exclusive with content)
- Implement Structured<T> wrapper for type-safe structured outputs
- Update #[tool] macro to automatically generate output schemas from return types
- Add validation of structured outputs against their schemas
- Update all examples and tests for breaking change (CallToolResult.content now Option)
- Add comprehensive documentation and rustdoc
- Add structured_output example demonstrating the feature

BREAKING CHANGE: CallToolResult.content is now Option<Vec<Content>> instead of Vec<Content>

Closes #312

* fix: correct structured output doctest to use Parameters wrapper

The #[tool] macro requires Parameters<T> wrapper for tool inputs.
This fixes the pre-existing broken doctest in the structured output
documentation example.

* feat: replace Structured<T> with Json<T> for structured output

- Remove Structured<T> type definition and implementations
- Reuse existing Json<T> wrapper for structured content
- Update IntoCallToolResult implementations to use Json<T>
- Add JsonSchema implementation for Json<T> delegating to T
- Update all examples and tests to use Json<T> instead of Structured<T>
- Update documentation and exports

BREAKING CHANGE: Structured<T> has been replaced with Json<T>. Users must update their code to use Json<T> for structured tool outputs.

* feat: add output_schema() method to IntoCallToolResult trait

- Add output_schema() method with default None implementation
- Implement output_schema() for Json<T> to return cached schema
- Implement output_schema() for Result<Json<T>, E> delegating to Json<T>
- Enable trait-based schema generation for structured outputs

* feat: update macro to detect Json<T> wrapper for output schemas

- Add extract_json_inner_type() helper to detect Json<T> types
- Update schema generation to only occur for Json<T> wrapped types
- Remove generic Result<T, E> detection in favor of specific Json<T> detection
- Add comprehensive tests to verify schema generation behavior

* feat: add builder methods to Tool struct for setting schemas

- Add with_output_schema<T>() method to set output schema from type
- Add with_input_schema<T>() method to set input schema from type
- Both methods use cached_schema_for_type internally
- Add comprehensive tests for builder methods

* fix: address clippy warnings

- Add Default implementation for StructuredOutputServer
- Fix collapsible else-if in simple-chat-client
- No functional changes

* style: apply cargo fmt

Apply automatic formatting changes to:
- examples/simple-chat-client/src/chat.rs - fix line wrapping
- crates/rmcp-macros/src/tool.rs - format method chaining
- examples/servers/src/structured_output.rs - reorder imports and format function signatures

* chore: fix formatting

* chore: fix rustdoc redundant link warning

* refactor: validate_against_schema

* feat: enforce structured_content usage when output_schema is defined

This commit implements strict validation to ensure tools with output_schema
consistently use structured_content for both success and error responses.

Changes:
- Enhanced ToolRouter::call() validation to require structured_content when output_schema is present
- Added validation that tools with output_schema cannot use regular content field
- Added comprehensive tests covering the new strict validation behavior
- Created example demonstrating proper structured output usage
- Updated TODO.md to track validation improvements

This ensures consistent response format and better type safety for MCP clients.

* chore: remove TODO.md

* refactor: simplify output schema extraction logic in tool macro

- Extract complex nested logic into dedicated helper function
- Replace deeply nested if-else chains with functional approach
- Use early returns and ? operator for cleaner code flow
- Reduce 46 lines to 7 lines in main logic while improving readability

* chore: run cargo fmt

* fix: enforce structured_content usage when output_schema is defined

Structured content is returned as a JSON object in the structuredContent
field of a result.For backwards compatibility, a tool that returns
structured content SHOULD also return the serialized JSON in a
TextContent block.

https://modelcontextprotocol.io/specification/2025-06-18/server/tools#structured-content

Tools may also provide an output schema for validation of structured
results. If an output schema is provided:

- Servers MUST provide structured results that conform to this schema.
- Clients SHOULD validate structured results against this schema.

https://modelcontextprotocol.io/specification/2025-06-18/server/tools#output-schema

* chore: cargo fmt
2025-07-31 16:32:24 +08:00

233 lines
7.2 KiB
Rust

//cargo test --test test_structured_output --features "client server macros"
use rmcp::{
Json, ServerHandler,
handler::server::{router::tool::ToolRouter, tool::Parameters},
model::{CallToolResult, Content, Tool},
tool, tool_handler, tool_router,
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::json;
#[derive(Serialize, Deserialize, JsonSchema)]
pub struct CalculationRequest {
pub a: i32,
pub b: i32,
}
#[derive(Serialize, Deserialize, JsonSchema)]
pub struct CalculationResult {
pub sum: i32,
pub product: i32,
}
#[derive(Serialize, Deserialize, JsonSchema)]
pub struct UserInfo {
pub name: String,
pub age: u32,
}
#[tool_handler(router = self.tool_router)]
impl ServerHandler for TestServer {}
#[derive(Debug, Clone)]
pub struct TestServer {
tool_router: ToolRouter<Self>,
}
impl Default for TestServer {
fn default() -> Self {
Self::new()
}
}
#[tool_router(router = tool_router)]
impl TestServer {
pub fn new() -> Self {
Self {
tool_router: Self::tool_router(),
}
}
/// Tool that returns structured output
#[tool(name = "calculate", description = "Perform calculations")]
pub async fn calculate(
&self,
params: Parameters<CalculationRequest>,
) -> Result<Json<CalculationResult>, String> {
Ok(Json(CalculationResult {
sum: params.0.a + params.0.b,
product: params.0.a * params.0.b,
}))
}
/// Tool that returns regular string output
#[tool(name = "get-greeting", description = "Get a greeting")]
pub async fn get_greeting(&self, name: Parameters<String>) -> String {
format!("Hello, {}!", name.0)
}
/// Tool that returns structured user info
#[tool(name = "get-user", description = "Get user info")]
pub async fn get_user(&self, user_id: Parameters<String>) -> Result<Json<UserInfo>, String> {
if user_id.0 == "123" {
Ok(Json(UserInfo {
name: "Alice".to_string(),
age: 30,
}))
} else {
Err("User not found".to_string())
}
}
}
#[tokio::test]
async fn test_tool_with_output_schema() {
let server = TestServer::new();
let tools = server.tool_router.list_all();
// Find the calculate tool
let calculate_tool = tools.iter().find(|t| t.name == "calculate").unwrap();
// Verify it has an output schema
assert!(calculate_tool.output_schema.is_some());
let schema = calculate_tool.output_schema.as_ref().unwrap();
// Check that the schema contains expected fields
let schema_str = serde_json::to_string(schema).unwrap();
assert!(schema_str.contains("sum"));
assert!(schema_str.contains("product"));
}
#[tokio::test]
async fn test_tool_without_output_schema() {
let server = TestServer::new();
let tools = server.tool_router.list_all();
// Find the get-greeting tool
let greeting_tool = tools.iter().find(|t| t.name == "get-greeting").unwrap();
// Verify it doesn't have an output schema (returns String)
assert!(greeting_tool.output_schema.is_none());
}
#[tokio::test]
async fn test_structured_content_in_call_result() {
// Test creating a CallToolResult with structured content
let structured_data = json!({
"sum": 7,
"product": 12
});
let result = CallToolResult::structured(structured_data.clone());
assert!(result.content.is_none());
assert!(result.structured_content.is_some());
assert_eq!(result.structured_content.unwrap(), structured_data);
assert_eq!(result.is_error, Some(false));
}
#[tokio::test]
async fn test_structured_error_in_call_result() {
// Test creating a CallToolResult with structured error
let error_data = json!({
"error_code": "NOT_FOUND",
"message": "User not found"
});
let result = CallToolResult::structured_error(error_data.clone());
assert!(result.content.is_none());
assert!(result.structured_content.is_some());
assert_eq!(result.structured_content.unwrap(), error_data);
assert_eq!(result.is_error, Some(true));
}
#[tokio::test]
async fn test_mutual_exclusivity_validation() {
// Test that content and structured_content are mutually exclusive
let content_result = CallToolResult::success(vec![Content::text("Hello")]);
let structured_result = CallToolResult::structured(json!({"message": "Hello"}));
// Verify the validation
assert!(content_result.validate().is_ok());
assert!(structured_result.validate().is_ok());
// Try to create an invalid result with both fields
let invalid_json = json!({
"content": [{"type": "text", "text": "Hello"}],
"structuredContent": {"message": "Hello"}
});
// The deserialization itself should fail due to validation
let deserialized: Result<CallToolResult, _> = serde_json::from_value(invalid_json);
assert!(deserialized.is_err());
}
#[tokio::test]
async fn test_structured_return_conversion() {
// Test that Json<T> converts to CallToolResult with structured_content
let calc_result = CalculationResult {
sum: 7,
product: 12,
};
let structured = Json(calc_result);
let result: Result<CallToolResult, rmcp::ErrorData> =
rmcp::handler::server::tool::IntoCallToolResult::into_call_tool_result(structured);
assert!(result.is_ok());
let call_result = result.unwrap();
assert!(call_result.content.is_none());
assert!(call_result.structured_content.is_some());
let structured_value = call_result.structured_content.unwrap();
assert_eq!(structured_value["sum"], 7);
assert_eq!(structured_value["product"], 12);
}
#[tokio::test]
async fn test_tool_serialization_with_output_schema() {
let server = TestServer::new();
let tools = server.tool_router.list_all();
let calculate_tool = tools.iter().find(|t| t.name == "calculate").unwrap();
// Serialize the tool
let serialized = serde_json::to_value(calculate_tool).unwrap();
// Check that outputSchema is included
assert!(serialized["outputSchema"].is_object());
// Deserialize back
let deserialized: Tool = serde_json::from_value(serialized).unwrap();
assert!(deserialized.output_schema.is_some());
}
#[tokio::test]
async fn test_output_schema_requires_structured_content() {
// Test that tools with output_schema must use structured_content
let server = TestServer::new();
let tools = server.tool_router.list_all();
// The calculate tool should have output_schema
let calculate_tool = tools.iter().find(|t| t.name == "calculate").unwrap();
assert!(calculate_tool.output_schema.is_some());
// Directly call the tool and verify its result structure
let params = rmcp::handler::server::tool::Parameters(CalculationRequest { a: 5, b: 3 });
let result = server.calculate(params).await.unwrap();
// Convert the Json<CalculationResult> to CallToolResult
let call_result: Result<CallToolResult, rmcp::ErrorData> =
rmcp::handler::server::tool::IntoCallToolResult::into_call_tool_result(result);
assert!(call_result.is_ok());
let call_result = call_result.unwrap();
// Verify it has structured_content and no content
assert!(call_result.structured_content.is_some());
assert!(call_result.content.is_none());
}