Skip to content

Function Calls

The AI Context module provides three AI function call plugins grouped under Context Tools (context_tools), registered via the Drupal\ai_context\Plugin\AiFunctionGroup\ContextTools plugin. This group label appears in AI module tool pickers alongside other function groups.

Plugins in this group:

  • ai_context:list_ai_context_items
  • ai_context:load_ai_context_items
  • ai_context:get_relevant_ai_context_items

The group also has ai_context:load_ai_context_item_by_id, a deprecated alias of ai_context:load_ai_context_items that is removed in 2.0.0. See Deprecated alias.

Agents can invoke them as tools for context discovery, loading, and relevance-based selection.

Tools that should inherit the running consumer ID when consumer_id is omitted implement AiContextInheritsConsumerIdInterface. GetRelevantAiContextItems and ListAiContextItems do; LoadAiContextItems does not.

All three plugins, and the deprecated alias, require the current user to have at least one of:

  • Use Published AI Context in AI Features (access published ai context)
  • View AI Context Items in CCC (view ai context items)
  • Administer AI Context (administer ai context)

See Configuration for how these permissions differ from CCC listing and editorial access.

The readable output of all three tools neutralizes Drupal tokens in item text, for example [user:mail] becomes [user : mail]. AI Agents replaces tokens in default information tool output that it adds to the system prompt, so item text must not expand to data of the user who runs the agent. The tools neutralize on every call, also for agent tool calls, MCP, and other callers where no replacement runs. An item that documents real token patterns, such as [node:title] for URL alias patterns, therefore shows them with a space on both sides of the colon in tool output. See Authoring trust.

Tool workflow

For agent-driven discovery, the typical flow is:

  1. List available items with ai_context:list_ai_context_items
  2. Load chosen items with ai_context:load_ai_context_items

Alternatively, Get relevant (ai_context:get_relevant_ai_context_items) runs the full selection pipeline in one step (scope scoring, subcontext resolution, and rendering).

GetRelevantAiContextItems

Plugin ID: ai_context:get_relevant_ai_context_items

Function name: ai_context_get_relevant_ai_context_items

Class: Drupal\ai_context\Plugin\AiFunctionCall\GetRelevantAiContextItems

Selects and returns relevant context for a task using the same pipeline as automatic system-prompt injection. Builds a selection with fromParameters() and runs it with getResultFromSelection().

Parameters

Parameter Type Required Description
task string Yes Current task or user prompt to select context for
scope_subscriptions object No Scope plugin IDs and their subscribed value lists. Context Tags and Taxonomy Terms values are taxonomy term UUIDs (e.g. {"language":["en"],"tag":["7a1e8432-3616-438e-a411-76c93afba733"]})
always_include list No Context item UUIDs (or leftover numeric IDs) to always include
never_include list No Context item UUIDs (or leftover numeric IDs) to never include
consumer_id string No Canonical consumer ID (for example agent:my_agent). Formerly agent_id. When omitted during an agent run, the running agent is used as agent:{id}. Non-canonical values are ignored
selection_mode string No minimal, relevant, or broad -- a nested ladder where each mode includes everything the one before it does. Omit to inherit the consumer's saved mode when consumer_id is set; otherwise the site-wide default applies (relevant unless changed). An explicit value overrides the consumer
entity_type string No Entity type for contextual scope matching (e.g. node)
entity_id string or integer No Entity ID for contextual scope matching (used with entity_type). Numeric IDs and machine-name IDs are valid. Other types are ignored
max_tokens integer No Override for max tokens in rendered text

Behavior

Builds an AiContextSelection from the parameters and returns the rendered context text. If no items match, the output is "No relevant context items found."

When consumer_id is provided, that consumer's saved scope subscriptions, always/never include lists, and limits are merged with explicit parameters. Explicit scope values replace the same scope key, include lists are combined, and explicit limits or selection_mode override consumer values. Exported consumer config stores those include lists as context item UUIDs. Runtime selection still accepts leftover numeric IDs.

Get Relevant still constrains selection_mode with Choice (minimal, relevant, broad). An unrecognized tool-call value, including leftover match_all, can fail AI Agents validation before fromArray() runs. Direct fromArray() callers drop unrecognized string modes to NULL and inherit the consumer or site default. The no-throw path is only for those direct callers.

