Add subscribe to resource functionality #170
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When the connected MCP server's capabilities include resource subscriptions:
ServerNotificationSchema
messages to be handled.Incidental out-of band update: Jetbrains IDE falls into a loop prompting for
npm install
to be run because these versions were not up to date."@modelcontextprotocol/inspector-client": "^0.5.1"
"@modelcontextprotocol/inspector-server": "^0.5.1"
This fixes #166
Motivation and Context
Dynamic resources can change over time and the protocol specifies a way for clients to subscribe to individual resources so they can be updated when those resources change.
However the existing reference client - the MCP Inspector - does not provide any affordance for the user to subscribe to a resource. It provides a way to list resources and read resources.
Implementation
There is a panel for displaying the selected resource, and that panel's header already contained a Refresh button. This PR adds a Subscribe or Unsubscribe button there, but only if the server supports resource subscriptions.
When clicked, it passes the selected resource's URI to the subscribe or unsubscribe handler, which in turn sends the appropriate message to the server.
A slight modification was made to
useConnection
andnotificationTypes
to support handlingServerNotificationSchema
messages, a union of whichResourceUpdatedNotificationSchema
is a member.How Has This Been Tested?
Button placement
The buttons are in a right justified div that is 2/5 wide in the panel header. Just large enough to contain the buttons, leaving 3/5 width to the heading without overlap.
Server capabilities
I have an SSE-based MCP server called puzzlebox. It hosts finite state machines (called puzzles) and allows clients to perform actions on them to change their states. The puzzles are exposed as dynamic resources with a resource template. They could already be listed and read, but there was no way to test that clients could subscribe to individual puzzles for updates. Puzzlebox provided the perfect test server for this functionality, since I had handy local access to both projects for troubleshooting.
Test 1 - Check for no subscription support on server
1A) Turn OFF subscriptions on the server
1B) Observe NO subscription button on selected resource
Test 2 - Check for subscription support on server
2A) Turn ON subscriptions on the server
2B) Observe subscription button on selected resource
Test 3 - Test outgoing requests and appropriate button display
3A) Click Subscribe button, observe Unsubscribe button
3B) Observe subscribe request
3C) Click Unsubscribe button, observe Subscribe button again
3D) Observe unsubscribe request
Test 4 - Check notifications when subscribed resource changes
4A) Use
perform_action_on_puzzle
tool to change subscribed puzzle's state4B) Observe resource updated notification from server
Test 5 - Check notifications no longer sent when unsubscribed resource changes
5A) Click Unsubscribe button on same puzzle,
5B) Use
perform_action_on_puzzle
tool to change unsubscribed puzzle's state, observe NO new server notificationBreaking Changes
Nope.
Types of changes
Checklist
Additional context