Skip to content

Add RemoveResource method to MCPServer #141

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

Merged
merged 1 commit into from
Apr 19, 2025

Conversation

isaacphi
Copy link
Contributor

@isaacphi isaacphi commented Apr 13, 2025

First of all, thank you for this library!

Adds a simple RemoveResource method similar to the existing AddResource one. It allows you to handle dynamic resources that need to be removed based on external events. I added this because I want to be able to react to removed files in my filesystem server.

The method handles proper ListChanged notifications when configured and includes comprehensive tests. Let me know if there's any interest in merging this!

Summary by CodeRabbit

  • New Features
    • Introduced dynamic resource removal with automatic notifications to clients when applicable.
  • Tests
    • Implemented comprehensive tests to validate resource removal across various scenarios, ensuring correct behavior and notification handling.

Copy link
Contributor

coderabbitai bot commented Apr 13, 2025

Walkthrough

This pull request introduces a new method in the MCPServer for removing resources and adds a corresponding test file to validate its functionality. The new RemoveResource method ensures thread-safe removal of resources from the server’s map and sends notifications if the server is capable. The test file (resource_test.go) defines multiple test cases covering resource removal in various scenarios, including successful removal, handling non-existent resources, and managing cases where notifications are disabled.

Changes

File(s) Change Summary
server/.../resource_test.go Added a new test file with TestMCPServer_RemoveResource that defines several test cases to validate resource removal behavior and notification handling under different scenarios.
server/.../server.go Introduced the RemoveResource method to the MCPServer, which safely removes a resource using a write lock and conditionally sends notifications based on server capabilities.

Tip

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.

📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c509bf9 and 40c7740.

📒 Files selected for processing (2)
  • server/resource_test.go (1 hunks)
  • server/server.go (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
server/resource_test.go (3)
server/server.go (3)
  • MCPServer (143-159)
  • NewMCPServer (366-392)
  • WithResourceCapabilities (288-296)
mcp/types.go (2)
  • Result (173-177)
  • ListResourcesResult (406-409)
mcp/resources.go (3)
  • NewResource (12-23)
  • WithResourceDescription (27-31)
  • WithMIMEType (35-39)
🔇 Additional comments (6)
server/server.go (1)

410-420: Clean implementation of the RemoveResource method.

The new RemoveResource method provides a clean, thread-safe implementation for removing resources from the server's resource map. It follows the same pattern as other resource management methods in the codebase, like AddResource and DeleteTools.

I particularly like:

  • Proper mutex locking/unlocking to ensure thread safety
  • Checking for nil capabilities before attempting to access properties
  • Following the notification pattern consistent with other methods in the codebase
server/resource_test.go (5)

13-19: Well-structured test setup using table-driven tests.

The test suite is well-structured using a table-driven approach with clear separation of test setup, execution, and validation. This makes the tests easy to understand and maintain.


20-99: Comprehensive test for successful resource removal.

This test case thoroughly validates that:

  1. Resources are properly added initially
  2. The server's resource list is correctly updated after removal
  3. Proper notifications are sent when a resource is removed
  4. The remaining resources are still accessible

100-148: Good edge case testing for removing non-existent resources.

Testing the removal of non-existent resources ensures the method doesn't break when handling invalid inputs. The test verifies that:

  1. No errors occur when removing a non-existent resource
  2. Notifications are still sent (consistent with the implementation)
  3. Existing resources remain unaffected

149-197: Thorough capability testing.

This test case properly validates that the notification behavior respects the server's capabilities configuration. By creating a server with listChanged disabled, it confirms that no notifications are sent when this capability is turned off.


200-253: Robust test execution with proper timeout handling.

The test execution code includes:

  1. Clean server setup with necessary capabilities
  2. Proper notification collection with timeout handling
  3. Different notification handling based on expectations
  4. Clear validation of results

The timeout handling in the notification collection is particularly well-implemented, ensuring tests don't hang indefinitely.

✨ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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.

@ezynda3 ezynda3 merged commit 79a0ac0 into mark3labs:main Apr 19, 2025
2 checks passed
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.

2 participants