Skip to content

Notify MCP clients about shell location changes - #223

Open
Mike Krüger (mkrueger) wants to merge 2 commits into
mainfrom
dev/mkrueger/mcp-location-notifications
Open

Mike Krüger (mkrueger) wants to merge 2 commits into
mainfrom
dev/mkrueger/mcp-location-notifications

Conversation

@mkrueger

Copy link
Copy Markdown
Collaborator

Summary

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": ... } (null when disconnected, /, /db, or /db/container).
  • 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 value.

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 and drops sessions whose notification fails.
  • 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 InvalidParams McpProtocolException 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.

Testing

  • New end-to-end test (McpLocationSubscriptionTests): a real MCP HTTP client subscribes, receives the notification after a state change, and reads the updated value. It also checks that an invalid subscription URI yields InvalidParams.
  • Unit tests for resource content (ResourceOperationsTests) and change detection (ShellLocationChangedTests).
  • Full suite passes locally: 3145 passed, 0 failed, 101 skipped (emulator integration tests).

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>
Comment thread CosmosDBShell.Tests/McpLocationSubscriptionTests.cs Fixed
@github-code-quality

github-code-quality Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Code Coverage Overview

Languages: C#

C# / code-coverage/dotnet

The overall line coverage in commit 2a7424b in the dev/mkrueger/mcp-loc... branch is 65%. The line coverage in commit 345ddb8 in the main branch is 64%.

Show a line coverage summary of the most impacted files.
File main 345ddb8 dev/mkrueger/mcp-loc... 2a7424b +/-
D:\a\CosmosDBSh...lInterpreter.cs 69% 69% 0%
D:\a\CosmosDBSh...olOperations.cs 93% 93% 0%
D:\a\CosmosDBSh...ceOperations.cs 100% 100% 0%
D:\a\CosmosDBSh...onMiddleware.cs 63% 92% +29%
D:\a\CosmosDBSh...ubscriptions.cs 0% 84% +84%
D:\a\CosmosDBSh...cp\McpServer.cs 0% 100% +100%

Updated September 29, 2026 11:53 UTC

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

The reconnect and ARM-reattachment change-detection branch lacks regression coverage.

Review effort: Balanced
Findings: 1 Low severity

Open (1)
What changed in this PR

Adds a subscribable MCP resource that reports shared shell location changes.

Changes:

  • Exposes cosmos://shell/current-location.
  • Tracks subscriptions and publishes coalesced update notifications.
  • Adds documentation and MCP integration tests.
File Description
README.md Links to location subscription documentation.
docs/​mcp.md Documents resource and subscription behavior.
ToolOperations.cs Handles subscribe and unsubscribe requests.
ServerInstructions.md Informs clients about the resource.
ResourceOperations.cs Implements the location resource.
McpServer.cs Registers subscriptions and advertises capability.
LocationResourceSubscriptions.cs Tracks clients and sends notifications.
ShellInterpreter.cs Emits location and connection change events.
ToolOperationsCallToolTests.cs Updates test construction and cleanup.
ShellLocationChangedTests.cs Tests basic location-change detection.
ResourceOperationsTests.cs Tests resource JSON content.
McpLocationSubscriptionTests.cs Tests end-to-end subscriptions.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread CosmosDBShell/Azure.Data.Cosmos.Shell.Core/ShellInterpreter.cs
- 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>
Copilot AI review requested due to automatic review settings September 29, 2026 11:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

The implementation, documentation, lifecycle handling, and automated coverage align with the stated MCP subscription behavior.

Review effort: Balanced
Findings: 1 Low severity

Open (1)

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