From 105e85c7b60ecb3c0cfe12bac0791aa0ad1875d9 Mon Sep 17 00:00:00 2001 From: bfs2021 <101915083+bfs2021@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:12:46 +0800 Subject: [PATCH] fix(mcp): correct SSE endpoint path and Apache Answer server name The SSE endpoint was configured as /answer/api/v1/mcp/see (typo), which does not match the /answer/api/v1/mcp/sse path mounted by the router. While mcp-go v0.43.2 does not validate it, CompleteSsePath() consumers (URL parsing, health checks, future path validation) break. The endpoint path was also duplicated across the SSE config, the gin mount and the advertised client URL, and the server was registered with a non-Apache name. Centralize the MCP endpoints and server name in shared constants, fix the see->sse typo, and advertise the shared path to clients. Signed-off-by: bfs2021 <101915083+bfs2021@users.noreply.github.com> --- internal/base/constant/mcp.go | 28 +++++++++ internal/router/mcp_router.go | 17 ++++-- internal/router/mcp_router_test.go | 59 +++++++++++++++++++ internal/service/siteinfo/siteinfo_service.go | 2 +- 4 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 internal/base/constant/mcp.go create mode 100644 internal/router/mcp_router_test.go diff --git a/internal/base/constant/mcp.go b/internal/base/constant/mcp.go new file mode 100644 index 000000000..a840d8932 --- /dev/null +++ b/internal/base/constant/mcp.go @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package constant + +const ( + MCPServerName = "Apache Answer MCP Server" + MCPServerVersion = "1.0.0" + + MCPSSEPath = "/answer/api/v1/mcp/sse" + MCPMessagePath = "/answer/api/v1/mcp/message" +) diff --git a/internal/router/mcp_router.go b/internal/router/mcp_router.go index 54f41971d..8ff5fe2af 100644 --- a/internal/router/mcp_router.go +++ b/internal/router/mcp_router.go @@ -20,13 +20,23 @@ package router import ( + "github.com/apache/answer/internal/base/constant" "github.com/apache/answer/internal/schema/mcp_tools" "github.com/gin-gonic/gin" "github.com/mark3labs/mcp-go/server" ) +// newMCPSSEServer builds the SSE server from the shared MCP endpoint constants +// so the configured endpoints cannot drift from the paths mounted below. +func newMCPSSEServer(s *server.MCPServer) *server.SSEServer { + return server.NewSSEServer(s, + server.WithSSEEndpoint(constant.MCPSSEPath), + server.WithMessageEndpoint(constant.MCPMessagePath), + ) +} + func (a *AnswerAPIRouter) RegisterMCPRouter(r *gin.RouterGroup) { - s := server.NewMCPServer("Answer Enterprise MCP Server", "1.0.0") + s := server.NewMCPServer(constant.MCPServerName, constant.MCPServerVersion) s.AddTool(mcp_tools.NewQuestionsTool(), a.mcpController.MCPQuestionsHandler()) s.AddTool(mcp_tools.NewAnswersTool(), a.mcpController.MCPAnswersHandler()) @@ -35,10 +45,7 @@ func (a *AnswerAPIRouter) RegisterMCPRouter(r *gin.RouterGroup) { s.AddTool(mcp_tools.NewTagDetailTool(), a.mcpController.MCPTagDetailsHandler()) s.AddTool(mcp_tools.NewUserTool(), a.mcpController.MCPUserDetailsHandler()) - sseServer := server.NewSSEServer(s, - server.WithSSEEndpoint("/answer/api/v1/mcp/see"), - server.WithMessageEndpoint("/answer/api/v1/mcp/message"), - ) + sseServer := newMCPSSEServer(s) r.GET("/mcp/sse", gin.WrapH(sseServer.SSEHandler())) r.POST("/mcp/message", gin.WrapH(sseServer.MessageHandler())) } diff --git a/internal/router/mcp_router_test.go b/internal/router/mcp_router_test.go new file mode 100644 index 000000000..c3b30b484 --- /dev/null +++ b/internal/router/mcp_router_test.go @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package router + +import ( + "testing" + + "github.com/apache/answer/internal/base/constant" + "github.com/mark3labs/mcp-go/server" +) + +// TestMCPSSEEndpointConfig guards the MCP endpoint configuration against the +// "see"/"sse" typo and against the path drifting between the SSE server config, +// the gin mount (via the shared constants) and the advertised client URL. +func TestMCPSSEEndpointConfig(t *testing.T) { + tests := []struct { + name string + got string + want string + }{ + {name: "server name", got: constant.MCPServerName, want: "Apache Answer MCP Server"}, + {name: "server version", got: constant.MCPServerVersion, want: "1.0.0"}, + {name: "SSE path", got: constant.MCPSSEPath, want: "/answer/api/v1/mcp/sse"}, + {name: "message path", got: constant.MCPMessagePath, want: "/answer/api/v1/mcp/message"}, + } + for _, tt := range tests { + if tt.got != tt.want { + t.Errorf("%s = %q, want %q", tt.name, tt.got, tt.want) + } + } + + // CompleteSsePath is consumed by URL parsing / health checks / future path + // validation in mcp-go. It must resolve to the same path the router mounts + // and that clients are told to connect to. + sseServer := newMCPSSEServer(server.NewMCPServer(constant.MCPServerName, constant.MCPServerVersion)) + if got, want := sseServer.CompleteSsePath(), "/answer/api/v1/mcp/sse"; got != want { + t.Errorf("CompleteSsePath() = %q, want %q", got, want) + } + if got, want := sseServer.CompleteMessagePath(), "/answer/api/v1/mcp/message"; got != want { + t.Errorf("CompleteMessagePath() = %q, want %q", got, want) + } +} diff --git a/internal/service/siteinfo/siteinfo_service.go b/internal/service/siteinfo/siteinfo_service.go index 8b32b722e..1a37e5e6e 100644 --- a/internal/service/siteinfo/siteinfo_service.go +++ b/internal/service/siteinfo/siteinfo_service.go @@ -479,7 +479,7 @@ func (s *SiteInfoService) GetSiteMCP(ctx context.Context) (resp *schema.SiteMCPR } resp.Type = "Server-Sent Event (SSE)" - resp.URL = fmt.Sprintf("%s/answer/api/v1/mcp/sse", siteInfo.SiteUrl) + resp.URL = fmt.Sprintf("%s%s", siteInfo.SiteUrl, constant.MCPSSEPath) resp.HTTPHeader = "Authorization={key}" return }