Define input schemas
Every tool can have aninputSchema that describes the arguments it accepts. The runtime uses this schema to validate input from AI agents before calling your execute function.
Literal JSON Schema
Write the schema as a plain object. This works with every WebMCP runtime and requires no additional dependencies.json-schema-tool.ts
args parameter.
Literal JSON Schema with type inference
Addas const satisfies JsonSchemaForInference to preserve the literal schema type. TypeScript then infers args from the schema at compile time.
inferred-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 Record<string, unknown>.Standard Schema (Zod, Valibot, ArkType)
The polyfill accepts any Standard Schema v1 object asinputSchema. This includes Zod v4, Valibot, and ArkType validators. The runtime extracts JSON Schema for agent discovery and validates input using the schema’s ~standard.validate function.
With Zod v4:
zod-tool.ts
When both a Standard Schema validator and Standard JSON Schema are present on the same object,
JSON Schema conversion is preferred for validation parity. The runtime attempts conversion with
draft-2020-12 first, then falls back to draft-07.Zod with @mcp-b/react-webmcp
The React hooks in @mcp-b/react-webmcp accept Zod schemas in a shorthand form where you pass the shape directly (without wrapping in z.object):
SearchTool.tsx
Choosing a schema style
Define output schemas
AnoutputSchema describes the shape of the structured data your tool returns. When present, the tool response includes both a human-readable content array and a machine-readable structuredContent value.
Add an output schema
output-schema-tool.ts
outputSchema is a literal schema and you use @mcp-b/webmcp-types, structuredContent is type-checked against the schema at compile time. MCP-B runtime helpers can use it to shape structured MCP responses. Native Chrome WebMCP does not yet define or enforce outputSchema; 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
Always return bothcontent and structuredContent. The text in content is for display in chat UIs. The structuredContent is for programmatic consumption by the AI agent.
content and return only the structured data, the runtime wraps it in a text content block with pretty-printed JSON.
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
@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.
