Authentication Mechanisms
Relevant source files
The following files were used as context for generating this wiki page:
- core/src/utcp/data/auth.py
- core/src/utcp/data/auth_implementations/api_key_auth.py
- core/src/utcp/data/auth_implementations/basic_auth.py
- core/src/utcp/data/auth_implementations/oauth2_auth.py
- plugins/communication_protocols/http/src/utcp_http/http_communication_protocol.py
- plugins/communication_protocols/http/src/utcp_http/openapi_converter.py
- README.md
The Universal Tool Calling Protocol (UTCP) provides a flexible and extensible authentication system to securely interact with tools across various communication protocols. The architecture is designed to be protocol-agnostic, allowing different authentication schemes to be applied on a per-provider basis within a CallTemplate. This enables the UTCP client to connect to a wide range of existing services, each with its own security requirements.
The core of the authentication system is a set of Pydantic models representing different auth types. A base Auth model provides a common interface, with specific implementations for API Key, Basic Authentication, and OAuth2. This structure allows for easy serialization and configuration through JSON or Python dictionaries, and it is a key part of UTCP's emphasis on interoperability and ease of use.
Sources: CLAUDE.md, core/README.md
Core Architecture
The authentication mechanism is built around a base Auth Pydantic model and a dynamic serializer, AuthSerializer, which can validate and instantiate the correct authentication model based on the auth_type field.
Sources: core/src/utcp/data/auth.py
Class Diagram
This diagram shows the inheritance structure of the authentication models.
Auth Application Flow
Communication protocol implementations, such as HttpCommunicationProtocol, use a dedicated function to apply the configured authentication to an outgoing request. The _apply_auth function inspects the auth object within the HttpCallTemplate and modifies the request headers or query parameters accordingly.
This diagram illustrates the logic within the _apply_auth function in HttpCommunicationProtocol.
Sources: plugins/communication_protocols/http/src/utcp_http/http_communication_protocol.py:73-103
Authentication Types
UTCP supports three primary authentication schemes out of the box.
API Key Authentication
This method uses a single key for authentication. It is highly flexible, supporting placement of the key in request headers, query parameters, or cookies.
| Field | Type | Description |
|---|---|---|
auth_type | Literal["api_key"] | The type identifier for this authentication method. |
api_key | str | The API key value. Can contain variables like ${API_KEY}. |
var_name | str | The name of the header, query parameter, or cookie. Defaults to X-Api-Key. |
location | Literal["header", "query", "cookie"] | Where to place the API key. Defaults to header. |
Sources: core/src/utcp/data/auth_implementations/api_key_auth.py:11-23, README.md
Configuration Example
{
"auth": {
"auth_type": "api_key",
"api_key": "Bearer $API_KEY",
"var_name": "Authorization",
"location": "header"
}
}
Sources: README.md
Basic Authentication
This method uses a standard username and password combination, typically encoded and sent in the Authorization header.
| Field | Type | Description |
|---|---|---|
auth_type | Literal["basic"] | The type identifier for this authentication method. |
username | str | The username for authentication. |
password | str | The password for authentication. |
Sources: core/src/utcp/data/auth_implementations/basic_auth.py:9-16
Configuration Example
{
"auth": {
"auth_type": "basic",
"username": "${USERNAME}",
"password": "${PASSWORD}"
}
}
Sources: README.md
OAuth2 Authentication
UTCP supports the OAuth2 Client Credentials flow. The protocol implementation handles the process of obtaining an access token from a token URL and caching it for subsequent requests.
| Field | Type | Description |
|---|---|---|
auth_type | Literal["oauth2"] | The type identifier for this authentication method. |
token_url | str | The URL of the OAuth2 token endpoint. |
client_id | str | The client ID for the application. |
client_secret | str | The client secret for the application. |
scope | str | An optional string of space-separated scopes. |
Sources: core/src/utcp/data/auth_implementations/oauth2_auth.py:9-18
OAuth2 Token Flow
The HttpCommunicationProtocol and StreamableHttpCommunicationProtocol contain a _handle_oauth2 method that manages fetching and caching tokens. It first attempts to get a token by sending credentials in the request body, and if that fails, it retries by sending them as a Basic Auth header, which covers common variations of the OAuth2 spec.
This diagram shows the logic in the _handle_oauth2 function.
Sources: plugins/communication_protocols/http/src/utcp_http/http_communication_protocol.py:298-330, plugins/communication_protocols/http/src/utcp_http/streamable_http_communication_protocol.py:214-246
Configuration Example
{
"auth": {
"auth_type": "oauth2",
"token_url": "https://auth.example.com/token",
"client_id": "${CLIENT_ID}",
"client_secret": "${CLIENT_SECRET}",
"scope": "read:tools"
}
}
Sources: README.md
Auth in OpenAPI Conversion
A key feature of the utcp-http plugin is its ability to automatically convert an OpenAPI specification into a set of UTCP tools. The OpenApiConverter class is responsible for this, and it includes logic to map OpenAPI securitySchemes to UTCP Auth models.
Sources: plugins/communication_protocols/http/src/utcp_http/openapi_converter.py
Auth Extraction and Mapping
The converter's _extract_auth method determines the required authentication for an API endpoint by checking both operation-level and global security requirements in the OpenAPI spec. It then uses _create_auth_from_scheme to build the corresponding UTCP Auth object.
When converting, the converter generates placeholder variables (e.g., ${API_KEY_1}, ${USERNAME_2}) for credentials. This ensures that the generated UtcpManual is self-contained and ready for configuration without exposing secrets.
Sources: plugins/communication_protocols/http/src/utcp_http/openapi_converter.py:126-163, plugins/communication_protocols/http/src/utcp_http/openapi_converter.py:223-311
auth vs. auth_tools
In a CallTemplate, two fields can hold authentication configuration:
auth: This is used for authenticating the request to fetch theUtcpManualor OpenAPI specification itself.auth_tools: This is passed to theOpenApiConverterand is applied to the individual tools generated from the specification. This allows the client to use one set of credentials to fetch the spec and another set to call the actual API endpoints. The converter checks for compatibility between the auth scheme defined in the OpenAPI spec and the one provided inauth_toolsbefore applying it.
Sources: README.md, plugins/communication_protocols/http/src/utcp_http/openapi_converter.py:165-193
Summary
UTCP's authentication system is a cornerstone of its flexibility. By modeling authentication schemes as distinct, configurable Pydantic objects, it allows a single client to interface with numerous services, each with unique security requirements. The system is seamlessly integrated into CallTemplate configurations and is intelligently handled during automatic tool generation from OpenAPI specifications, making it both powerful for developers and easy to configure for end-users.