Skip to content
Open
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
54 changes: 27 additions & 27 deletions internal/controller/mcp_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,17 @@ func (c *MCPController) MCPSemanticSearchHandler() func(ctx context.Context, req
var meta plugin.VectorSearchMetadata
_ = json.Unmarshal([]byte(r.Metadata), &meta)

// The vector index holds every synced record, so a question hidden or
// deleted after the sync is still in it. A result is only answered from
// what the other MCP handlers would show, never from the index alone.
question, qErr := c.questioncommon.Info(ctx, meta.QuestionID, "")
if qErr != nil {
log.Warnf("get question %s for semantic search failed: %v", meta.QuestionID, qErr)
}
if !mcpQuestionIsPublic(question) {
continue
}

item := &schema.MCPSemanticSearchResp{
ObjectID: r.ObjectID,
ObjectType: r.ObjectType,
Expand All @@ -434,18 +445,13 @@ func (c *MCPController) MCPSemanticSearchHandler() func(ctx context.Context, req

// Query content from DB using IDs stored in metadata
if r.ObjectType == "question" {
question, qErr := c.questioncommon.Info(ctx, meta.QuestionID, "")
if qErr != nil {
log.Warnf("get question %s for semantic search failed: %v", meta.QuestionID, qErr)
} else {
item.Title = question.Title
item.Content = question.Content
}
item.Title = question.Title
item.Content = question.Content

// Fetch answers by ID from metadata
for _, a := range meta.Answers {
answerEntity, exist, aErr := c.answerRepo.GetAnswer(ctx, a.AnswerID)
if aErr != nil || !exist {
if aErr != nil || !exist || answerEntity.Status != entity.AnswerStatusAvailable {
continue
}
answerItem := &schema.MCPSemanticSearchAnswer{
Expand All @@ -455,7 +461,7 @@ func (c *MCPController) MCPSemanticSearchHandler() func(ctx context.Context, req
// Fetch comments on this answer from DB
for _, ac := range a.Comments {
cmt, cExist, cErr := c.commentRepo.GetComment(ctx, ac.CommentID)
if cErr == nil && cExist {
if cErr == nil && cExist && cmt.Status == entity.CommentStatusAvailable {
answerItem.Comments = append(answerItem.Comments, &schema.MCPSemanticSearchComment{
CommentID: ac.CommentID,
Content: cmt.OriginalText,
Expand All @@ -468,38 +474,32 @@ func (c *MCPController) MCPSemanticSearchHandler() func(ctx context.Context, req
// Fetch question comments from DB
for _, qc := range meta.Comments {
cmt, cExist, cErr := c.commentRepo.GetComment(ctx, qc.CommentID)
if cErr == nil && cExist {
if cErr == nil && cExist && cmt.Status == entity.CommentStatusAvailable {
item.Comments = append(item.Comments, &schema.MCPSemanticSearchComment{
CommentID: qc.CommentID,
Content: cmt.OriginalText,
})
}
}
} else if r.ObjectType == "answer" {
// Fetch question title for context
question, qErr := c.questioncommon.Info(ctx, meta.QuestionID, "")
if qErr == nil {
item.Title = question.Title
// Fetch answer content from DB; the answer a result stands for must
// still be available, or there is nothing to show for this result.
answerID := meta.AnswerID
if answerID == "" && len(meta.Answers) > 0 {
answerID = meta.Answers[0].AnswerID
}

// Fetch answer content from DB
if meta.AnswerID != "" {
answerEntity, exist, aErr := c.answerRepo.GetAnswer(ctx, meta.AnswerID)
if aErr == nil && exist {
item.Content = answerEntity.OriginalText
}
} else if len(meta.Answers) > 0 {
answerEntity, exist, aErr := c.answerRepo.GetAnswer(ctx, meta.Answers[0].AnswerID)
if aErr == nil && exist {
item.Content = answerEntity.OriginalText
}
answerEntity, exist, aErr := c.answerRepo.GetAnswer(ctx, answerID)
if aErr != nil || !exist || answerEntity.Status != entity.AnswerStatusAvailable {
continue
}
item.Title = question.Title
item.Content = answerEntity.OriginalText

// Fetch answer comments from DB
if len(meta.Answers) > 0 {
for _, ac := range meta.Answers[0].Comments {
cmt, cExist, cErr := c.commentRepo.GetComment(ctx, ac.CommentID)
if cErr == nil && cExist {
if cErr == nil && cExist && cmt.Status == entity.CommentStatusAvailable {
item.Comments = append(item.Comments, &schema.MCPSemanticSearchComment{
CommentID: ac.CommentID,
Content: cmt.OriginalText,
Expand Down
26 changes: 19 additions & 7 deletions internal/router/mcp_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package router

import (
"github.com/apache/answer/internal/base/constant"
"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"
Expand All @@ -38,15 +39,26 @@ func newMCPSSEServer(s *server.MCPServer) *server.SSEServer {
func (a *AnswerAPIRouter) RegisterMCPRouter(r *gin.RouterGroup) {
s := server.NewMCPServer(constant.MCPServerName, constant.MCPServerVersion)

s.AddTool(mcp_tools.NewQuestionsTool(), a.mcpController.MCPQuestionsHandler())
s.AddTool(mcp_tools.NewQuestionDetailTool(), a.mcpController.MCPQuestionDetailHandler())
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 := newMCPSSEServer(s)
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.NewQuestionDetailTool(), c.MCPQuestionDetailHandler())
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())
}
}
56 changes: 56 additions & 0 deletions internal/router/mcp_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,68 @@
package router

import (
"sort"
"testing"

"github.com/apache/answer/internal/base/constant"
"github.com/apache/answer/internal/controller"
"github.com/mark3labs/mcp-go/server"
)

const semanticSearchToolName = "semantic_search"

var basicMCPToolNames = []string{
"get_questions",
"get_question_detail",
"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))
}
}
}

// 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.
Expand Down