Tools
Enable LLMs to perform actions through your server
As per the MCP specification:
Enable LLMs to perform actions through your server
Tools are a powerful primitive in the Model Context Protocol (MCP) that enable servers to expose executable functionality to clients. Through tools, LLMs can interact with external systems, perform computations, and take actions in the real world.
Learn more about tools
MCP Documentation
Defining a tool
Description
The first argument of the tool
function is the description of the tool. Providing a detailed description is crucial, as it helps the LLM understand how and when to use the tool effectively.
Input and output schema
You have to define input and output schemas for your tool using zod
, valibot
, or arktype
.
The output schema is mandatory. It generates the types for the tool to provide a better developer experience when using your MCP server.
Handle
The handle
function is the core of the tool and will be called when the tool is executed.
It receives the following parameters:
input
: The data passed to the tool whose type is inferred from the input schema.error
: A function to return errorssessionId
: The ID of the current sessionctx
: The context of the tool execution that you can create using thecreateCtx
function.env
: The environment variables of your MCP server. As defined in the.env
file of your project.
You can return an error to the LLM using the error function, which will build a standard RPC error.
The sessionId
identifies the current execution session and can be used to store associated data.
The handle function can return either a string or an object. You don’t need to manually format the return value according to the MCP specification - this is handled automatically by the SDK.