Function: withToolContext()
@providerprotocol/agents / index / withToolContext
Function: withToolContext()
Section titled “Function: withToolContext()”withToolContext(
tool,handler):Tool
Defined in: src/execution/tool-context.ts:239
Creates a context-aware tool wrapper for existing tools.
This utility function allows you to add context support to a tool that doesn’t natively support it. The handler function receives both the parameters and the optional execution context.
This is useful when:
- Wrapping third-party tools to add context awareness
- Creating tools that need to spawn sub-agents
- Adding logging or tracing to existing tools
Parameters
Section titled “Parameters”Tool
The original tool to wrap
handler
Section titled “handler”Function that receives params and context, returns result
Returns
Section titled “Returns”Tool
New tool with context support via the provided handler
Examples
Section titled “Examples”import { withToolContext } from '@providerprotocol/agents/execution';
// Wrap an existing tool to add context awarenessconst originalTool = { name: 'search', description: 'Search the web', parameters: { type: 'object', properties: { query: { type: 'string' } } }, run: async (params) => searchWeb(params.query),};
const contextAwareTool = withToolContext(originalTool, async (params, context) => { // Log which agent is using this tool console.log(`Agent ${context?.agentId} searching for: ${params.query}`);
// Optionally emit sub-agent events if (context?.onSubagentEvent) { // Could spawn a sub-agent here and emit events }
// Call the original implementation return searchWeb(params.query as string);});// Create a tool that spawns sub-agentsconst explorerTool = withToolContext(baseTool, async (params, context) => { const subagentId = generateUUID();
// Emit start event context?.onSubagentEvent?.({ type: 'subagent_start', subagentId, subagentType: 'explorer', parentToolCallId: context.toolCallId, prompt: params.task as string, timestamp: Date.now(), });
try { const result = await runExploration(params.task);
// Emit end event on success context?.onSubagentEvent?.({ type: 'subagent_end', subagentId, subagentType: 'explorer', parentToolCallId: context.toolCallId, success: true, result, timestamp: Date.now(), });
return result; } catch (error) { // Emit end event on failure context?.onSubagentEvent?.({ type: 'subagent_end', subagentId, subagentType: 'explorer', parentToolCallId: context.toolCallId, success: false, error: error.message, timestamp: Date.now(), }); throw error; }});