-
Notifications
You must be signed in to change notification settings - Fork 36
Update docs #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Update docs #63
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
# OpenAPI Ingestion Methods in python-utcp | ||
|
||
UTCP automatically converts OpenAPI 2.0/3.0 specifications into UTCP tools, enabling AI agents to interact with REST APIs without requiring server modifications or additional infrastructure. | ||
|
||
## Method 1: Direct OpenAPI Converter | ||
|
||
Use the `OpenApiConverter` class for maximum control over the conversion process. | ||
|
||
```python | ||
from utcp_http.openapi_converter import OpenApiConverter # utcp-http plugin | ||
import json | ||
|
||
# From local JSON file | ||
with open("api_spec.json", "r") as f: | ||
openapi_spec = json.load(f) | ||
|
||
converter = OpenApiConverter(openapi_spec) | ||
manual = converter.convert() | ||
|
||
print(f"Generated {len(manual.tools)} tools") | ||
``` | ||
|
||
```python | ||
from utcp_http.openapi_converter import OpenApiConverter # utcp-http plugin | ||
import yaml | ||
|
||
# From YAML file (can also be JSON) | ||
with open("api_spec.yaml", "r") as f: | ||
openapi_spec = yaml.safe_load(f) | ||
|
||
converter = OpenApiConverter(openapi_spec) | ||
manual = converter.convert() | ||
``` | ||
|
||
## Method 2: Remote OpenAPI Specification | ||
|
||
Fetch and convert OpenAPI specifications from remote URLs. | ||
|
||
```python | ||
import aiohttp | ||
from utcp_http.openapi_converter import OpenApiConverter # utcp-http plugin | ||
|
||
async def load_remote_spec(url): | ||
async with aiohttp.ClientSession() as session: | ||
async with session.get(url) as response: | ||
response.raise_for_status() | ||
openapi_spec = await response.json() | ||
|
||
converter = OpenApiConverter(openapi_spec, spec_url=url) | ||
return converter.convert() | ||
|
||
# Usage | ||
manual = await load_remote_spec("https://api.example.com/openapi.json") | ||
``` | ||
|
||
## Method 3: UTCP Client Configuration | ||
|
||
Include OpenAPI specs directly in your UTCP client configuration. | ||
|
||
```python | ||
from utcp.utcp_client import UtcpClient # core utcp package | ||
|
||
config = { | ||
"manual_call_templates": [ | ||
{ | ||
"name": "weather_api", | ||
"call_template_type": "http", | ||
"url": "https://api.weather.com/openapi.json", | ||
"http_method": "GET" | ||
} | ||
] | ||
} | ||
|
||
client = await UtcpClient.create(config=config) | ||
``` | ||
|
||
```python | ||
# With authentication | ||
config = { | ||
"manual_call_templates": [ | ||
{ | ||
"name": "authenticated_api", | ||
"call_template_type": "http", | ||
"url": "https://api.example.com/openapi.json", | ||
"auth": { | ||
"auth_type": "api_key", | ||
"api_key": "${API_KEY}", | ||
"var_name": "Authorization", | ||
"location": "header" | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
|
||
## Method 4: Batch Processing | ||
|
||
Process multiple OpenAPI specifications programmatically. | ||
|
||
```python | ||
import aiohttp | ||
from utcp_http.openapi_converter import OpenApiConverter # utcp-http plugin | ||
from utcp.data.utcp_manual import UtcpManual # core utcp package | ||
|
||
async def process_multiple_specs(spec_urls): | ||
all_tools = [] | ||
|
||
for i, url in enumerate(spec_urls): | ||
async with aiohttp.ClientSession() as session: | ||
async with session.get(url) as response: | ||
openapi_spec = await response.json() | ||
|
||
converter = OpenApiConverter(openapi_spec, spec_url=url, call_template_name=f"api_{i}") | ||
manual = converter.convert() | ||
all_tools.extend(manual.tools) | ||
|
||
return UtcpManual(tools=all_tools) | ||
|
||
# Usage | ||
spec_urls = [ | ||
"https://api.github.com/openapi.json", | ||
"https://api.stripe.com/openapi.yaml" | ||
] | ||
|
||
combined_manual = await process_multiple_specs(spec_urls) | ||
``` | ||
|
||
## Key Features | ||
|
||
### Authentication Mapping | ||
OpenAPI security schemes automatically convert to UTCP auth objects: | ||
|
||
- `apiKey` → `ApiKeyAuth` | ||
- `http` (basic) → `BasicAuth` | ||
- `http` (bearer) → `ApiKeyAuth` | ||
- `oauth2` → `OAuth2Auth` | ||
|
||
### Multi-format Support | ||
- **OpenAPI 2.0 & 3.0**: Full compatibility | ||
- **JSON & YAML**: Automatic format detection | ||
- **Local & Remote**: Files or URLs | ||
|
||
### Schema Resolution | ||
- Handles `$ref` references automatically | ||
- Resolves nested object definitions | ||
- Detects circular references | ||
|
||
## Examples | ||
|
||
See the [examples repository](https://github.com/universal-tool-calling-protocol/utcp-examples) for complete working examples. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docstring omits that successful stdout is parsed as JSON when possible; this line now misstates the function’s behavior, which returns parsed JSON via _parse_combined_output when the output looks like JSON.
Prompt for AI agents