Skip to main content

Define input schemas

Every tool can have an inputSchema 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-generic registerTool() overload infers the callback input without a runtime dependency.
json-schema-tool.ts

Reusable JSON Schema constant

When a schema is declared separately, use as 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
Use this when you already use Zod and want to advertise its JSON Schema while validating native calls inside the handler.
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.
Native Chrome receives the emitted JSON Schema metadata; it does not execute your schema library’s refinements before invoking the page handler. Parse inside execute when native calls must enforce schema-library-specific rules.

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

An outputSchema 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
Typing the descriptor separately checks 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
If outputSchema is defined, the tool implementation must return a JSON-serializable value that matches the schema for MCP-B helpers that consume it. Native WebMCP does not enforce this field. The MCP transport bridge currently forwards object-shaped structuredContent; use the feature-detected Chrome-compatible executeTool() path for primitive or array structured outputs until downstream MCP clients accept SEP-2106 shapes.

Keep text and structure aligned

When you construct an MCP response directly, keep content and structuredContent aligned. Text is for chat UIs; structured content is for programmatic consumption.
With usewebmcp, return the raw data instead. The hook creates text content and preserves a JSON-compatible value as structuredContent.

When to use output schemas

Add outputSchema 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.
Skip 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: false where they reduce ambiguity.
  • Return concise human-readable content and machine-readable structuredContent when 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