OpenAPI Tool: Dynamic Tool Generation from REST APIs for Agent Invocation
OpenAPI Tool: Dynamic Tool Generation from REST APIs for Agent Invocation
Many internal services have REST APIs but no MCP interface. OpenAPI Tool lets you dynamically generate Tools from OpenAPI specs—as long as the API has documentation, it can connect to an Agent.
Use Cases
- Internal REST API, no MCP support
- Third-party REST API with OpenAPI spec
- Need to quickly connect an API to Agent without hand-writing Tools
Quick Start
1. Prepare OpenAPI spec
Say there’s a weather API, OpenAPI spec looks roughly like this:
openapi: 3.0.0
info:
title: Weather API
version: 1.0.0
paths:
/weather:
get:
operationId: getWeather
parameters:
- name: city
in: query
required: true
schema:
type: string
responses:
'200':
description: OK
content:
application/json:
schema:
type: object
properties:
temp:
type: number
condition:
type: string
Save as weather-api.yaml.
2. Generate Tool
import "google.golang.org/adk/tool/openapi"
openapiTool, err := openapi.NewOpenAPITool(ctx,
openapi.WithSpecFile("weather-api.yaml"),
)
3. Register to Agent
agent, _ := llmagent.New(llmagent.Config{
Name: "weather_assistant",
Model: model,
Instruction: "你是天气助手,帮我查询天气信息。",
Tools: []tool.Tool{
openapiTool, // Exposes getWeather Tool
},
})
Load spec from URL
Can also load from URL:
openapiTool, err := openapi.NewOpenAPITool(ctx,
openapi.WithSpecURL("https://api.example.com/openapi.yaml"),
)
Auth Configuration
If API needs auth:
openapiTool, err := openapi.NewOpenAPITool(ctx,
openapi.WithSpecFile("internal-api.yaml"),
openapi.WithAuth(openapi.APIKeyAuth("X-API-Key", "your-key")),
)
Supported methods:
- API Key (header or query)
- Bearer Token
- Basic Auth
Common Issues
Q: Spec too complex, Tool generation errors A: Simplify spec, only expose necessary endpoints. OpenAPI Tool may have incomplete support for some spec features.
Q: API needs POST body but Tool call fails
A: Check if content/application/json’s schema definition in spec is complete.
Next Steps
Finally, look at Tool Confirmation—when Agent needs to do risky operations (delete data, transfer money), how to let user confirm before executing.
← MCP Server Integration | Tool Confirmation & Security →
Follow “Mengshou Programming” on WeChat for more Go ADK hands-on tutorials, weekly updates on Go / AI programming 实战干货.
