You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add Tool output_schema support with comprehensive validation
This commit implements optional JSON Schema validation for tool outputs according to the MCP specification:
- Add MCP::Tool::OutputSchema class mirroring InputSchema functionality
- Support output_schema in class definitions, Tool.define, and OutputSchema objects
- Include validate_result method for runtime validation
- Update MCP::Client::Tool to track output schemas
- Add comprehensive test coverage with 249 passing tests
- Update README with dedicated Tool Output Schemas section
- Follow MCP spec requirements for server/client validation
Copy file name to clipboardExpand all lines: README.md
+105-1Lines changed: 105 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -385,6 +385,14 @@ class MyTool < MCP::Tool
385
385
},
386
386
required: ["message"]
387
387
)
388
+
output_schema(
389
+
properties: {
390
+
result: { type:"string" },
391
+
success: { type:"boolean" },
392
+
timestamp: { type:"string", format:"date-time" }
393
+
},
394
+
required: ["result", "success", "timestamp"]
395
+
)
388
396
annotations(
389
397
read_only_hint:true,
390
398
destructive_hint:false,
@@ -448,6 +456,102 @@ Tools can include annotations that provide additional metadata about their behav
448
456
449
457
Annotations can be set either through the class definition using the `annotations` class method or when defining a tool using the `define` method.
450
458
459
+
### Tool Output Schemas
460
+
461
+
Tools can optionally define an `output_schema` to specify the expected structure of their results. This works similarly to how `input_schema` is defined and can be used in three ways:
MCP spec for the [Output Schema](https://modelcontextprotocol.io/specification/2025-06-18/server/tools#output-schema) specifies that:
547
+
548
+
-**Server Validation**: Servers MUST provide structured results that conform to the output schema
549
+
-**Client Validation**: Clients SHOULD validate structured results against the output schema
550
+
-**Better Integration**: Enables strict schema validation, type information, and improved developer experience
551
+
-**Backward Compatibility**: Tools returning structured content SHOULD also include serialized JSON in a TextContent block
552
+
553
+
The output schema follows standard JSON Schema format and helps ensure consistent data exchange between MCP servers and clients.
554
+
451
555
### Prompts
452
556
453
557
MCP spec includes [Prompts](https://modelcontextprotocol.io/specification/2025-06-18/server/prompts), which enable servers to define reusable prompt templates and workflows that clients can easily surface to users and LLMs.
@@ -755,7 +859,7 @@ The client provides a wrapper class for tools returned by the server:
755
859
756
860
-`MCP::Client::Tool` - Represents a single tool with its metadata
757
861
758
-
This class provide easy access to tool properties like name, description, and input schema.
862
+
This class provides easy access to tool properties like name, description, input schema, and output schema.
0 commit comments