Skip to content

Documentation navigation

Tools

Define function tools and model tools with the right execution boundaries.

#Tools and Dependencies

Tools are how an agent acts in the world, and dependencies are how those actions compose into a workflow. This guide walks from the simplest idea ("a tool is just your function the agent can call") up to declaring that some tools need the output of others, and letting the system work out the order and what can run at the same time.

If you are new to the SDK, read this top to bottom. If you already know the basics, jump to Declaring Dependencies or DAG Execution at a Glance.

#What a Tool Is

A tool is one of your Python functions (or a Pydantic model) that the agent is allowed to call. You write the function; the agent decides when to call it and with what arguments, based on the task it was given.

Python
from maivn import Agent
from maivn.messages import HumanMessage

agent = Agent(name='helper', api_key='...')

@agent.toolify(description='Get current weather for a city')
def get_weather(city: str) -> dict:
    return {'city': city, 'temp': 72, 'condition': 'sunny'}

response = agent.invoke([HumanMessage(content='What is the weather in Austin?')])
print(response.response)

The agent never sees the body of get_weather. It only sees a description of the tool: its name, what it does, and what arguments it takes (derived from your type hints and docstring). When the agent chooses to call it, your code runs locally in your own environment and only the result is returned. The SDK does not upload the function body. Tool arguments and results, plus any private-data maps you supply, still cross the documented runtime boundaries. Keep sensitive data out of outputs that should not leave your application.

Two kinds of tools. A tool is either a function that does something and returns a value, or a Pydantic model that defines a structured shape the agent should fill in. Both are registered the same way. The difference is whether the tool executes logic or describes output.

#The Three Registration Styles

There are three ways to register a tool. All three use the same tool registry and support the same dependency decorators, so pick whichever reads best for your code.

#1. Decorator — @agent.toolify(...)

Best when you define the tool right next to the agent.

Python
agent = Agent(name='helper', api_key='...')

@agent.toolify(description='Add two numbers together')
def add_numbers(a: int, b: int) -> dict:
    return {'sum': a + b}

#2. Constructor — Agent(..., tools=[...])

Best when functions or models already exist and you want the agent's configuration to show the full tool surface in one place.

Python
def add_numbers(a: int, b: int) -> dict:
    """Add two numbers together."""
    return {'sum': a + b}

agent = Agent(name='helper', api_key='...', tools=[add_numbers])

#3. Imperative — agent.add_tool(...)

Best when tools are imported, assembled conditionally, or need extra options.

Python
agent = Agent(name='helper', api_key='...')
agent.add_tool(
    add_numbers,
    name='add_numbers',
    description='Add two numbers together',
    tags=['math'],
)

add_tool accepts name, description, permissions, destructive, always_execute, final_tool, metadata, tags, before_execute and after_execute. It does not accept an override= argument. Use a small wrapper when an app needs different defaults or output handling:

Python
def inbox_search(query: str, limit: int = 10) -> dict:
    """Search recent inbox messages for triage."""
    return search(query=query, max_results=limit)

agent.add_tool(inbox_search, name='inbox_search', tags=['email', 'read'])

#Tool Types

#Function Tools

A function tool runs logic and returns a value. The SDK serializes the return value (dict, list, primitive, Pydantic model, dataclass, set, or tuple) before handing it back to the agent.

Python
@agent.toolify(description='Fetch current weather data for any city worldwide')
def get_weather(city: str) -> dict:
    return {'city': city, 'temp': 72}

A tool can return a Pydantic model instance. The SDK sends its JSON form, the same fields model_dump(mode='json') gives you:

Python
from pydantic import BaseModel


class Forecast(BaseModel):
    city: str
    high: int
    low: int


@agent.toolify(description='Fetch the next-day forecast for a city')
def get_forecast(city: str) -> Forecast:
    return Forecast(city=city, high=75, low=58)

