MCP客户端无法通过streamable-http传输连接FastMCP服务器
客户端代码
config = { "mcpServers":{ "server name": { "url": "http://host.docker.internal:3002/mcp", } } } self.mcp_client = MCPClient.from_dict(config) adapter = LangChainAdapter() self.mcp_tools = await adapter.create_tools(self.mcp_client)
服务端代码
mcp = FastMCP("CyberChef API MCP Server",port=3002,host="0.0.0.0") transport = "streamable-http" mcp.run(transport=transport)
相关日志
INFO: mcp.server.streamable_http_manager:Created new transport with session ID: 510d091ef8fb4fb1a84e93696bed5164
cyberchef-api-mcp-server | INFO: 172.18.0.1:51336 - "GET /mcp HTTP/1.1" 400 Bad Request
packet_analyzer | DEBUG:httpcore.http11:receive_response_headers.complete return_value=(b'HTTP/1.1', 400, b'Bad Request', [(b'date', b'Thu, 16 Oct 2025 10:05:20 GMT'), (b'server', b'uvicorn'), (b'content-type', b'application/json'), (b'mcp-session-id', b'510d091ef8fb4fb1a84e93696bed5164'), (b'content-length', b'105')])
packet_analyzer | INFO:httpx:HTTP Request: GET http://host.docker.internal:3002/mcp "HTTP/1.1 400 Bad Request"
解决方向
- 补充客户端传输协议配置
当前客户端配置未指定传输类型,streamable-http需要明确声明。修改config:
config = { "mcpServers":{ "server name": { "url": "http://host.docker.internal:3002/mcp", "transport": "streamable-http" # 新增传输协议指定 } } }
检查请求方法匹配性
streamable-http传输通常要求POST请求,但日志显示客户端发起的是GET。确认MCPClient或LangChainAdapter是否自动适配传输对应的请求方法,必要时手动指定请求类型为POST。获取详细错误信息
服务端返回了105字节的JSON错误内容,可在客户端捕获完整响应体,或在服务端开启debug级日志,查看400错误的具体原因(如缺少必填请求头、参数格式错误等)。验证Docker网络请求
在客户端容器内执行curl -v http://host.docker.internal:3002/mcp,模拟请求并查看完整响应细节,排查是协议不兼容还是配置遗漏问题。
内容的提问来源于stack exchange,提问作者sk2000

