GraphQL (GQL) Protocol Plugin
Relevant source files
The following files were used as context for generating this wiki page:
The GraphQL (GQL) Protocol Plugin for UTCP enables the client to interact with GraphQL APIs. It acts as a transport layer that can discover available queries, mutations, and subscriptions from a GraphQL endpoint's schema and execute them. This plugin is currently in a "Beta" or "Work in progress" state.
The plugin leverages the gql library to handle GraphQL-specific communication, including schema introspection and query execution. It is designed to be stateless for each operation, ensuring robust, production-ready interactions.
Sources: README.md, plugins/communication_protocols/gql/pyproject.toml, plugins/communication_protocols/gql/src/utcp_gql/gql_communication_protocol.py:18-21
Architecture and Core Components
The plugin's functionality is primarily encapsulated within the GraphQLClientTransport class, which implements the ClientTransportInterface from the UTCP core.
Sources: plugins/communication_protocols/gql/src/utcp_gql/gql_communication_protocol.py:23
GraphQLClientTransport Class
This class manages the entire lifecycle of interacting with a GraphQL endpoint, from tool registration to execution. It uses aiohttp for the underlying HTTP transport.
The following diagram illustrates the structure of the GraphQLClientTransport class.
Dependencies
The plugin requires the following main dependencies to function:
utcp>=1.0pydantic>=2.0gql>=3.0
Sources: plugins/communication_protocols/gql/pyproject.toml:10-14
Configuration
To use the GraphQL plugin, a GraphQLProvider (referred to as a call_template in client configuration) must be defined. While the GraphQLCallTemplate model is not explicitly defined in the provided files, its attributes can be inferred from the GraphQLClientTransport implementation.
| Field | Type | Required | Description |
|---|---|---|---|
call_template_type | string | Yes | Must be gql. |
url | string | Yes | The URL of the GraphQL endpoint. |
headers | object | No | A dictionary of static headers to send with each request. |
auth | object | No | Authentication configuration object. See Authentication. |
operation_type | string | No | The type of operation to perform. Defaults to query. Can be mutation or subscription. |
Sources: plugins/communication_protocols/gql/src/utcp_gql/gql_communication_protocol.py, plugins/communication_protocols/gql/old_tests/test_graphql_transport.py:72-73
Tool Discovery
Tool discovery is handled by the register_tool_provider method. It connects to the specified GraphQL endpoint, fetches the schema via introspection, and creates a Tool object for each discovered query, mutation, and subscription.
The process is as follows:
- The
urlis validated to ensure it uses HTTPS or is a localhost address. - Authentication headers are prepared.
- An
AIOHTTPTransportis created. - A
GqlClientfetches the schema from the transport. - The schema's
query_type,mutation_type, andsubscription_typefields are iterated to createToolinstances. The tool's name is set to the field name, and its description is taken from the schema.
Sources: plugins/communication_protocols/gql/src/utcp_gql/gql_communication_protocol.py:83-118
This sequence diagram shows the tool registration flow.
Tool Execution
Tools are executed via the call_tool method. It constructs and sends a GraphQL request based on the tool being called and the provided arguments.
The execution flow is:
- The
urlis validated for security. - Authentication headers are prepared.
- An
AIOHTTPTransportis established. - If a raw
querystring is provided in thecall_toolarguments, it is executed directly. - If no
querystring is provided, the plugin builds a simple GraphQL operation string. It uses theoperation_typefrom the provider (defaulting toquery), thetool_name, and thetool_argsto construct the query. - The
GqlClientexecutes the document with thetool_argsas variables. - The result from the GraphQL endpoint is returned.
Sources: plugins/communication_protocols/gql/src/utcp_gql/gql_communication_protocol.py:123-143
The following sequence diagram illustrates the call_tool process.
Example of Auto-Generated Query
If call_tool is invoked without a query parameter, the plugin constructs a query. For a call like client.call_tool("my_query", {"id": "123"}), the generated GQL string would be similar to this:
query ($id: String) {
my_query(id: $id)
}
Sources: plugins/communication_protocols/gql/src/utcp_gql/gql_communication_protocol.py:136-141
Authentication
The plugin supports ApiKeyAuth, BasicAuth, and OAuth2Auth through the _prepare_headers method.
This flowchart shows how authentication headers are added to a request.
Supported Authentication Methods
| Auth Type | Description |
|---|---|
ApiKeyAuth | Adds the API key to the request headers. The location must be header. |
BasicAuth | Encodes the username and password and adds them to the Authorization header. |
OAuth2Auth | Handles the client credentials flow to obtain a bearer token. Fetched tokens are cached in memory for the lifetime of the GraphQLClientTransport instance. |
Sources: plugins/communication_protocols/gql/src/utcp_gql/gql_communication_protocol.py:64-80
Security
The plugin enforces a security measure to prevent unencrypted communication over public networks. The _enforce_https_or_localhost method ensures that all GraphQL endpoint URLs either use https:// or point to a localhost address (http://localhost or http://127.0.0.1). An error is raised if a non-compliant URL is provided.
# plugins/communication_protocols/gql/src/utcp_gql/gql_communication_protocol.py:27-32
def _enforce_https_or_localhost(self, url: str):
if not (url.startswith("https://") or url.startswith("http://localhost") or url.startswith("http://127.0.0.1")):
raise ValueError(
f"Security error: URL must use HTTPS or start with 'http://localhost' or 'http://127.0.0.1'. Got: {url}. "
"Non-secure URLs are vulnerable to man-in-the-middle attacks."
)
Sources: plugins/communication_protocols/gql/src/utcp_gql/gql_communication_protocol.py:27-32