Two things make a function a good tool:

  • Type hints. The JSON schema the agent sees is built from your signature. Use Annotated[...] with a short description or a Pydantic Field(...) to add constraints the agent can read and that Pydantic enforces at call time.
  • A clear description. Pass description= or rely on the docstring. Say when to use the tool, what the arguments mean, and what shape it returns.
#Declaring Function Output Schemas

Use @tool_output(...) to declare a function output schema. Ordinary Pydantic return annotations alone are not currently inferred as output schemas. The SDK has special inference for GeneratedFile and GeneratedFiles:

Python
from pydantic import BaseModel

from maivn import tool_output


class SearchMessagesResult(BaseModel):
    message_ids: list[str]
    next_page_token: str | None = None


@tool_output(SearchMessagesResult)
def search_messages(query: str) -> dict[str, object]:
    """Search recent messages."""
    return {'message_ids': ['m-1'], 'next_page_token': None}


agent.add_tool(search_messages)

@tool_output(...) is independent of @toolify(...): direct add_tool(fn) registration consumes it. For toolsets, the decorator is consumed only for methods that are discovered as tools; @tool_output(...) by itself does not expose a method from a @toolset(require_marker=True) class.

For app-specific output handling, write a wrapper and apply @tool_output to that wrapper. add_tool does not accept an output-schema override argument.

Python
from typing import Annotated
from pydantic import Field

@agent.toolify()
def dispatch_vehicle(
    vehicle_id: Annotated[
        str,
        Field(description="A single vehicle ID like 'VAN-103'.", pattern=r'^VAN-\d+$'),
    ],
    estimated_km: Annotated[int, Field(description='Trip distance in km.', gt=0)],
) -> dict:
    ...

Async functions work the same way. Just declare async def.

See the deep reference below for return-value conventions, error handling, and parameter-documentation guidance.

#Pydantic-Model Tools

A model tool describes a structured shape for the agent to produce. Register the model the same way you register a function. The class docstring becomes the description and each Field(description=...) guides the agent's output.

Python
from pydantic import BaseModel, Field

@agent.toolify(description='Generate a weather report')
class WeatherReport(BaseModel):
    """Structured weather report."""
    city: str = Field(..., description='City name')
    temperature: int = Field(..., description='Temperature in Fahrenheit')
    conditions: str = Field(..., description='Weather conditions')

Model tools support nested models to any depth, lists of models, and optional fields. Marking one with final_tool=True makes it eligible to provide the terminal typed answer. Use force_final_tool=True when the invocation must return through that path; see Structured Output.

#MCP tools

Register an MCP server with agent.add_mcp_server(server). Discovery compiles its tools for the scope; it does not return public BaseTool objects for add_tool(). See MCP integration for transport configuration and cleanup.

#Toolsets: Register Many Methods at Once

When several related tools share configuration (a database handle, an HTTP client, an OAuth token), group them on one class instead of scattering free functions. Decorate the class with @toolset(prefix=...), mark each tool method with @toolify(...), then register the instance with agent.add_toolset(instance).

Python
from maivn import Agent, PermissionFlag, PermissionSet, toolify, toolset


@toolset(prefix='reports')
class WeeklyReportsToolSet:
    """Pull weekly KPI reports from an internal warehouse."""

    def __init__(self, db_path: str):
        self._db_path = db_path

    @toolify(permissions=PermissionSet(PermissionFlag.READ))
    def weekly_signups(self, week_of: str) -> list[dict]:
        """Signup counts by source for the week starting ``week_of`` (YYYY-MM-DD)."""
        ...

    @toolify(permissions=PermissionSet(PermissionFlag.READ))
    def revenue_per_signup(self, week_of: str) -> float:
        """Compute revenue per signup for ``week_of``."""
        ...


agent = Agent(name='analyst', api_key='...')
agent.add_toolset(WeeklyReportsToolSet('/srv/warehouse.db'))

After add_toolset returns, the agent has two new tools: REPORTS_weekly_signups and REPORTS_revenue_per_signup.

#Name Prefixing

