-
Notifications
You must be signed in to change notification settings - Fork 38
Add WebSocket transport implementation for real-time communication #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
h3xxit
merged 15 commits into
universal-tool-calling-protocol:dev
from
alimoradi296:feature/websockets
Oct 7, 2025
Merged
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0ff23ba
Add WebSocket transport implementation for real-time communication
alimoradi296 dea5b6f
Address PR feedback: individual tool providers and flexible message f…
alimoradi296 01a0c18
Merge upstream/dev and align WebSocket implementation with UDP patterns
alimoradi296 a110a8b
Fix WebSocket transport per reviewer feedback
alimoradi296 7a31f82
Migrate WebSocket transport to UTCP 1.0.0 plugin architecture
alimoradi296 86389da
Add WebSocket plugin tests and update main README
alimoradi296 8be2248
Address WebSocket flexibility feedback and add plugin to main README
alimoradi296 089ef7a
Fix placeholder format: change from dollar-brace to UTCP_ARG format
alimoradi296 fdd51e8
Update example/src/websocket_example/websocket_server.py
alimoradi296 579f301
Update example/src/websocket_example/websocket_server.py
alimoradi296 71f8079
Update example/src/websocket_example/websocket_client.py
alimoradi296 f24ec4a
Update plugins/communication_protocols/websocket/README.md
alimoradi296 bb36dc2
Update example/src/websocket_example/websocket_client.py
alimoradi296 08dd85d
Update src/utcp/client/transport_interfaces/websocket_transport.py
alimoradi296 117465e
Complete remaining cubic-dev-ai fixes
alimoradi296 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| # CLAUDE.md | ||
|
|
||
| This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. | ||
|
|
||
| ## Project Overview | ||
|
|
||
| This is the Python implementation of the Universal Tool Calling Protocol (UTCP), a flexible and scalable standard for defining and interacting with tools across various communication protocols. UTCP emphasizes scalability, interoperability, and ease of use compared to other protocols like MCP. | ||
|
|
||
| ## Development Commands | ||
|
|
||
| ### Building and Installation | ||
| ```bash | ||
| # Create virtual environment and install dependencies | ||
| conda create --name utcp python=3.10 | ||
| conda activate utcp | ||
| pip install -r requirements.txt | ||
| python -m pip install --upgrade pip | ||
|
|
||
| # Build the package | ||
| python -m build | ||
|
|
||
| # Install locally | ||
| pip install dist/utcp-<version>.tar.gz | ||
| ``` | ||
|
|
||
| ### Testing | ||
| ```bash | ||
| # Run all tests | ||
| pytest | ||
|
|
||
| # Run tests with coverage | ||
| pytest --cov=src/utcp | ||
|
|
||
| # Run specific test files | ||
| pytest tests/client/test_openapi_converter.py | ||
| pytest tests/client/transport_interfaces/test_http_transport.py | ||
alimoradi296 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| ### Development Dependencies | ||
| - Install dev dependencies: `pip install -e .[dev]` | ||
| - Key dev tools: pytest, pytest-asyncio, pytest-aiohttp, pytest-cov, coverage, fastapi, uvicorn | ||
|
|
||
| ## Architecture Overview | ||
|
|
||
| ### Core Components | ||
|
|
||
| **Client Architecture (`src/utcp/client/`)**: | ||
| - `UtcpClient`: Main entry point for UTCP ecosystem interaction | ||
| - `UtcpClientConfig`: Pydantic model for client configuration | ||
| - `ClientTransportInterface`: Abstract base for transport implementations | ||
| - `ToolRepository`: Interface for storing/retrieving tools (default: `InMemToolRepository`) | ||
| - `ToolSearchStrategy`: Interface for tool search algorithms (default: `TagSearchStrategy`) | ||
|
|
||
| **Shared Models (`src/utcp/shared/`)**: | ||
| - `Tool`: Core tool definition with inputs/outputs schemas | ||
| - `Provider`: Defines communication protocols for tools | ||
| - `UtcpManual`: Contains discovery information for tool collections | ||
| - `Auth`: Authentication models (API key, Basic, OAuth2) | ||
|
|
||
| **Transport Layer (`src/utcp/client/transport_interfaces/`)**: | ||
| Each transport handles protocol-specific communication: | ||
| - `HttpClientTransport`: RESTful HTTP/HTTPS APIs | ||
| - `CliTransport`: Command Line Interface tools | ||
| - `SSEClientTransport`: Server-Sent Events | ||
| - `StreamableHttpClientTransport`: HTTP chunked transfer | ||
| - `MCPTransport`: Model Context Protocol interoperability | ||
| - `TextTransport`: Local file-based tool definitions | ||
| - `GraphQLClientTransport`: GraphQL APIs | ||
|
|
||
| ### Key Design Patterns | ||
|
|
||
| **Provider Registration**: Tools are discovered via `UtcpManual` objects from providers, then registered in the client's `ToolRepository`. | ||
|
|
||
| **Namespaced Tool Calling**: Tools are called using format `provider_name.tool_name` to avoid naming conflicts. | ||
|
|
||
| **OpenAPI Auto-conversion**: HTTP providers can point to OpenAPI v3 specs for automatic tool generation. | ||
|
|
||
| **Extensible Authentication**: Support for API keys, Basic auth, and OAuth2 with per-provider configuration. | ||
|
|
||
| ## Configuration | ||
|
|
||
| ### Provider Configuration | ||
| Tools are configured via `providers.json` files that specify: | ||
| - Provider name and type | ||
| - Connection details (URL, method, etc.) | ||
| - Authentication configuration | ||
| - Tool discovery endpoints | ||
|
|
||
| ### Client Initialization | ||
| ```python | ||
| client = await UtcpClient.create( | ||
| config={ | ||
| "providers_file_path": "./providers.json", | ||
| "load_variables_from": [{"type": "dotenv", "env_file_path": ".env"}] | ||
| } | ||
| ) | ||
| ``` | ||
|
|
||
| ## File Structure | ||
|
|
||
| - `src/utcp/client/`: Client implementation and transport interfaces | ||
| - `src/utcp/shared/`: Shared models and utilities | ||
| - `tests/`: Comprehensive test suite with transport-specific tests | ||
| - `example/`: Complete usage examples including LLM integration | ||
| - `scripts/`: Utility scripts for OpenAPI conversion and API fetching | ||
|
|
||
| ## Important Implementation Notes | ||
|
|
||
| - All async operations use `asyncio` | ||
| - Pydantic models throughout for validation and serialization | ||
| - Transport interfaces are protocol-agnostic and swappable | ||
| - Tool search supports tag-based ranking and keyword matching | ||
| - Variable substitution in configuration supports environment variables and .env files | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| # WebSocket Transport Example | ||
|
|
||
| This example demonstrates how to use the UTCP WebSocket transport for real-time communication. | ||
|
|
||
| ## Overview | ||
|
|
||
| The WebSocket transport provides: | ||
| - Real-time bidirectional communication | ||
| - Tool discovery via WebSocket handshake | ||
| - Streaming tool execution | ||
| - Authentication support (API Key, Basic Auth, OAuth2) | ||
| - Automatic reconnection and keep-alive | ||
|
|
||
| ## Files | ||
|
|
||
| - `websocket_server.py` - Mock WebSocket server implementing UTCP protocol | ||
| - `websocket_client.py` - Client example using WebSocket transport | ||
| - `providers.json` - WebSocket provider configuration | ||
|
|
||
| ## Protocol | ||
|
|
||
| The UTCP WebSocket protocol uses JSON messages: | ||
|
|
||
| ### Tool Discovery | ||
| ```json | ||
| // Client sends: | ||
| {"type": "discover", "request_id": "unique_id"} | ||
|
|
||
| // Server responds: | ||
| { | ||
| "type": "discovery_response", | ||
| "request_id": "unique_id", | ||
| "tools": [...] | ||
| } | ||
| ``` | ||
|
|
||
| ### Tool Execution | ||
| ```json | ||
| // Client sends: | ||
| { | ||
| "type": "call_tool", | ||
| "request_id": "unique_id", | ||
| "tool_name": "tool_name", | ||
| "arguments": {...} | ||
| } | ||
|
|
||
| // Server responds: | ||
| { | ||
| "type": "tool_response", | ||
| "request_id": "unique_id", | ||
| "result": {...} | ||
| } | ||
| ``` | ||
|
|
||
| ## Running the Example | ||
|
|
||
| 1. Start the mock WebSocket server: | ||
| ```bash | ||
| python websocket_server.py | ||
| ``` | ||
|
|
||
| 2. In another terminal, run the client: | ||
| ```bash | ||
| python websocket_client.py | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| The `providers.json` shows how to configure WebSocket providers with authentication: | ||
|
|
||
| ```json | ||
| [ | ||
| { | ||
| "name": "websocket_tools", | ||
| "provider_type": "websocket", | ||
| "url": "ws://localhost:8765/ws", | ||
| "auth": { | ||
| "auth_type": "api_key", | ||
| "api_key": "your-api-key", | ||
| "var_name": "X-API-Key", | ||
| "location": "header" | ||
| }, | ||
| "keep_alive": true, | ||
| "protocol": "utcp-v1" | ||
| } | ||
| ] | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| [ | ||
| { | ||
| "name": "websocket_tools", | ||
| "provider_type": "websocket", | ||
| "url": "ws://localhost:8765/ws", | ||
| "keep_alive": true, | ||
| "headers": { | ||
| "User-Agent": "UTCP-WebSocket-Client/1.0" | ||
| } | ||
| } | ||
| ] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.