#!/usr/bin/env bash # 一次性调用 ai-speaker MCP 工具并打印 JSON 结果 set -uo pipefail BASE="http://192.168.1.111:8765/mcp" ORIGIN="http://appassets.androidplatform.net" SID=$(curl -sS -i -X POST "$BASE" \ -H "Origin: $ORIGIN" \ -H 'Content-Type: application/json' \ -H 'Accept: application/json, text/event-stream' \ -d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"probe","version":"1"}}}' \ 2>/dev/null | tr -d '\r' | awk -F': ' 'tolower($1)=="mcp-session-id"{print $2; exit}') if [ -z "$SID" ]; then echo "ERROR: 未能获取 session id" >&2 exit 1 fi tool="$1" args="${2:-{\}}" curl -sS -X POST "$BASE" \ -H "Origin: $ORIGIN" \ -H 'Content-Type: application/json' \ -H 'Accept: application/json, text/event-stream' \ -H "mcp-session-id: $SID" \ -d "{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"tools/call\",\"params\":{\"name\":\"$tool\",\"arguments\":$args}}" \ 2>&1 | sed 's/^data: //' | grep '^{' | python3 -c " import json, sys raw = sys.stdin.read().strip() if not raw: print(' (空响应)') raise SystemExit(1) d = json.loads(raw) r = d.get('result') if r is None: print(' RPC ERROR:', json.dumps(d.get('error'), ensure_ascii=False)) raise SystemExit(1) txt = '' if isinstance(r.get('content'), list): for c in r['content']: if isinstance(c, dict) and isinstance(c.get('text'), str): txt += c['text'] if r.get('isError'): print(' TOOL ERROR:', txt[:300]); raise SystemExit(1) try: print(json.dumps(json.loads(txt), ensure_ascii=False, indent=2)) except Exception: print(txt[:600]) "