Bidirectional WebSocket channel SDK for MCP servers and Agent clients
Project description
env-channel User Guide (Concise Version)
1. Installation
1.1 Install from Local Wheel (Recommended)
cd env-channel
uv build # Build wheel package
ls dist/ # View generated .whl file
# Install in other projects (example)
cd /your/other/project
uv pip install /absolute/path/env-channel/dist/env_channel-0.1.0-py3-none-any.whl
1.2 Local Development Mode Installation (Linked)
cd /your/other/project
uv pip install -e /absolute/path/env-channel
2. Core Concepts
- EnvChannelServer: WebSocket server that forwards messages.
- EnvChannelPublisher: Publisher that pushes messages to a
topic. - @env_channel_sub: Subscriber decorator that marks a function as a subscription handler for a
topic.
3. Start WebSocket Server (Environment Side)
Simplest example (can be placed in a standalone script or your service startup logic):
import asyncio
from env_channel.server import EnvChannelServer
async def main():
server = EnvChannelServer(host="0.0.0.0", port=8765)
await server.start()
print("EnvChannelServer started at ws://0.0.0.0:8765")
try:
while True:
await asyncio.sleep(1)
except KeyboardInterrupt:
await server.stop()
if __name__ == "__main__":
asyncio.run(main())
In actual projects, you can refer to
env-channel-demoto integrateEnvChannelServerinto FastAPI's lifespan for unified management.
4. Publish Messages (Publisher)
import asyncio
from env_channel.client import EnvChannelPublisher
async def main():
publisher = EnvChannelPublisher(
server_url="ws://localhost:8765",
auto_connect=True,
auto_reconnect=True,
)
await publisher.publish(
topic="demo-channel",
message={"text": "hello env-channel"},
)
if __name__ == "__main__":
asyncio.run(main())
topic: Business channel name, such as"order-updates","task-progress".message:dict, contains business data.
5. Subscribe to Messages (Subscriber)
5.1 Direct Use of EnvChannelSubscriber
import asyncio
import logging
from env_channel.client import EnvChannelSubscriber
from env_channel.common.message import EnvChannelMessage
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
async def main():
sub = EnvChannelSubscriber(
server_url="ws://localhost:8765",
auto_connect=True,
auto_reconnect=True,
reconnect_interval=10.0,
)
async def handle(msg: EnvChannelMessage):
logger.info("received: %s", msg.message)
await sub.subscribe(topics=["demo-channel"], handler=handle)
logger.info("listening on demo-channel...")
try:
while True:
await asyncio.sleep(1)
except KeyboardInterrupt:
await sub.unsubscribe(["demo-channel"])
await sub.disconnect()
if __name__ == "__main__":
asyncio.run(main())
5.2 Using Decorator @env_channel_sub (Zero Boilerplate)
import asyncio
import logging
from env_channel.client import env_channel_sub
from env_channel.common.message import EnvChannelMessage
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@env_channel_sub(
server_url="ws://localhost:8765",
topics=["demo-channel"],
auto_connect=True,
auto_reconnect=True,
reconnect_interval=10.0,
# auto_start=True (default): Automatically start subscription thread after module import
)
async def handle_demo(msg: EnvChannelMessage):
logger.info("decorator received: %s", msg.message)
async def main():
logger.info("Listening... (Ctrl+C to stop)")
try:
while True:
await asyncio.sleep(1)
except KeyboardInterrupt:
logger.info("Stopping...")
if __name__ == "__main__":
asyncio.run(main())
6. env-channel-demo Quick Start
The project includes env-channel-demo with integrated FastAPI + EnvChannelServer + publish + decorator subscription.
- Start the demo:
cd env-channel/env-channel-demo/src/env_channel_demo
uv run main.py
- Publish messages via HTTP:
curl "http://127.0.0.1:8000/publish?text=hello-from-demo"
- You can see subscription logs in the demo console:
Decorator subscriber 111 received: {'text': 'hello-from-demo'}
7. Summary
- Environment Side: Start
EnvChannelServer, useEnvChannelPublisher.publish(topic, message)in business code to push messages. - Agent / Client Side: Use
EnvChannelSubscriberor@env_channel_subto subscribe to the correspondingtopic, and handleEnvChannelMessagein the handler.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file env_channel-0.1.0.tar.gz.
File metadata
- Download URL: env_channel-0.1.0.tar.gz
- Upload date:
- Size: 13.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7984369b8f409fb5b24bdfc7da5e37a82b548d92db9f40e744b6cd23e1f92f41
|
|
| MD5 |
bc91ed3a0055e1538e43f3456e176b45
|
|
| BLAKE2b-256 |
85bb5d8e4de7d4834357ab395f68bde86d6b6d0b9d194cf664a69ba4cf1e8425
|
File details
Details for the file env_channel-0.1.0-py3-none-any.whl.
File metadata
- Download URL: env_channel-0.1.0-py3-none-any.whl
- Upload date:
- Size: 18.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ee9ba2c03ff6738c924c1e9590b974c42ce6aedfcec5a4ae8ef11d593d3c935
|
|
| MD5 |
b6ac82b4531b063262dd79c9efaa3e28
|
|
| BLAKE2b-256 |
6b2ecf94600e270d40c0ad49ce3edaec84358ba9c1b0059773a2ae547c7fb356
|