fix(routes): return JSON 404 for OAuth discovery probes#636
fix(routes): return JSON 404 for OAuth discovery probes#636androidZzT wants to merge 1 commit intoxpzouying:mainfrom
Conversation
Claude Code's MCP HTTP client probes a number of OAuth .well-known endpoints (per the MCP authorization spec) on every reconnect. When those paths return Gin's default plain-text "404 page not found", the client tries to parse the body as a JSON OAuth error response, fails with a JSON parse error, and ends up stuck in a "needs authentication" state with no OAuth metadata to act on. The MCP endpoint itself still works, but the auth state machine on the client side never recovers without a process restart, causing the MCP server to repeatedly appear as "disconnected" or "needs auth". Server log captured during one such failure: GET /.well-known/oauth-protected-resource/mcp -> 404 GET /.well-known/oauth-protected-resource -> 404 GET /.well-known/oauth-authorization-server -> 404 GET /.well-known/openid-configuration -> 404 GET /.well-known/oauth-authorization-server/mcp -> 404 GET /.well-known/openid-configuration/mcp -> 404 GET /mcp/.well-known/openid-configuration -> 400 POST /register -> 404 POST /mcp -> 200 (works!) The fix is minimal: register handlers for the well-known OAuth probe paths and POST /register that return a JSON 404 body with a clear error_description. The client now sees a parseable JSON response, concludes no OAuth metadata is available, and falls back to anonymous mode cleanly. This is a one-shot fix for a recurring "MCP keeps disconnecting" problem on the Claude Code side; no behavior change for any existing endpoint.
|
🔍 Code Review - PR #636📌 概要本 PR 修复了 Claude Code MCP 客户端在每次重连时探测 OAuth 修复方案是为这些 OAuth 探测路径注册处理器,返回 JSON 格式的 404 响应体,让客户端能正确解析并回退到匿名模式。 改动范围: ✅ 优点
|
| 维度 | 评价 |
|---|---|
| 问题真实性 | ✅ 真实且影响用户体验 |
| 修复方向 | ✅ 正确 |
| 代码质量 | ✅ 良好 |
| 路由冲突风险 | |
| 测试覆盖 | |
| 可维护性 | 🟡 建议考虑 NoRoute 方案 |
总体评价:PR 解决了一个实际且影响用户体验的问题,修复方向正确,代码质量良好。主要建议是确认 /mcp/.well-known/*path 与现有 router.Any("/mcp/*path") 的路由冲突问题,并强烈建议考虑使用 NoRoute 作为更简洁、更安全的替代方案。如果路由冲突问题确认不存在,当前实现也是可以接受的。
🤖 本评论由 Kimi Code Agent 自动生成
|
现在我已经收集了所有需要的信息,让我撰写审查意见并发布。 审查摘要PR 背景:Claude Code 的 MCP 客户端在重连时探测 OAuth 主要发现:
总体评价:问题真实、修复方向正确、代码质量良好,建议优先确认路由冲突问题并考虑 |
Problem
Claude Code's MCP HTTP client probes a number of OAuth
.well-knownendpoints (per the MCP authorization spec) on every reconnect. When those paths return Gin's default plain-text404 page not found, the client tries to parse the body as a JSON OAuth error response, fails with a JSON parse error, and ends up stuck in a "needs authentication" state with no OAuth metadata to act on.The MCP endpoint itself still works, but the auth state machine on the client side never recovers without a process restart. End-user symptoms: the MCP server repeatedly appears as "disconnected" or "needs auth" even though the actual
/mcpPOST is fine.Server log captured during one failure
And the client-side error:
Fix
Register lightweight handlers for the well-known OAuth probe paths and `POST /register` that return a JSON 404 body with a clear `error_description`:
```json
{
"error": "not_found",
"error_description": "This MCP server does not require authentication"
}
```
The client now sees a parseable JSON response, concludes no OAuth metadata is available, and falls back to anonymous mode cleanly. No behavior change for any existing endpoint.
Verification
Files