toolcontext

A programming guide describing the context object passed to a tool while an AI agent runs it. The object gives the tool access to the current session, stored state, and details about the specific tool call.

In plain words
What is it for?
Use it when building tools that need session data, state changes, saved files, user authorization, or a link to the agent's function call.
Why use it?
It explains how a tool can read or update session information and report results back to the agent framework.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/altaidevorg/rules-for-ai/toolcontext
Clone the repo
git clone --depth 1 https://github.com/altaidevorg/rules-for-ai
Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 3,817 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.03817
Opus 5 $0.00000 $0.01909
Sonnet 5 $0.00000 $0.00763
Haiku 4.5 $0.00000 $0.00382

Measured yesterday against content hash 4748bd9e2bc2, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

toolcontext scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

examples/google-adk/toolcontext.mdc · 258 lines

How it starts

The opening of the file, as written. The whole thing — 258 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Chapter 10: ToolContext

In the previous chapter, we learned about InvocationContext, the object encapsulating all necessary information for a single agent turn. While components like callbacks receive a CallbackContext that wraps InvocationContext and provides delta-aware state access, Tools (BaseTool) require slightly more specific information during their execution. This chapter introduces ToolContext, the specialized context object passed directly to a tool's run_async method.

Motivation and Use Case

When an LLM decides to invoke a Tool (BaseTool), the framework needs to execute the tool's specific logic. This logic often requires:

  1. Access to the general turn context (session, services, etc.) provided by InvocationContext.
  2. Access to the session's State, both for reading existing information and for recording changes made by the tool.
  3. A unique identifier linking this specific tool execution back to the LLM's function call request, especially if multiple tools might be called in parallel.
  4. A mechanism for the tool to signal outcomes or requirements back to the framework, such as state updates, artifact creation/updates, or the need for user authorization.

ToolContext fulfills these needs. It inherits from CallbackContext, giving it access to the delta-aware state and services. Crucially, it adds the function_call_id specific to the tool invocation and directly exposes the actions object (EventActions) for the tool to modify.

Central Use Case: A user asks an agent equipped with a get_weather tool: "What's the weather like in London?". The agent's BaseLlmFlow receives a FunctionCall from the LLM.

  1. The flow creates a ToolContext instance, passing the current InvocationContext and the id from the FunctionCall.
  2. The flow calls get_weather_tool.run_async(args={'location': 'London'}, tool_context=ctx).
  3. Inside run_async, the tool uses ctx.function_call_id for logging or associating results.
  4. It might read user preferences from state: units = ctx.state.get(State.USER_PREFIX + 'weather_units', 'celsius').
  5. After fetching the weather (e.g., 15 degrees Celsius), it uses ctx.actions.state_delta to store the result: ctx.state['last_weather_query'] = {'location': 'London', 'temp': 15, 'units': 'celsius'}. (Remember, ctx.state writes to the delta in ctx.actions).
  6. The tool returns its primary result (e.g., the dictionary {'location': 'London', 'temp': 15, 'units': 'celsius'}).
  7. The flow uses the function_call_id from the context to correctly format the FunctionResponse and includes the state_delta captured in ctx.actions within the resulting Event.

Read the full file on GitHub · 258 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 258 lines · 0 tokens per session scan A 4748bd9e2bc2

Subscribe to this mod's changes

toolcontext is a cursor rule published in the GitHub repository altaidevorg/rules-for-ai (2 stars, last pushed 1y ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 3,817 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.