feat: add comprehensive MCP verification in init.sh

Three-step verification after setup:
1. Server connection test (initialize → HTTP 200)
2. Tool registration test (tools/list → tool count)
3. Claude Code MCP test (claude mcp list → Connected)

Uses Mcp-Session-Id from initialize response for tools/list call.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-15 13:25:05 +10:30
parent 185c4d0681
commit 58516a57a9

43
init.sh
View File

@@ -214,18 +214,49 @@ echo "✅ 技能安装完成 → $SKILLS_DIR"
echo ""
echo "🔍 验证 MCP 连接..."
if [ "$MODE" = "remote" ]; then
VERIFY_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -X POST "$SSE_URL" \
# Step 1: 服务端连接测试 (initialize)
INIT_RESPONSE=$(curl -s -D /tmp/mcp_headers -X POST "$SSE_URL" \
-H "Content-Type: application/json" \
-H "X-API-Key: $TOKEN" \
-d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-03-26","capabilities":{},"clientInfo":{"name":"init-verify","version":"1.0"}}}' \
--connect-timeout 10 --max-time 15 2>/dev/null)
if [ "$VERIFY_RESPONSE" = "200" ]; then
echo " ✅ MCP 服务端连接正常 (HTTP $VERIFY_RESPONSE)"
INIT_HTTP=$(grep -i "^HTTP" /tmp/mcp_headers 2>/dev/null | tail -1 | awk '{print $2}')
SESSION_ID=$(grep -i "^mcp-session-id:" /tmp/mcp_headers 2>/dev/null | tail -1 | tr -d '\r' | awk '{print $2}')
if [ "$INIT_HTTP" = "200" ]; then
echo " ✅ 服务端连接正常 (HTTP 200)"
# Step 2: 工具列表测试 (tools/list)
if [ -n "$SESSION_ID" ]; then
TOOLS_RESPONSE=$(curl -s -X POST "$SSE_URL" \
-H "Content-Type: application/json" \
-H "X-API-Key: $TOKEN" \
-H "Mcp-Session-Id: $SESSION_ID" \
-d '{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}' \
--connect-timeout 10 --max-time 15 2>/dev/null)
TOOL_COUNT=$(echo "$TOOLS_RESPONSE" | grep -o '"name"' | wc -l | tr -d ' ')
if [ "$TOOL_COUNT" -gt 0 ] 2>/dev/null; then
echo " ✅ 工具注册正常 ($TOOL_COUNT 个工具可用)"
else
echo " ⚠️ MCP 服务端连接异常 (HTTP $VERIFY_RESPONSE)"
echo " 请检查: 1) API Key 是否正确 2) 服务端是否运行 3) 网络是否通畅"
echo " 测试命令: curl -s -X POST '$SSE_URL' -H 'X-API-Key: \$TOKEN' -H 'Content-Type: application/json' -d '{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{}}'"
echo " ⚠️ 工具列表为空,请检查服务端日志"
fi
fi
# Step 3: claude mcp list 测试 (如果有 Claude CLI)
if $HAS_CLAUDE; then
MCP_STATUS=$("$CLAUDE_BIN" mcp list 2>&1 | grep "ai-proj")
if echo "$MCP_STATUS" | grep -q "Connected"; then
echo " ✅ Claude Code MCP 连接正常"
else
echo " ⚠️ Claude Code MCP 未连接: $MCP_STATUS"
fi
fi
else
echo " ❌ MCP 服务端连接失败 (HTTP ${INIT_HTTP:-无响应})"
echo " 请检查: 1) API Key 是否正确 2) 服务端是否运行 3) 网络是否通畅"
fi
rm -f /tmp/mcp_headers
fi
echo ""