OpenAPIConnector
The OpenAPIConnector is a powerful IToolProvider in AgentB that automatically generates callable ITool instances from an OpenAPI (v3.x, with some Swagger 2.0 compatibility) specification. This makes it incredibly easy to connect your agents to existing REST APIs.
Key Features
Automatic Tool Generation: Parses an OpenAPI spec and creates an
IToolfor each discoverable operation (e.g.,GET /users/{id}becomes agetUserByIdtool).Parameter Handling: Tool definitions automatically include parameters derived from the OpenAPI operation's path, query, header, and request body definitions. JSON Schemas for these parameters are used to guide the LLM.
Authentication: Supports common authentication schemes like Bearer tokens and API keys, configurable statically or dynamically per request (via
requestAuthOverridesinAgentRunConfig).Tag Filtering: Can be configured to expose only tools related to specific API tags, allowing for more focused toolsets.
Generic HTTP Tool: Optionally includes a
GenericHttpApiTool(namedgenericHttpRequest) that allows the LLM to make arbitrary calls to the API if a specific operation tool isn't suitable or available.Spec Loading: Can load OpenAPI specs from a URL or a pre-loaded JSON/YAML object.
How it Works
Initialization:
You create an
OpenAPIConnectorinstance withOpenAPIConnectorOptions.These options include the
specobject orspecUrl,authenticationdetails, ansourceId, and an optionaltagFilter.During
ensureInitialized(), the connector fetches (ifspecUrl) and parses the OpenAPI spec usingOpenAPISpecParser.
Tool Creation:
The
OpenAPISpecParserextracts all valid operations (those with anoperationId).If a
tagFilteris active, only operations matching the tag are considered.For each selected operation, an
OpenAPIOperationToolinstance is created.The
OpenAPIOperationTool'sgetDefinition()method constructs anIToolDefinitionusing the operation'soperationId(sanitized) as the tool name, itssummary/description, and its parameters (path, query, header, requestBody).
If configured (no
tagFilterandincludeGenericToolIfNoTagFilteris true), aGenericHttpApiToolis also added.
Tool Execution:
When an agent's LLM decides to call a tool provided by the
OpenAPIConnector(e.g.,getUserById):The
ToolExecutorcalls theexecute()method of the correspondingOpenAPIOperationTool.The
OpenAPIOperationToolthen delegates toOpenAPIConnector.executeSpecificOperationInternal().This internal method:
Constructs the full request URL using the connector's base URL and the operation's path template, substituting path parameters from the LLM's input.
Assembles query parameters.
Prepares request headers, including handling
Content-TypeandAccept.Serializes the
requestBody(usually as JSON).Applies authentication (static or dynamic override from
agentContext.runConfig.requestAuthOverridesmatched bysourceId).Makes the HTTP request using
fetch.Parses the response and returns an
IToolResult.
If
genericHttpRequestis called,OpenAPIConnector.executeGenericOperationInternal()is used, which takes more explicit HTTP details (method, path, queryParams, headers, body) from the LLM.
OpenAPIConnectorOptions
OpenAPIConnectorOptionsWhen creating an OpenAPIConnector, you use these options:
sourceId: string(Crucial): A unique identifier for this specific connector instance. This ID is used byApiInteractionManagerandAgentBfacade to match dynamicrequestAuthOverridesfrom theAgentRunConfigto this provider. It should match theidfield of theToolProviderSourceConfigif you're using the facade orToolsetOrchestrator.specUrl/spec: How to load the OpenAPI definition.authentication: Static authentication configuration (Bearer, API Key, None). See the Authentication Guide.tagFilter: If you only want to expose a subset of your API based on a tag (e.g., "User Management" tools only).includeGenericToolIfNoTagFilter: Iftrue(default) and notagFilteris set, the powerfulgenericHttpRequesttool is added. Set tofalseto only include tools generated from specific operations.businessContextText: Added to system prompts when tools from this connector are presented to the LLM.
Example Usage (Directly)
While often used via AgentB facade or ApiInteractionManager, here's how you might use it directly:
System Prompts
When tools from an OpenAPIConnector are made available to an LLM (usually via ApiInteractionManager in genericOpenApi mode), a system prompt is generated using generateGenericHttpToolSystemPrompt. This prompt lists the available operations (tool names, descriptions, parameters) and instructs the LLM on how to use them, or how to use the genericHttpRequest tool if it's included.
Best Practices
Ensure
operationIds: Your OpenAPI operations must have uniqueoperationIds forOpenAPIConnectorto generate specific tools for them.Clear
summaryanddescription: Thesummaryanddescriptionfields for operations and parameters in your OpenAPI spec are directly used to generate tool descriptions for the LLM. Make them clear and informative.Define Parameter Schemas: Use JSON Schema within your OpenAPI parameter and request body definitions to specify types, formats, enums, and requirements. This greatly helps the LLM construct valid arguments for your tools.
Use
sourceIdfor Auth: When using dynamic authentication (requestAuthOverrides), ensure thesourceIdinOpenAPIConnectorOptionsmatches the key you use in the overrides object.Consider
tagFilter: For large APIs, usingtagFiltercan create smaller, more manageable sets of tools, which can improve LLM performance and reduce token usage.GenericHttpApiTool: Use with caution. While powerful, it gives the LLM broad ability to call any path. It's often best used when you trust the LLM's ability to interpret API documentation (provided in the prompt) or when specific operation tools are too numerous.
The OpenAPIConnector is a cornerstone of AgentB's tool integration capabilities, bridging the gap between natural language instructions and structured API interactions.
Last updated