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
ImportMCPServer 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.
schema
The input schema for the tool.
Tool Results
Tool callbacks must return an MCP-compatible result. mcpfy provides response helpers that create the required MCP content structure.Text Results
Usetext() when the tool should return plain text.
Structured Results
Useobject() when the tool should return structured data.
object():
Markdown Results
When a response is intended to contain Markdown content, usemarkdown().
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:a and b to be numbers.
A string such as:
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:Returning Errors
Tool implementations can throw errors when an operation cannot be completed.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 toimage():
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: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:Organizing Tools
For larger projects, tools can be separated into individual modules. Example: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: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: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: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:add— returns structured data usingobject()greet— returns text usingtext()

