The ITool & IToolProvider Interfaces
At the heart of AgentB's ability to perform actions and interact with external systems are Tools. This guide explains the fundamental interfaces: ITool for defining individual tools and IToolProvider for supplying them to agents.
The ITool Interface: Defining a Capability
ITool Interface: Defining a CapabilityAn ITool represents a single, distinct capability that an agent can use. To create a custom tool, you implement this interface.
Core Methods of ITool:
getDefinition(): Promise<IToolDefinition> | IToolDefinitionPurpose: This method is crucial. It returns an
IToolDefinitionobject that describes your tool to the Large Language Model (LLM). The LLM uses this information to understand what the tool does and how to call it.IToolDefinitionStructure:name: string: The unique, programmatic name of the tool (e.g.,searchFlights,getWeatherForecast). Must be LLM-friendly (e.g., alphanumeric, underscores, hyphens, typically max 64 chars).description: string: A clear, natural language description of what the tool does, its purpose, and when it should be used. This is very important for the LLM's decision-making process.parameters: IToolParameter[]: An array describing the input parameters the tool expects.IToolParameterStructure:name: string: Name of the parameter (e.g.,destinationCity,date).type: string: The basic data type (e.g.,string,number,boolean,object,array).description: string: Natural language description of this parameter for the LLM.required: boolean: Whether this parameter is mandatory.schema?: any: (Highly Recommended) A JSON Schema object defining the structure, constraints (enums, min/max, pattern), and detailed type for this parameter. This gives the LLM precise instructions for formatting arguments.
execute(input: Input, agentContext?: IAgentContext): Promise<IToolResult>Purpose: This method contains the actual logic that your tool performs.
input: Input: An object containing the arguments for the tool. The LLM constructs this object based on yourIToolDefinition'sparameters. The shape ofInputshould match the properties defined in your parameters. For example, if you define parameterscityanddate,inputwould be{ city: "Paris", date: "2024-12-25" }.agentContext?: IAgentContext(Optional): If your tool needs access to broader runtime information (likerunConfig,threadId, or other services available during the agent's run), theIAgentContextcan be passed here.Promise<IToolResult>: The method must return a Promise that resolves to anIToolResultobject.IToolResultStructure:success: boolean:trueif the tool's operation was successful,falseotherwise.data: any: The data payload resulting from the tool's successful execution. This is what gets passed back to the LLM.error?: string: A descriptive error message ifsuccessisfalse.metadata?: Record<string, any>: Optional additional information about the tool's execution (e.g., API call duration, units of data processed).
Example (Conceptual ITool):
(See the Custom Tool Tutorial for a runnable example).
The IToolProvider Interface: Supplying Tools
IToolProvider Interface: Supplying ToolsAn agent doesn't directly manage a list of ITool instances. Instead, it receives an IToolProvider via its IAgentContext. The IToolProvider is responsible for making tools available.
Core Methods of IToolProvider:
getTools(): Promise<ITool[]>:Returns a Promise that resolves to an array of all
IToolinstances this provider can supply.
getTool(toolName: string): Promise<ITool | undefined>:Returns a Promise that resolves to a specific
IToolinstance by its name, orundefinedif not found.
ensureInitialized?(): Promise<void>(Optional):Some providers might need an asynchronous initialization step (e.g., an
OpenAPIConnectorneeds to fetch and parse its spec). This method allows the system (likeApiInteractionManager) to ensure the provider is ready before attempting to get tools from it.
Why IToolProvider?
Dynamic Tool Loading: Providers can dynamically load or generate tools (e.g.,
OpenAPIConnectorcreates tools based on an API spec at runtime).Abstraction: Agents interact with the
IToolProviderinterface, not concrete tool sources.Composition: Providers like
AggregatedToolProvidercan combine tools from multiple other providers.Specialization: You can have providers specialized for certain types of tools (e.g., database tools, file system tools).
Common Implementations in AgentB:
OpenAPIConnector: ImplementsIToolProviderto serve tools derived from an OpenAPI specification.AggregatedToolProvider: ImplementsIToolProviderby taking an array of otherIToolProviders and presenting their tools as a single collection (handles de-duplication by name).Custom Providers: You can implement
IToolProviderto supply your custom tools, as shown in the Custom Tool Tutorial.
By implementing ITool and making them available via an IToolProvider, you give your AgentB agents the capabilities they need to perform meaningful actions and interact with the world. The quality of your IToolDefinitions (especially the descriptions) directly impacts how well the LLM can understand and utilize your tools.
Last updated