Skip to content

Function: injectToolContext()

@providerprotocol/agents


@providerprotocol/agents / index / 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

Tool<unknown, unknown>[]

Original tool array to wrap

ExecutionContext

Execution context from the agent

InjectToolContextOptions = {}

Additional options like event callbacks

Tool<unknown, unknown>[]

New array of wrapped tools with context injection

import { injectToolContext } from '@providerprotocol/agents/execution';
// In an execution strategy
const 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 tools
const llmWithContext = llm({ model, tools: wrappedTools });