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
{{ message }}
Repository navigation
Commit 5ee54ee
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: .changeset/register-tool-output-typing.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,13 +2,13 @@
2
2
'@modelcontextprotocol/server': minor
3
3
---
4
4
5
-
`McpServer.registerTool` now type-checks a callback's successful result against the tool's `outputSchema`, for Standard Schema and deprecated raw-shape schemas. A successful result must include `structuredContent` that matches the schema and is not `undefined`; `isError: true` results, input-required results and tools without an `outputSchema` are unchanged. Runtime output validation is unchanged.
5
+
`McpServer.registerTool` now type-checks a callback's successful result against the tool's `outputSchema`, for Standard Schema and deprecated raw-shape schemas. A successful result must include `structuredContent` that matches the schema and is not `undefined`; `isError: true` results, input-required results, tools without an `outputSchema`, and tools whose schema has an `unknown` output type (such as an untyped `fromJsonSchema(...)`) are unchanged. Runtime output validation is unchanged.
6
6
7
7
New exported types: `ToolResultFor`, `ToolStructuredContent`, `ToolResultWithStructuredContent`, `ToolErrorResult`. `ToolCallback` takes an optional second type parameter for the output schema.
8
8
9
-
Code that compiled before can now fail to compile, for a tool with an`outputSchema`, when the callback:
9
+
Code that compiled before can now fail to compile, for a tool with a typed`outputSchema`, when the callback:
10
10
11
11
- returns `structuredContent` that does not match the schema, or a successful result without it;
12
12
- builds the value from a wider type than the schema (e.g. `string` for an enum);
13
13
- has its result typed as plain `CallToolResult` (a `Promise<CallToolResult>` annotation, a helper returning `CallToolResult`, or `ToolCallback<typeof inputSchema>`); use `ToolCallback<typeof inputSchema, typeof outputSchema>` or drop the annotation;
14
-
- is registered with an `outputSchema`typed `S | undefined`, which is checked as if present.
14
+
- is registered with a typed `outputSchema`that may be `undefined`, which is checked as if present.
A `McpServer` tool result with `structuredContent` and no `content` now gets a text block holding the serialized JSON of `structuredContent`, as the tools spec recommends, instead of `content: []`. This applies to every tool result, with or without an `outputSchema` or `isError`. A result that supplies `content`, even an empty array, is handled as before. To match, successful results of a tool with a typed `outputSchema` may omit `content` in TypeScript, both in `registerTool` and in `RegisteredTool.update({ callback })`.
Copy file name to clipboardExpand all lines: docs/servers/tools.md
+3-1Lines changed: 3 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -116,12 +116,14 @@ server.registerTool(
116
116
);
117
117
```
118
118
119
-
TypeScript checks the callback against `outputSchema`: in this example, returning a string, leaving out `price`, or returning `price: '24'` is a compile error, and so is a successful result with no `structuredContent`. Results with `isError: true` and [input-required results](input-required.md) are exempt. `structuredContent: undefined` counts as missing; `null` is fine when the schema allows it.
119
+
TypeScript checks the callback against `outputSchema`: in this example, returning a string, leaving out `price`, or returning `price: '24'` is a compile error, and so is a successful result with no `structuredContent`. Results with `isError: true` and [input-required results](input-required.md) are exempt. `structuredContent: undefined` counts as missing; `null` is fine when the schema allows it. A schema with no static output type, such as an untyped `fromJsonSchema(...)`, is not checked at compile time.
120
120
121
121
The SDK validates the value you return and sends it as is, without the parsed result. So the type TypeScript asks for is one that satisfies both the input and the output side of the schema: fields with a `.default()` must be supplied, coerced fields must already have the output type, and a `.transform()` whose output is not a valid input cannot be satisfied at all. Values built from wider types, such as a `string` that should be one of an enum's members, need narrowing or a `satisfies` annotation before you return them.
122
122
123
123
The SDK also validates `structuredContent` against `outputSchema` at runtime before the result leaves your server, and advertises the derived JSON Schema in `tools/list` so clients can validate it too. The runtime check is still the one that counts: it covers JavaScript callers, values cast with `as`, and constraints TypeScript cannot express, such as `.min()`, `.email()` or `.refine()`.
124
124
125
+
With a typed `outputSchema` you may leave out `content`; the server then sends the JSON of `structuredContent` as a single text block, so clients that ignore `structuredContent` still see the result. Supply your own `content` to control that text.
126
+
125
127
Calling `product-details` with `{ name: 'Travel mug' }` returns both renderings:
0 commit comments