Skip to main content

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.3.tar.gz (15.7 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.3-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: da2_xiaozhi_tunnel-0.1.3.tar.gz
  • Upload date:
  • Size: 15.7 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.3.tar.gz
Algorithm Hash digest
SHA256 e897bb7050fc54485a5a2fbe99ef813e3fb221bf9b1969f64c9546b8bb2eb990
MD5 222c68ba03aa640e44bfba9e36b687fb
BLAKE2b-256 16ddcd4b08382bc44213577cea7807d754afd1d67ceeb6afe33217a39aabe7a3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for da2_xiaozhi_tunnel-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b56652e2218168f35b9a4a8e4686249bac2d3d49b0b65720ddd1d7ad9e59a9dc
MD5 64601b0599c21a83a1643ad5e0eb6515
BLAKE2b-256 02cfa1c05ff555b49998d3ef68f3e8920b7442ad144a7803ae624f404de0b4e3

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