When the tool runs inside an agent and consumer_id is omitted, AiContextAgentToolSubscriber fills it from AgentToolPreExecuteEvent before execute() as agent:{running agent}. Saved subscriptions, include lists, and selection mode are then merged the same way as an explicit ID. An explicit canonical consumer_id still wins. A non-empty non-canonical value is replaced the same way as omitted and logged at debug. Non-agent callers never fire that event, so they keep consumer=none unless they pass an ID. getResult() and getRenderedContext() still leave consumerId null for non-agent callers. See Page and entity context.

entity_type and entity_id are likewise optional. Without them, entity-dependent scopes have no request entity to match. entity_id accepts a numeric ID or a machine-name ID. Other types are dropped, matching fromArray(). See Page and entity context.

Example output

## Brand Guidelines
Use our primary brand color (#1a73e8) for all headings...

## Product Categories
Our catalog is organized into three main categories...

ListAiContextItems

Plugin ID: ai_context:list_ai_context_items

Function name: ai_context_list_ai_context_items

Class: Drupal\ai_context\Plugin\AiFunctionCall\ListAiContextItems

Lists published, accessible context items with ID, label, and purpose. Intended as a lightweight discovery tool so agents can choose what to load via LoadAiContextItems. Does not return full item content.

Results are capped at 200 items (sorted by ID ascending).

Parameters

Parameter Type Required Description
scope_subscriptions object No Filter by scope subscriptions
consumer_id string No Canonical consumer ID (for example agent:my_agent). Formerly agent_id. When omitted during an agent run, the running agent is used as agent:{id}
entity_type string No Entity type for contextual scope matching
entity_id string or integer No Entity ID for contextual scope matching; used with entity_type. Numeric IDs and machine-name IDs are valid. Other types are ignored

Behavior

Loads published items, then applies optional filters via AiContextScopeResolver (hard context filters, scope subscriptions, entity item filters, and consumer never/always include lists). Each remaining item is then switched to its translation for the current content language. An item is left out when that translation is not published or the current user cannot view it. See Multilingual.

When the tool runs inside an agent and consumer_id is omitted, AiContextAgentToolSubscriber fills it from AgentToolPreExecuteEvent before execute(), the same way as GetRelevantAiContextItems. An explicit canonical consumer_id still wins. A non-empty non-canonical value is replaced the same way as omitted and logged at debug. Non-agent callers never fire that event.

Example output

Available context items (3):

- [5] Brand Guidelines — Tone, voice, and visual identity rules
- [12] Product Categories — Catalog organization overview
- [3] Compliance Notes — Medical device regulatory context

LoadAiContextItems

Plugin ID: ai_context:load_ai_context_items

Function name: ai_context_load_ai_context_items

Class: Drupal\ai_context\Plugin\AiFunctionCall\LoadAiContextItems

Loads one or more context items by ID and returns fully rendered content. Use after listing items. Requested IDs appear in tool arguments for observability.

Parameters

Parameter Type Required Description
context_item_ids list Yes One or more numeric context item IDs
task string No Task description for conditional subcontext resolution
max_tokens integer No Max tokens override for rendered text
include_subcontext boolean No Include child items of loaded parents (default: true)

Behavior

Loads published items by ID and translates them to the current content language. An item is left out when that translation is not published or the current user cannot view it. It then optionally resolves subcontext children via AiContextSubcontextResolver, with the same rule for each child, and renders via AiContextRenderer within the token limit.

If subcontext is disabled in module settings, child items are not included regardless of include_subcontext. See Subcontext.

Example output

## Brand Guidelines
Use our primary brand color (#1a73e8) for all headings...

Deprecated alias: load_ai_context_item_by_id

Plugin ID: ai_context:load_ai_context_item_by_id

Function name: ai_context_load_ai_context_item_by_id

Class: Drupal\ai_context\Plugin\AiFunctionCall\LoadAiContextItemById

This plugin ID is deprecated in 1.0.0 and is removed in 2.0.0. It has the same parameters, permission checks, and output as ai_context:load_ai_context_items, so agents that use it keep working. Its tool name is "Load AI Context Item By ID (deprecated)". Its description starts with "Deprecated: use ai_context:load_ai_context_items." and then tells the model that the tool still works and returns the same result, because the model receives this description and an older agent often has only the old tool. Each call triggers an E_USER_DEPRECATED notice. Listing the tools on the agent form does not. Use ai_context:load_ai_context_items for new agents, and in the Default information tools of existing agents. See Upgrade from 1.0.0-rc1: Load tool plugin ID.