Skip to content

Refactor: Resource Management with Ordered Pagination, Duplicate Control #408

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

cryo-zd
Copy link
Contributor

@cryo-zd cryo-zd commented Jun 19, 2025

Description

Fixes #397
  Refactors the internal resource management logic to preserve resource insertion order while maintaining O(1) lookup and deletion. This addresses the need for stable ordering in ListResources and aligns with use cases such as time-ordered note management.
Key Changes

  • Introduced resourceManager: Combines a linked list and a map to track insertion order and enable fast access/removal.
  • Configurable duplicate handling: Added warnOnDuplicateResources to control whether duplicate resources are ignored (with warning) or replaced.
  • Improved cursor-based pagination:
    • Pagination now uses Resource.URI instead of Resource.Name for cursor positioning.
    • Cursor semantics remain consistent with the original listByPagination: it points to the last element of the current page, with the next page starting from the next element.
    • Two improvements over the original:
    • Fixed an edge case where a non-empty cursor was returned even when no elements remained (ptr != nil check in if paginationLimit != nil && len(templatesToReturn) >= *paginationLimit && ptr != nil).
    • Eliminated the need for full list materialization and sorting by Name before each page, improving performance and reducing complexity.

Why We Avoided Generics
  Although listResources and listResourceTemplates share similar logic, we intentionally avoided abstracting them into a single generic function due to practical performance considerations:

  • A generic implementation would require passing a URI extractor function (e.g., func(entry T) string) to support cursor encoding.
  • The generic function could only return []resourceEntry or []resourceTemplateEntry, meaning that the caller must re-allocate and extract the final []mcp.Resource or []mcp.ResourceTemplate afterward.
  • For large resource sets, this adds non-trivial memory allocation and object copying overhead, reducing overall performance.
  • By keeping two specialized implementations, we ensure zero-copy iteration, minimal memory pressure, and better cache locality.

  This trade-off prioritizes runtime efficiency over code deduplication, which is more appropriate given the core role and high-call frequency of these functions in the MCPServer.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • MCP spec compatibility implementation
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Code refactoring (no functional changes)
  • Performance improvement
  • Tests only (no functional changes)
  • Other (please describe):

Checklist

  • My code follows the code style of this project
  • I have performed a self-review of my own code
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the documentation accordingly

Summary by CodeRabbit

  • New Features

    • Introduced enhanced resource and template management with efficient listing, adding, and removal, including support for pagination and duplicate resource handling options.
    • Added warning-level logging capability for improved diagnostics.
  • Refactor

    • Centralized all resource and template management into a dedicated component, simplifying server logic and improving maintainability.
  • Chores

    • Updated server options to allow configuration of duplicate resource handling behavior.

Copy link
Contributor

coderabbitai bot commented Jun 19, 2025

Walkthrough

The resource management logic in the server has been refactored into a new resourceManager type, which maintains insertion order and supports efficient lookups, removals, and pagination. Resource and template handlers are now managed centrally. The Logger interface now includes a warning-level method. Server code delegates resource operations to the new manager.

Changes

Files/Groups Change Summary
server/resource_manager.go Introduced resourceManager type for managing resources/templates with insertion order, efficient lookup, and handlers.
server/server.go Refactored MCPServer to delegate all resource/template operations to resourceManager. Removed direct map and locking logic. Added WithWarnOnDuplicateResources option.
util/logger.go Added Warnf method to Logger interface and implementation.

Assessment against linked issues