The prefix is uppercased and joined to the method name with an underscore. The uppercase form keeps generated names within the provider tool-name regex (^[a-zA-Z0-9_-]{1,64}$) and makes the namespace boundary readable at a glance in logs and traces.

#Filtering with include / exclude and tags

add_toolset accepts four optional filters so one class can be narrowed at registration time. A method must pass every active filter.

Python
agent.add_toolset(instance, include_tags=['read'])        # only read-tagged methods
agent.add_toolset(instance, exclude_tags=['destructive']) # skip destructive methods
agent.add_toolset(instance, include=['weekly_signups'])   # whitelist method names
agent.add_toolset(instance, exclude=['revenue_per_signup'])  # skip method names

Tags are auto-derived from each method's permissions= and destructive= markers (PermissionSet(PermissionFlag.READ) adds "read", and PermissionFlag.DELETE adds both "delete" and "destructive"), and any tags= you declare on @toolset / @toolify are merged on top. include / exclude match the unprefixed method name (or a name= alias from @toolify).

#App-specific adapters

add_toolset supports name/tag filters but not overrides=. Register a wrapper function with add_tool when an application needs a different name, description, default argument, or output contract. Apply @tool_output to that wrapper when its function output needs an explicit schema.

See the Decorators API for the class-decorator reference, including toolset_options and toolify_options, which read back what the decorators recorded.

#Approval for privileged tools

Tools marked destructive=True, or declaring delete, import, admin or impersonate permission, are privileged. The SDK refuses a model's call to a privileged tool by default. Nothing runs, and the model gets the error code tool_authorization_required and the message "Privileged tool execution requires authenticated approval." Ordinary tools, including those with only read, write or export permission, are not affected.

To keep such methods out of an agent, register a toolset with exclude_tags=['destructive'] (see Filtering). To let a person approve them instead, use an approval policy.

Per-tool approval is opt-in: if you omit approval_policy, privileged tools stay refused exactly as described above.

An approval_policy key in a tool's registration metadata sets the choice:

ValueEffect on a privileged tool
omittedRefused, as above
'deny'Refused. Use it to state the choice explicitly.
'ask'The run pauses, your approval_handler asks a person, and the call runs only if the handler returns True
'allow'Runs without asking. Set it only in developer-authored registration code.

A value outside these four marks the tool as destructive and keeps it refused. The policy only matters for privileged tools; on an ordinary tool it changes nothing.

For 'ask', also set approval_handler in the same metadata. This example deletes only a note held in memory:

Python
from collections.abc import Mapping

from maivn import Agent


notes = {'demo-note': 'An example note.'}


def callback(
    prompt: str, tool_name: str = '', arguments: Mapping[str, object] | None = None
) -> bool:
    answer = input(f'{prompt} {tool_name} {dict(arguments or {})} [y/N] ')
    return answer.strip().lower() == 'y'


def delete_note(note_id: str) -> dict[str, bool]:
    """Delete the note with the given ID from this example's in-memory notebook."""
    return {'deleted': notes.pop(note_id, None) is not None}


agent = Agent(name='notebook', api_key='...')
agent.add_tool(
    delete_note,
    metadata={'destructive': True, 'approval_policy': 'ask', 'approval_handler': callback},
)

if __name__ == '__main__':
    response = agent.invoke('Use delete_note to delete demo-note.')
    print(response.response)

The same keys work with @agent.toolify(destructive=True, metadata={...}) and on @toolify(...) methods inside a toolset.

When the model calls the tool, the run pauses at a durable approval checkpoint before the tool executes. Your handler stays in your process. The SDK calls it with a fixed prompt, Approve this privileged tool invocation?. A handler that declares tool_name, arguments or call_id parameters, or accepts **kwargs, also receives those values as keywords. arguments is a read-only copy of the call's real arguments, with private values shown as their placeholders. The platform sends these arguments only to the SDK process that started the run. It never stores them with the checkpoint or the thread's history, and they never appear in Portal or Studio thread views, in run events, or in webhooks; those show the approval with empty arguments. The approval applies only to the call with exactly those arguments: approving one set of arguments does not let the same call run with different ones. tool_name and call_id are always set. A handler that takes only the prompt is called as before. The handler may be async. Return True to approve that one call. False, or a checkpoint that expires, refuses it with the same tool_authorization_required error; denial and timeout do not copy arguments or a reason into the refusal. If the handler raises, the exception ends the stream and is not treated as a denial.

