fix(router): trust all proxies to fix MCP 'invalid Host header' error
Build and Deploy / build-and-push (push) Successful in 53s
Build and Deploy / build-and-push (push) Successful in 53s
- Set TrustedProxies to nil to allow reverse proxy requests - Add explicit routes for /mcp without trailing slash
This commit is contained in:
@@ -14,7 +14,16 @@ func initMCPRoutes(root *gin.RouterGroup, c *Controllers) {
|
|||||||
{
|
{
|
||||||
// 所有 MCP 请求都通过同一个 handler 处理
|
// 所有 MCP 请求都通过同一个 handler 处理
|
||||||
// StreamableHTTP 协议支持 POST 和 GET
|
// StreamableHTTP 协议支持 POST 和 GET
|
||||||
|
// 使用 /*action 匹配 /mcp 和 /mcp/ 以及 /mcp/xxx
|
||||||
mcp.POST("/*action", c.MCP.HandleMCP)
|
mcp.POST("/*action", c.MCP.HandleMCP)
|
||||||
mcp.GET("/*action", c.MCP.HandleMCP)
|
mcp.GET("/*action", c.MCP.HandleMCP)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 同时处理 /mcp 根路径(无尾部斜杠)
|
||||||
|
mcpRoot := root.Group("/mcp")
|
||||||
|
mcpRoot.Use(middleware.OpenapiRequired())
|
||||||
|
{
|
||||||
|
mcpRoot.POST("", c.MCP.HandleMCP)
|
||||||
|
mcpRoot.GET("", c.MCP.HandleMCP)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,6 +43,11 @@ func Setup(c *Controllers) *gin.Engine {
|
|||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
}
|
}
|
||||||
router := gin.New()
|
router := gin.New()
|
||||||
|
|
||||||
|
// 信任所有反向代理(Docker/Nginx 场景)
|
||||||
|
// 设置为 nil 表示信任所有,避免 "invalid Host header" 错误
|
||||||
|
router.SetTrustedProxies(nil)
|
||||||
|
|
||||||
router.Use(middleware.GinLogger(), middleware.GinRecovery())
|
router.Use(middleware.GinLogger(), middleware.GinRecovery())
|
||||||
router.Use(middleware.TravelProxyMiddleware())
|
router.Use(middleware.TravelProxyMiddleware())
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user