Skip to content

Commit 5c1949e

Browse files
committed
add Tool.outputSchema
1 parent dc4fda9 commit 5c1949e

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

docs/specification/draft/server/tools.mdx

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ A tool definition includes:
181181
- `name`: Unique identifier for the tool
182182
- `description`: Human-readable description of functionality
183183
- `inputSchema`: JSON Schema defining expected parameters
184+
- `outputSchema`: Optional JSON Schema defining the expected output structure
184185
- `annotations`: optional properties describing tool behavior
185186

186187
<Warning>For trust & safety and security, clients **MUST** consider
@@ -235,6 +236,76 @@ or data, behind a URI that can be subscribed to or fetched again by the client l
235236
}
236237
```
237238

239+
### Output Schema
240+
241+
The optional `outputSchema` property provides a JSON Schema that describes the expected structure of the tool's output. This schema applies to the `text` property of the TextContent object returned by the tool.
242+
243+
When a tool specifies an `outputSchema`:
244+
245+
1. Clients **SHOULD** validate that the tool's result:
246+
- Contains exactly one content item
247+
- This item is a `TextContent` object
248+
- The `text` property of this object validates against the schema
249+
250+
2. Servers **SHOULD**:
251+
- Provide responses that conform to their declared schema
252+
- Use proper JSON or structured formats in their responses when the schema specifies structured data
253+
254+
Example tool with output schema:
255+
256+
```json
257+
{
258+
"name": "search_database",
259+
"description": "Search a database for records",
260+
"inputSchema": {
261+
"type": "object",
262+
"properties": {
263+
"query": {
264+
"type": "string",
265+
"description": "Search query"
266+
}
267+
},
268+
"required": ["query"]
269+
},
270+
"outputSchema": {
271+
"type": "array",
272+
"items": {
273+
"type": "object",
274+
"properties": {
275+
"id": { "type": "string" },
276+
"title": { "type": "string" },
277+
"url": { "type": "string" }
278+
},
279+
"required": ["id", "title"]
280+
}
281+
}
282+
}
283+
```
284+
285+
Example valid response for this tool:
286+
287+
```json
288+
{
289+
"jsonrpc": "2.0",
290+
"id": 5,
291+
"result": {
292+
"content": [
293+
{
294+
"type": "text",
295+
"text": "[{\"id\":\"doc-1\",\"title\":\"Introduction to MCP\",\"url\":\"https://example.com/docs/1\"},{\"id\":\"doc-2\",\"title\":\"Tool Usage Guide\",\"url\":\"https://example.com/docs/2\"}]"
296+
}
297+
]
298+
}
299+
}
300+
```
301+
302+
The `outputSchema` helps clients and LLMs understand and properly handle tool outputs by:
303+
304+
- Enabling schema validation of responses
305+
- Providing type information for better integration with programming languages
306+
- Guiding LLMs to properly parse and utilize the returned data
307+
- Supporting better documentation and developer experience
308+
238309
## Error Handling
239310

240311
Tools use two error reporting mechanisms:

schema/draft/schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,6 +2049,12 @@
20492049
"name": {
20502050
"description": "The name of the tool.",
20512051
"type": "string"
2052+
},
2053+
"outputSchema": {
2054+
"additionalProperties": true,
2055+
"description": "A JSON Schema object defining the structure of the tool's output.\n\nIf set, a client SHOULD validate a CallToolResult for this Tool as follows:\n1. check that the length of the result's `content` property == 1\n2. check that this single item is a `TextContent` object\n3. validate this object's `text` property against this schema.\n\nIn other words, for a CallToolResult to be valid with respect to this schema,\nit must first satisfy the precondition that its payload be a single TextContent \nobject.",
2056+
"properties": {},
2057+
"type": "object"
20522058
}
20532059
},
20542060
"required": [

schema/draft/schema.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,20 @@ export interface Tool {
803803
required?: string[];
804804
};
805805

806+
/**
807+
* A JSON Schema object defining the structure of the tool's output.
808+
*
809+
* If set, a client SHOULD validate a CallToolResult for this Tool as follows:
810+
* 1. check that the length of the result's `content` property == 1
811+
* 2. check that this single item is a `TextContent` object
812+
* 3. validate this object's `text` property against this schema.
813+
*
814+
* In other words, for a CallToolResult to be valid with respect to this schema,
815+
* it must first satisfy the precondition that its payload be a single TextContent
816+
* object.
817+
*/
818+
outputSchema?: object;
819+
806820
/**
807821
* Optional additional tool information.
808822
*/

0 commit comments

Comments
 (0)