-
Notifications
You must be signed in to change notification settings - Fork 413
feat(MCPServer): avoid unnecessary notifications when Resource/Tool not exists #266
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
feat(MCPServer): avoid unnecessary notifications when Resource/Tool not exists #266
Conversation
WalkthroughThis change updates the logic for removing resources and tools in the server to ensure that deletions and notifications only occur when the specified items actually exist. Corresponding test cases are updated and expanded to verify that no notifications are sent when attempting to remove non-existent resources or tools. Changes
Possibly related PRs
Tip ⚡️ Faster reviews with caching
Enjoy the performance boost—your workflow just got faster. ✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed 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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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/server.go (1)
454-460
: Existence tracking for tool deletion.This implementation tracks whether any tools were actually deleted by setting the
exists
flag when a tool is found and removed. Minor suggestion: consider explicitly initializingexists
tofalse
for clarity, though it's implicitly initialized correctly as is.-var exists bool +var exists bool = false
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
server/resource_test.go
(2 hunks)server/server.go
(2 hunks)server/server_test.go
(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
server/resource_test.go (1)
mcp/types.go (2)
JSONRPCNotification
(206-209)JSONRPCMessage
(89-89)
server/server_test.go (4)
server/server.go (2)
MCPServer
(126-151)ServerTool
(50-53)mcp/types.go (5)
JSONRPCNotification
(206-209)JSONRPCMessage
(89-89)MethodNotificationToolsListChanged
(62-62)JSONRPCResponse
(212-216)Result
(187-191)mcp/tools.go (3)
Tool
(69-80)NewTool
(161-183)ListToolsResult
(19-22)testdata/mockstdio_server.go (1)
JSONRPCResponse
(18-26)
🔇 Additional comments (6)
server/resource_test.go (2)
100-101
: Test name update clarifies intent.Updated test name clearly indicates that we're now also verifying notification behavior for non-existent resources, which aligns with the PR objective to avoid unnecessary notifications.
133-137
: Proper verification of absence of notifications.These changes correctly implement the PR objective by:
- Setting
expectedNotifications
to 0- Adding an explicit assertion to verify that no notifications are sent when attempting to remove a non-existent resource
This is consistent with the MCP specification which states that notifications should only be sent when the list actually changes.
server/server_test.go (1)
311-338
: Good additional test case for non-existent tools.This test correctly verifies that attempting to delete non-existent tools doesn't trigger unnecessary notifications, which aligns with the PR objective. The test:
- Sets up two existing tools with
SetTools
(generating one notification)- Attempts to delete two non-existent tools
- Verifies only the original notification from
SetTools
is received- Confirms the tool list remains unchanged with the two original tools
This is a good companion to the resource test update and ensures consistent behavior across both resources and tools.
server/server.go (3)
339-342
: Existence check before resource deletion.Good change to check if the resource exists before attempting to delete it. This allows tracking whether a deletion actually occurred, which is necessary to determine if a notification should be sent.
345-346
: Conditional notification for resource deletion.This change correctly implements the PR objective by sending notifications only when:
- The resource actually existed and was deleted (
exists
is true)- The resource capabilities are enabled
- The
listChanged
capability is enabledThis prevents unnecessary notifications when trying to delete non-existent resources.
464-464
: Conditional notification for tool deletion.This change correctly implements the PR objective by sending notifications only when:
- At least one tool actually existed and was deleted (
exists
is true)- The tool capabilities are enabled
- The
listChanged
capability is enabledThis prevents unnecessary notifications when trying to delete non-existent tools.
Motivation
While working on an MCPClient, I unintentionally sent
DeleteResource
request twice with the same URI. I observed that the MCPServer responded with tworesources/list_changed
notifications, even though the second deletion had no actual effect (the resource had already been removed).As a result, the client’s
NotificationHandler
was triggered twice, leading to redundant processing.This behavior highlighted that the server does not currently check whether a resource or tool actually exists before broadcasting a
list_changed
event.Problem
In the original implementation of
RemoveResource
(and similarlyDeleteTools
), the server always sends alist_changed
notification, regardless of whether the deletion modified the internal state.Although safe from a language standpoint (Go’s delete is a no-op for nonexistent keys), this can lead to:
Also, from the specification:
I think it could be better for MCPServer to send notifications to MCPClients when the ResourceList/ToolsList actually changes.
What This PR Changes
This PR updates
RemoveResource
andDeleteTools
to:list_changed
notification if the internal state was modified.Summary by CodeRabbit
Bug Fixes
Tests