Skip to content

Notify MCP clients about shell location changes - #223

Merged
Mike Krüger (mkrueger) merged 7 commits into
mainfrom
dev/mkrueger/mcp-location-notifications
Oct 1, 2026
Merged

Mike Krüger (mkrueger) merged 7 commits into
mainfrom
dev/mkrueger/mcp-location-notifications

Conversation

@mkrueger

@mkrueger Mike Krüger (mkrueger) commented Sep 29, 2026 •

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": ..., "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 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.

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>
@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 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.
File main 5773b0a dev/mkrueger/mcp-loc... ad028e1 +/-
D:\a\CosmosDBSh...olOperations.cs 95% 95% 0%
D:\a\CosmosDBSh...ceOperations.cs 100% 100% 0%
D:\a\CosmosDBSh...lInterpreter.cs 71% 72% +1%
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 October 01, 2026 07:54 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.

- 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)
Comment thread docs/mcp.md Resolved
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings September 30, 2026 13:08

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

🔵 Needs a closer look

MCP SDK transport failures are swallowed, preventing the advertised pruning of failed subscriber sessions.

Review effort: Balanced
Findings: None

Resolved since last review (1)
Previously missed (1)

In code that hasn't changed since last review

Medium severity Stale sessions aren't removed when SSE notification writes fail

CosmosDBShell/​Azure.Data.Cosmos.Shell.Mcp/​LocationResourceSubscriptions.cs:82

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>
Copilot AI balanced review requested due to automatic review settings September 30, 2026 13:53

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

A rejected secondary GET can incorrectly remove an active session’s valid location subscription.

Review effort: Balanced
Findings: 1 High severity

Open (1)
This reverts commit 132f7e4.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings September 30, 2026 14:03

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

🔵 Needs a closer look

The integration test releases its reserved port before server startup, creating an intermittent port-allocation race.

Review effort: Balanced
Findings: None

Resolved since last review (1)
Previously missed (1)

In code that hasn't changed since last review

Medium severity Avoid port release race before Kestrel binds

CosmosDBShell.Tests/​McpLocationSubscriptionTests.cs:28

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.

Copilot AI balanced review requested due to automatic review settings October 1, 2026 07:49
@mkrueger
Mike Krüger (mkrueger) enabled auto-merge (squash) October 1, 2026 07:51

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

Disconnected HTTP sessions are not reliably removed from the subscription list because SDK notification writes swallow transport failures.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
@mkrueger
Mike Krüger (mkrueger) merged commit 231d3ca into main Oct 1, 2026
12 checks passed
@mkrueger
Mike Krüger (mkrueger) deleted the dev/mkrueger/mcp-location-notifications branch October 1, 2026 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants