|
| 1 | +# OpenAPI Ingestion Methods in python-utcp |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +## Method 1: Direct OpenAPI Converter |
| 6 | + |
| 7 | +Use the `OpenApiConverter` class for maximum control over the conversion process. |
| 8 | + |
| 9 | +```python |
| 10 | +from utcp_http.openapi_converter import OpenApiConverter # utcp-http plugin |
| 11 | +import json |
| 12 | + |
| 13 | +# From local JSON file |
| 14 | +with open("api_spec.json", "r") as f: |
| 15 | + openapi_spec = json.load(f) |
| 16 | + |
| 17 | +converter = OpenApiConverter(openapi_spec) |
| 18 | +manual = converter.convert() |
| 19 | + |
| 20 | +print(f"Generated {len(manual.tools)} tools") |
| 21 | +``` |
| 22 | + |
| 23 | +```python |
| 24 | +from utcp_http.openapi_converter import OpenApiConverter # utcp-http plugin |
| 25 | +import yaml |
| 26 | + |
| 27 | +# From YAML file (can also be JSON) |
| 28 | +with open("api_spec.yaml", "r") as f: |
| 29 | + openapi_spec = yaml.safe_load(f) |
| 30 | + |
| 31 | +converter = OpenApiConverter(openapi_spec) |
| 32 | +manual = converter.convert() |
| 33 | +``` |
| 34 | + |
| 35 | +## Method 2: Remote OpenAPI Specification |
| 36 | + |
| 37 | +Fetch and convert OpenAPI specifications from remote URLs. |
| 38 | + |
| 39 | +```python |
| 40 | +import aiohttp |
| 41 | +from utcp_http.openapi_converter import OpenApiConverter # utcp-http plugin |
| 42 | + |
| 43 | +async def load_remote_spec(url): |
| 44 | + async with aiohttp.ClientSession() as session: |
| 45 | + async with session.get(url) as response: |
| 46 | + response.raise_for_status() |
| 47 | + openapi_spec = await response.json() |
| 48 | + |
| 49 | + converter = OpenApiConverter(openapi_spec, spec_url=url) |
| 50 | + return converter.convert() |
| 51 | + |
| 52 | +# Usage |
| 53 | +manual = await load_remote_spec("https://api.example.com/openapi.json") |
| 54 | +``` |
| 55 | + |
| 56 | +## Method 3: UTCP Client Configuration |
| 57 | + |
| 58 | +Include OpenAPI specs directly in your UTCP client configuration. |
| 59 | + |
| 60 | +```python |
| 61 | +from utcp.utcp_client import UtcpClient # core utcp package |
| 62 | + |
| 63 | +config = { |
| 64 | + "manual_call_templates": [ |
| 65 | + { |
| 66 | + "name": "weather_api", |
| 67 | + "call_template_type": "http", |
| 68 | + "url": "https://api.weather.com/openapi.json", |
| 69 | + "http_method": "GET" |
| 70 | + } |
| 71 | + ] |
| 72 | +} |
| 73 | + |
| 74 | +client = await UtcpClient.create(config=config) |
| 75 | +``` |
| 76 | + |
| 77 | +```python |
| 78 | +# With authentication |
| 79 | +config = { |
| 80 | + "manual_call_templates": [ |
| 81 | + { |
| 82 | + "name": "authenticated_api", |
| 83 | + "call_template_type": "http", |
| 84 | + "url": "https://api.example.com/openapi.json", |
| 85 | + "auth": { |
| 86 | + "auth_type": "api_key", |
| 87 | + "api_key": "${API_KEY}", |
| 88 | + "var_name": "Authorization", |
| 89 | + "location": "header" |
| 90 | + } |
| 91 | + } |
| 92 | + ] |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +## Method 4: Batch Processing |
| 97 | + |
| 98 | +Process multiple OpenAPI specifications programmatically. |
| 99 | + |
| 100 | +```python |
| 101 | +import aiohttp |
| 102 | +from utcp_http.openapi_converter import OpenApiConverter # utcp-http plugin |
| 103 | +from utcp.data.utcp_manual import UtcpManual # core utcp package |
| 104 | + |
| 105 | +async def process_multiple_specs(spec_urls): |
| 106 | + all_tools = [] |
| 107 | + |
| 108 | + for i, url in enumerate(spec_urls): |
| 109 | + async with aiohttp.ClientSession() as session: |
| 110 | + async with session.get(url) as response: |
| 111 | + openapi_spec = await response.json() |
| 112 | + |
| 113 | + converter = OpenApiConverter(openapi_spec, spec_url=url, call_template_name=f"api_{i}") |
| 114 | + manual = converter.convert() |
| 115 | + all_tools.extend(manual.tools) |
| 116 | + |
| 117 | + return UtcpManual(tools=all_tools) |
| 118 | + |
| 119 | +# Usage |
| 120 | +spec_urls = [ |
| 121 | + "https://api.github.com/openapi.json", |
| 122 | + "https://api.stripe.com/openapi.yaml" |
| 123 | +] |
| 124 | + |
| 125 | +combined_manual = await process_multiple_specs(spec_urls) |
| 126 | +``` |
| 127 | + |
| 128 | +## Key Features |
| 129 | + |
| 130 | +### Authentication Mapping |
| 131 | +OpenAPI security schemes automatically convert to UTCP auth objects: |
| 132 | + |
| 133 | +- `apiKey` → `ApiKeyAuth` |
| 134 | +- `http` (basic) → `BasicAuth` |
| 135 | +- `http` (bearer) → `ApiKeyAuth` |
| 136 | +- `oauth2` → `OAuth2Auth` |
| 137 | + |
| 138 | +### Multi-format Support |
| 139 | +- **OpenAPI 2.0 & 3.0**: Full compatibility |
| 140 | +- **JSON & YAML**: Automatic format detection |
| 141 | +- **Local & Remote**: Files or URLs |
| 142 | + |
| 143 | +### Schema Resolution |
| 144 | +- Handles `$ref` references automatically |
| 145 | +- Resolves nested object definitions |
| 146 | +- Detects circular references |
| 147 | + |
| 148 | +## Examples |
| 149 | + |
| 150 | +See the [examples repository](https://github.com/universal-tool-calling-protocol/utcp-examples) for complete working examples. |
0 commit comments