This is an automated email from the ASF dual-hosted git repository.

LinkinStars pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/answer.git


The following commit(s) were added to refs/heads/dev by this push:
     new d0263acd fix(mcp): correct SSE endpoint path and server name in MCP 
router (#1680)
d0263acd is described below

commit d0263acde51b3781734ba7f4927f72f89c50f90d
Author: zgy <[email protected]>
AuthorDate: Thu Nov 19 15:53:57 2026 +0800

    fix(mcp): correct SSE endpoint path and server name in MCP router (#1680)
    
    ## Problem
    
    Fixes #1673
    
    The MCP SSE endpoint was configured as `/answer/api/v1/mcp/see` (typo:
    `see` instead of `sse`) while the router actually mounts `GET
    /answer/api/v1/mcp/sse`. In addition, the endpoint path was duplicated
    as bare string literals across the SSE server config, the gin mount, and
    the client-facing URL returned by `GetSiteMCP`, and the server was
    registered under the non-Apache name `"Answer Enterprise MCP Server"`.
    
    ## Root cause
    
    `internal/router/mcp_router.go` passed
    `server.WithSSEEndpoint("/answer/api/v1/mcp/see")` to mcp-go. mcp-go
    v0.43.2 does not validate this value so the server still starts, but
    `CompleteSsePath()` and anything deriving URLs from it (URL parsing,
    health checks, future path validation) resolve to the wrong `/see` path.
    The same path also existed as independent literals in three places, so
    the configured path, mounted route, and advertised client URL could
    drift apart. The registered server name referenced "Answer Enterprise"
    rather than Apache Answer.
    
    ## Fix
    
    - Added shared constants in `internal/base/constant/mcp.go`:
    `MCPServerName` = `"Apache Answer MCP Server"`, `MCPServerVersion`,
    `MCPSSEPath` = `/answer/api/v1/mcp/sse`, and `MCPMessagePath` =
    `/answer/api/v1/mcp/message`.
    - Corrected the `see` -> `sse` typo by building the SSE server from
    these constants via a small `newMCPSSEServer` helper, so the configured
    endpoints cannot drift from the mounted routes.
    - Updated `GetSiteMCP` in `siteinfo_service.go` to advertise the same
    shared `MCPSSEPath` constant instead of a duplicated literal.
    - Added `internal/router/mcp_router_test.go` asserting the constants and
    verifying `CompleteSsePath()` / `CompleteMessagePath()` of the
    constructed server resolve to the mounted paths.
    
    ## Testing
    
    - Added reproduction test `TestMCPSSEEndpointConfig`; confirmed it fails
    on the original values (server name, SSE path, and `CompleteSsePath()`
    all mismatched) and passes after the fix.
    - `go test ./internal/router/... -count=1` — PASS.
    - `go test ./internal/service/siteinfo/... -run '^$' -count=1` — PASS
    (compilation).
    - `go build ./internal/base/constant/... ./internal/router/...
    ./internal/service/siteinfo/...` — PASS.
    - `gofmt -l` on all changed files — clean.
    
    <!-- oss-bugfix-bot -->
    
    Signed-off-by: bfs2021 <[email protected]>
---
 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(-)

diff --git a/internal/base/constant/mcp.go b/internal/base/constant/mcp.go
new file mode 100644
index 00000000..a840d893
--- /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 54f41971..8ff5fe2a 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 00000000..c3b30b48
--- /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 8542538e..edcadd2f 100644
--- a/internal/service/siteinfo/siteinfo_service.go
+++ b/internal/service/siteinfo/siteinfo_service.go
@@ -487,7 +487,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
 }

Reply via email to