WebSocket Protocol Plugin
Relevant source files
The following files were used as context for generating this wiki page:- plugins/communication_protocols/websocket/README.md
- plugins/communication_protocols/websocket/src/utcp_websocket/websocket_communication_protocol.py
- plugins/communication_protocols/websocket/src/utcp_websocket/websocket_call_template.py
- plugins/communication_protocols/websocket/pyproject.toml
- plugins/communication_protocols/websocket/tests/test_websocket_call_template.py
- README.md
The UTCP WebSocket plugin enables real-time, bidirectional communication with any WebSocket endpoint, providing a flexible mechanism for integrating WebSocket-based tools into the UTCP ecosystem. A core design principle of this plugin is maximum flexibility, allowing it to connect to and interact with existing WebSocket servers without requiring any modifications to the server-side implementation. This is achieved through flexible message templating and response handling.
This plugin is part of the broader UTCP plugin-based architecture, which supports various communication protocols like HTTP, CLI, and more. It implements the CommunicationProtocol interface to manage the lifecycle of WebSocket tools, from discovery and registration to execution and deregistration. Security is enforced by requiring wss:// for all connections, with an exception for ws://localhost during development.
Sources: plugins/communication_protocols/websocket/README.md, plugins/communication_protocols/websocket/src/utcp_websocket/websocket_communication_protocol.py:1-13, 39-53, plugins/communication_protocols/websocket/src/utcp_websocket/websocket_call_template.py:100-106
Installation and Dependencies
The plugin can be installed as a standalone package.
pip install utcp-websocket
It has the following primary dependencies:
utcp>=1.0pydantic>=2.0aiohttp>=3.8
Sources: plugins/communication_protocols/websocket/README.md:25-27, plugins/communication_protocols/websocket/pyproject.toml:10-14
Architecture and Core Components
The plugin's architecture is centered around two main classes that work together to manage WebSocket communications within the UTCP framework.
Sources: plugins/communication_protocols/websocket/src/utcp_websocket/websocket_communication_protocol.py, plugins/communication_protocols/websocket/src/utcp_websocket/websocket_call_template.py
Core Classes
WebSocketCommunicationProtocol: This is the central class that implements theCommunicationProtocolinterface. It manages WebSocket connections, handles authentication, formats and sends messages, processes responses, and orchestrates the entire lifecycle of tool discovery and execution over WebSockets. It maintains a pool of active connections to different providers.WebSocketCallTemplate: A Pydantic model that extends the baseCallTemplate. It defines all the necessary configuration for a WebSocket connection, including the URL, message templates, authentication details, and response handling rules.
The diagram below shows the relationship between the plugin's components and the core UTCP interfaces.
Configuration
Configuration for a WebSocket tool provider is defined using the WebSocketCallTemplate. The call_template_type must be set to "websocket".
Sources: plugins/communication_protocols/websocket/README.md:65-79, plugins/communication_protocols/websocket/src/utcp_websocket/websocket_call_template.py:12-108
WebSocketCallTemplate Fields
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
call_template_type | string | Yes | websocket | Must be "websocket" to use this plugin. |
url | string | Yes | - | The WebSocket URL. Must be wss:// or ws://localhost. |
message | string|dict | No | null | A template for the message body with UTCP_ARG_arg_name_UTCP_ARG placeholders. If null, args are sent as JSON. |
response_format | string | No | null | Expected response format: "json", "text", or "raw". If null, the raw response is returned. |
protocol | string | No | null | WebSocket subprotocol to use. |
keep_alive | boolean | No | true | Enables a persistent connection with a heartbeat. |
timeout | integer | No | 30 | Timeout in seconds for connection and response waits. |
headers | object | No | null | Static headers to include in the connection handshake. |
header_fields | array | No | null | A list of tool argument keys whose values should be mapped to headers. |
auth | object | No | null | Authentication configuration object (API Key, Basic, or OAuth2). |
Sources: plugins/communication_protocols/websocket/README.md:65-79, plugins/communication_protocols/websocket/src/utcp_websocket/websocket_call_template.py:12-108
Message Templating
The plugin offers flexible message formatting to support a wide variety of WebSocket server implementations. This is controlled by the message field in the WebSocketCallTemplate.
Sources: plugins/communication_protocols/websocket/README.md:81-125, plugins/communication_protocols/websocket/src/utcp_websocket/websocket_communication_protocol.py:118-155
No Template (Default)
If the message field is not specified, the plugin sends the tool arguments directly as a JSON-serialized object. This provides maximum compatibility with endpoints that expect a simple JSON payload.
- Configuration:
{"call_template_type": "websocket", "url": "wss://api.example.com/ws"} - Tool Call:
await client.call_tool("ws.tool", {"foo": "bar"}) - Message Sent:
{"foo": "bar"}
Sources: plugins/communication_protocols/websocket/README.md:84-93, plugins/communication_protocols/websocket/src/utcp_websocket/websocket_communication_protocol.py:152-155
Dictionary Template
For structured messages like JSON-RPC, a dictionary can be used as a template. Placeholders in the format UTCP_ARG_arg_name_UTCP_ARG are recursively replaced with the provided tool arguments.
- Configuration:
{ "message": { "jsonrpc": "2.0", "method": "UTCP_ARG_method_UTCP_ARG", "params": "UTCP_ARG_params_UTCP_ARG", "id": 1 } } - Message Sent: If called with
{"method": "getUser", "params": {"id": 123}}, theparamsvalue will be JSON-stringified and substituted.
Sources: plugins/communication_protocols/websocket/README.md:95-107, plugins/communication_protocols/websocket/src/utcp_websocket/websocket_communication_protocol.py:91-116
String Template
For text-based protocols, a string template can be used. Placeholders are replaced in the same way.
- Configuration:
{ "message": "CMD:UTCP_ARG_command_UTCP_ARG;DEVICE:UTCP_ARG_device_id_UTCP_ARG" } - Message Sent: If called with
{"command": "SET_TEMP", "device_id": "dev123"}, the message becomesCMD:SET_TEMP;DEVICE:dev123.
Sources: plugins/communication_protocols/websocket/README.md:55-63, plugins/communication_protocols/websocket/src/utcp_websocket/websocket_communication_protocol.py:91-116
The following diagram illustrates the message formatting logic.
Protocol Lifecycle and Flows
The WebSocketCommunicationProtocol class manages the entire lifecycle of interaction with a WebSocket provider.
Connection Management
A connection is established when a manual is first registered or a tool is called. The _get_connection method handles this by either retrieving an existing, active connection from a pool or creating a new one. Connections are keyed by a combination of the template name and URL. If keep_alive is true, a heartbeat is used to maintain the connection.
Tool Discovery (register_manual)
Tool discovery is initiated by the register_manual method. It establishes a WebSocket connection and sends a discovery message. The server is expected to respond with a UtcpManual JSON object.
The sequence diagram below illustrates the tool discovery process.
Tool Execution (call_tool)
When call_tool is invoked, the plugin formats a message based on the tool's call template and arguments, sends it over the WebSocket, and waits for a single response. The handling of this response depends on the response_format setting.
The sequence diagram below shows the flow for a single tool call.
Streaming Tool Execution (call_tool_streaming)
The call_tool_streaming method follows a similar initial flow but yields each message received from the server as part of an AsyncGenerator until a stream_end message is received or the connection times out.
Deregistration (deregister_manual)
Calling deregister_manual closes the associated WebSocket connection and cleans up the session resources from the connection pool.
Authentication
The plugin supports multiple authentication schemes, which are configured in the auth field of the WebSocketCallTemplate. The _prepare_headers method is responsible for adding the correct authentication headers to the initial connection request.
Sources: plugins/communication_protocols/websocket/README.md:183-211, plugins/communication_protocols/websocket/src/utcp_websocket/websocket_communication_protocol.py:183-200
API Key
Sends a key in a specified header.
{
"auth": {
"auth_type": "api_key",
"api_key": "${API_KEY}",
"var_name": "Authorization",
"location": "header"
}
}
Sources: plugins/communication_protocols/websocket/README.md:184-192, plugins/communication_protocols/websocket/src/utcp_websocket/websocket_communication_protocol.py:187-190
Basic Authentication
Sends a Base64-encoded username and password.
{
"auth": {
"auth_type": "basic",
"username": "${USERNAME}",
"password": "${PASSWORD}"
}
}
Sources: plugins/communication_protocols/websocket/README.md:194-201, plugins/communication_protocols/websocket/src/utcp_websocket/websocket_communication_protocol.py:192-194
OAuth2 (Client Credentials)
Fetches a bearer token from a token URL and includes it in the Authorization header. Tokens are cached by client_id.
{
"auth": {
"auth_type": "oauth2",
"client_id": "${CLIENT_ID}",
"client_secret": "${CLIENT_SECRET}",
"token_url": "https://auth.example.com/token",
"scope": "read write"
}
}
Sources: plugins/communication_protocols/websocket/README.md:203-211, plugins/communication_protocols/websocket/src/utcp_websocket/websocket_communication_protocol.py:169-181, 196-198