Skip to content

Commit 359a355

Browse files
author
Tarek Mahmoud Sayed
committed
Merge main into protocol negotiation branch
Reconcile with PR #1685 (request-scoped draft client capabilities), now in main. Keep the request-scoped resolution and drop the shared _clientCapabilities write to preserve the #1670 race fix, while retaining this branch's protocol negotiation boundary logic (ValidateRequestMethodBoundary, required per-request metadata validation) and the McpProtocolVersions helper naming. Update the UrlElicitationTests helper to reference McpProtocolVersions.November2025ProtocolVersion.
2 parents 42629f0 + a8d374b commit 359a355

5 files changed

Lines changed: 305 additions & 39 deletions

File tree

‎src/ModelContextProtocol.Core/Server/DestinationBoundMcpServer.cs‎

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,66 @@
55
namespace ModelContextProtocol.Server;
66

77
#pragma warning disable MCPEXP002
8-
internal sealed class DestinationBoundMcpServer(McpServerImpl server, ITransport? transport) : McpServer
8+
internal sealed class DestinationBoundMcpServer(McpServerImpl server, ITransport? transport, JsonRpcMessageContext? requestContext = null) : McpServer
99
#pragma warning restore MCPEXP002
1010
{
11+
private readonly bool _isJuly2026OrLaterRequest = server.IsJuly2026OrLaterProtocolRequest(requestContext);
12+
private readonly ClientCapabilities? _requestClientCapabilities = requestContext?.ClientCapabilities;
13+
private readonly Implementation? _requestClientInfo = requestContext?.ClientInfo;
14+
1115
public override string? SessionId => transport?.SessionId ?? server.SessionId;
1216
public override string? NegotiatedProtocolVersion => server.NegotiatedProtocolVersion;
13-
public override ClientCapabilities? ClientCapabilities => server.ClientCapabilities;
14-
public override Implementation? ClientInfo => server.ClientInfo;
1517
public override McpServerOptions ServerOptions => server.ServerOptions;
1618
public override IServiceProvider? Services => server.Services;
1719
[Obsolete(Obsoletions.DeprecatedLogging_Message, DiagnosticId = Obsoletions.Deprecated_DiagnosticId, UrlFormat = Obsoletions.Deprecated_Url)]
1820
public override LoggingLevel? LoggingLevel => server.LoggingLevel;
1921

22+
public override ClientCapabilities? ClientCapabilities
23+
{
24+
get
25+
{
26+
// In stateless transport mode, a single request does not have a persistent bidirectional channel.
27+
// Server-to-client requests (sampling, roots, elicitation) are unsupported in this mode and the
28+
// capability gates rely on a null ClientCapabilities value to report that unsupported-state path.
29+
if (!server.HasStatefulTransport())
30+
{
31+
return null;
32+
}
33+
34+
// On protocol revision 2026-07-28+, client capabilities are request-scoped (_meta on each request)
35+
// and must not be inferred from prior requests. Missing per-request capabilities therefore means
36+
// "no declared capabilities for this request", represented by an empty object. A fresh instance is
37+
// returned deliberately: ClientCapabilities is a mutable DTO handed to user handlers, so a shared
38+
// static empty instance could be mutated and leak across requests.
39+
if (_isJuly2026OrLaterRequest)
40+
{
41+
return _requestClientCapabilities ?? new ClientCapabilities();
42+
}
43+
44+
// Legacy protocol behavior uses session-scoped capabilities established during initialize (or
45+
// pre-populated migration data), so ignore per-request values and return the server session state.
46+
return server.ClientCapabilities;
47+
}
48+
}
49+
50+
public override Implementation? ClientInfo
51+
{
52+
get
53+
{
54+
// On protocol revision 2026-07-28+, client info is request-scoped (carried in each request's _meta),
55+
// mirroring how ClientCapabilities is resolved above. Return only this request's declared value and
56+
// do not fall back to shared session state, which under a stateful transport could belong to a
57+
// different concurrent request.
58+
if (_isJuly2026OrLaterRequest)
59+
{
60+
return _requestClientInfo;
61+
}
62+
63+
// Legacy protocol behavior uses session-scoped client info established during initialize.
64+
return server.ClientInfo;
65+
}
66+
}
67+
2068
/// <summary>
2169
/// Gets or sets the MRTR context for the current request, if any.
2270
/// Set by <see cref="McpServerImpl.CreateDestinationBoundServer"/> when an MRTR-aware handler invocation is in progress.

‎src/ModelContextProtocol.Core/Server/McpServer.cs‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,19 @@ protected McpServer()
2121
/// </summary>
2222
/// <remarks>
2323
/// <para>
24-
/// These capabilities are established during the initialization handshake and indicate
25-
/// which features the client supports, such as sampling, roots, and other
26-
/// protocol-specific functionality.
24+
/// On protocol revisions that use the <c>initialize</c> handshake (<c>2025-11-25</c> and earlier), these
25+
/// capabilities are established once during initialization and are session-scoped: they are available both
26+
/// on the root <see cref="McpServer"/> and on the server exposed to request handlers.
27+
/// </para>
28+
/// <para>
29+
/// On the <c>2026-07-28</c> revision and later (SEP-2575) there is no <c>initialize</c> handshake; the client
30+
/// declares its capabilities per-request in <c>_meta</c>, and the server MUST NOT infer them from previous
31+
/// requests. In that mode this property is only meaningful on the request-scoped server accessed via
32+
/// the <c>Server</c> property of the <see cref="RequestContext{TParams}"/> passed to a handler; on the
33+
/// root <see cref="McpServer"/> (for example one constructed manually over a
34+
/// <see cref="System.IO.Stream"/>) it is <see langword="null"/>.
35+
/// It is also <see langword="null"/> in stateless transport mode, where server-to-client requests are
36+
/// unsupported.
2737
/// </para>
2838
/// <para>
2939
/// Server implementations can check these capabilities to determine which features
@@ -38,7 +48,14 @@ protected McpServer()
3848
/// <remarks>
3949
/// <para>
4050
/// This property contains identification information about the client that has connected to this server,
41-
/// including its name and version. This information is provided by the client during initialization.
51+
/// including its name and version.
52+
/// </para>
53+
/// <para>
54+
/// On protocol revisions that use the <c>initialize</c> handshake (<c>2025-11-25</c> and earlier) this
55+
/// information is provided once during initialization and is session-scoped. On the <c>2026-07-28</c>
56+
/// revision and later it is carried per-request in <c>_meta</c>, so read it from the request-scoped server
57+
/// accessed via the <c>Server</c> property of the <see cref="RequestContext{TParams}"/> passed to a handler
58+
/// rather than from the root <see cref="McpServer"/>.
4259
/// </para>
4360
/// <para>
4461
/// Server implementations can use this information for logging, tracking client versions,

‎src/ModelContextProtocol.Core/Server/McpServerImpl.cs‎

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,16 @@ void Register<TPrimitive>(McpServerPrimitiveCollection<TPrimitive>? collection,
166166

167167
/// <summary>
168168
/// Wraps <paramref name="inner"/> so that, for every JSON-RPC request, a built-in filter first
169-
/// synchronizes server-side state (<see cref="_negotiatedProtocolVersion"/>,
170-
/// <see cref="_clientCapabilities"/>, <see cref="_clientInfo"/>) from the per-request <c>_meta</c>
171-
/// values projected onto <see cref="JsonRpcMessageContext"/> and validates the per-request protocol
172-
/// version, before delegating to the user-supplied incoming filters.
169+
/// synchronizes server-side state (<see cref="_negotiatedProtocolVersion"/>, <see cref="_clientInfo"/>)
170+
/// from the per-request <c>_meta</c> values projected onto <see cref="JsonRpcMessageContext"/> and
171+
/// validates the per-request protocol version, before delegating to the user-supplied incoming filters.
173172
/// </summary>
174173
/// <remarks>
175174
/// Under the 2026-07-28 protocol revision (SEP-2575) there is no <c>initialize</c> handshake, so these values
176-
/// MUST be populated per-request. For initialize-handshake clients the per-request values are absent and the built-in
175+
/// MUST be populated per-request. Per-request client capabilities and client info are consumed request-scoped
176+
/// by <see cref="DestinationBoundMcpServer"/> and are not read from server-wide state by request handlers. The
177+
/// shared <see cref="_clientInfo"/> write below is best-effort and used only to derive the session endpoint
178+
/// name for logging/telemetry. For initialize-handshake clients the per-request values are absent and the built-in
177179
/// filter is a no-op (the values were captured during the initialize handler).
178180
/// </remarks>
179181
private JsonRpcMessageFilter PrependMetaReadingFilter(JsonRpcMessageFilter inner)
@@ -259,23 +261,16 @@ private JsonRpcMessageFilter PrependMetaReadingFilter(JsonRpcMessageFilter inner
259261

260262
ValidateRequestMethodBoundary(request);
261263

262-
if (context?.ClientCapabilities is { } clientCapabilities && IsJuly2026OrLaterProtocol() && HasStatefulTransport())
263-
{
264-
// Under the 2026-07-28 revision the per-request _meta envelope carries the client's FULL
265-
// capabilities (SEP-2575), so a plain overwrite is correct. The IsJuly2026OrLaterProtocol() gate
266-
// makes any initialize-handshake per-request envelope a no-op (capabilities stay as the
267-
// initialize handshake established them); the HasStatefulTransport() gate keeps
268-
// _clientCapabilities null under StreamableHttpServerTransport { Stateless = true }
269-
// (where the same server instance handles every request, so persisting per-request
270-
// capability state would both leak across requests and break the StatelessServerTests
271-
// invariant that surfaces the "X is not supported in stateless mode" errors).
272-
_clientCapabilities = clientCapabilities;
273-
}
274-
275264
if (context?.ClientInfo is { } clientInfo &&
276265
(_clientInfo is null || !string.Equals(_clientInfo.Name, clientInfo.Name, StringComparison.Ordinal) ||
277266
!string.Equals(_clientInfo.Version, clientInfo.Version, StringComparison.Ordinal)))
278267
{
268+
// This shared write is best-effort and used only to derive the session endpoint name for
269+
// logging/telemetry. It is intentionally NOT read by request handlers on 2026-07-28+ sessions:
270+
// DestinationBoundMcpServer resolves ClientInfo (and ClientCapabilities) request-scoped from
271+
// the per-request _meta so concurrent requests never observe each other's values. Under a
272+
// draft stateful session with differing per-request client info, the last writer wins here,
273+
// which only affects the logged endpoint name and never the request-scoped values handlers see.
279274
_clientInfo = clientInfo;
280275
endpointNameNeedsRefresh = true;
281276
}
@@ -1884,7 +1879,7 @@ async ValueTask<TResult> InvokeScopedAsync(
18841879

18851880
private DestinationBoundMcpServer CreateDestinationBoundServer(JsonRpcRequest jsonRpcRequest)
18861881
{
1887-
var server = new DestinationBoundMcpServer(this, jsonRpcRequest.Context?.RelatedTransport);
1882+
var server = new DestinationBoundMcpServer(this, jsonRpcRequest.Context?.RelatedTransport, jsonRpcRequest.Context);
18881883

18891884
if (_mrtrContextsByRequestId.TryRemove(jsonRpcRequest.Id, out var mrtrContext))
18901885
{
@@ -1997,7 +1992,7 @@ private JsonRpcMessageFilter BuildMessageFilterPipeline(IList<McpMessageFilter>
19971992
{
19981993
// Ensure message has a Context so Items can be shared through the pipeline
19991994
message.Context ??= new();
2000-
var context = new MessageContext(new DestinationBoundMcpServer(this, message.Context.RelatedTransport), message);
1995+
var context = new MessageContext(new DestinationBoundMcpServer(this, message.Context.RelatedTransport, message.Context), message);
20011996
await current(context, cancellationToken).ConfigureAwait(false);
20021997
};
20031998
};
@@ -2052,8 +2047,12 @@ internal bool HasStatefulTransport() =>
20522047
/// Used to gate the SEP-2663 Tasks extension, which only interoperates on the 2026-07-28 revision.
20532048
/// </summary>
20542049
private bool IsJuly2026OrLaterProtocolRequest(JsonRpcRequest? request) =>
2050+
IsJuly2026OrLaterProtocolRequest(request?.Context);
2051+
2052+
/// <inheritdoc cref="IsJuly2026OrLaterProtocolRequest(JsonRpcRequest?)"/>
2053+
internal bool IsJuly2026OrLaterProtocolRequest(JsonRpcMessageContext? requestContext) =>
20552054
McpProtocolVersions.IsJuly2026OrLaterProtocolVersion(
2056-
request?.Context?.ProtocolVersion ?? NegotiatedProtocolVersion);
2055+
requestContext?.ProtocolVersion ?? NegotiatedProtocolVersion);
20572056

20582057
/// <inheritdoc />
20592058
public override bool IsMrtrSupported => ClientSupportsMrtr() || HasStatefulTransport();

0 commit comments

Comments
 (0)