With verbose=True, the SDK's terminal reporter asks the question in the terminal instead of calling your handler, so run without verbose to exercise a custom handler.

An unanswered checkpoint expires after the deployment's human-wait timeout, 30 minutes by default. A blocking handler should use its own timeout too, so it stops waiting when the checkpoint expires.

Policy belongs to each registered tool. A parent agent, swarm or calling tool does not pass its policy to the tools below it, and approving one call does not approve any call it triggers. If the same tool name is registered by two owners with different policies or handlers, the SDK raises ValueError ("Conflicting local tool approval policy") before the run starts. Register each owner under a distinct name.

See Interrupts for how the checkpoint is answered.

#Tool Execution Hooks

Every tool can run a callback before and after it executes, which is useful for logging, auditing, or timing.

Python
def log_start(ctx):
    print(f"Starting: {ctx['tool_id']}")

def log_end(ctx):
    print(f"Finished: {ctx['tool_id']}, result: {ctx['result']}")

@agent.toolify(before_execute=log_start, after_execute=log_end)
def my_tool() -> dict:
    return {'done': True}

Each hook receives one dict with the keys stage, tool, tool_id, tool_name, arguments, result and error. Each firing emits a normalized hook_fired event (carrying the hook name, the before/after stage, a completed/failed/blocked status, and elapsed time) that custom frontends can subscribe to via the events API.

#Blocking a tool call

A before_execute hook can refuse one tool call by raising ToolBlockedError with a plain-text reason:

Python
from maivn import ToolBlockedError

def refund_limit(ctx):
    if ctx['arguments'].get('amount', 0) > 500:
        raise ToolBlockedError('Refunds above 500 need a manager. Ask the user to escalate.')

@agent.toolify(before_execute=refund_limit)
def issue_refund(order_id: str, amount: int) -> dict:
    ...

The rule is exact:

  • A before_execute hook that raises ToolBlockedError blocks that call. The tool does not run, the remaining before_execute hooks and every after_execute hook for that call are skipped, and the model receives a refusal for the call with the code tool_blocked_by_hook and your reason. The run continues, and the model can choose what to do next. The decision is made in your process before your tool code runs. The hook_fired event for that hook has the status blocked.
  • Any other exception from a hook is reported and the tool still runs. The hook's hook_fired event has the status failed. A hook that returns normally changes nothing.
  • An after_execute hook cannot block. The tool has already run, so raising ToolBlockedError there is handled like any other exception. The same is true when the tool itself raises it: the call fails with an ordinary tool error.
  • Blocking works per tool call. It applies to hooks on the tool itself and to Agent or Swarm hooks in the default hook_execution_mode='tool', for function tools, Pydantic model tools and MCP tools alike. In 'scope' mode and in the swarm 'agent' mode a hook surrounds a whole run or a member that has already started, so there is no single call to refuse: ToolBlockedError is reported as a failed hook and the run carries on.
  • Your reason is written for the model. The reason is sent to the model and kept with the run, so do not put private values in it. A hook sees the real values of private data, so if the call could carry private data (private data on the run, a private-data dependency, or a private workspace), the reason is replaced with the fixed text "Tool call blocked by a before_execute hook." and your reason stays in your process. The reason must be a non-blank string.

With verbose=True, a blocked call prints Blocked <tool>: <reason> and counts under Tools Blocked in the summary, not under Tools Executed.

#Per-tool vs per-scope: hook_execution_mode

