Files
ai-speaker/test_client.py
jmj 4ac2e959e0 ai-speaker MCP server 初始提交
- MCP Streamable HTTP (:8765 HTTP / :8766 HTTPS 自签)
- 工具:speak / play_url / play_file / download / stop / set_volume / list_voices / list_music / player_status / status
- 音量:上限 100,默认 30,夜间 22:00-08:30 静默压到 60
- CORS + Origin 白名单(局域网网段 + Android WebView 虚拟域)
- audio.py:PipeWire 蓝牙输出、JBL 绝对音量校验、播放状态/进度/设备信息
- tts.py:edge-tts 合成 + 缓存
- bt-play.sh:命令行播放工具
2026-11-30 02:20:37 +08:00

36 lines
1.3 KiB
Python

#!/usr/bin/env python3
"""本地测试 MCP server 的工具调用。用法: test_client.py <tool> [json_args]"""
import json
import sys
import anyio
from mcp import ClientSession
from mcp.client.streamable_http import streamable_http_client # mcp 2.x
URL = "http://127.0.0.1:8765/mcp"
async def main():
tool = sys.argv[1] if len(sys.argv) > 1 else "status"
args = json.loads(sys.argv[2]) if len(sys.argv) > 2 else {}
async with streamable_http_client(URL) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
if tool == "list":
tools = await session.list_tools()
for t in tools.tools:
print(f"- {t.name}: {t.description.splitlines()[0]}")
return
res = await session.call_tool(tool, arguments=args)
for c in res.content:
if hasattr(c, "text"):
try:
print(json.dumps(json.loads(c.text), ensure_ascii=False, indent=2))
except Exception:
print(c.text)
if res.is_error:
print("[ERROR]")
anyio.run(main) # 用 anyio 事件循环,避免和 MCP 客户端的 TaskGroup 清理冲突