MCP Tools
Tools represent callable functions that language models can invoke. Each tool has a name, description, parameters, and a handler function.
Tool Structure
Every tool in ModelContextProtocol.jl is represented by the MCPTool
struct, which contains:
name
: Unique identifier for the tooldescription
: Human-readable explanation of the tool's purposeparameters
: List of input parameters the tool acceptshandler
: Function that executes when the tool is calledreturn_type
: The type of content the tool returns (defaults toTextContent
)
Creating Tools
Here's how to create a basic tool:
calculator_tool = MCPTool(
name = "calculate",
description = "Perform basic arithmetic",
parameters = [
ToolParameter(
name = "expression",
type = "string",
description = "Math expression to evaluate",
required = true
)
],
handler = params -> TextContent(
text = JSON3.write(Dict(
"result" => eval(Meta.parse(params["expression"]))
))
)
)
Parameters
Tool parameters are defined using the ToolParameter
struct:
name
: Parameter identifierdescription
: Explanation of the parametertype
: JSON schema type (e.g., "string", "number", "boolean")required
: Whether the parameter must be provided (default: false)
Return Values
Tools must return one of the following content types:
TextContent
: For text-based responsesImageContent
: For binary image data
Registering Tools
Tools can be registered with a server in two ways:
- During server creation:
server = mcp_server(
name = "my-server",
tools = my_tool # Single tool or vector of tools
)
- After server creation:
register!(server, my_tool)
Directory-Based Organization
Tools can be organized in directory structures and auto-registered:
my_server/
└── tools/
├── calculator.jl
└── time_tool.jl
Each file should export one or more MCPTool
instances:
# calculator.jl
using ModelContextProtocol
using JSON3
calculator_tool = MCPTool(
name = "calculate",
description = "Basic calculator",
parameters = [
ToolParameter(name = "expression", type = "string", required = true)
],
handler = params -> TextContent(
text = JSON3.write(Dict("result" => eval(Meta.parse(params["expression"]))))
)
)
Then auto-register from the directory:
server = mcp_server(
name = "my-server",
auto_register_dir = "my_server"
)