By default, hooks fire once per tool call. Set hook_execution_mode on the Agent (or Swarm) to change that:

  • 'tool' (default) — fire for each tool execution.
  • 'scope' — fire once per invoke() / stream() call.
  • 'agent' — swarm callbacks at member-assignment boundaries. This is not an Agent invocation mode; use 'scope' on an Agent.
Python
agent = Agent(
    name='auditor',
    api_key='...',
    before_execute=audit_log,     # fires per-tool by default
    hook_execution_mode='scope',  # now fires once per invoke()/stream()
)

#Declaring Dependencies

So far each tool stands alone. The next step is composition: some tools need the output of other tools. A report tool needs the data a fetch tool produced, and a write tool needs to run after a validation tool.

You declare these relationships with decorators. You never write the scheduling code yourself. You describe what each tool needs, and the system derives the execution order and what can run concurrently from those declarations.

Python
from maivn import Agent, depends_on_tool

agent = Agent(name='data_agent', api_key='...')

@agent.toolify(description='Fetch raw data from source')
def fetch_data(source: str) -> dict:
    return {'source': source, 'records': [1, 2, 3]}

@agent.toolify(description='Process fetched data')
@depends_on_tool(fetch_data, arg_name='raw_data')
def process_data(raw_data: dict) -> dict:
    return {'processed': len(raw_data['records'])}

When process_data is needed, fetch_data runs first and its output is passed into the raw_data argument. You did not order them. The dependency declaration did.

Dependencies are independent of registration style. The same graph works whether you register with @agent.toolify, Agent(..., tools=[...]), or agent.add_tool.

#@depends_on_tool — output from another tool

Inject one tool's result into another tool's argument. Stack the decorator to depend on several tools at once; independent ones run in parallel.

Python
@agent.toolify()
def fetch_users() -> dict:
    return {'users': [...]}

@agent.toolify()
def fetch_orders() -> dict:
    return {'orders': [...]}

@agent.toolify()
@depends_on_tool(fetch_users, arg_name='users')
@depends_on_tool(fetch_orders, arg_name='orders')
def generate_report(users: dict, orders: dict) -> dict:
    return {'report': 'Combined users and orders'}

fetch_users and fetch_orders have no dependency on each other, so they run at the same time, and generate_report waits for both.

#@depends_on_agent — output from another agent

In a multi-agent system, a tool can depend on the full output of another agent. The dependency agent runs first, and its result is passed into the dependent tool.

Python
from maivn import Agent, Swarm, depends_on_agent

researcher = Agent(name='researcher', api_key='...')
writer = Agent(name='writer', api_key='...')

@researcher.toolify(description='Research a topic')
def research(topic: str) -> dict:
    return {'findings': f'Research on {topic}'}

@writer.toolify(description='Write based on research')
@depends_on_agent(researcher, arg_name='research_result')
def write_article(research_result: dict) -> dict:
    return {'article': f'Based on: {research_result}'}

swarm = Swarm(name='team', agents=[researcher, writer])

See the Multi-Agent Guide for swarm-member dependencies and agent-to-agent handoffs.

Injecting secrets. A related decorator, @depends_on_private_data, injects private values into a tool argument at execution time. The value is never shown to the model and is redacted from results. It is covered in the Private Data Guide.

#Gating and Replanning

Two more controls shape ordering and planning without injecting any data. They affect when a tool runs, not what arguments it receives.

#@depends_on_await_for — order without data flow

Enforced since maivn 2.5.0. Earlier releases recorded the declaration but never sent it, so it did nothing. Use @depends_on_tool when the second tool can take the first one's output.

Use it for a tool that must wait for another without consuming its return value:

Python
from maivn import depends_on_await_for

@agent.toolify(description='Fetch latest records')
def fetch_records() -> dict:
    return {'records': [...]}

@agent.toolify(description='Write compliance audit entry after fetch')
@depends_on_await_for(fetch_records, timing='after', instance_control='all')
def write_audit_log() -> dict:
    return {'logged': True}

timing ('before' / 'after') and instance_control ('each', 'first', 'last' or 'all') are keyword-only.

