From 0ed4fecd5513c970d8dae7ad0ca5483b9fc640c0 Mon Sep 17 00:00:00 2001 From: Anurag Mishra Date: Sat, 21 Nov 2026 04:47:26 +0000 Subject: [PATCH] feat(mcp): expose semantic_search tool on the MCP server The semantic search tool, handler and availability check already existed but RegisterMCPRouter only registered the six basic tools, so external MCP clients could not use semantic search even with a VectorSearch plugin enabled. Register semantic_search next to the basic tools when a VectorSearch plugin is available, matching how the AI chat tool list hides the tool when it cannot succeed. The registration is split into addMCPTools so the conditional is covered by a test without a live plugin registry. --- internal/router/mcp_router.go | 24 ++++++--- internal/router/mcp_router_test.go | 81 ++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 6 deletions(-) create mode 100644 internal/router/mcp_router_test.go 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)) + } + } +}