Skip to content

Conversation

@brunoselvacj
Copy link

Description

This change adds pagination support to SearchIndexTool and GetSegmentsTool to prevent token overflow errors when MCP tool responses exceed the 25,000 token limit.

SearchIndexTool changes:

  • Added size parameter (default: 10, max: 100) to limit number of search results returned
  • Added from parameter (default: 0) to support offset-based pagination
  • Automatic capping at 100 results to prevent token overflow
  • 7 new comprehensive tests covering pagination scenarios

GetSegmentsTool changes:

  • Added limit parameter (default: 1000) to cap number of segments returned
  • Prevents token overflow for clusters with many segments
  • 3 new tests covering limit functionality

Additional improvements:

  • Updated README.md with new pagination parameters
  • Updated tool descriptions in TOOL_REGISTRY
  • All 50 tests passing with TDD approach (wrote failing tests first, then implemented features)
  • Maintains backward compatibility with default values

Issues Resolved

Fixes token overflow issues where SearchIndexTool and GetSegmentsTool responses exceed the 25,000 token MCP limit, causing errors like: MCP tool 'SearchIndexTool' response (35969 tokens) exceeds maximum allowed tokens (25000).

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check
here.

@brunoselvacj brunoselvacj changed the title Add pagination support to SearchIndexTool and GetSegmentsTool to prevent token overflow feat: Add pagination support to SearchIndexTool and GetSegmentsTool to prevent token overflow Oct 15, 2025
…ent token overflow

Signed-off-by: Bruno Selva <bselva@cj.com>
@nathaliellenaa
Copy link
Collaborator

Thanks for raising this PR @brunoselvacj! Can you add the changes to the changelog? I also have a high level question regarding the SearchIndexTool size parameter. I'm wondering whether we should maintain a hard maximum limit of 100 or consider a more flexible approach. An alternative would be to remove the hard limit and instead throw a warning when the size exceeds 100. What do you think about this?

Copy link
Collaborator

@rithin-pullela-aws rithin-pullela-aws left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks like a positive step to improve reliability of the tools, but added few concerns with the change

Comment on lines +65 to +67
effective_size = min(args.size, 100) if args.size else 10
query_body['size'] = effective_size
query_body['from'] = args.from_ if args.from_ is not None else 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make the max limit variable configurable? Either via CLI parameter or config file? We can default to a maximum of 100 if the variable is not provided

Comment on lines +99 to +102
# Apply limit to prevent token overflow
if args.limit and isinstance(response, list):
return response[:args.limit]

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we apply the max limit, perhaps adding the information that the size has been truncated in the response message sent back to the MCP client would help?

The same thing can be applied for the Search Index tool response as well

'SearchIndexTool': {
'display_name': 'SearchIndexTool',
'description': 'Searches an index using a query written in query domain-specific language (DSL) in OpenSearch',
'description': 'Searches an index using a query written in query domain-specific language (DSL) in OpenSearch. Supports pagination with size (default: 10, max: 100) and from parameters to limit response size and prevent token overflow.',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new information about the parameters is sent to the LLM as parameter description. Maybe we can limit the tool description to just it's function and not describe its parameters?

'GetSegmentsTool': {
'display_name': 'GetSegmentsTool',
'description': 'Gets information about Lucene segments in indices, including memory usage, document counts, and segment sizes. Can be filtered by specific indices.',
'description': 'Gets information about Lucene segments in indices, including memory usage, document counts, and segment sizes. Can be filtered by specific indices. Supports limit parameter (default: 1000) to prevent token overflow.',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same concern as above

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.

3 participants