If the model requests both tools in one batch, the waiting call runs second. If it requests the waiting tool first, the platform refuses that call with the retryable error code tool_await_for_pending, which names the tool to call first, and the model can retry after calling it. The Dependencies guide and the Decorators API cover instance_control, reference rules and the platform version it needs.

#@depends_on_reevaluate — pause, inspect, replan

Use this when planning should stop at a boundary, look at the results gathered so far, and then plan the next step using that real output instead of placeholder arguments.

Python
from maivn import depends_on_reevaluate

@agent.toolify(description='Fetch document text')
def fetch_document() -> dict:
    return {'text': '...'}

@agent.toolify(description='Create final summary after reviewing fetched content')
@depends_on_reevaluate(fetch_document, timing='after', instance_control='all')
def summarize_document() -> dict:
    return {'summary': '...'}

This is a firm boundary, not a hint. The SDK sends it with the invocation, and when the model requests the referenced tool the platform runs the calls up to the boundary, then plans again with those results in context. Each boundary fires once per invocation and emits a raw reevaluate_accrued event you can observe in the stream. instance_control decides where a boundary after the referenced tool falls; the Decorators API has the full table.

#DAG Execution at a Glance

When you stack these decorators you are, in effect, describing a directed acyclic graph (DAG): a set of steps where edges point from a tool to the tools that depend on it. You declare the edges; the system figures out the rest.

Python
from pydantic import BaseModel

from maivn import depends_on_tool

# Level 1: no dependencies
@agent.toolify()
def step_a() -> dict:
    return {'step': 'A'}

@agent.toolify()
def step_b() -> dict:
    return {'step': 'B'}

# Level 2: depends on level 1
@agent.toolify()
@depends_on_tool(step_a, 'a_result')
def step_c(a_result: dict) -> dict:
    return {'step': 'C', 'from': a_result}

@agent.toolify()
@depends_on_tool(step_b, 'b_result')
def step_d(b_result: dict) -> dict:
    return {'step': 'D', 'from': b_result}

# Level 3: depends on level 2
@agent.toolify(final_tool=True)
@depends_on_tool(step_c, 'c_result')
@depends_on_tool(step_d, 'd_result')
class FinalReport(BaseModel):
    c_result: dict
    d_result: dict
    combined: str

From those declarations the system derives the schedule:

  1. step_a and step_b are independent, so they run concurrently.
  2. step_c and step_d each wait for their single dependency, then run.
  3. FinalReport runs last, once both branches complete.

The two guarantees worth remembering:

  • Independent branches can run concurrently. If tools share no dependency, the runtime may schedule them in the same batch. Actual latency still depends on the plan, concurrency limits, provider calls, and the tools themselves.
  • Dependent steps wait. A tool never starts until everything it declared a dependency on has produced a result.

You get parallelism for free and correct ordering by construction, without writing any orchestration code, threads, or await chains yourself. Do not create cycles: the SDK does not reject a tool cycle when you decorate or call compile_tools(). The dependency decorators also do not validate argument names at import time. Match each arg_name to an actual parameter or model field, inspect compiled schemas, and exercise the dependency path in tests.

#Deep References

This page is the conceptual spine. For the full option-by-option detail, see:

  • Dependencies Guide — every dependency and control decorator with validation rules, combined-dependency examples, and execution-timing walkthroughs.
  • Decorators API — exact signatures, parameter tables, and the full toolset decorator reference (@toolset, @toolify, toolify_options, toolset_options, permission helpers, ProviderMetadata), plus compose_argument_policy and requires_producer_reference.
  • Agent API — toolify(), add_tool(...), add_toolset(...), tools=[...], and the scope-hook matrix.

#Next Steps

  • Structured Output — turn a Pydantic model into a guaranteed typed answer with final_tool and structured_output().
  • Private Data — inject secrets into tools without ever exposing them to the model.
  • Multi-Agent — compose agents into a Swarm and wire dependencies across agent boundaries.