MCPcopy
hub / github.com/apache/answer / MCPQuestionsHandler

Method MCPQuestionsHandler

internal/controller/mcp_controller.go:89–126  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

87}
88
89func (c *MCPController) MCPQuestionsHandler() func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
90 return func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
91 if err := c.ensureMCPEnabled(ctx); err != nil {
92 return nil, err
93 }
94 cond := schema.NewMCPSearchCond(request)
95
96 siteGeneral, err := c.siteInfoService.GetSiteGeneral(ctx)
97 if err != nil {
98 log.Errorf("get site general info failed: %v", err)
99 return nil, err
100 }
101
102 searchResp, err := c.searchService.Search(ctx, &schema.SearchDTO{
103 Query: cond.ToQueryString() + " is:question",
104 Page: 1,
105 Size: 5,
106 Order: "newest",
107 })
108 if err != nil {
109 return nil, err
110 }
111
112 resp := make([]*schema.MCPSearchQuestionInfoResp, 0)
113 for _, question := range searchResp.SearchResults {
114 t := &schema.MCPSearchQuestionInfoResp{
115 QuestionID: question.Object.QuestionID,
116 Title: question.Object.Title,
117 Content: question.Object.Excerpt,
118 Link: fmt.Sprintf("%s/questions/%s", siteGeneral.SiteUrl, question.Object.QuestionID),
119 }
120 resp = append(resp, t)
121 }
122
123 data, _ := json.Marshal(resp)
124 return mcp.NewToolResultText(string(data)), nil
125 }
126}
127
128func (c *MCPController) MCPQuestionDetailHandler() func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
129 return func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {

Callers 2

callMCPToolMethod · 0.80
RegisterMCPRouterMethod · 0.80

Calls 5

ensureMCPEnabledMethod · 0.95
ToQueryStringMethod · 0.95
NewMCPSearchCondFunction · 0.92
GetSiteGeneralMethod · 0.65
SearchMethod · 0.45

Tested by

no test coverage detected