Skip to main content

Tools

Tools allow an MCP server to expose executable functionality to MCP clients. A tool has a name, description, input schema, and callback. The callback receives validated input and returns an MCP-compatible result.

Basic Tool

Import MCPServer and the response helpers from mcpfy-sdk/server, and use Zod to define the input schema.
zod is a peer dependency of the SDK; install it explicitly in applications that define Zod schemas. The schema property must be a Zod schema. For object-shaped input, use z.object(...). The callback receives input that has already been validated against the schema.

Tool Definition

A tool definition can contain the following commonly used properties:

name

The unique name of the tool.

description

A human-readable description of what the tool does.
A useful description helps MCP clients and models understand when the tool should be used.

schema

The input schema for the tool.
mcpfy uses Zod schemas for tool input validation. For example:

Tool Results

Tool callbacks must return an MCP-compatible result. mcpfy provides response helpers that create the required MCP content structure.

Text Results

Use text() when the tool should return plain text.

Structured Results

Use object() when the tool should return structured data.
The important distinction is that a tool callback should not return an arbitrary object such as:
Instead, wrap structured data with object():
This produces the MCP-compatible response content expected by the SDK.

Markdown Results

When a response is intended to contain Markdown content, use markdown().

Optional Input

Zod can be used to define optional values.

Tool Validation

Because the schema is defined with Zod, invalid input is rejected according to the schema before the tool callback receives it. For example:
expects both a and b to be numbers. A string such as:
does not satisfy the schema.

Multiple Tools

An MCP server can register multiple tools.

Tool Context

Tool callbacks can use the context provided by mcpfy for operations such as logging and interacting with the MCP runtime. A tool callback can receive the context as an additional argument:
The log method requires a log level followed by the message:
Do not use:

Returning Errors

Tool implementations can throw errors when an operation cannot be completed.
Use meaningful error messages so that clients can understand what went wrong.

Images

mcpfy supports image content in tool responses. When returning image content, the image data must be supplied as base64-encoded data rather than as a remote URL. For example, fetch the image, convert it to base64, and pass that data to image():
Return the result of loadImage() from the tool callback. Do not pass the URL directly where base64 image data is required.

Client Configuration for Tools

Tools can be exposed by an HTTP MCP server and consumed by an MCP client. For example, an HTTP server can be started with:
The MCP endpoint is:
A client can connect using the server URL:
For a stdio server, configure the client with the command used to start the server:
The transport is inferred from the server configuration. There is no transport: "stdio" property in the client ServerConfig.

Typed Tool Calls

mcpfy supports typed tool calls when schemas and types are available. Defining a Zod schema provides the foundation for strongly typed tool input:
The callback input is inferred from the schema:
This reduces the need for manual input type declarations and keeps runtime validation aligned with TypeScript usage.

Organizing Tools

For larger projects, tools can be separated into individual modules. Example:
A tool module can export a registration function:
The main server can then register the tool:

Forwarding Authentication Headers

When an HTTP MCP server needs to make authenticated upstream requests using authentication received from the MCP request, use the SDK’s supported authentication-header forwarding helpers. The relevant APIs include:
These helpers provide the supported mechanism for forwarding permitted inbound authentication headers to upstream requests. Authentication configuration and token verification are covered in Authentication.

Tools with Widgets

Tools can be associated with MCP Apps widgets when the server needs to return an interactive UI. A tool can specify a widget using the SDK’s widget configuration. The widget itself must follow the project’s widget directory and build conventions. See Widgets for the complete widget workflow.

Best Practices

Use descriptive names

Prefer:
over:

Write useful descriptions

A description should explain what the tool does and when it should be used.

Validate all structured input

Use Zod schemas instead of accepting unvalidated objects:

Return SDK-compatible results

Use the response helpers:
or:
rather than returning an arbitrary object.

Keep tools focused

A tool should generally perform one well-defined operation. Smaller, focused tools are easier for MCP clients and models to understand and use correctly.

Complete Example

The following example combines schema validation, multiple tools, and MCP-compatible responses:
The server exposes two tools:
  • add — returns structured data using object()
  • greet — returns text using text()
The examples in this guide use the actual mcpfy tool schema and response patterns so they can be checked against the SDK rather than relying on arbitrary MCP-shaped objects.