Skip to content

Add truncated and total envelope to MCP search responses #1808

Description

@Widthdom

Summary

MCP tool responses for search, definition, and references (McpToolHandlers.cs:300-314, 390-408, 461-481) return a results array without any truncated boolean or total count. An agent that receives 20 results cannot tell whether the query produced exactly 20 hits, or 20 out of 980 hits silently clamped by the server-side limit. The result: the agent either over-trusts a clamped list or wastes turns paginating by guessing larger limit values that may or may not have been honoured.

Where

  • src/CodeIndex/Mcp/McpToolHandlers.cs:300-314 (search response)
  • src/CodeIndex/Mcp/McpToolHandlers.cs:390-408 (definition response)
  • src/CodeIndex/Mcp/McpToolHandlers.cs:461-481 (references response)

Suggested approach

(1) Extend every MCP tool result envelope with truncated: bool (true if the server stopped at the configured ceiling) and total: int|null (null when computing total would be too expensive, otherwise the true count). (2) When the underlying SQL query is naturally cardinality-bounded (e.g. references for one symbol), compute and return the true total. (3) For full-text search where computing total is expensive, run an inexpensive COUNT(*) only when truncated=true and the result count equals the requested limit. (4) Mirror the same envelope on CLI --json output so scripts get the same signal. (5) Document the envelope under "MCP response envelope" and reference it from each tool's description. (6) Add regression tests that assert truncated=true and a sensible total when results exceed the limit, and truncated=false otherwise. (7) Cross-link with #1616 (batch_query partial completion) and #1472 (pagination cursors).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions