Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions internal/base/constant/mcp.go
Original file line number Diff line number Diff line change
@@ -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"
)
17 changes: 12 additions & 5 deletions internal/router/mcp_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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()))
}
59 changes: 59 additions & 0 deletions internal/router/mcp_router_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 1 addition & 1 deletion internal/service/siteinfo/siteinfo_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down