Skip to content

Enhancement: Return pagination metadata in semantic_code_search response #31

Description

@kapral18

Summary

Add totalHits to the semantic_code_search response to help agents make informed decisions about whether to paginate further or switch to other tools like map_symbols_by_query.

Current Behavior

The tool accepts page and size parameters but returns only the hits array:

// src/mcp_server/tools/semantic_code_search.ts:84-101
return {
  content: [
    {
      type: 'text',
      text: JSON.stringify(
        response.hits.hits.map((hit) => {
          const { type, language, kind, filePath, content } = hit._source;
          return { score: hit._score, type, language, kind, filePath, content };
        })
      ),
    },
  ],
};

Response shape:

[
  { "score": 0.92, "type": "code", "filePath": "...", "content": "..." },
  { "score": 0.87, "type": "code", "filePath": "...", "content": "..." }
]

Proposed Behavior

Include pagination metadata from the already-available response.hits.total:

{
  "hits": [
    { "score": 0.92, "type": "code", "filePath": "...", "content": "..." },
    { "score": 0.87, "type": "code", "filePath": "...", "content": "..." }
  ],
  "total": 142
}

Why This Helps Agents

┌────────────────────────────────────────────────────────────────────┐
│  Agent receives: { hits: [...25 items], total: 142 }               │
├────────────────────────────────────────────────────────────────────┤
│                                                                    │
│  Agent can now reason:                                             │
│                                                                    │
│  • "142 total results, I have 25 - there's more if I need it"     │
│  • "142 is a lot - maybe map_symbols_by_query is better for       │
│     exhaustive coverage"                                           │
│  • "Only 8 total results - no need to paginate"                   │
│                                                                    │
└────────────────────────────────────────────────────────────────────┘

Implementation

Minimal change in src/mcp_server/tools/semantic_code_search.ts:

// Extract total from ES response (already available)
const total = typeof response.hits.total === 'number' 
  ? response.hits.total 
  : response.hits.total?.value ?? 0;

return {
  content: [
    {
      type: 'text',
      text: JSON.stringify({
        hits: response.hits.hits.map((hit) => {
          const { type, language, kind, filePath, content } = hit._source;
          return { score: hit._score, type, language, kind, filePath, content };
        }),
        total,
      }),
    },
  ],
};

Acceptance Criteria

  • Response includes total field with the total number of matching documents
  • Existing hits array structure unchanged (backward compatible for agents parsing the array)

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions