Tool

Tool(func, name, description, parameters)

Define a tool

Define a Python function for use by a chatbot. The function will always be invoked in the current Python process.

Parameters

Name Type Description Default
func Callable[…, Any] | Callable[…, Awaitable[Any]] The function to be invoked when the tool is called. required
name str The name of the tool. required
description str A description of what the tool does. required
parameters dict[str, Any] A dictionary describing the input parameters and their types. required

Methods

Name Description
from_func Create a Tool from a Python function
from_mcp Create a Tool from an MCP tool

from_func

Tool.from_func(func, *, model=None)

Create a Tool from a Python function

Parameters

Name Type Description Default
func Callable[…, Any] | Callable[…, Awaitable[Any]] The function to wrap as a tool. required
model Optional[type[BaseModel]] A Pydantic model that describes the input parameters for the function. If not provided, the model will be inferred from the function’s type hints. The primary reason why you might want to provide a model in Note that the name and docstring of the model takes precedence over the name and docstring of the function. None

Returns

Name Type Description
Tool A new Tool instance wrapping the provided function.

Raises

Name Type Description
ValueError If there is a mismatch between model fields and function parameters.

from_mcp

Tool.from_mcp(session, mcp_tool)

Create a Tool from an MCP tool

Parameters

Name Type Description Default
session 'MCPClientSession' The MCP client session to use for calling the tool. required
mcp_tool 'MCPTool' The MCP tool to wrap. required

Returns

Name Type Description
Tool A new Tool instance wrapping the MCP tool.