Skip to main content

AI xiaozhi 小智 WebSocket MCP (Model Context Protocol) 橋接模組,支援 WebSocket 與 HTTP/SSE 傳輸

Project description

da2-xiaozhi-tunnel

PyPI version License: MIT

MCP (Model Context Protocol) 橋接模組,用於連接小智雲端與其他 MCP 伺服器。

作者: Danny | 工作室: DA2 Studio | 網站: https://da2.35g.tw


功能特色

  • WebSocketTunnelMCP: WebSocket 隧道伺服器,對接小智雲端
  • MCPHttpClient: HTTP/SSE 客戶端,連接遠端 MCP 伺服器
  • 支援多伺服器工具聚合與智慧路由
  • 自動重連機制

安裝

pip install da2-xiaozhi-tunnel

快速開始

基礎範例 (僅使用 WebSocketTunnelMCP)

連接小智雲端,註冊本地工具供 AI 呼叫:

import asyncio
from da2_xiaozhi_tunnel import WebSocketTunnelMCP

TOKEN_URL = "wss://api.xiaozhi.me/mcp/?token=YOUR_TOKEN_HERE"

mcp = WebSocketTunnelMCP(server_name="MyServer")

# 連線事件
@mcp.on_connected()
async def on_connected():
    print("✅ 已連線")

@mcp.on_disconnected()
async def on_disconnected():
    print("❌ 連線中斷")

# 註冊工具
@mcp.tool(
    name="get_greeting",
    description="取得問候語",
    input_schema={
        "type": "object",
        "properties": {
            "name": {"type": "string", "description": "名稱"}
        },
        "required": ["name"]
    }
)
async def get_greeting(name: str):
    return f"哈囉 {name}!"

# 主程式
async def main():
    while True:
        if await mcp.connect(TOKEN_URL):
            await mcp.listen_loop()
        await asyncio.sleep(5)  # 重連間隔

asyncio.run(main())

橋接範例 (WebSocketTunnelMCP + MCPHttpClient)

連接小智雲端,同時橋接遠端 MCP Server,聚合所有工具:

import asyncio
from da2_xiaozhi_tunnel import WebSocketTunnelMCP, MCPHttpClient

TOKEN_URL = "wss://api.xiaozhi.me/mcp/?token=YOUR_TOKEN_HERE"
EXTERNAL_MCP_URL = "http://your-mcp-server/mcp"

mcp = WebSocketTunnelMCP(server_name="BridgeServer")

# 建立外部 MCP Client
external_client = MCPHttpClient(
    base_url=EXTERNAL_MCP_URL,
    client_name="hub",  # 工具將有 hub_ 前綴
    use_sse=False       # 純 HTTP 模式
)

# 連線事件
@mcp.on_connected()
async def on_connected():
    print(f"✅ 已連線 | 本地工具: {len(mcp.tools)} | 外部工具: {len(mcp.external_tools)}")

# 本地工具
@mcp.tool("local_echo", "本地回聲", {"type": "object", "properties": {"msg": {"type": "string"}}})
async def local_echo(msg: str):
    return f"[回聲] {msg}"

# 主程式
async def main():
    # 掛載外部 Client
    mcp.mount_client(external_client)
    
    while True:
        if await mcp.connect(TOKEN_URL):
            await mcp.listen_loop()
        await asyncio.sleep(5)

asyncio.run(main())

橋接後,AI 可以同時呼叫:

  • 本地工具:local_echo
  • 遠端工具:hub_time-get_current_time, hub_time-convert_time (自動加上前綴)

API 參考

WebSocketTunnelMCP

方法 說明
connect(uri) 連線到 WebSocket URI
disconnect() 斷開連線
listen_loop() 進入訊息監聽迴圈
mount_client(client) 掛載外部 MCPHttpClient
裝飾器 說明
@mcp.tool(name, description, input_schema) 註冊工具
@mcp.on_connected() 連線成功事件
@mcp.on_disconnected() 連線中斷事件

MCPHttpClient

參數 說明
base_url MCP Server URL
client_name 客戶端名稱 (用於工具前綴)
use_sse True = SSE 模式, False = HTTP 模式
方法 說明
start() 啟動背景連線
stop() 停止連線
call_tool(name, args) 呼叫遠端工具

範例程式

完整範例請參考 examples/ 目錄:

  • example_basic.py - 基礎範例
  • example_bridge.py - 橋接範例

License

MIT License - DA2 Studio

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

da2_xiaozhi_tunnel-0.1.4.tar.gz (15.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

da2_xiaozhi_tunnel-0.1.4-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file da2_xiaozhi_tunnel-0.1.4.tar.gz.

File metadata

  • Download URL: da2_xiaozhi_tunnel-0.1.4.tar.gz
  • Upload date:
  • Size: 15.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for da2_xiaozhi_tunnel-0.1.4.tar.gz
Algorithm Hash digest
SHA256 128019fed77f36c9d5d3cab5d652392813248404c8f1588be7e36d82a9cb2af6
MD5 f3e80c0c077f8a0a25adc39e62d72994
BLAKE2b-256 de46e0b3a9e1486dd21982c8c07f08489342d026c4009d148637b367aeb8e87c

See more details on using hashes here.

File details

Details for the file da2_xiaozhi_tunnel-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for da2_xiaozhi_tunnel-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 39937611657ab036983ffde8a0043f9ed59ee358610e4c7abc9f459ad3705e33
MD5 a3d7d2e8d66fec9801b40a940f817698
BLAKE2b-256 3dfaeee249df075e9442506da74068232e5f1bf064f4a823fb0d06bc0707aa75

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page