Repository navigation
Add stdio support for EverythingServer sample (continues #950) - #1880
FengFafa97 wants to merge 17 commits into
Conversation
Co-authored-by: asklar <22989529+asklar@users.noreply.github.com>
Co-authored-by: asklar <22989529+asklar@users.noreply.github.com>
- EverythingServerExtensions.cs: SampleAsync's named parameter was renamed to chatOptions upstream; update the call site. - EverythingServer.Core.csproj: suppress MCP9005 (Sampling deprecated per SEP-2577), matching the sibling EverythingServer.Http.csproj. - EverythingServer.Http/Program.cs: suppress MCPEXP002 around the experimental RunSessionHandler assignment, matching the original Program.cs and docs/experimental.md. - launchSettings.json: remove trailing commas that broke JSON parsing and silently disabled both launch profiles. Verified: dotnet build succeeds with 0 warnings/errors, and both transports complete a live MCP initialize handshake (stdio and Streamable HTTP). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
|
Hi @jeffhandley — an update on #950: This PR has been rebased onto the current |
|
Hi @halter73 and @jeffhandley — this part is ready for review. This is the version @asklar split off from #950 and rebased onto the current |
|
Thanks for checking in and following up, @FengFafa97, and for picking up this work and resolving the rebase issues. The Core/HTTP/Stdio split matches the previously discussed direction, and I recommend keeping this PR moving forward. Specifically:
Since the protocol has evolved since the original proposal, @jeffhandley and @halter73, please confirm whether this PR should preserve the existing sample behavior only, or also update the HTTP sample for native Once this PR is completed and merged, we can close #950 as superseded. |
…om main - Core: move the ServerInfo (name/title/description/website/icons) and the multi-icon EchoTool registration from main's Program.cs into AddEverythingMcpHandlers so both the HTTP and stdio hosts expose them. - Http: restore SessionMode = Stateful (and its explanatory comment). The default is now Stateless, so without it sampling, resource subscriptions and RunSessionHandler silently lost their session. - Core: SubscriptionMessageSender sent "notifications/resource/updated"; the spec method is "notifications/resources/updated", so clients never received resource update notifications. Use NotificationMethods.ResourceUpdatedNotification. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks @tarekgh for the detailed review. I've pushed 91d1b4c (plus a merge of current
¹ The server rejects Two fixes came out of the validation:
On point 3, @jeffhandley @halter73: should this PR only preserve the existing behavior (stateful HTTP, with a note that the session-dependent demonstrations target Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com |
|
A few remaining items before this is ready:
|
|
I agree with keeping the protocol update separate. Can we avoid acknowledging resource subscriptions that won't receive updates? #1930 tracks the related SDK gap. |
|
Thanks, @halter73. I agree with your assessment. I will ensure that this PR does not alter existing behavior, document the protocol limitations in the README files for each example, and address the other pending review items. Regarding the issue where confirmation responses are received for resource subscriptions that fail to actually deliver updates: I have investigated this and reproduced the behavior in the latest commit (HEAD) of this PR. This confirmation response originates from the SDK’s built-in
{"notifications":{"resourceSubscriptions":["test://direct/text/resource"]},"_meta":{"io.modelcontextprotocol/subscriptionId":8}}Subsequently, no
The reason no messages are received over the
Consequently, the example cannot, on its own, prevent the inclusion of these subscriptions in the acknowledgement—unless the entire listen handler is replaced via I see two potential approaches:
I lean toward the first option, as the changes required are minimal; however, I do not want to expand the scope of this PR—which is focused on the example—without prior confirmation. Which approach do you prefer? |
- Subscribe handler: only sample when the client advertises sampling, and record the subscription after sampling, so a failed request no longer leaves behind a subscription that keeps receiving updates. - printEnv: return environment variable names only, since values can contain secrets and any connected client can call the tool. - Stdio: enable the OTLP exporter only when OTEL_EXPORTER_OTLP_ENDPOINT is set, avoiding a ~5 s flush delay on exit without a collector. - HTTP README: fix the project path and default ports, and document that the stateful server negotiates up to 2025-11-25. - Stdio README: document the 2026-07-28 limitations for resource subscriptions and logging/setLevel, and the OTLP opt-in.
|
Splits the
Changes beyond the split
Protocol versionsThis PR preserves the existing sample behavior; native
Open question: on stdio with Verification
¹ HTTP results are from the run before the latest commit (35694a2); a re-run is pending. Automated coverage for the sampling and subscription flows is not included yet; I'll add it to this PR next. Co-Authored-By: Claude Sonnet 5 |
There was a problem hiding this comment.
🟢 Approval recommended
There are no active review issues blocking approval.
No open issues 🎉
What changed in this PR
Splits the EverythingServer sample into shared handlers and separate HTTP and stdio hosts, addressing #889 and continuing #950.
Changes:
- Shares tools, prompts, resources, and handlers between both transports.
- Adds stdio hosting with stderr logging and optional telemetry export, while retaining stateful HTTP hosting.
- Documents transport compatibility, fixes launch settings, and limits environment output to variable names.
| File | Description |
|---|---|
| samples/EverythingServer/Tools/PrintEnvTool.cs | Removes the original environment tool. |
| samples/EverythingServer/Program.cs | Removes the monolithic host. |
| samples/EverythingServer.Stdio/README.md | Documents stdio setup and protocol compatibility. |
| samples/EverythingServer.Stdio/Program.cs | Adds stdio hosting and background services. |
| samples/EverythingServer.Stdio/EverythingServer.Stdio.csproj | Defines the stdio executable. |
| samples/EverythingServer.Http/README.md | Documents HTTP setup and session requirements. |
| samples/EverythingServer.Http/Properties/launchSettings.json | Removes invalid trailing commas. |
| samples/EverythingServer.Http/Program.cs | Hosts shared handlers with stateful HTTP sessions. |
| samples/EverythingServer.Http/EverythingServer.Http.csproj | References the shared core project. |
| samples/EverythingServer.Http/EverythingServer.http | Provides HTTP request examples. |
| samples/EverythingServer.Http/appsettings.json | Configures logging and allowed hosts. |
| samples/EverythingServer.Http/appsettings.Development.json | Provides development logging settings. |
| samples/EverythingServer.Core/Tools/TinyImageTool.cs | Moves the image tool into the shared namespace. |
| samples/EverythingServer.Core/Tools/SampleLlmTool.cs | Moves the sampling tool into the shared namespace. |
| samples/EverythingServer.Core/Tools/PrintEnvTool.cs | Returns environment names without values. |
| samples/EverythingServer.Core/Tools/LongRunningTool.cs | Shares the progress-reporting tool. |
| samples/EverythingServer.Core/Tools/EchoTool.cs | Shares the echo tool. |
| samples/EverythingServer.Core/Tools/AnnotatedMessageTool.cs | Shares the annotated-message tool. |
| samples/EverythingServer.Core/Tools/AddTool.cs | Shares the addition tool. |
| samples/EverythingServer.Core/SubscriptionMessageSender.cs | Shares subscription updates and corrects the notification method. |
| samples/EverythingServer.Core/Resources/SimpleResourceType.cs | Moves resources into the shared namespace. |
| samples/EverythingServer.Core/ResourceGenerator.cs | Moves resource generation into the shared namespace. |
| samples/EverythingServer.Core/README.md | Documents shared components and registration. |
| samples/EverythingServer.Core/Prompts/SimplePromptType.cs | Shares the simple prompt. |
| samples/EverythingServer.Core/Prompts/ComplexPromptType.cs | Shares the complex prompt and updates imports. |
| samples/EverythingServer.Core/LoggingUpdateMessageSender.cs | Shares periodic logging notifications. |
| samples/EverythingServer.Core/EverythingServerExtensions.cs | Centralizes metadata, primitives, and request handlers. |
| samples/EverythingServer.Core/EverythingServer.Core.csproj | Defines the shared library and sampling-warning suppression. |
| ModelContextProtocol.slnx | Registers the three replacement projects. |
Review effort: Balanced
|
Could you add a stdio-only incoming-message filter that removes well-formed Please add focused tests to confirm resource grants are withheld, list-change notifications and cancellation still work, malformed requests are rejected, and legacy subscriptions still receive updates. The README should explain the limitation and link to #1930. This approach withholds the grant in the acknowledgement but does not change the server capability. Would you be able to implement this sample-scoped fix? |
|
Could you add automated tests for the sampling and subscription flows? In particular, please cover subscribing without sampling capability, a sampling failure leaving no stale subscription, and the expected notification delivery for the supported protocol versions. Could you also rerun the HTTP validation on the latest commit and update the PR description and verification summary to reflect the current split, protocol limitations, and latest test results? The current PR description still describes the earlier state. |
|
Thanks @tarekgh, this is cleaner than either option in my earlier comment, so yes, I'll implement the stdio-only filter. @halter73, since you raised the acknowledgement issue: this would replace options 1 and 2. Let me know if you'd prefer something else. How the filter will work:
Tests:
There's no test coverage for the C# EverythingServer sample yet (the On the PR description: I posted the updated text as a comment instead of editing the description, sorry about that. I'll move it into the description, rerun the HTTP validation on the latest commit, and add the current verification results there. |
On 2026-07-28 the SDK's built-in subscriptions/listen handler acknowledges every requested resourceSubscriptions URI, but the sample can't deliver notifications/resources/updated on a listen stream yet (modelcontextprotocol#1930). - Core: add WithholdListenResourceSubscriptions, an incoming message filter that removes well-formed resourceSubscriptions from subscriptions/listen requests before the built-in handler runs, so the SDK still handles the acknowledgement, list-changed grants and cancellation. Malformed values are left for the SDK to reject. - Stdio: register the filter; HTTP and legacy resources/subscribe are unaffected. - Stdio README: document the limitation and link to modelcontextprotocol#1930. - Tests: compile EverythingServer.Core into ModelContextProtocol.Tests, as modelcontextprotocol#1993 does for SkillsServer, and cover withheld resource grants, list-changed delivery and cancellation, malformed requests, and legacy subscriptions still receiving updates. The tests are skipped where reflection-based serialization is disabled (net9.0), which the sample's tools rely on. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Cover the shared resources/subscribe handler with the subscription tracking the stdio host uses: - Without sampling capability, subscribing succeeds and the subscription receives notifications/resources/updated on every initialize-handshake protocol version (2024-11-05 through 2025-11-25). - On 2026-07-28, resources/subscribe is rejected with MethodNotFound and nothing is recorded. - With sampling, the handler samples once, then records the subscription. - When sampling fails, the request fails and no subscription is left behind. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@tarekgh I've pushed two commits for your two comments and updated the PR description and verification results.
A few things differ from what I described earlier:
I also checked that each test fails when the behavior it covers is broken, for example without the filter, with the subscription recorded before sampling, or with the old singular Side note, not changed here: the SDK returns |
| if (context.JsonRpcMessage is JsonRpcRequest { Method: RequestMethods.SubscriptionsListen, Params: JsonObject listenParams } && | ||
| listenParams["notifications"] is JsonObject notifications && | ||
| notifications["resourceSubscriptions"] is JsonArray uris && | ||
| uris.All(uri => uri?.GetValueKind() == JsonValueKind.String)) |
There was a problem hiding this comment.
Thanks for addressing the earlier feedback on telemetry, subscription handling, tests, and documentation. I found one remaining edge case in this filter: with resourceSubscriptions: [null, "test://direct/text/resource"], the null entry makes uri?.GetValueKind() == JsonValueKind.String false, so the filter leaves the entire property in place. The SDK currently accepts and acknowledges the valid URI in that array, even though the sample cannot deliver its updates. I reproduced this against the latest head; the focused regression test fails. Please explicitly reject null entries here, rather than relying on SDK validation, and add a test for a mixed null/string array.
Continues #950, fixes #889.
Splits the
EverythingServersample into three projects, picking up @asklar's work from #950:EverythingServer.Core: all shared MCP handlers, registered throughAddEverythingMcpHandlers, including theServerInfometadata (title, description, website, icons) and the multi-iconechotool from currentmain.EverythingServer.Http: ASP.NET Core host. Keepsmain'sSessionMode = HttpServerSessionMode.Stateful, which sampling and resource subscriptions depend on.EverythingServer.Stdio: generic host using the stdio transport.dnxand MCPB packaging are out of scope, as discussed in #950.Changes beyond the split
SubscriptionMessageSenderonmainsentnotifications/resource/updated(singular), which clients don't recognize. It now usesNotificationMethods.ResourceUpdatedNotification.subscriptions/listenon stdio: on2026-07-28, the SDK's built-in handler acknowledgedresourceSubscriptionsthat never receive updates (related to Route application-sentnotifications/resources/updatedto matchingsubscriptions/listenstreams #1930). As proposed by @tarekgh in this comment, the stdio host registersWithholdListenResourceSubscriptions, an incoming message filter that removes well-formedresourceSubscriptionsfromsubscriptions/listenrequests before the built-in handler sees them, so the SDK still handles the acknowledgement, list-changed grants and cancellation.server/discoverstill advertisesresources.subscribe.printEnv: returns environment variable names only, since values can contain secrets and any connected client can call the tool.OTEL_EXPORTER_OTLP_ENDPOINTis set, so the process exits promptly when no collector is running instead of after a ~5 s flush.SampleAsync's renamedchatOptionsparameter, theMCP9005suppression inEverythingServer.Core.csproj, theMCPEXP002pragma aroundRunSessionHandler, and trailing commas inlaunchSettings.json.Protocol versions
This PR preserves the existing sample behavior; native
2026-07-28support (MRTR,subscriptions/listen) is a separate scope. Both READMEs document the limits:2025-11-25and rejects2026-07-28.2026-07-28, butresources/subscribe,resources/unsubscribeandlogging/setLevelare rejected there, so those demonstrations need2025-11-25or earlier.subscriptions/listengrants list-changed notifications but notresourceSubscriptions(Route application-sentnotifications/resources/updatedto matchingsubscriptions/listenstreams #1930).Tests
New tests in
tests/ModelContextProtocol.Tests/Samples/run the sample's handlers over an in-memory stream.EverythingServer.Coretargets onlynet9.0, so, following #1993, its sources are compiled intoModelContextProtocol.Tests(except onnet472). The tests are skipped onnet9.0, where the test build disables the reflection-based serialization that the sample's tools rely on.EverythingServerListenFilterTests: resource grants are withheld; list-changed notifications are still delivered andnotifications/cancelledends the subscription; malformedresourceSubscriptionsare still rejected; legacyresources/subscribestill receives updates.EverythingServerSubscriptionTests: subscribing without sampling capability receives updates on2024-11-05through2025-11-25;resources/subscribeis rejected on2026-07-28and records nothing; with sampling, the handler samples once and then records the subscription; when sampling fails, no subscription is left behind.ModelContextProtocol.Testspasses onnet10.0,net9.0andnet8.0.Verification
All results are from the latest commit (a63e154).
dotnet build: 0 warnings, 0 errors. Manual checks with a throwaway client built against this branch (its sampling handler returns a canned reply):2025-11-252026-07-282025-11-25¹serverInfo+ icons,echotool iconssampleLLMtool)printEnvlists a probe variable's name but not its valueresources/subscribe→notifications/resources/updatedlogging/setLevel→notifications/messagesubscriptions/listenwithresourceSubscriptionsresourceSubscriptionsDELETE, old session id → 404¹ The stateful HTTP server negotiates up to
2025-11-25. A client requesting2026-07-28fails atinitialize, and the error lists the supported versions (2024-11-05to2025-11-25).² Each case starts its own stdio server process, so the run is stdio-only.
Co-Authored-By: Claude Sonnet 5