You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Notify MCP clients about shell location changes - #223
MCP clients had no way to learn that the shared shell location changed between tool calls (for example when the user runs cd interactively). The location was only visible in the currentLocation field of the next tool response.
This PR exposes the navigation state as a subscribable MCP resource:
New resource cosmos://shell/current-location returning { "currentLocation": ..., "currentAccountEndpoint": ... }. currentLocation is null when disconnected, /, /db, or /db/container; currentAccountEndpoint is the connected account URL or null.
The server advertises resources.subscribe and handles resources/subscribe / resources/unsubscribe for that URI.
Subscribed sessions receive notifications/resources/updated whenever the location or connection changes, including interactive cd, connect, and disconnect. Clients re-read the resource for the new values.
Implementation notes
ShellInterpreter.LocationChanged fires from the State setter only when the location string or the underlying CosmosClient changes. cd's double assignment and ARM-context reattachment do not produce duplicate events. The handler only writes to a channel, so it never blocks under the execution lock.
LocationResourceSubscriptions (hosted service) tracks subscribed sessions via weak references.
Sessions whose notification send throws are dropped.
Pending changes are coalesced through a single-slot channel (DropWrite), since the notification carries only the URI. Scripts with many cd calls do not flood clients.
Subscribing to any other URI, including the documentation resources, returns an InvalidParamsMcpProtocolException with a readable message, not a generic internal error.
The MCP SDK in use (1.1.0) implements the 2025-11-25 subscription model (resources/subscribe). The 2026-07-28 subscriptions/listen flow is out of scope until the SDK supports it.
Docs
docs/mcp.md: new "Shell Location Updates" section.
README.md and ServerInstructions.md: short pointers.
Tests
McpLocationSubscriptionTests: a real MCP HTTP client subscribes, receives the notification after a state change, and reads the updated values; an invalid subscription URI yields InvalidParams.
ResourceOperationsTests for resource content and ShellLocationChangedTests for change detection, including client identity and ARM context changes.
Expose the shared shell navigation state as the MCP resource
cosmos://shell/current-location and support resources/subscribe for it.
Subscribed clients receive notifications/resources/updated whenever the
location or connection changes, including interactive cd, connect, and
disconnect, and can re-read the resource for the new value.
- Raise ShellInterpreter.LocationChanged only when the location or the
underlying CosmosClient actually changes.
- Coalesce pending notifications through a single-slot channel.
- Reject subscriptions to other URIs with an InvalidParams protocol error.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The overall line coverage in commit ad028e1 in the dev/mkrueger/mcp-loc... branch is 65%. The line coverage in commit 5773b0a in the main branch is 64%.
Show a line coverage summary of the most impacted files.
- Dispose the port-probe TcpListener in the subscription test.
- Limit the per-subscriber catch to the running state via an exception
filter so a failed notification only drops that subscriber.
- Cover client-identity change detection: a new client at the same
location notifies; a changed ARM context with the same client does not.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This catch does not remove sessions when the HTTP notification write fails. With the pinned MCP SDK 1.1.0, StreamableHttpServerTransport.SendMessageAsync catches SSE WriteAsync failures internally and completes the GET response task exceptionally without rethrowing, so SendNotificationAsync returns normally and the dead session remains in this list (potentially until the SDK's two-hour idle timeout). Repeated location changes can therefore keep retrying every stale session, contrary to the PR's stated failure-pruning behavior. Please tie removal to the MCP session/transport lifecycle or another failure signal that the SDK actually exposes.
MCP SDK 1.1.0 accepts a single notification (GET) stream per session
and swallows write failures on it, so a failed notification never
surfaced and stale subscribers were kept until the session idled out.
Once that stream ends, the session cannot receive notifications again,
so drop its subscriptions when the GET request completes.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This releases the OS-assigned port before Kestrel binds it, leaving a TOCTOU race where another process can claim the port and make the test fail intermittently. Please start Kestrel on port 0 and obtain its bound loopback address from IServerAddressesFeature after StartAsync, rather than reserving and releasing a port first.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Summary
MCP clients had no way to learn that the shared shell location changed between tool calls (for example when the user runs
cdinteractively). The location was only visible in thecurrentLocationfield of the next tool response.This PR exposes the navigation state as a subscribable MCP resource:
cosmos://shell/current-locationreturning{ "currentLocation": ..., "currentAccountEndpoint": ... }.currentLocationisnullwhen disconnected,/,/db, or/db/container;currentAccountEndpointis the connected account URL ornull.resources.subscribeand handlesresources/subscribe/resources/unsubscribefor that URI.notifications/resources/updatedwhenever the location or connection changes, including interactivecd,connect, anddisconnect. Clients re-read the resource for the new values.Implementation notes
ShellInterpreter.LocationChangedfires from theStatesetter only when the location string or the underlyingCosmosClientchanges.cd's double assignment and ARM-context reattachment do not produce duplicate events. The handler only writes to a channel, so it never blocks under the execution lock.LocationResourceSubscriptions(hosted service) tracks subscribed sessions via weak references.DropWrite), since the notification carries only the URI. Scripts with manycdcalls do not flood clients.InvalidParamsMcpProtocolExceptionwith a readable message, not a generic internal error.resources/subscribe). The 2026-07-28subscriptions/listenflow is out of scope until the SDK supports it.Docs
docs/mcp.md: new "Shell Location Updates" section.README.mdandServerInstructions.md: short pointers.Tests
McpLocationSubscriptionTests: a real MCP HTTP client subscribes, receives the notification after a state change, and reads the updated values; an invalid subscription URI yieldsInvalidParams.ResourceOperationsTestsfor resource content andShellLocationChangedTestsfor change detection, including client identity and ARM context changes.