Skip to content

fix(mcp): enforce visibility and input bounds in MCP tools - #1690

Open
NUAAwqy wants to merge 1 commit into
apache:devfrom
NUAAwqy:fix/mcp-semantic-search-visibility
Open

NUAAwqy wants to merge 1 commit into
apache:devfrom
NUAAwqy:fix/mcp-semantic-search-visibility

Conversation

@NUAAwqy

@NUAAwqy NUAAwqy commented Nov 28, 2026

Copy link
Copy Markdown

Proposed changes

Fixes #1689 - MCP tools now enforce the visibility and input contracts they advertise.

Visibility gating in semantic_search - the handler previously published title/content for whatever the vector metadata referenced. Every fetch is now gated by the same rules the question-detail, answers and comments tools apply:

  • questions: mcpQuestionIsPublic (not deleted, not hidden) - a stale embedding can no longer expose a deleted or hidden question;
  • answers: new mcpSemanticSearchAnswerVisible (status available, non-nil);
  • comments: new mcpSemanticSearchCommentVisible (status available, non-nil);
  • answer-type results verify the parent question is public before the question title is exposed.

Input contracts - tools now accept what their schemas document:

  • the tag filter accepts both the documented semicolon-separated form and the comma form, trims whitespace and drops empty entries (splitTagList);
  • score, declared as a string argument in the tool schema, is read with request.GetInt so the string form works instead of being silently ignored; the number form keeps working;
  • top_k is read the same way and clamped to a new MCPMaxTopK = 50, mirroring the page_size cap.

Answers handler - fills the question_title field its response schema declares and removes the unreachable duplicated result-assembly block (the branch below if len(cond.QuestionID) > 0 could never run because the empty-ID case returns earlier).

Validation

New tests:

  • internal/schema/mcp_schema_test.go: TestNewMCPSearchCondScore (string form of score), TestNewMCPSearchCondTagSeparators (semicolon/comma/mixed/empty entries), TestNewMCPSemanticSearchCondTopKBounds (defaults, clamping, fallbacks). Before the fix 8 subtests of these fail on dev; after the fix all pass.
  • internal/controller/mcp_controller_test.go: TestMCPSemanticSearchAnswerVisible, TestMCPSemanticSearchCommentVisible covering the new policy helpers.

go build ./... passes; go test ./internal/schema/ ./internal/controller/ ./pkg/... ./internal/service/... all pass (schema: 6 tests incl. the 3 new ones; controller: 3 tests incl. the 2 new ones). gofmt clean.

semantic_search exposed content that every other MCP tool excludes: a
stale embedding handed the controller a question, answer or comment ID,
and the handler returned title and content without checking that the
question is public, that the answer is available, or that the comment
is in a visible state.  Deleted and hidden questions, deleted answers
and their comments could therefore be read through MCP after deletion.
Every fetch in the semantic search handler is now gated by the same
rules the question-detail, answers and comments tools apply.

The get_questions tool documents its tag filter as "semicolon
separated" but the parser split on commas only and kept surrounding
whitespace, so a client following the documented contract got zero
results; both separators are now accepted and entries are trimmed.
The tool declares score as a string argument while the parser only
accepted numbers, so a conforming client had its filter silently
dropped; numeric arguments are now read consistently.  top_k had no
upper bound, in contrast with page_size, and could request an
unbounded dump of stored vectors; it is now capped like page_size.

The answers tool also fills the question_title field its response
schema declares, and the unreachable duplicated block in the answers
handler is removed.

Fixes apache#1689

Signed-off-by: NUAAwqy <146524024+NUAAwqy@users.noreply.github.com>

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.

1 participant