Objective Addressed Explanation
ListResources should follow the order in which resources are written (#397)

Assessment against linked issues: Out-of-scope changes

Code Change Explanation
Addition of Warnf to Logger interface and stdLogger (util/logger.go) This logging enhancement is not related to the resource ordering or ListResources objectives.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (1.64.8)

Error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2
Failed executing command with error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
server/resource_manager.go (1)

261-324: Consider performance optimization for template matching.

The current implementation iterates through all templates to find a match (lines 286-300). For a large number of templates, this could be inefficient.

Consider implementing a more efficient URI template matching strategy, such as:

  1. Pre-computing template patterns during registration
  2. Using a trie-based structure for pattern matching
  3. Ordering templates by specificity

However, this optimization should only be considered if you expect a large number of resource templates in typical usage.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a077d27 and 8143512.

📒 Files selected for processing (3)
  • server/resource_manager.go (1 hunks)
  • server/server.go (9 hunks)
  • util/logger.go (2 hunks)
🔇 Additional comments (12)
util/logger.go (1)

11-11: LGTM: Clean addition of warning-level logging.

The Warnf method follows the existing pattern and is correctly implemented in the stdLogger. This enhancement supports the resource manager's duplicate warning functionality.

Also applies to: 36-38

server/resource_manager.go (5)

54-64: Well-designed data structure for O(1) operations with insertion order.

The combination of linked list and map is an excellent choice for maintaining insertion order while providing O(1) lookup and deletion. The thread safety approach using RWMutex is appropriate.


84-108: Verify duplicate handling behavior aligns with expectations.

The duplicate handling logic looks correct, but there's a potential race condition concern. When warnOnDuplicateResources is true and a duplicate is found, the function continues to the next iteration without setting listChanged = true. However, when it's false, the old element is removed and replaced, correctly setting listChanged = true.

The behavior seems intentional - when warnings are enabled, duplicates are truly skipped (no list change), but when warnings are disabled, duplicates are replaced (list changes). Could you confirm this is the intended behavior?


266-282: Verify resource handler mutex is released in all paths.

The RWMutex is correctly released before calling the handler to prevent holding the lock during potentially long-running operations. This is good practice for avoiding deadlocks.


215-259: Apply the same pagination fix to resource templates.

The same off-by-one issue exists here as in the listResources method.

-	if paginationLimit != nil && len(templatesToReturn) >= *paginationLimit && ptr != nil {
+	if paginationLimit != nil && len(templatesToReturn) == *paginationLimit && ptr != nil {

Likely an incorrect or invalid review comment.


160-204: Potential off-by-one error in pagination cursor generation.

The cursor generation logic has a subtle issue. The condition on line 199 checks len(resourcesToReturn) >= *paginationLimit, but this could generate a cursor even when there are no more elements, if the last page happens to be exactly the pagination limit size.

-	if paginationLimit != nil && len(resourcesToReturn) >= *paginationLimit && ptr != nil {
+	if paginationLimit != nil && len(resourcesToReturn) == *paginationLimit && ptr != nil {

The same issue likely exists in listResourceTemplates on line 254.

Likely an incorrect or invalid review comment.

server/server.go (6)

136-136: Clean integration of resourceManager.

The resourceManager is properly embedded in the MCPServer struct and initialized in the constructor. Good separation of concerns.

Also applies to: 288-288


182-190: Server option implementation looks correct.

The WithWarnOnDuplicateResources option properly configures the resourceManager's duplicate handling behavior. The documentation clearly explains the two modes.


308-308: Verify delegation calls return expected types.

The delegation to resourceManager.addResources() and resourceManager.addResourceTemplate() looks correct. These methods return boolean values indicating if the list changed, which is properly used for notifications.

Also applies to: 343-343


654-657: Pagination delegation simplifies the implementation.

The delegation to resourceManager.listResources() and resourceManager.listResourceTemplates() removes the need for the complex sorting and pagination logic that was likely present before. The cursor and pagination limit are passed through correctly.

Also applies to: 680-683


706-708: Direct delegation to resource manager.

The handleReadResource method now cleanly delegates to the resourceManager, which handles both direct resources and template matching internally.


613-647: Generic pagination function may be unused after refactoring.

The listByPagination generic function appears to still be present but may no longer be used for resources and resource templates since they now use the resourceManager's specialized pagination methods.

Please verify if this function is still needed or if it can be removed/marked for future cleanup. It's currently used for prompts and tools but not for resources.

#!/bin/bash
# Search for usage of listByPagination function
rg -A 3 "listByPagination" --type go

@douglarek
Copy link

douglarek commented Jun 20, 2025

cc @ezynda3 @pottekkat

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.

bug: The ListResources function should follow the order in which resources are written
2 participants