tool_context
Shared context object passed to tools that need platform access.
Tools opt-in to receiving context by declaring a ctx parameter in
their run() signature. The ToolRegistry inspects the
handler at call-time and injects the context automatically – the LLM
never sees or fills this parameter.
- class tool_context.ToolContext(platform='', channel_id='', user_id='', user_name='', guild_id='', adapter=None, message_id='', config=None, redis=None, message_cache=None, kg_manager=None, task_manager=None, threadweave=None, tool_registry=None, conversation_manager=None, openrouter=None, all_adapters=<factory>, adapters_by_name=<factory>, injected_tools=None, sent_files=<factory>)[source]
Bases:
objectRuntime context injected into tool handlers that request it.
- Parameters:
platform (str)
channel_id (str)
user_id (str)
user_name (str)
guild_id (str)
adapter (PlatformAdapter | None)
message_id (str)
config (Config | None)
redis (Any)
message_cache (MessageCache | None)
kg_manager (KnowledgeGraphManager | None)
task_manager (TaskManager | None)
threadweave (ThreadweaveManager | None)
tool_registry (ToolRegistry | None)
conversation_manager (ConversationManager | None)
openrouter (Any)
- adapter: PlatformAdapter | None = None
The
PlatformAdapterinstance for the originating platform.For Discord this is a
DiscordPlatformwhose.clientproperty exposes the underlyingdiscord.Client.
- config: Config | None = None
Global bot
Config. Gives tools access toredis_url,model, and other settings.
- redis: Any = None
Shared async Redis client (
redis.asyncio.Redis).Nonewhen Redis is not configured. Tools should guard access with anif ctx.redis is not Nonecheck.
- message_cache: MessageCache | None = None
Shared
MessageCacheinstance for retrieving cached messages asCachedMessageobjects.Nonewhen Redis is not configured.
- kg_manager: KnowledgeGraphManager | None = None
Shared
KnowledgeGraphManagerfor the knowledge graph system.Nonewhen Redis is not configured.
- task_manager: TaskManager | None = None
Shared
TaskManagerfor checking background task results. Injected by the message processor so tools likecheck_taskcan access it.
- threadweave: ThreadweaveManager | None = None
Shared
ThreadweaveManagerfor the Threadweave persistent knowledge system (DNA Vault, Persistent Weave, Shadow Memory).Nonewhen Redis is not configured.
- tool_registry: ToolRegistry | None = None
The live
ToolRegistrythat holds all loaded tools.Allows meta-tools like
list_all_toolsandreload_toolsto inspect or modify the running tool set.
- conversation_manager: ConversationManager | None = None
Shared
ConversationManagerfor per-channel conversation histories.Allows tools to read or manipulate the in-memory context window for any channel.
- openrouter: Any = None
The
OpenRouterClientinstance for the current session.Allows tools like
extend_tool_loopto adjust the tool-calling loop parameters at runtime.
- all_adapters: list[Any]
All running
PlatformAdapterinstances.Allows cross-platform tools (e.g.
list_active_servers) to query every connected platform, not just the one that triggered the current message.
- adapters_by_name: dict[str, Any]
All platform adapters keyed by name (e.g.
"discord","discord-self","matrix").Allows tools to look up a specific adapter directly:
selfbot = ctx.adapters_by_name.get("discord-self")
- injected_tools: list[str] | None = None
Tool names requested for injection by
request_tool_injection.Populated at runtime by tool handlers; the
chat()loop inOpenRouterClientchecks this after each round and merges any new names into the active tool list.When several tools run in the same model round they share this
ToolContextand execute concurrently viaasyncio.gather. Use only atomic updates (e.g.list.append); avoid read-modify-write races on the same list without coordination.
- sent_files: list[dict[str, Any]]
Media files sent to the channel during tool execution.
Each entry has keys:
data(rawbytes),filename(str),mimetype(str), and optionallyfile_url(str). After the LLM call, the message processor converts these into real multimodal content parts (image_url,input_audio, etc.) viamedia_to_content_parts()and appends them to conversation history so the bot can see its own visual and audio outputs on subsequent turns.Parallel tools in one LLM round share this list; appends are safe, but do not assume ordering of entries matches tool-call order unless you serialize tool execution.