diff --git a/internal/router/mcp_router.go b/internal/router/mcp_router.go index 54f41971d..c07b8cc51 100644 --- a/internal/router/mcp_router.go +++ b/internal/router/mcp_router.go @@ -20,6 +20,7 @@ package router import ( + "github.com/apache/answer/internal/controller" "github.com/apache/answer/internal/schema/mcp_tools" "github.com/gin-gonic/gin" "github.com/mark3labs/mcp-go/server" @@ -28,12 +29,7 @@ import ( func (a *AnswerAPIRouter) RegisterMCPRouter(r *gin.RouterGroup) { s := server.NewMCPServer("Answer Enterprise MCP Server", "1.0.0") - s.AddTool(mcp_tools.NewQuestionsTool(), a.mcpController.MCPQuestionsHandler()) - s.AddTool(mcp_tools.NewAnswersTool(), a.mcpController.MCPAnswersHandler()) - s.AddTool(mcp_tools.NewCommentsTool(), a.mcpController.MCPCommentsHandler()) - s.AddTool(mcp_tools.NewTagsTool(), a.mcpController.MCPTagsHandler()) - s.AddTool(mcp_tools.NewTagDetailTool(), a.mcpController.MCPTagDetailsHandler()) - s.AddTool(mcp_tools.NewUserTool(), a.mcpController.MCPUserDetailsHandler()) + addMCPTools(s, a.mcpController, a.mcpController.SemanticSearchAvailable()) sseServer := server.NewSSEServer(s, server.WithSSEEndpoint("/answer/api/v1/mcp/see"), @@ -42,3 +38,19 @@ func (a *AnswerAPIRouter) RegisterMCPRouter(r *gin.RouterGroup) { r.GET("/mcp/sse", gin.WrapH(sseServer.SSEHandler())) r.POST("/mcp/message", gin.WrapH(sseServer.MessageHandler())) } + +// addMCPTools registers the tools the public MCP server exposes. semantic_search +// is registered only when a VectorSearch plugin is enabled, so an external client +// is never offered a capability that always fails, matching the tool list the AI +// chat builds. +func addMCPTools(s *server.MCPServer, c *controller.MCPController, semanticSearchAvailable bool) { + s.AddTool(mcp_tools.NewQuestionsTool(), c.MCPQuestionsHandler()) + s.AddTool(mcp_tools.NewAnswersTool(), c.MCPAnswersHandler()) + s.AddTool(mcp_tools.NewCommentsTool(), c.MCPCommentsHandler()) + s.AddTool(mcp_tools.NewTagsTool(), c.MCPTagsHandler()) + s.AddTool(mcp_tools.NewTagDetailTool(), c.MCPTagDetailsHandler()) + s.AddTool(mcp_tools.NewUserTool(), c.MCPUserDetailsHandler()) + if semanticSearchAvailable { + s.AddTool(mcp_tools.NewSemanticSearchTool(), c.MCPSemanticSearchHandler()) + } +} diff --git a/internal/router/mcp_router_test.go b/internal/router/mcp_router_test.go new file mode 100644 index 000000000..7051993d3 --- /dev/null +++ b/internal/router/mcp_router_test.go @@ -0,0 +1,81 @@ +/* + * 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 ( + "sort" + "testing" + + "github.com/apache/answer/internal/controller" + "github.com/mark3labs/mcp-go/server" +) + +const semanticSearchToolName = "semantic_search" + +var basicMCPToolNames = []string{ + "get_questions", + "get_answers_by_question_id", + "get_comments", + "get_tags", + "get_tag_detail", + "get_user", +} + +func registeredMCPToolNames(tools map[string]*server.ServerTool) []string { + names := make([]string, 0, len(tools)) + for name := range tools { + names = append(names, name) + } + sort.Strings(names) + return names +} + +func TestAddMCPToolsRegistersSemanticSearchWhenAvailable(t *testing.T) { + s := server.NewMCPServer("test", "1.0.0") + addMCPTools(s, &controller.MCPController{}, true) + + tools := s.ListTools() + if _, ok := tools[semanticSearchToolName]; !ok { + t.Fatalf("semantic_search must be registered when a vector search plugin is available: %v", registeredMCPToolNames(tools)) + } + for _, name := range basicMCPToolNames { + if _, ok := tools[name]; !ok { + t.Fatalf("%s must stay registered: %v", name, registeredMCPToolNames(tools)) + } + } +} + +func TestAddMCPToolsOmitsSemanticSearchWhenUnavailable(t *testing.T) { + s := server.NewMCPServer("test", "1.0.0") + addMCPTools(s, &controller.MCPController{}, false) + + tools := s.ListTools() + if _, ok := tools[semanticSearchToolName]; ok { + t.Fatalf("semantic_search must not be registered without a vector search plugin: %v", registeredMCPToolNames(tools)) + } + if len(tools) != len(basicMCPToolNames) { + t.Fatalf("expect %d tools, got %d: %v", len(basicMCPToolNames), len(tools), registeredMCPToolNames(tools)) + } + for _, name := range basicMCPToolNames { + if _, ok := tools[name]; !ok { + t.Fatalf("%s must stay registered: %v", name, registeredMCPToolNames(tools)) + } + } +}