clear
echo -e "${BLUE}[2/2]${NC} Verificando handshake MCP por stdio..."

RESULT=0
TMP_OUT="$(mktemp)"

(
  cat <<'EOF'
{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"mcp-test","version":"1.0.0"}}}
EOF
  sleep 1
) | npx -y chrome-devtools-mcp@latest >"$TMP_OUT" 2>/dev/null || true

if grep -q '"capabilities"' "$TMP_OUT"; then
  echo -e "      ${GREEN}✓${NC} MCP responde correctamente por stdio"
  RESULT=0
else
  echo -e "      ${RED}✗${NC} No se recibió respuesta MCP válida"
  echo "      Salida parcial:"
  sed 's/^/      /' "$TMP_OUT" | head -n 10
  RESULT=1
fi

rm -f "$TMP_OUT"

echo ""
echo "==========================================="

if [[ "$RESULT" -eq 0 ]]; then
  echo -e "${GREEN}✓ DevTools MCP está funcionando correctamente${NC}"
  exit 0
else
  echo -e "${RED}✗ DevTools MCP tiene problemas${NC}"
  exit 1
fi
