Skip to content

Commit fbc7ab7

Browse files
authored
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
1 parent b1da5e8 commit fbc7ab7

File tree

17 files changed

+2658
-41
lines changed

17 files changed

+2658
-41
lines changed

crates/rmcp-macros/src/tool.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ pub struct ToolAttribute {
1010
pub description: Option<String>,
1111
/// A JSON Schema object defining the expected parameters for the tool
1212
pub input_schema: Option<Expr>,
13+
/// An optional JSON Schema object defining the structure of the tool's output
14+
pub output_schema: Option<Expr>,
1315
/// Optional additional tool information.
1416
pub annotations: Option<ToolAnnotationsAttribute>,
1517
}
@@ -18,6 +20,7 @@ pub struct ResolvedToolAttribute {
1820
pub name: String,
1921
pub description: Option<String>,
2022
pub input_schema: Expr,
23+
pub output_schema: Option<Expr>,
2124
pub annotations: Expr,
2225
}
2326

@@ -27,19 +30,26 @@ impl ResolvedToolAttribute {
2730
name,
2831
description,
2932
input_schema,
33+
output_schema,
3034
annotations,
3135
} = self;
3236
let description = if let Some(description) = description {
3337
quote! { Some(#description.into()) }
3438
} else {
3539
quote! { None }
3640
};
41+
let output_schema = if let Some(output_schema) = output_schema {
42+
quote! { Some(#output_schema) }
43+
} else {
44+
quote! { None }
45+
};
3746
let tokens = quote! {
3847
pub fn #fn_ident() -> rmcp::model::Tool {
3948
rmcp::model::Tool {
4049
name: #name.into(),
4150
description: #description,
4251
input_schema: #input_schema,
52+
output_schema: #output_schema,
4353
annotations: #annotations,
4454
}
4555
}
@@ -89,6 +99,63 @@ fn none_expr() -> Expr {
8999
syn::parse2::<Expr>(quote! { None }).unwrap()
90100
}
91101

102+
/// Check if a type is Json<T> and extract the inner type T
103+
fn extract_json_inner_type(ty: &syn::Type) -> Option<&syn::Type> {
104+
if let syn::Type::Path(type_path) = ty {
105+
if let Some(last_segment) = type_path.path.segments.last() {
106+
if last_segment.ident == "Json" {
107+
if let syn::PathArguments::AngleBracketed(args) = &last_segment.arguments {
108+
if let Some(syn::GenericArgument::Type(inner_type)) = args.args.first() {
109+
return Some(inner_type);
110+
}
111+
}
112+
}
113+
}
114+
}
115+
None
116+
}
117+
118+
/// Extract schema expression from a function's return type
119+
/// Handles patterns like Json<T> and Result<Json<T>, E>
120+
fn extract_schema_from_return_type(ret_type: &syn::Type) -> Option<Expr> {
121+
// First, try direct Json<T>
122+
if let Some(inner_type) = extract_json_inner_type(ret_type) {
123+
return syn::parse2::<Expr>(quote! {
124+
rmcp::handler::server::tool::cached_schema_for_type::<#inner_type>()
125+
})
126+
.ok();
127+
}
128+
129+
// Then, try Result<Json<T>, E>
130+
let type_path = match ret_type {
131+
syn::Type::Path(path) => path,
132+
_ => return None,
133+
};
134+
135+
let last_segment = type_path.path.segments.last()?;
136+
137+
if last_segment.ident != "Result" {
138+
return None;
139+
}
140+
141+
let args = match &last_segment.arguments {
142+
syn::PathArguments::AngleBracketed(args) => args,
143+
_ => return None,
144+
};
145+
146+
let ok_type = match args.args.first()? {
147+
syn::GenericArgument::Type(ty) => ty,
148+
_ => return None,
149+
};
150+
151+
let inner_type = extract_json_inner_type(ok_type)?;
152+
153+
syn::parse2::<Expr>(quote! {
154+
rmcp::handler::server::tool::cached_schema_for_type::<#inner_type>()
155+
})
156+
.ok()
157+
}
158+
92159
// extract doc line from attribute
93160
fn extract_doc_line(existing_docs: Option<String>, attr: &syn::Attribute) -> Option<String> {
94161
if !attr.path().is_ident("doc") {
@@ -192,12 +259,22 @@ pub fn tool(attr: TokenStream, input: TokenStream) -> syn::Result<TokenStream> {
192259
} else {
193260
none_expr()
194261
};
262+
// Handle output_schema - either explicit or generated from return type
263+
let output_schema_expr = attribute.output_schema.or_else(|| {
264+
// Try to generate schema from return type
265+
match &fn_item.sig.output {
266+
syn::ReturnType::Type(_, ret_type) => extract_schema_from_return_type(ret_type),
267+
_ => None,
268+
}
269+
});
270+
195271
let resolved_tool_attr = ResolvedToolAttribute {
196272
name: attribute.name.unwrap_or_else(|| fn_ident.to_string()),
197273
description: attribute
198274
.description
199275
.or_else(|| fn_item.attrs.iter().fold(None, extract_doc_line)),
200276
input_schema: input_schema_expr,
277+
output_schema: output_schema_expr,
201278
annotations: annotations_expr,
202279
};
203280
let tool_attr_fn = resolved_tool_attr.into_fn(tool_attr_fn_ident)?;

crates/rmcp/src/handler/server/router/tool.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use schemars::JsonSchema;
66
use crate::{
77
handler::server::tool::{
88
CallToolHandler, DynCallToolHandler, ToolCallContext, schema_for_type,
9+
validate_against_schema,
910
},
1011
model::{CallToolResult, Tool, ToolAnnotations},
1112
};
@@ -242,7 +243,23 @@ where
242243
.map
243244
.get(context.name())
244245
.ok_or_else(|| crate::ErrorData::invalid_params("tool not found", None))?;
245-
(item.call)(context).await
246+
247+
let result = (item.call)(context).await?;
248+
249+
// Validate structured content against output schema if present
250+
if let Some(ref output_schema) = item.attr.output_schema {
251+
// When output_schema is defined, structured_content is required
252+
if result.structured_content.is_none() {
253+
return Err(crate::ErrorData::invalid_params(
254+
"Tool with output_schema must return structured_content",
255+
None,
256+
));
257+
}
258+
// Validate the structured content against the schema
259+
validate_against_schema(result.structured_content.as_ref().unwrap(), output_schema)?;
260+
}
261+
262+
Ok(result)
246263
}
247264

248265
pub fn list_all(&self) -> Vec<crate::model::Tool> {

crates/rmcp/src/handler/server/tool.rs

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
1+
//! Tool handler traits and types for MCP servers.
2+
//!
3+
//! This module provides the infrastructure for implementing tools that can be called
4+
//! by MCP clients. Tools can return either unstructured content (text, images) or
5+
//! structured JSON data with schemas.
6+
//!
7+
//! # Structured Output
8+
//!
9+
//! Tools can return structured JSON data using the [`Json`] wrapper type.
10+
//! When using `Json<T>`, the framework will:
11+
//! - Automatically generate a JSON schema for the output type
12+
//! - Validate the output against the schema
13+
//! - Return the data in the `structured_content` field of [`CallToolResult`]
14+
//!
15+
//! # Example
16+
//!
17+
//! ```rust,ignore
18+
//! use rmcp::{tool, Json};
19+
//! use schemars::JsonSchema;
20+
//! use serde::{Serialize, Deserialize};
21+
//!
22+
//! #[derive(Serialize, Deserialize, JsonSchema)]
23+
//! struct AnalysisResult {
24+
//! score: f64,
25+
//! summary: String,
26+
//! }
27+
//!
28+
//! #[tool(name = "analyze")]
29+
//! async fn analyze(&self, text: String) -> Result<Json<AnalysisResult>, String> {
30+
//! Ok(Json(AnalysisResult {
31+
//! score: 0.95,
32+
//! summary: "Positive sentiment".to_string(),
33+
//! }))
34+
//! }
35+
//! ```
36+
137
use std::{
238
any::TypeId, borrow::Cow, collections::HashMap, future::Ready, marker::PhantomData, sync::Arc,
339
};
@@ -10,6 +46,7 @@ use tokio_util::sync::CancellationToken;
1046
pub use super::router::tool::{ToolRoute, ToolRouter};
1147
use crate::{
1248
RoleServer,
49+
handler::server::wrapper::Json,
1350
model::{CallToolRequestParam, CallToolResult, IntoContents, JsonObject},
1451
schemars::generate::SchemaSettings,
1552
service::RequestContext,
@@ -30,6 +67,43 @@ pub fn schema_for_type<T: JsonSchema>() -> JsonObject {
3067
}
3168
}
3269

70+
/// Validate that a JSON value conforms to basic type constraints from a schema.
71+
///
72+
/// Note: This is a basic validation that only checks type compatibility.
73+
/// For full JSON Schema validation, a dedicated validation library would be needed.
74+
pub fn validate_against_schema(
75+
value: &serde_json::Value,
76+
schema: &JsonObject,
77+
) -> Result<(), crate::ErrorData> {
78+
// Basic type validation
79+
if let Some(schema_type) = schema.get("type").and_then(|t| t.as_str()) {
80+
let value_type = get_json_value_type(value);
81+
82+
if schema_type != value_type {
83+
return Err(crate::ErrorData::invalid_params(
84+
format!(
85+
"Value type does not match schema. Expected '{}', got '{}'",
86+
schema_type, value_type
87+
),
88+
None,
89+
));
90+
}
91+
}
92+
93+
Ok(())
94+
}
95+
96+
fn get_json_value_type(value: &serde_json::Value) -> &'static str {
97+
match value {
98+
serde_json::Value::Null => "null",
99+
serde_json::Value::Bool(_) => "boolean",
100+
serde_json::Value::Number(_) => "number",
101+
serde_json::Value::String(_) => "string",
102+
serde_json::Value::Array(_) => "array",
103+
serde_json::Value::Object(_) => "object",
104+
}
105+
}
106+
33107
/// Call [`schema_for_type`] with a cache
34108
pub fn cached_schema_for_type<T: JsonSchema + std::any::Any>() -> Arc<JsonObject> {
35109
thread_local! {
@@ -97,8 +171,26 @@ pub trait FromToolCallContextPart<S>: Sized {
97171
) -> Result<Self, crate::ErrorData>;
98172
}
99173

174+
/// Trait for converting tool return values into [`CallToolResult`].
175+
///
176+
/// This trait is automatically implemented for:
177+
/// - Types implementing [`IntoContents`] (returns unstructured content)
178+
/// - `Result<T, E>` where both `T` and `E` implement [`IntoContents`]
179+
/// - [`Json<T>`](crate::handler::server::wrapper::Json) where `T` implements [`Serialize`] (returns structured content)
180+
/// - `Result<Json<T>, E>` for structured results with errors
181+
///
182+
/// The `#[tool]` macro uses this trait to convert tool function return values
183+
/// into the appropriate [`CallToolResult`] format.
100184
pub trait IntoCallToolResult {
101185
fn into_call_tool_result(self) -> Result<CallToolResult, crate::ErrorData>;
186+
187+
/// Returns the output schema for this type, if any.
188+
///
189+
/// This is used by the macro to automatically generate output schemas
190+
/// for tool functions that return structured data.
191+
fn output_schema() -> Option<Arc<JsonObject>> {
192+
None
193+
}
102194
}
103195

104196
impl<T: IntoContents> IntoCallToolResult for T {
@@ -125,6 +217,40 @@ impl<T: IntoCallToolResult> IntoCallToolResult for Result<T, crate::ErrorData> {
125217
}
126218
}
127219

220+
// Implementation for Json<T> to create structured content
221+
impl<T: Serialize + JsonSchema + 'static> IntoCallToolResult for Json<T> {
222+
fn into_call_tool_result(self) -> Result<CallToolResult, crate::ErrorData> {
223+
let value = serde_json::to_value(self.0).map_err(|e| {
224+
crate::ErrorData::internal_error(
225+
format!("Failed to serialize structured content: {}", e),
226+
None,
227+
)
228+
})?;
229+
230+
Ok(CallToolResult::structured(value))
231+
}
232+
233+
fn output_schema() -> Option<Arc<JsonObject>> {
234+
Some(cached_schema_for_type::<T>())
235+
}
236+
}
237+
238+
// Implementation for Result<Json<T>, E>
239+
impl<T: Serialize + JsonSchema + 'static, E: IntoContents> IntoCallToolResult
240+
for Result<Json<T>, E>
241+
{
242+
fn into_call_tool_result(self) -> Result<CallToolResult, crate::ErrorData> {
243+
match self {
244+
Ok(value) => value.into_call_tool_result(),
245+
Err(error) => Ok(CallToolResult::error(error.into_contents())),
246+
}
247+
}
248+
249+
fn output_schema() -> Option<Arc<JsonObject>> {
250+
Json::<T>::output_schema()
251+
}
252+
}
253+
128254
pin_project_lite::pin_project! {
129255
#[project = IntoCallToolResultFutProj]
130256
pub enum IntoCallToolResultFut<F, R> {
Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
1-
use serde::Serialize;
1+
use std::borrow::Cow;
22

3-
use crate::model::IntoContents;
3+
use schemars::JsonSchema;
44

5-
/// Json wrapper
5+
/// Json wrapper for structured output
66
///
7-
/// This is used to tell the SDK to serialize the inner value into json
7+
/// When used with tools, this wrapper indicates that the value should be
8+
/// serialized as structured JSON content with an associated schema.
9+
/// The framework will place the JSON in the `structured_content` field
10+
/// of the tool result rather than the regular `content` field.
811
pub struct Json<T>(pub T);
912

10-
impl<T> IntoContents for Json<T>
11-
where
12-
T: Serialize,
13-
{
14-
fn into_contents(self) -> Vec<crate::model::Content> {
15-
let result = crate::model::Content::json(self.0);
16-
debug_assert!(
17-
result.is_ok(),
18-
"Json wrapped content should be able to serialized into json"
19-
);
20-
match result {
21-
Ok(content) => vec![content],
22-
Err(e) => {
23-
tracing::error!("failed to convert json content: {e}");
24-
vec![]
25-
}
26-
}
13+
// Implement JsonSchema for Json<T> to delegate to T's schema
14+
impl<T: JsonSchema> JsonSchema for Json<T> {
15+
fn schema_name() -> Cow<'static, str> {
16+
T::schema_name()
17+
}
18+
19+
fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
20+
T::json_schema(generator)
2721
}
2822
}

0 commit comments

Comments
 (0)