Basic Usage
Signature
The decorator is generic over the input shapeI and (optionally) the output
schema O. It returns a class decorator that rewraps the decorated class,
inferring the ToolContext input/output type parameters from the options — so
you write a plain class … extends ToolContext with no explicit generics. The
signature below is illustrative:
Type Safety
The@Tool decorator provides compile-time type checking:
- Input validation: The
execute()parameter type must exactly match theinputSchema. Mismatches produce a descriptive error at compile time. - Output validation: When
outputSchemais provided, theexecute()return type must be assignable to the inferred output type. - Context check: The decorated class must extend
ToolContext. Using@Toolon a plain class produces a compile error. - Invalid options: Typos in decorator options (e.g.,
concurrency: { maxConcurrensst: 5 }) are caught at compile time with specific error messages, without losing autocomplete on other fields.
Configuration Options
Required Properties
Optional Properties
Output Types
outputSchema, a plain value (string, number, boolean or array) is sent wrapped as { value: … }. Use a primitive literal to send it as is.
Authorization
authProviders entries default to required: true. A required: true provider
gates the call before execute() when its credential isn’t connected for the
session (-32001 / data.authUrl); set required: false for optional providers,
which never gate. The gate is a no-op for tools with no authProviders and for
unauthenticated / public requests.
Annotations
Examples
UI Configuration
Theui.template field accepts a FileSource (recommended), a builder function, an HTML or Markdown string, or a React component. MDX is not compiled: a string containing < and > is used as HTML.
html tagged template, which escapes interpolated values (plain string results render as markup unless escapeStringResults: true):
ui options (widgetDescription, widgetAccessible, displayMode, hydrate, mdxComponents, htmlResponsePrefix, and others) are accepted but have no effect yet; startup logs a warning when one is set.
See the Tool UI guide for the full ToolUIConfig shape (CSP, sanitization, helpers, etc.), and Trusted markup for html / trustedHtml and the escapeStringResults option (escaping becomes the default in 1.9).
Function-Based Alternative
For simpler tools, use thetool() function:
Context Methods
TheToolContext base class provides:
Dependency Injection
Notifications
Elicitation (Interactive Input)
Platform Detection
Authentication
HTTP Requests
Error Handling
Fail with aPublicMcpError subclass, such as InvalidInputError, so the client gets its message and code. A plain Error is an internal error: the client gets _meta.code: "SERVER_ERROR", and in production only “Internal FrontMCP error. Please contact support with error ID: err_…”.
Type Inference
FrontMCP provides helper types for extracting input/output types:Full Example
Related
ToolContext
Context class details
ToolRegistry
Tool registry API
Tool Errors
Tool-related errors
@Resource
Define resources