async Python function decorated with @tool. The function
signature is the schema the LLM sees, and there is no separate registration
step.
Defining a tool
skills/check_balance/tools.py
tool and ToolContext come from rasa.mantle.tools.decorator, and
ToolResult from rasa.mantle.tools.result.
Requirements:
- The function must be
async. @tooltakesdescriptionas a keyword argument.@tool(description="…"). A bare@toolwithout it raises.- The return type is
ToolResult. - Name the injected parameter
context. The runtime passes it as the keyword argumentcontext=, and leaves it out of the LLM’s schema.
@tool calls are subject to a wall-clock timeout. Configure it with
tool_timeout in agent.yml (default
10 seconds). MCP tools use the same agent default unless the server sets its
own tool_timeout in integrations.yml.
How the schema is derived
Type mapping:
The generated object sets
additionalProperties: false.
Each generated property carries its type. Put the meaning of an argument into
the tool’s
description, or name the parameter so its purpose is obvious.Where tools are discovered
For a skillskills/<id>/, in resolution order:
- Skill-local:
skills/<id>/tools.pyandskills/<id>/tools/*.py. Both locations are scanned; no declaration needed. A name defined intools/*.pyshadows the same name intools.py(logged as a duplicate). - Shared:
tools/*.pyat the agent root. Only tools the skill declares viaimport_toolsare attached to it; an undeclared shared tool is not available to that skill at all. - MCP:
mcp/<server-id>:<tool-name>entries inimport_tools. The skill receives only those allowlisted tools; the LLM sees the bare tool name. The server must be declared undermcp_servers.
rasa train — see
import_tools.
When a tool module cannot be imported, rasa train
fails. When a referenced MCP server cannot connect, authenticate, or list tools,
agent startup stops with an error naming the failure, so a misconfiguration
surfaces before the first conversation rather than as a missing tool mid-turn.
Importing shared project code
While tool modules are imported, the project root is onsys.path, so a lib/
package at the agent root is importable from any tool location:
ToolContext
The runtime constructs one ToolContext per invocation and injects it:
context.model carries two read-only values that are fixed for the loaded
model: llm_config, the same provider settings the orchestrator uses from
integrations.yml, and references_index, the packaged knowledge index (or
None when the project ships no reference files). Use it when a tool needs to
run its own model call or query the same knowledge the agent searches.
send is a coroutine, so await it: await context.send("One moment…").context.events is a fresh list, so appending to or removing from it does not
touch conversation state. Memory is the only supported write path.
If the user has already interrupted the turn (voice barge-in), send drops the
message.
context.memory
- a bare name as declared in
memory.yml— resolved against the active skill’s schema first, then the project-rootmemory.yml - a fully-qualified name (
<skill_id>.<entry>orproject.<entry>) — same visibility as conditions / prompt memory: the active skill’s public and private fields, every other skill’s public fields, and project fields.
session. prefixes here (that form is for conditions). For
writes, use a bare name (active skill, then project) or a
fully-qualified name for a field this skill may write: project.<entry>
always targets project memory (even when the skill declares the same name),
and <active_skill_id>.<entry> always targets this skill’s field (it does
not fall through to project). Cross-skill writes stay out of scope. Do not
use the internal memory manager or clear memory entries — rasa data validate
rejects that. To clear a field this skill owns, use
context.memory.set(field, None). Fields another skill owns must be cleared
from that skill.
getreturnsNonewhen the entry is unset, stored asNone, or outside the readable set (for example another skill’sprivatefield) — unless you pass a second argument, which is returned instead. StoredNonecannot be distinguished from never-written, so it also yields that default. It never raises.setwrites through immediately and raisesMemoryWriteErrorwhen the entry is undeclared, the name is a cross-skill write, the name collides with the reservedsystem.*namespace, or aproject.*field is already set (ProjectMemoryAlreadySetError). YAMLset_memory:steps andrun_after_setting_*memory validation tools skip an already-set project field instead of failing the step, so a later collect or correct is not rolled back.
Every key a tool writes must be declared in the skill’s
memory.yml or the
project-root memory.yml. rasa train rejects an undeclared write with
undeclared_memory_write. This is the most common authoring failure.
See the memory.yml reference.ToolResult
llm_response: any JSON-serialisable value; this is what the LLM reads as the tool’s result. It is used for LLM-invoked tools; framework-invoked tools (ordered-blockexecute_tool:steps, memory validation tools) act throughcontext.next: skills to activate once the tool’s result is recorded, in list order. SeeActivateSkill.- A tool that only sends a message or writes memory returns a bare
ToolResult().
ActivateSkill
skill_id: the skill to activate. Withoutordered_block, Mantle enters the skill the same way the LLM would: its instructions, or itsmainblock (the first block when there is nomain) for a skill made only of blocks.ordered_block: one of that skill’s routable ordered blocks. The block always starts at its first step.
requires_confirmation, the handoff
waits until the user has answered. A paused tool’s own next runs when the
user confirms; a declined tool never runs, so its next never applies.
Rules. An ActivateSkill works like a call step or an @skill_id
reference in prose:
- The current skill waits. When the new skill finishes, the conversation returns to where it left off.
- Hidden skills can be targeted, so a skill can be reachable only through a
tool. Disabled skills and internal blocks (
routable: false) are refused. - A skill that is already on the stack is left alone.
skill_id, an ordered_block the skill does not
have, or a refused target is logged as a warning and skipped. At most 10 instructions run per turn;
any beyond that are dropped with an error log. Memory validation tools, button_source tools, and tools re-run to correct a value can not set a next skill and will log a warning.
To call another tool from a tool, call it as plain Python:
await other_tool(..., context=context).
Gating a tool
By default every skill-local tool is visible to the LLM whenever its skill is active. Addtool_constraints in the frontmatter to gate one behind a memory
condition:
- A skill-local tool is visible unless it appears in
tool_constraintswith an unmet condition. - A shared tool must be listed in
import_tools; atool_constraintsentry then gates it as well.
Built-in framework tools
These names are reserved. A builder tool that reuses one raises at model load.
Behaviors worth knowing:
complete_skillis withheld while an ordered block is active. The block finishes atEND; a hybrid skill offerscomplete_skillagain after returning to its prose instructions. Do not add a final block instruction that asks the model to callcomplete_skill.- When
complete_skillis offered, it is refused with a tool error while a tool confirmation is pending for the active skill (Mantle resolves it after the LLM callsresolve_tool_confirmation) or when skill-levelcomplete_whencondition or criteria are unmet.cancel_skillis never gated that way. - Once
search_knowledgehas run in a turn, the tool list collapses toactivate,search_knowledge, andcannot_help, plusresolve_tool_confirmationwhen a confirmation is pending. The skill’s own tools andlistenare withheld so a grounded answer neither re-drives the skill nor ends silently before presenting the retrieved information. They return on the next turn. cannot_helpis withheld until a search has run when a knowledge base exists, so the model cannot decline before retrieval could have answered.- If
listenis mixed with text or other tools, the engine ignoreslistenand follows the text and sibling tools. - Guided collect steps, post-search answer calls, and pending tool confirmation
do not offer
listen. listenalone ends the turn with no message. It is the LLM wait tool. Ordered-blockaction: action_listenis a separate wait step.hangupis unavailable to the model. To end a conversation, explicitly useaction: hangupin a skill ordered block after an authored goodbye response.
Memory validation tools
A tool namedrun_after_setting_<entry> validates or derives state after a
memory write. It is not an LLM tool, and it is not a Mantle hook.
Define it with @tool in the skill’s tools.py (one tool per entry). The
runtime calls it; it is never offered to the model. It runs after a top-level
set_fields, correct, or static set: button
write to that entry.
skills/card_disambiguation/tools.py
- Returning
llm_responsewith a non-nullerrorkey rolls the triggering write back and surfaces the message to the LLM.{"error": None}is success. - Writes made inside a memory validation tool do not run another one.
- A skill-local
run_after_setting_<field>tool can write project memory after a skill field is recorded. Shared or skill tools named after a project field are invalid at train — the LLM never sets project memory, and button taps that writeproject.*do not run a project-named tool. rasa trainonly accepts the tool when<entry>isllm_settableor collect-owned. A successfulset:button write still runs that tool even when the field is notllm_settable.
Cancellation
A turn can end while a tool is still running, whether from a voice barge-in, a session timeout, or a dropped connection. The runtime raisesasyncio.CancelledError inside the tool at whatever await it is suspended on.
There is nothing to wire up.
Only await points are interruptible; synchronous code between them runs to
completion.
What happens to your tool
When your tool returns normally, the runtime records the call and advances the skill to its next step. When your tool is cancelled mid-await, it does neither. The step stays
pending, so the same tool can run again on the next turn. Memory it already
wrote with context.memory.set(...) is kept.
That gap matters for a side-effecting tool: your backend may have been called
successfully while the conversation holds no record of it.
Check before irreversible work
context.is_cancelled is True once the turn has been cancelled. Check it
immediately before anything you cannot safely repeat, and between consecutive
side effects:
await for a cancellation to slip into.
Release resources on cancellation
If your tool holds a resource, catchasyncio.CancelledError, release it, and
re-raise. The turn has already ended, so the runtime discards anything you
return from the handler.
See also
- Tools concept: when to reach for a tool, including MCP imports
- Conditions: the
requires:expression grammar memory.ymlreference: declaring what tools may writeskill.mdreference:import_toolsandtool_constraintsintegrations.yml: MCP server configagent.yml: default tool-call timeout