Files
assistant/plans/mcp-container-restart-diagnosis.md
wl 3ad6eee0d9 feat: 初始化 B2B AI Shopping Assistant 项目
- 配置 Docker Compose 多服务编排
- 实现 Chatwoot + Agent 集成
- 配置 Strapi MCP 知识库
- 支持 7 种语言的 FAQ 系统
- 实现 LangGraph AI 工作流

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-14 19:25:22 +08:00

3.8 KiB
Raw Permalink Blame History

MCP 容器重启问题诊断报告

问题概述

MCP 服务容器strapi_mcp, order_mcp, aftersale_mcp, product_mcp一直在重启。

容器架构

graph TB
    subgraph "MCP Servers"
        A[strapi_mcp:8001]
        B[order_mcp:8002]
        C[aftersale_mcp:8003]
        D[product_mcp:8004]
    end

    subgraph "外部依赖"
        E[Strapi CMS<br/>49.234.16.160:1337]
        F[Hyperf API<br/>apicn.qa1.gaia888.com]
    end

    subgraph "内部依赖"
        G[agent:8005]
    end

    A --> E
    B --> F
    C --> F
    D --> F
    G --> A
    G --> B
    G --> C
    G --> D

诊断结果

1. 环境变量配置问题(主要原因)

.env 文件中发现以下问题:

环境变量 当前值 问题
STRAPI_API_TOKEN "" (空) strapi_mcp 无法认证
HYPERF_API_TOKEN "" (空) order_mcp, aftersale_mcp, product_mcp 无法认证
REDIS_PASSWORD "" (空) Redis 健康检查可能失败

2. 客户端初始化问题

mcp_servers/shared/strapi_client.pymcp_servers/shared/hyperf_client.py 中:

class StrapiSettings(BaseSettings):
    strapi_api_url: str
    strapi_api_token: str  # 必需字段,但值为空字符串

settings = StrapiSettings()  # 模块加载时执行

api_token 为空时:

  • 不会立即抛出异常(空字符串是有效的 str 值)
  • 但所有 API 请求都会因认证失败而报错
  • 可能导致服务启动失败或运行时崩溃

3. 健康检查配置

docker-compose.yml 中,所有 MCP 服务都配置了健康检查:

healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost:8001/health"]
  interval: 30s
  timeout: 10s
  start-period: 5s
  retries: 3

如果服务启动失败或 /health 端点不可用健康检查会失败Docker 可能会重启容器。

4. 服务启动流程

MCP 服务的启动流程:

sequenceDiagram
    participant DC as Docker
    participant SVR as Server.py
    participant ENV as Settings
    participant CLI as HTTP Client
    participant API as External API

    DC->>SVR: python server.py
    SVR->>ENV: 加载环境变量
    ENV-->>SVR: api_token = ""
    SVR->>CLI: 初始化客户端
    CLI->>API: 首次请求(如果需要)
    API-->>CLI: 401 Unauthorized
    CLI-->>SVR: 抛出异常
    SVR-->>DC: 启动失败
    DC->>DC: 重启容器

可能的错误信息

基于代码分析,容器日志中可能出现以下错误:

  1. 认证失败:

    httpx.HTTPStatusError: 401 Unauthorized
    
  2. 连接错误:

    httpx.ConnectError: Connection refused
    
  3. 超时错误:

    httpx.TimeoutException
    
  4. 健康检查失败:

    curl: (7) Failed to connect to localhost port 8001
    

影响范围

服务 影响原因
strapi_mcp STRAPI_API_TOKEN 为空
order_mcp HYPERF_API_TOKEN 为空
aftersale_mcp HYPERF_API_TOKEN 为空
product_mcp HYPERF_API_TOKEN 为空
agent 依赖所有 MCP 服务,可能间接受影响

修复建议

方案 1: 配置正确的 API Token推荐

  1. 获取 Strapi API Token
  2. 获取 Hyperf API Token
  3. 更新 .env 文件

方案 2: 使服务在 Token 为空时也能启动(临时方案)

修改客户端代码,延迟初始化或添加容错逻辑。

方案 3: 禁用需要认证的服务

如果某些服务暂时不需要,可以将其从 docker-compose 中移除。

下一步行动

请确认:

  1. 是否有可用的 Strapi 和 Hyperf API Token
  2. 是否需要修改代码以支持空 Token 的启动模式?
  3. 是否需要查看具体的容器日志以确认错误?