Define input schemas
Every tool can have aninputSchema that describes the arguments it accepts. MCP transport calls
validate the schema before calling execute. Direct WebMCP and testing-shim calls do not guarantee
validation, so handlers should validate untrusted input when required.
Literal JSON Schema
Write the schema inline as a plain object. The const-genericregisterTool()
overload infers the callback input without a runtime dependency.
json-schema-tool.ts
Reusable JSON Schema constant
When a schema is declared separately, useas const satisfies JsonSchemaForInference to preserve its literal type and check the schema shape.
reusable-schema-tool.ts
Inference works only with literal schemas. If the schema type is widened (for example, loaded from
an API at runtime),
args falls back to an object-or-array WebMCP input.Standard JSON Schema with Zod
MCP-B runtimes accept Standard JSON Schema v1 objects that expose~standard.jsonSchema.input(). This guide uses the Zod 4.2 path
covered by the repository’s compatibility tests. The emitted JSON Schema is
used for browser discovery. MCP transport calls use ~standard.validate() when
present. Direct WebMCP and testing-shim calls pass input to the handler, so
validate inside execute when those routes matter.
With Zod 4.2 or newer:
Install schema dependencies
zod-tool.ts
The MCP-B schema adapter rejects validation-only Standard Schema objects because WebMCP callers
need advertised JSON Schema. It attempts Standard JSON Schema conversion with
draft-2020-12
first, then falls back to draft-07.Standard JSON Schema with @mcp-b/react-webmcp
The React hooks in @mcp-b/react-webmcp accept the same Standard JSON Schema objects for input
schemas. Pass the schema object itself, not a raw Zod shape map:
SearchTool.tsx
Choosing a schema style
Define output schemas
AnoutputSchema describes the intended shape of structured tool data. It
drives TypeScript inference and MCP-B metadata; it does not validate a return
value or add structuredContent by itself. A handler that constructs an MCP
response directly should return both human-readable content and
machine-readable structuredContent when clients need both.
Add an output schema
output-schema-tool.ts
structuredContent against a literal outputSchema while registration still uses the canonical document API. MCP-B consumes the extra metadata; native Chrome and the strict polyfill ignore it. The current W3C and Chrome tool dictionary defines inputSchema only. MCP SEP-2106 applies to MCP tool output schemas and structured content.
Non-object output schemas
Use array or primitive output schemas when the natural result is not an object:array-output-tool.ts
Output schemas with usewebmcp
The usewebmcp hook infers state.lastResult from the output schema:
CounterTool.tsx
Keep text and structure aligned
When you construct an MCP response directly, keepcontent and structuredContent aligned. Text is
for chat UIs; structured content is for programmatic consumption.
usewebmcp, return the raw data instead. The hook creates text content and preserves a
JSON-compatible value as structuredContent.
When to use output schemas
AddoutputSchema when:
- The AI agent needs to pass your tool’s result to another tool (structured data enables chaining).
- You want compile-time type checking on the return value.
- You need the agent to parse specific fields from the response rather than interpreting free text.
outputSchema when the tool returns simple text responses where structured parsing adds no value.
Tool design checklist
Before publishing a tool:- Give it one clear job and use a stable, action-oriented name.
- State when an agent should use it, including important exclusions.
- Keep required inputs minimal and describe every property.
- Use enums, bounds, formats, and
additionalProperties: falsewhere they reduce ambiguity. - Return concise human-readable
contentand machine-readablestructuredContentwhen another tool may consume the result. - Mark destructive or externally visible actions with appropriate annotations and require confirmation in the product UI.
- Test success, validation failure, permission failure, and cancellation paths.
Further reading
- Chrome’s WebMCP best practices for tool naming, descriptions, and schema design.
@mcp-b/webmcp-typesreference for the full list of schema types and inference utilities.- Add Tools to an Existing App for the broader pattern of integrating tools into a working application.
