0

Creating Custom Tools and Toolkits

Turn typed Python functions into tools with schemas, risk metadata, caching, and lazy toolkit loading.

Cr
Creating Custom Tools and Toolkits
Chapter 005Core runtimeToolsIndex
01Orientation

MTP supports custom tools via plain Python functions and toolkit loaders.

Use this when

Turn typed Python functions into tools with schemas, risk metadata, caching, and lazy toolkit loading.

  • 1) Python functions as tools
  • 2) Build a toolkit from functions
  • 3) Register toolkit in registry
  • 4) Use with agent
02 / syntax starter
from mtp import Agent

@Agent.mtp_tool(
    description="Add two integers.",
    risk_level=Agent.ToolRiskLevel.READ_ONLY,
    cache_ttl_seconds=60,
)
def add(a: int, b: int) -> int:
    return a + b
03 / manual

Read the system

The complete source manual—syntax, examples, linked references, edge cases, and implementation notes.

Documentation index ↗

MTP supports custom tools via plain Python functions and toolkit loaders.

This guide mirrors common patterns used in agent frameworks:

  • Python functions as tools
  • Grouping tools into a toolkit
  • Registering toolkit into a ToolRegistry

1) Python functions as tools

Use @mtp_tool to attach metadata and keep definitions explicit:

pythonfrom mtp import Agent

@Agent.mtp_tool(
    description="Add two integers.",
    risk_level=Agent.ToolRiskLevel.READ_ONLY,
    cache_ttl_seconds=60,
)
def add(a: int, b: int) -> int:
    return a + b

If you omit input_schema, MTP infers a schema from function signatures/type hints.

2) Build a toolkit from functions

pythonfrom mtp import Agent

toolkit = Agent.toolkit_from_functions("custom", add)

This produces tool names like:

  • custom.add

3) Register toolkit in registry

pythonfrom mtp import Agent

registry = Agent.ToolRegistry()
registry.register_toolkit_loader("custom", toolkit)

4) Use with agent

pythonfrom mtp import Agent
from mtp.providers import Groq

provider = Groq(model="openai/gpt-oss-120b")
agent = Agent.MTPAgent(provider=provider, tools=registry)
print(agent.run("Use custom.add with a=20 and b=22"))

Manual ToolSpec generation (advanced)

If you need explicit control:

pythonfrom mtp import Agent

spec = Agent.tool_spec_from_callable(add, namespace="custom")

Notes for robust tools

  • Write precise docstrings/descriptions. The model reads them.
  • Keep parameter names explicit and stable.
  • Mark risk level correctly (read_only, write, destructive).
  • For side effects, prefer explicit user confirmation policy.

Related:

01

Read

Understand where Creating Custom Tools and Toolkits sits in the agent loop before adding abstractions.

02

Wire

Connect the smallest useful provider, registry, store, or event stream first.

03

Observe

Expose events, logs, results, and failure states while the runtime is still moving.

04

Harden

Add policy, tests, retries, and audit traces after the behavior is visible.

Next docEvent Stream Contract