View the source code for this module on GitHub: https://github.com/mcp-use/mcp-use/blob/main/libraries/typescript/packages/mcp-use/src/server/mcp-server.ts
server.tool(), passing a ToolDefinition (name, optional Zod schema, optional outputSchema, annotations, and widget config) plus a ToolCallback that runs when the tool is invoked. This page documents the registration method and the related types: ToolDefinition, ToolCallback, ToolAnnotations, InputDefinition, and the per-tool widget config.
Methods
Registers a tool on the server and returns the server instance for chaining.tool
Registers a tool that MCP clients and models can invoke to perform actions such as reading files, calling APIs, running commands, or rendering widgets. Each tool has a name, an optional input schema, an optional output schema, and a callback. The callback may be supplied either as thecb field inside the definition or as the second callback argument. When both are present, the second argument wins (callback || toolDefinition.cb). If neither is supplied, the tool is still registered with the MCP SDK but without a handler. Calling tool() again with the same name re-registers (overwrites) that tool, which is how hot module reloading updates a tool during mcp-use dev.
The input type passed to the callback is inferred from the definition’s schema via InferToolInput, and the structured output type is inferred from outputSchema via InferToolOutput, so destructured parameters and structuredContent are fully typed. The HasOAuth type parameter on the server controls whether ctx.auth is available in the callback.
If the definition includes a widget config, tool() automatically attaches widget metadata (for example openai/outputTemplate, openai/toolInvocation/invoking, openai/toolInvocation/invoked, openai/widgetAccessible, and openai/resultCanProduceWidget) to the tool’s _meta, and wraps the callback so an empty text content entry is backfilled with Displaying {widgetName}. See the per-tool widget config below for defaults.
ParametersThe tool definition type. ItsschemaandoutputSchemadrive input/output inference for the callback.
ReturnsThe tool configuration object. SeeToolDefinitionfor every field.Optional handler invoked when the tool is called. Alternative totoolDefinition.cb; if provided it takes precedence overcb.
SignatureThe server instance, enabling method chaining.
Types
ToolDefinition
Describes a single tool: its identity, schemas, callback, annotations, metadata, and optional widget config. This is the object passed as the first argument toserver.tool(). Both schema and outputSchema are Zod schemas, and the type parameters TInput, TOutput, and HasOAuth flow into the cb callback so its parameters and structured output are typed.
SignatureUnique identifier for the tool. Must match the name passed touseCallTool("name")in widget components. Example:"search-products".Human-readable title displayed in clients and the inspector. If omitted,nameis used.LLM-facing description of what the tool does. Helps the model decide when to invoke it.Deprecated. Legacy input parameter definitions. Useschemainstead.Zod schema for input validation. Use.describe()on each field for model hints. Preferred overinputsfor type safety.Zod schema for structured output. Enables output type inference inuseCallTool(); types are generated to.mcp-use/tool-registry.d.tswhen runningmcp-use dev.Async callback that executes the tool. May be omitted here and passed as the second argument toserver.tool()instead.Behavior hints for clients (read-only, destructive, etc.). SeeToolAnnotations.Arbitrary metadata for the tool. When awidgetconfig is present,server.tool()augments this with widget-related keys.Configuration for tools that return a widget via thewidget()helper. Sets up widget metadata at registration time. See the per-tool widget config below.
schema, outputSchema, and annotations together (lifted from the everything example):
ToolCallback
The async handler that runs when a tool is invoked. It receives the validated input parameters as its first argument and an enhanced context object as its second, and must resolve to a tool result. The result may be aTypedCallToolResult<TOutput> (produced by helpers like object() when outputSchema is set) or a plain CallToolResult (produced by helpers like text(), markdown(), mix(), error()), since the SDK validates structuredContent at runtime only when present.
The context exposes LLM sampling via ctx.sample(), progress reporting via ctx.reportProgress(), elicitation via ctx.elicit(), authentication info via ctx.auth (when OAuth is configured, gated by the HasOAuth type parameter), the HTTP request via ctx.req, and all other Hono Context properties. The type is defined through a method-signature helper to enable bivariant parameter checking, which lets callbacks destructure optional fields without explicitly marking them optional.
Parameters (of the callback)Input parameters type, normally inferred from the definition’sschema.Output type that constrainsstructuredContentwhenoutputSchemais defined.Whether OAuth is configured, which controls availability ofctx.auth.
ReturnsThe validated tool input.The enhanced context combiningToolContext(sampling, progress, elicitation) with the Hono request context.
SignatureThe tool result, typically built with a response helper frommcp-use/server.
ToolAnnotations
Behavior hints that describe a tool to clients so they can decide how to present or guard it (for example, marking a tool read-only or warning before a destructive call).ToolAnnotations is re-exported from @modelcontextprotocol/sdk/types.js for convenience and assigned to ToolDefinition.annotations. All fields are optional and act as hints only; they do not change server-side behavior.
Usage (lifted from theHuman-readable title for the tool, for display purposes.If true, the tool does not modify its environment.If true, the tool may perform destructive updates (only meaningful whenreadOnlyHintis false).If true, repeated calls with the same arguments have no additional effect (only meaningful whenreadOnlyHintis false).If true, the tool may interact with an open, external set of entities (for example, the web).
everything example):
InputDefinition
A single legacy input parameter definition, used byToolDefinition.inputs (and by prompt args). It predates Zod schema support. Prefer a Zod schema with .describe() on the ToolDefinition instead, which gives full type inference and richer model guidance. InputDefinition remains available for cases that build parameter lists dynamically.
SignatureParameter name (camelCase or kebab-case). Example:"maxResults".Parameter type.Human-readable description that helps the model understand the parameter.Whether the parameter is required. Defaults tofalse.Default value used when the parameter is omitted. Example:10.
ToolWidgetConfig
The per-tool widget configuration, set as thewidget field of a ToolDefinition. It configures the metadata the server emits so a tool’s result renders as a widget in the inspector and in clients such as ChatGPT. When this config is present and has a name, server.tool() resolves the widget output template (ui://widget/{name}.html, with the server build id appended when set), attaches widget keys to the tool’s _meta, and wraps the callback to backfill an empty text content entry.
This shape is the type of
ToolDefinition.widget. A separate exported WidgetConfig type exists in mcp-use/server, but that one describes a discovered widget directory (its name, path, manifest, and component) and is not the per-tool config used here.SignatureWidget name. Must match a file inresources/(for exampleresources/weather-display.tsx). Example:"weather-display".Status text shown while the tool is running. Defaults toLoading {name}...when omitted.Status text shown after the tool completes. Defaults to{name} readywhen omitted.Whether the widget can initiate tool calls (for example viauseCallTool). Defaults totrue.Whether this tool result can produce a widget. Defaults totrue.
everything example):
See also
Server
The
MCPServer class that hosts these tools, plus its constructor and lifecycle methods.