Skip to content

feat: Add Pagination for requesting list of prompts #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

denniskawurek
Copy link
Contributor

Adds the Pagination feature to the prompts/list feature to fulfil the specification.

Motivation and Context

This adds the Pagination feature to the prompts/list feature as described in the specification.

To make this possible mainly two changes are made:

  1. The logic for cursor handling is added.
  2. Handling for invalid parameters (MCP error code -32602 (Invalid params)) is added to the McpServerSession.

Decisions made:

  1. For now the cursor is the integer value of the start index encoded as a base64 string.
  2. The page size is set to 10.
  3. When parameters are found to be invalid the newly introduced McpParamsValidationError is returned to handle it properly in the McpServerSession.

If this change is ok, I'm happy to create a follow-up PR for the other MCP features. Maybe some parts of the cursor handling can be moved out to separate methods.

How Has This Been Tested?

Integration tests are added for the prompts/list feature.

Types of changes

  • New feature (non-breaking change which adds functionality)

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Adds the Pagination feature to the `prompts/list` feature as described in the specification.

To make this possible mainly two changes are made:

1. The logic for cursor handling is added.
2. Handling for invalid parameters (MCP error code `-32602 (Invalid params)`) is added to the `McpServerSession`.

For now the cursor is the base64 encoded start index of the next page.

The page size is set to 10.

When parameters are found to be invalid the newly introduced `McpParamsValidationError` is returned to handle it properly in the `McpServerSession`.
Copy link
Member

@chemicL chemicL left a comment

Choose a reason for hiding this comment

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

Thank you for the contribution! It is a great start, would you like to take this further and unify the logic for other listing requests?


var errorCode = McpSchema.ErrorCodes.INTERNAL_ERROR;

if (error instanceof McpParamsValidationError) {
Copy link
Member

Choose a reason for hiding this comment

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

I suspect we have some convoluted handling of McpError here and on the client side too, but we can address this later on and clean up.


public class McpParamsValidationError extends McpError {

public McpParamsValidationError(McpSchema.JSONRPCResponse.JSONRPCError jsonRpcError) {
Copy link
Member

Choose a reason for hiding this comment

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

It feels that we don't need this constructor at this time. Let's remove it.

super(jsonRpcError.message());
}

public McpParamsValidationError(Object error) {
Copy link
Member

Choose a reason for hiding this comment

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

This can be a string at the moment I believe.

new TypeReference<McpSchema.PaginatedRequest>() {
});

if (!isCursorValid(request.cursor(), this.prompts.size())) {
Copy link
Member

Choose a reason for hiding this comment

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

We can nest this under the request.cursor() != null and avoid returning true inside the validation method.

int requestedStartIndex = 0;

if (request.cursor() != null) {
requestedStartIndex = decodeCursor(request.cursor());
Copy link
Member

Choose a reason for hiding this comment

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

I think we could take into account the hash of the prompts collection and based on the page size infer whether the collection has not changed - if it has we should return an error allowing the client to restart fetching the results.

@chemicL chemicL added the waiting for user Waiting for user feedback or more details label Apr 23, 2025
@denniskawurek
Copy link
Contributor Author

Hi,

thanks for your review and your feedback!

Thank you for the contribution! It is a great start, would you like to take this further and unify the logic for other listing requests?

Yes I will do this. I should be able to update the pull request by the end of the week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for user Waiting for user feedback or more details
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants