rig-openapi-tools
Point at an OpenAPI 3.0 spec, get a rig agent that can call every endpoint. No codegen, no macros.
Parses the spec at runtime into ToolDyn trait objects. Supports path/query/header parameters, JSON request bodies, and $ref resolution. Parse once at startup, clone cheaply per request with per-user context injection.
Quick start
[]
= { = "." }
= "0.32"
= { = "1", = ["macros", "rt-multi-thread"] }
use ;
use Prompt;
use OpenApiToolset;
async
Builder
let toolset = builder_from_file?
.base_url
.bearer_token
.build?;
| Method | Description |
|---|---|
.base_url(url) |
Override the base URL from the spec |
.bearer_token(token) |
Set a Bearer token for all requests |
.client(client) |
Provide a pre-configured reqwest::Client |
.hidden_context(key, value) |
Auto-inject a param into tool calls, invisible to the LLM |
Context
Real apps need per-request state: user IDs, session tokens, tenant info. The toolset supports two kinds of context.
Hidden context gets injected into tool calls at execution time. The LLM doesn't see these params in the schema at all:
// Parse once at startup
let toolset = builder_from_file?
.hidden_context // static, all requests
.build?;
// Per request
let ctx = from;
let tools = toolset.tools_with_context;
let agent = openai
.agent
.preamble
.tools
.build;
Visible context is for things the LLM should know about. Generate a preamble snippet:
let visible = from;
let agent = openai
.agent
.preamble
.tools
.build;
The LLM sees this in the system prompt:
The following context is available. Use these values when calling tools:
- username = alice
- role = admin
How it works
OpenAPI Spec (YAML/JSON)
|
v
OpenApiToolset::from_file() <-- parse once at startup
|
v
Vec<OpenApiTool> <-- internal, cloneable
|
+---> .into_tools() --> Vec<Box<dyn ToolDyn>> (simple case)
+---> .tools_with_context(&ctx) --> Vec<Box<dyn ToolDyn>> (per request)
|
v
rig AgentBuilder::tools()
|
v
LLM picks tool + fills args --> HTTP request --> response back to LLM
Each operation in the spec becomes a tool. The name comes from operationId (falls back to get_users style). Description comes from summary/description. Parameter schemas are passed through to the LLM as is.
Examples
Requires OPENAI_API_KEY in the environment.
License
MIT