Skip to content

feat(mcp): expose semantic_search tool on the MCP server - #1681

Open
shaktimaan00 wants to merge 4 commits into
apache:devfrom
shaktimaan00:fix/1674-mcp-semantic-search
Open

shaktimaan00 wants to merge 4 commits into
apache:devfrom
shaktimaan00:fix/1674-mcp-semantic-search

Conversation

@shaktimaan00

Copy link
Copy Markdown

Fixes #1674

Proposed Changes

  • internal/router/mcp_router.go: register semantic_search on the public MCP server when MCPController.SemanticSearchAvailable() reports a VectorSearch plugin, so external MCP clients (Claude Desktop, Cursor, …) can use the capability that already existed for the AI chat. The tool is registered last, after the six basic tools, and stays invisible on sites without a VectorSearch plugin — no tool that can never succeed is advertised.
  • The s.AddTool calls move into addMCPTools(s, c, semanticSearchAvailable bool). The bool seam exists for the same reason AIController.buildOpenAITools takes one (fix(ai): stop advertising semantic_search when no vector search plugin is enabled #1597): SemanticSearchAvailable() reads the process-wide plugin registry (plugin.IsVectorSearchEnabled()), which a unit test cannot switch on without a live plugin.
  • internal/router/mcp_router_test.go (new): regression coverage for the conditional, in the shape of internal/controller/ai_tools_test.go.

Acceptance criteria

The issue asks for the tool to be registered on the MCP server conditionally, and to stay hidden without a VectorSearch plugin:

  • With a VectorSearch plugin available, the MCP server registers semantic_search alongside the six basic tools — TestAddMCPToolsRegistersSemanticSearchWhenAvailable.
  • Without one, semantic_search is not registered and the six basic tools still are — TestAddMCPToolsOmitsSemanticSearchWhenUnavailable (asserts exactly 6 tools).
  • No new tool or handler: mcp_tools.NewSemanticSearchTool() and MCPController.MCPSemanticSearchHandler() are used as they are.

Validation

Run in a golang:1.25-bookworm container against main at 9e6ca4a:

gofmt -l internal/router/                     -> no output
go build ./...                                -> exit 0
go vet ./internal/router/ ./internal/controller/ -> exit 0
go test ./internal/router/ -run AddMCPTools -v   -> 2 tests, PASS
go test ./internal/router/ ./internal/controller/ ./internal/schema/mcp_tools/ -> ok
go test ./...                                 -> 25 packages ok, 0 FAIL
make test                                     -> ok  internal/repo/repo_test
golangci-lint v2.6.2 run ./internal/router/... -> 0 issues

The new test is not vacuous: forcing the gate to false fails the available-case test, and forcing it to true fails the unavailable-case test.

make lint itself needs Docker or Podman inside the runner for script/check-asf-header.sh, so it could not run here; that script was checked by diffing the ASF header of the two touched files against their neighbours (identical), and golangci-lint was run directly instead.

No frontend change, so no screenshot.

fenbox and others added 2 commits November 19, 2026 15:26
## Proposed Changes

- Add `AGENTS.md` with repository structure, development commands,
coding conventions, testing guidance, and commit/PR expectations.
- Add an English `DESIGN.md` following the design.md alpha
specification, documenting React Bootstrap as the design foundation and
the default primary color `#0033FF`.
- Document existing theme overrides, dark mode, layout conventions, and
component usage, with YAML design tokens and source references.

## Validation

- Checked commands and conventions against repository configuration and
source files.
- Confirmed `DESIGN.md` contains no Chinese text.
- `git diff --cached --check` passed before committing.
- Documentation-only changes; no application tests run.
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.
@LinkinStars

Copy link
Copy Markdown
Member

Thanks for the PR. Before exposing this handler through MCP, please apply the same visibility checks used by the existing MCP handlers. MCPSemanticSearchHandler currently retrieves questions and answers without checking question visibility/status or answer status, while bulk vector sync can index non-public records. This allows semantic search to return hidden, pending, or soft-deleted content. Please validate the parent question and filter unavailable answers before returning results.

Please also resolve the conflicts with dev, preserving its MCP endpoint configuration and existing tests.

The vector index keeps every synced record, so a question hidden or deleted
after the sync was still returned by semantic_search, and answers/comments
were read without a status check. Gate every result on the parent question
with mcpQuestionIsPublic, the same check the other MCP handlers use, and skip
answers and comments that are no longer available.
@shaktimaan00

Copy link
Copy Markdown
Author

Both points are addressed.

Visibility. MCPSemanticSearchHandler now gates every result on the parent question with
mcpQuestionIsPublic — the same predicate MCPAnswersHandler, MCPCommentsHandler and
MCPQuestionDetailHandler already use (Show == QuestionShow and status available or closed) — so a
question hidden, deleted or left pending after the vector sync is dropped from the response instead
of being read straight out of the index. Answers nested in a question result, and answers and
comments fetched by id, now also require their available status (AnswerStatusAvailable,
CommentStatusAvailable), and an answer result whose answer is no longer available is dropped.
Hoisting the question lookup above the two branches also removed the duplicate
questioncommon.Info call per result. Commit c9a4741.

Conflicts. Merged dev in (e6701a2). The resolution keeps dev's constant.MCPServerName /
constant.MCPServerVersion and the newMCPSSEServer helper, and addMCPTools registers dev's seven
tools including get_question_detail, with semantic_search appended only when a VectorSearch
plugin is up. basicMCPToolNames in the router test now lists get_question_detail too, since the
"not registered" case asserts the exact tool count.

Gates on the merged tree, in a container:

  • go build ./... — ok
  • go test ./internal/router/ ./internal/controller/ — ok
  • golangci-lint run ./internal/router/... ./internal/controller/... (v2.6.2, the version pinned in
    the Makefile) — 0 issues

One thing I did not add: a handler-level unit test for the new filtering. QuestionCommon is a
concrete struct, so exercising the handler means constructing the whole service graph; the decision
itself is mcpQuestionIsPublic, which TestMCPQuestionIsPublic already covers case by case. Happy
to build the fakes if you want the handler tested end to end.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(mcp): expose semantic_search tool on the MCP server

3 participants