Function: injectToolContext()
@providerprotocol/agents / index / injectToolContext
Function: injectToolContext()
Section titled “Function: injectToolContext()”injectToolContext(
tools,context,options):Tool<unknown,unknown>[]
Defined in: src/execution/tool-context.ts:67
Wraps tools to inject execution context when they support it.
Per UAP-1.0 Section 8.4, tools that accept a second parameter should receive
execution context including agentId, stateId, and sub-agent event callbacks.
This function wraps an array of tools to automatically inject this context
when the tool’s run function has arity > 1.
This enables:
- Sub-agent model/config inheritance: Tools can create sub-agents that inherit configuration from the parent agent
- Execution hierarchy tracking: The agentId and stateId enable tracing through nested agent executions
- Sub-agent event propagation: Tools can emit events about sub-agent lifecycle that bubble up to the parent
Parameters
Section titled “Parameters”Tool<unknown, unknown>[]
Original tool array to wrap
context
Section titled “context”Execution context from the agent
options
Section titled “options”Additional options like event callbacks
Returns
Section titled “Returns”Tool<unknown, unknown>[]
New array of wrapped tools with context injection
Example
Section titled “Example”import { injectToolContext } from '@providerprotocol/agents/execution';
// In an execution strategyconst wrappedTools = injectToolContext(tools, context, { onSubagentEvent: (event) => { // Handle sub-agent events (start, inner events, end) if (event.type === 'subagent_start') { console.log(`Sub-agent ${event.subagentId} started`); } },});
// Create LLM with wrapped toolsconst llmWithContext = llm({ model, tools: wrappedTools });- UAP-1.0 Spec Section 8.4
- ToolExecutionContext for the context structure
- isContextAwareTool to check if a tool accepts context