Socket (TCP/UDP) Protocol Plugin
Relevant source files
The following files were used as context for generating this wiki page:The UTCP WebSocket plugin provides real-time, bidirectional communication capabilities, designed with maximum flexibility to support any WebSocket endpoint format. Unlike implementations that enforce specific message structures, this plugin allows interaction with existing WebSocket servers without requiring any modifications on the server side. It achieves this through flexible message templating and by defaulting to sending raw arguments as JSON.
This plugin is a key component of the UTCP ecosystem, enabling integration with services that use WebSockets for features like live data streaming, chat applications, and IoT device control. It supports single-response and streaming execution, various authentication methods, and robust connection management.
Sources: plugins/communication_protocols/websocket/README.md:3-15, plugins/communication_protocols/websocket/src/utcp_websocket/websocket_communication_protocol.py:3-15
Architecture and Core Components
The plugin's functionality is centered around the WebSocketCommunicationProtocol class, which implements the CommunicationProtocol interface from the UTCP core. This class manages WebSocket connections, message formatting, authentication, and tool execution.
Class Diagram
The following diagram illustrates the structure of the WebSocketCommunicationProtocol class.
Key Attributes
| Attribute | Type | Description |
|---|---|---|
_connections | Dict[str, ClientWebSocketResponse] | Caches active WebSocket connections, keyed by a composite of the manual name and URL. |
_sessions | Dict[str, ClientSession] | Manages aiohttp.ClientSession objects for connection management. |
_oauth_tokens | Dict[str, Dict] | Caches OAuth2 access tokens, keyed by the client_id. |
Installation and Registration
To use the plugin, it must be installed alongside the core utcp package.
pip install utcp utcp-websocket
The plugin registers itself with the UTCP core via an entry point in its pyproject.toml file, making the websocket call_template_type available to the UtcpClient.
Sources: plugins/communication_protocols/websocket/README.md:20-22, plugins/communication_protocols/websocket/pyproject.toml:34-35
Configuration
The plugin is configured through a WebSocketCallTemplate object, which can be defined within the manual_call_templates section of the UtcpClient configuration.
WebSocketCallTemplate Fields
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
call_template_type | string | Yes | "websocket" | Must be "websocket". |
url | string | Yes | - | WebSocket URL. Must use wss:// or ws://localhost. |
message | string or dict | No | null | A template for the message body with UTCP_ARG_arg_name_UTCP_ARG placeholders. If omitted, arguments are sent as a JSON object. |
response_format | string | No | null | Expected response format. Can be "json", "text", or "raw". Defaults to returning the raw response. |
protocol | string | No | null | WebSocket subprotocol to use during the handshake. |
keep_alive | boolean | No | true | Enables a persistent connection with a heartbeat. |
timeout | integer | No | 30 | Timeout in seconds for connection and response. |
headers | object | No | null | Static headers to include in the connection handshake. |
header_fields | array | No | null | A list of tool argument names whose values should be mapped to headers. |
auth | object | No | null | Authentication configuration object. |
Sources: plugins/communication_protocols/websocket/README.md:65-77
Configuration Example
{
"name": "my_websocket_service",
"call_template_type": "websocket",
"url": "wss://api.example.com/ws",
"message": {
"type": "request",
"action": "UTCP_ARG_action_UTCP_ARG",
"params": {
"user_id": "UTCP_ARG_user_id_UTCP_ARG"
}
},
"auth": {
"auth_type": "api_key",
"api_key": "${MY_API_KEY}",
"var_name": "Authorization",
"location": "header"
}
}
Sources: plugins/communication_protocols/websocket/README.md:41-52, plugins/communication_protocols/websocket/README.md:161-169
Message Templating and Formatting
A core feature of the plugin is its flexible message formatting, which allows it to communicate with any WebSocket endpoint. This is handled by the _format_tool_call_message method.
The following diagram shows the logic for formatting an outgoing message.
This diagram illustrates the decision process in _format_tool_call_message to ensure maximum endpoint compatibility.
Template Types
- No Template (Default): If
messageis not specified, the tool arguments are sent directly as a JSON-encoded string. This provides maximum flexibility.
Sources: plugins/communication_protocols/websocket/README.md:81-89, plugins/communication_protocols/websocket/src/utcp_websocket/websocket_communication_protocol.py:126-129 - Dictionary Template: For structured JSON messages. Placeholders are recursively substituted.
Sources: plugins/communication_protocols/websocket/README.md:91-100, plugins/communication_protocols/websocket/src/utcp_websocket/websocket_communication_protocol.py:87-88 - String Template: For text-based protocols. Placeholders are substituted within the string.
Sources: plugins/communication_protocols/websocket/README.md:102-107, plugins/communication_protocols/websocket/src/utcp_websocket/websocket_communication_protocol.py:81-86
Connection and Session Management
The plugin manages persistent connections to WebSocket servers to reduce latency on subsequent calls. The _get_connection method is responsible for either retrieving an existing, open connection or establishing a new one.
Connection Flow
This sequence diagram shows how a connection is established or reused.
This diagram shows the logic within _get_connection for reusing or creating WebSocket connections.
Connections are closed and cleaned up when a manual is deregistered via deregister_manual or when the client is shut down via close.
Sources: plugins/communication_protocols/websocket/src/utcp_websocket/websocket_communication_protocol.py:255-263, plugins/communication_protocols/websocket/src/utcp_websocket/websocket_communication_protocol.py:332-337
Authentication
The plugin supports API Key, Basic, and OAuth2 authentication, which are configured in the auth field of the call template. Headers are prepared by the _prepare_headers method.
Sources: plugins/communication_protocols/websocket/README.md:160-185, plugins/communication_protocols/websocket/src/utcp_websocket/websocket_communication_protocol.py:148-163
Authentication Header Flow
This diagram shows how authentication information is translated into request headers.
Tool Discovery and Execution
Tool Registration
Tools are discovered by sending a standard UTCP discovery message to the WebSocket endpoint upon manual registration.
This diagram shows the register_manual flow, where the plugin discovers tools from a WebSocket endpoint.
Sources: plugins/communication_protocols/websocket/src/utcp_websocket/websocket_communication_protocol.py:218-220, plugins/communication_protocols/websocket/src/utcp_websocket/websocket_communication_protocol.py:226-241
Tool Calling (call_tool)
For single-response interactions, call_tool sends a formatted message and waits for a single reply.
This diagram illustrates the standard, single-response call_tool execution process.
Streaming Tool Calling (call_tool_streaming)
For streaming interactions, call_tool_streaming sends a message and then yields each subsequent message received from the server until a termination condition is met.
This diagram shows the call_tool_streaming flow, which yields multiple responses from the server.
Security
The plugin enforces security by default:
- WSS Required: Production URLs must use the secure
wss://protocol. - Localhost Exception:
ws://localhostandws://127.0.0.1are permitted for local development. - Authentication: Full support for API Key, Basic Auth, and OAuth2 to secure connections.
Sources: plugins/communication_protocols/websocket/README.md:187-192
Summary
The UTCP WebSocket plugin is a powerful and flexible tool for integrating with real-time services. Its key design principle is to adapt to any existing WebSocket endpoint, rather than enforcing a rigid message structure. Through configurable message templates, response formats, and authentication, it provides a seamless bridge between the UTCP client and the vast ecosystem of WebSocket-based APIs.
Sources: plugins/communication_protocols/websocket/README.md:5-15, README.md:33-34