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
ITool
for each discoverable operation (e.g.,GET /users/{id}
becomes agetUserById
tool).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
requestAuthOverrides
inAgentRunConfig
).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
OpenAPIConnector
instance withOpenAPIConnectorOptions
.These options include the
spec
object orspecUrl
,authentication
details, ansourceId
, and an optionaltagFilter
.During
ensureInitialized()
, the connector fetches (ifspecUrl
) and parses the OpenAPI spec usingOpenAPISpecParser
.
Tool Creation:
The
OpenAPISpecParser
extracts all valid operations (those with anoperationId
).If a
tagFilter
is active, only operations matching the tag are considered.For each selected operation, an
OpenAPIOperationTool
instance is created.The
OpenAPIOperationTool
'sgetDefinition()
method constructs anIToolDefinition
using the operation'soperationId
(sanitized) as the tool name, itssummary
/description
, and its parameters (path, query, header, requestBody).
If configured (no
tagFilter
andincludeGenericToolIfNoTagFilter
is true), aGenericHttpApiTool
is also added.
Tool Execution:
When an agent's LLM decides to call a tool provided by the
OpenAPIConnector
(e.g.,getUserById
):The
ToolExecutor
calls theexecute()
method of the correspondingOpenAPIOperationTool
.The
OpenAPIOperationTool
then 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-Type
andAccept
.Serializes the
requestBody
(usually as JSON).Applies authentication (static or dynamic override from
agentContext.runConfig.requestAuthOverrides
matched bysourceId
).Makes the HTTP request using
fetch
.Parses the response and returns an
IToolResult
.
If
genericHttpRequest
is called,OpenAPIConnector.executeGenericOperationInternal()
is used, which takes more explicit HTTP details (method, path, queryParams, headers, body) from the LLM.
OpenAPIConnectorOptions
OpenAPIConnectorOptions
When creating an OpenAPIConnector
, you use these options:
interface OpenAPIConnectorOptions extends BaseOpenAPIConnectorOptions {
sourceId: string; // Unique ID for this connector instance, matches ToolProviderSourceConfig.id
tagFilter?: string; // Optional: Filter tools by this API tag
includeGenericToolIfNoTagFilter?: boolean; // Default: true
}
interface BaseOpenAPIConnectorOptions {
specUrl?: string; // URL to OpenAPI spec (JSON/YAML)
spec?: OpenAPISpec; // Or, pre-loaded spec object
authentication?: ConnectorAuthentication; // See Authentication guide
businessContextText?: string; // For LLM prompts
}
sourceId: string
(Crucial): A unique identifier for this specific connector instance. This ID is used byApiInteractionManager
andAgentB
facade to match dynamicrequestAuthOverrides
from theAgentRunConfig
to this provider. It should match theid
field of theToolProviderSourceConfig
if 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 notagFilter
is set, the powerfulgenericHttpRequest
tool is added. Set tofalse
to 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:
import { OpenAPIConnector, OpenAPIConnectorOptions } from '@ulifeai/agentb';
// Assume 'myApiSpec' is a loaded OpenAPI spec object
// Assume 'myApiSpec' operations have 'operationId's
const connectorOptions: OpenAPIConnectorOptions = {
sourceId: 'myApiServiceV1',
spec: myApiSpec,
authentication: { type: 'bearer', token: 'static_fallback_token' }
};
const apiToolsProvider = new OpenAPIConnector(connectorOptions);
async function main() {
await apiToolsProvider.ensureInitialized();
const tools = await apiToolsProvider.getTools();
console.log(`Available tools from ${connectorOptions.sourceId}:`);
for (const tool of tools) {
const def = await tool.getDefinition();
console.log(`- ${def.name}: ${def.description}`);
}
// Example: Get a specific tool
const userTool = await apiToolsProvider.getTool('getUserById'); // Assuming operationId 'getUserById' exists
if (userTool) {
// In a real agent, the LLM would provide 'input'.
// The 'agentContext' would come from the agent's run.
// For dynamic auth, agentContext.runConfig.requestAuthOverrides would be checked.
const result = await userTool.execute(
{ id: 123 }, // Example input if getUserById takes an 'id'
// Mock agentContext for dynamic auth example:
// { runConfig: { requestAuthOverrides: { 'myApiServiceV1': { type: 'bearer', token: 'dynamic_user_token'} } } } as any
);
console.log("Tool execution result:", result);
}
}
main();
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
operationId
s: Your OpenAPI operations must have uniqueoperationId
s forOpenAPIConnector
to generate specific tools for them.Clear
summary
anddescription
: Thesummary
anddescription
fields 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
sourceId
for Auth: When using dynamic authentication (requestAuthOverrides
), ensure thesourceId
inOpenAPIConnectorOptions
matches the key you use in the overrides object.Consider
tagFilter
: For large APIs, usingtagFilter
can 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