Skip to content

Commit e024906

Browse files
author
tarekgh
committed
Merge branch 'main' into paulc/sep-2575-pr3002-alignment
2 parents 0ebe97e + 6787c0c commit e024906

19 files changed

Lines changed: 579 additions & 67 deletions

File tree

docs/list-of-diagnostics.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ When APIs are marked as obsolete, a diagnostic is emitted to warn users that the
4545
| `MCP9004` | In place | <xref:ModelContextProtocol.AspNetCore.HttpServerTransportOptions.EnableLegacySse> opts into the legacy SSE transport which has no built-in HTTP-level backpressure. Use Streamable HTTP instead. See [Stateless — Legacy SSE transport](xref:stateless#legacy-sse-transport) for details. |
4646
| `MCP9005` | In place | The Roots, Sampling, and Logging features are deprecated as of specification version 2026-07-28 and may be removed in a future version. See SEP-2577 for more information. |
4747
| `MCP9006` | In place | The stateful Streamable HTTP configuration knobs on <xref:ModelContextProtocol.AspNetCore.HttpServerTransportOptions>`EventStreamStore`, `SessionMigrationHandler`, `PerSessionExecutionContext`, `IdleTimeout`, and `MaxIdleSessionCount` — only apply when `Stateless = false`. Starting with the `2026-07-28` protocol revision, Streamable HTTP no longer supports sessions, and the SDK now defaults `Stateless` to `true`. These knobs remain available for back-compat with the legacy stateful Streamable HTTP transport but new code should target the stateless path. |
48-
| `MCP9007` | In place | `AuthorizationRedirectDelegate` and `ClientOAuthOptions.AuthorizationRedirectDelegate` are retained for source and binary compatibility but cannot provide the RFC 9207 authorization-response issuer. Use `ClientOAuthOptions.AuthorizationCallbackHandler` for issuer-aware authorization flows. |
48+
| `MCP9007` | In place | `AuthorizationRedirectDelegate` and `ClientOAuthOptions.AuthorizationRedirectDelegate` are retained for source and binary compatibility but cannot provide the authorization-response state or RFC 9207 issuer. State and issuer validation are skipped when these APIs are used. Use `ClientOAuthOptions.AuthorizationCallbackHandler` for response-bound, issuer-aware authorization flows. |

samples/ProtectedMcpClient/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
var context = await listener.GetContextAsync();
9999
var query = HttpUtility.ParseQueryString(context.Request.Url?.Query ?? string.Empty);
100100
var code = query["code"];
101+
var state = query["state"];
101102
var iss = query["iss"];
102103
var error = query["error"];
103104

@@ -121,7 +122,7 @@
121122
}
122123

123124
Console.WriteLine("Authorization code received successfully.");
124-
return new AuthorizationResult { Code = code, Iss = iss };
125+
return new AuthorizationResult { Code = code, State = state, Iss = iss };
125126
}
126127
catch (Exception ex)
127128
{

src/ModelContextProtocol.Core/Authentication/AuthorizationResult.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
namespace ModelContextProtocol.Authentication;
22

33
/// <summary>
4-
/// Represents the result of an OAuth authorization redirect, containing the authorization code
5-
/// and optionally the issuer identifier from the authorization response.
4+
/// Represents the result of an OAuth authorization redirect, containing the authorization code,
5+
/// state, and optionally the issuer identifier from the authorization response.
66
/// </summary>
77
/// <remarks>
88
/// <para>
9+
/// The <see cref="State"/> property must be populated from the <c>state</c> query parameter in the
10+
/// redirect URI. The SDK validates it against the value sent in the authorization request to bind
11+
/// the response to the initiating transaction and mitigate cross-site request forgery attacks.
12+
/// </para>
13+
/// <para>
914
/// The <see cref="Iss"/> property should be populated from the <c>iss</c> query parameter in the
1015
/// redirect URI when present, as specified by
1116
/// <see href="https://datatracker.ietf.org/doc/html/rfc9207">RFC 9207</see>.
@@ -20,6 +25,16 @@ public sealed class AuthorizationResult
2025
/// </summary>
2126
public string? Code { get; init; }
2227

28+
/// <summary>
29+
/// Gets the state value returned in the authorization response.
30+
/// </summary>
31+
/// <remarks>
32+
/// Implementations of <see cref="ClientOAuthOptions.AuthorizationCallbackHandler"/> must populate this
33+
/// property from the <c>state</c> query parameter of the redirect URI callback. The SDK requires an
34+
/// exact match with the state sent in the authorization request before exchanging the authorization code.
35+
/// </remarks>
36+
public string? State { get; init; }
37+
2338
/// <summary>
2439
/// Gets the issuer identifier returned in the authorization response per
2540
/// <see href="https://datatracker.ietf.org/doc/html/rfc9207">RFC 9207</see>.

src/ModelContextProtocol.Core/Authentication/ClientOAuthOptions.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ public sealed class ClientOAuthOptions
8080
/// </para>
8181
/// <para>
8282
/// Custom implementations might open a browser, start an HTTP listener, or use other mechanisms to capture
83-
/// the authorization response. They should return both the <c>code</c> and <c>iss</c> query parameters
84-
/// from the redirect URI callback. This enables the SDK to validate the <c>iss</c> parameter per
83+
/// the authorization response. They must return the <c>code</c> and <c>state</c> query parameters,
84+
/// and should return the <c>iss</c> query parameter when present, from the redirect URI callback.
85+
/// The SDK requires an exact state match before exchanging the code. Returning <c>iss</c> enables
86+
/// the SDK to validate the parameter per
8587
/// <see href="https://datatracker.ietf.org/doc/html/rfc9207">RFC 9207</see>, which mitigates
8688
/// mix-up attacks.
8789
/// </para>
@@ -96,9 +98,10 @@ public sealed class ClientOAuthOptions
9698
/// </summary>
9799
/// <remarks>
98100
/// <para>
99-
/// This delegate returns only the authorization code and cannot provide the <c>iss</c> parameter from
100-
/// the authorization response. Consequently, RFC 9207 issuer validation is skipped when this delegate
101-
/// is used. Use <see cref="AuthorizationCallbackHandler"/> for issuer-aware authorization flows.
101+
/// This delegate returns only the authorization code and cannot provide the <c>state</c> or <c>iss</c>
102+
/// parameter from the authorization response. Consequently, state and RFC 9207 issuer validation are
103+
/// skipped when this delegate is used. Use <see cref="AuthorizationCallbackHandler"/> for response-bound,
104+
/// issuer-aware authorization flows.
102105
/// </para>
103106
/// <para>
104107
/// This property cannot be configured together with <see cref="AuthorizationCallbackHandler"/>.
@@ -139,8 +142,9 @@ public sealed class ClientOAuthOptions
139142
/// </summary>
140143
/// <remarks>
141144
/// <para>
142-
/// Parameters specified cannot override or append to any automatically set parameters like the "redirect_uri",
143-
/// which should instead be configured via <see cref="RedirectUri"/>.
145+
/// Parameters specified cannot override or append to any automatically set parameters like
146+
/// <c>redirect_uri</c> or <c>state</c>. The redirect URI should instead be configured via
147+
/// <see cref="RedirectUri"/>, while state is generated uniquely for each authorization transaction.
144148
/// </para>
145149
/// </remarks>
146150
public IDictionary<string, string> AdditionalAuthorizationParameters { get; set; } = new Dictionary<string, string>();

src/ModelContextProtocol.Core/Authentication/ClientOAuthProvider.cs

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ internal sealed partial class ClientOAuthProvider : McpHttpClient
3232
private readonly IDictionary<string, string> _additionalAuthorizationParameters;
3333
private readonly Func<IReadOnlyList<Uri>, Uri?> _authServerSelector;
3434
private readonly Func<AuthorizationCallbackContext, CancellationToken, Task<AuthorizationResult?>> _authorizationCallbackHandler;
35+
private readonly bool _validateAuthorizationResponseState;
3536
private readonly bool _validateAuthorizationResponseIssuer;
3637
private readonly Uri? _clientMetadataDocumentUri;
3738

@@ -120,6 +121,7 @@ public ClientOAuthProvider(
120121
if (options.AuthorizationCallbackHandler is not null)
121122
{
122123
_authorizationCallbackHandler = options.AuthorizationCallbackHandler;
124+
_validateAuthorizationResponseState = true;
123125
_validateAuthorizationResponseIssuer = true;
124126
}
125127
else if (authorizationRedirectDelegate is not null)
@@ -131,11 +133,13 @@ public ClientOAuthProvider(
131133
context.RedirectUri,
132134
cancellationToken).ConfigureAwait(false),
133135
};
136+
_validateAuthorizationResponseState = false;
134137
_validateAuthorizationResponseIssuer = false;
135138
}
136139
else
137140
{
138141
_authorizationCallbackHandler = DefaultAuthorizationUrlHandler;
142+
_validateAuthorizationResponseState = true;
139143
_validateAuthorizationResponseIssuer = true;
140144
}
141145

@@ -155,11 +159,11 @@ public ClientOAuthProvider(
155159
private static Uri? DefaultAuthServerSelector(IReadOnlyList<Uri> availableServers) => availableServers.FirstOrDefault();
156160

157161
/// <summary>
158-
/// Default authorization URL handler that displays the URL to the user for manual input.
162+
/// Default authorization URL handler that displays the URL to the user and parses the resulting redirect URL.
159163
/// </summary>
160164
/// <param name="context">The context containing the authorization and redirect URIs.</param>
161165
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests.</param>
162-
/// <returns>The authorization result entered by the user, or null if none was provided.</returns>
166+
/// <returns>The authorization result parsed from the redirect URL.</returns>
163167
private static Task<AuthorizationResult?> DefaultAuthorizationUrlHandler(
164168
AuthorizationCallbackContext context,
165169
CancellationToken cancellationToken)
@@ -179,6 +183,7 @@ public ClientOAuthProvider(
179183
return Task.FromResult<AuthorizationResult?>(new()
180184
{
181185
Code = queryParams["code"],
186+
State = queryParams["state"],
182187
Iss = queryParams["iss"],
183188
});
184189
}
@@ -687,10 +692,11 @@ private async Task<string> InitiateAuthorizationCodeFlowAsync(
687692
AuthorizationServerMetadata authServerMetadata,
688693
CancellationToken cancellationToken)
689694
{
690-
var codeVerifier = GenerateCodeVerifier();
695+
var codeVerifier = GenerateRandomBase64UrlValue();
691696
var codeChallenge = GenerateCodeChallenge(codeVerifier);
697+
var state = GenerateRandomBase64UrlValue();
692698

693-
var authUrl = BuildAuthorizationUrl(protectedResourceMetadata, authServerMetadata, codeChallenge);
699+
var authUrl = BuildAuthorizationUrl(protectedResourceMetadata, authServerMetadata, codeChallenge, state);
694700

695701
var authResult = await _authorizationCallbackHandler(
696702
new AuthorizationCallbackContext
@@ -700,9 +706,19 @@ private async Task<string> InitiateAuthorizationCodeFlowAsync(
700706
},
701707
cancellationToken).ConfigureAwait(false);
702708

703-
if (authResult is null || string.IsNullOrEmpty(authResult.Code))
709+
if (authResult is null)
704710
{
705-
ThrowFailedToHandleUnauthorizedResponse($"The {nameof(ClientOAuthOptions.AuthorizationCallbackHandler)} returned a null or empty authorization code.");
711+
ThrowFailedToHandleUnauthorizedResponse($"The {nameof(ClientOAuthOptions.AuthorizationCallbackHandler)} returned a null authorization result.");
712+
}
713+
714+
if (_validateAuthorizationResponseState)
715+
{
716+
ValidateStateResponse(authResult!.State, state);
717+
}
718+
719+
if (string.IsNullOrEmpty(authResult.Code))
720+
{
721+
ThrowFailedToHandleUnauthorizedResponse("The authorization callback returned a null or empty authorization code.");
706722
}
707723

708724
if (_validateAuthorizationResponseIssuer)
@@ -721,7 +737,8 @@ private async Task<string> InitiateAuthorizationCodeFlowAsync(
721737
private Uri BuildAuthorizationUrl(
722738
ProtectedResourceMetadata protectedResourceMetadata,
723739
AuthorizationServerMetadata authServerMetadata,
724-
string codeChallenge)
740+
string codeChallenge,
741+
string state)
725742
{
726743
var resourceUri = GetResourceUri(protectedResourceMetadata);
727744

@@ -732,6 +749,7 @@ private Uri BuildAuthorizationUrl(
732749
["response_type"] = "code",
733750
["code_challenge"] = codeChallenge,
734751
["code_challenge_method"] = "S256",
752+
["state"] = state,
735753
};
736754

737755
if (resourceUri is not null)
@@ -1107,6 +1125,26 @@ private bool ChallengeIntroducesNewScopes(ProtectedResourceMetadata protectedRes
11071125
return scope + " " + OfflineAccess;
11081126
}
11091127

1128+
/// <summary>
1129+
/// Validates that an authorization response is bound to the transaction that initiated it.
1130+
/// </summary>
1131+
/// <param name="state">The state returned in the authorization response.</param>
1132+
/// <param name="expectedState">The state sent in the authorization request.</param>
1133+
private static void ValidateStateResponse(string? state, string expectedState)
1134+
{
1135+
if (string.IsNullOrEmpty(state))
1136+
{
1137+
ThrowFailedToHandleUnauthorizedResponse(
1138+
"The authorization response did not include the required state parameter.");
1139+
}
1140+
1141+
if (!string.Equals(state, expectedState, StringComparison.Ordinal))
1142+
{
1143+
ThrowFailedToHandleUnauthorizedResponse(
1144+
"The authorization response state did not match the state sent in the authorization request.");
1145+
}
1146+
}
1147+
11101148
/// <summary>
11111149
/// Validates the <c>iss</c> parameter from an authorization response per
11121150
/// <see href="https://datatracker.ietf.org/doc/html/rfc9207">RFC 9207</see>.
@@ -1378,7 +1416,7 @@ private async Task<ProtectedResourceMetadata> ExtractProtectedResourceMetadata(H
13781416
yield return (new Uri($"{hostBase}{ProtectedResourceMetadataWellKnownPath}"), new Uri(hostBase));
13791417
}
13801418

1381-
private static string GenerateCodeVerifier()
1419+
private static string GenerateRandomBase64UrlValue()
13821420
{
13831421
#if NET9_0_OR_GREATER
13841422
Span<byte> bytes = stackalloc byte[32];

src/ModelContextProtocol.Core/Server/McpServerImpl.cs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,10 @@ private void ConfigureInitialize(McpServerOptions options)
668668
Instructions = options.ServerInstructions,
669669
ServerInfo = options.ServerInfo ?? DefaultImplementation,
670670
Capabilities = ServerCapabilities ?? new(),
671-
ResultType = "complete",
671+
672+
// resultType is a 2026-07-28 result field. The initialize handshake is only available on
673+
// 2025-11-25 and earlier revisions (2026-07-28+ negotiate via server/discover and throw
674+
// above), so InitializeResult must never carry resultType (issue #1721).
672675
};
673676
},
674677
McpJsonUtilities.JsonContext.Default.InitializeRequestParams,
@@ -1693,8 +1696,12 @@ private void ConfigureLogging(McpServerOptions options)
16931696
return InvokeHandlerAsync(setLoggingLevelHandler, request!, jsonRpcRequest, cancellationToken);
16941697
}
16951698

1696-
// Otherwise, consider it handled.
1697-
return new ValueTask<EmptyResult>(EmptyResult.Instance);
1699+
// Otherwise, consider it handled. logging/setLevel is a legacy (<= 2025-11-25) method
1700+
// (2026-07-28+ is rejected above), so the response must not carry the 2026-07-28 resultType
1701+
// field. Return a fresh EmptyResult rather than the shared EmptyResult.Instance, which is
1702+
// pre-stamped with resultType="complete" for the 2026-07-28-only subscriptions/listen path
1703+
// (issue #1721).
1704+
return new ValueTask<EmptyResult>(new EmptyResult());
16981705
},
16991706
McpJsonUtilities.JsonContext.Default.SetLevelRequestParams,
17001707
McpJsonUtilities.JsonContext.Default.EmptyResult);
@@ -1765,7 +1772,11 @@ private void SetHandler<TParams, TResult>(
17651772
handler = async (request, cancellationToken) =>
17661773
{
17671774
var result = await innerHandler(request, cancellationToken).ConfigureAwait(false);
1768-
if (result is ICacheableResult cacheable)
1775+
1776+
// ttlMs and cacheScope are 2026-07-28 result fields; only stamp them when the request
1777+
// was negotiated under that revision or later. Earlier revisions (e.g. 2025-11-25) reject
1778+
// these as unrecognized keys (issue #1721).
1779+
if (result is ICacheableResult cacheable && IsJuly2026OrLaterProtocolRequest(request.JsonRpcRequest))
17691780
{
17701781
cacheable.TimeToLive ??= TimeSpan.Zero;
17711782
cacheable.CacheScope ??= CacheScope.Private;
@@ -1781,7 +1792,12 @@ private void SetHandler<TParams, TResult>(
17811792
handler = async (request, cancellationToken) =>
17821793
{
17831794
var result = await innerHandler(request, cancellationToken).ConfigureAwait(false);
1784-
if (result is Result protocolResult && protocolResult.ResultType is null)
1795+
1796+
// resultType is a 2026-07-28 result field; only stamp it when the request was negotiated
1797+
// under that revision or later. Earlier revisions (e.g. 2025-11-25) reject it as an
1798+
// unrecognized key (issue #1721).
1799+
if (result is Result protocolResult && protocolResult.ResultType is null &&
1800+
IsJuly2026OrLaterProtocolRequest(request.JsonRpcRequest))
17851801
{
17861802
protocolResult.ResultType = "complete";
17871803
}
@@ -1808,7 +1824,12 @@ private void SetWithAlternateHandler<TParams, TResult>(
18081824
handler = async (request, cancellationToken) =>
18091825
{
18101826
var result = await innerHandler(request, cancellationToken).ConfigureAwait(false);
1811-
if (!result.IsAlternate && result.Result is { ResultType: null } immediateResult)
1827+
1828+
// resultType is a 2026-07-28 result field; only stamp it when the request was negotiated
1829+
// under that revision or later. Earlier revisions (e.g. 2025-11-25) reject it as an
1830+
// unrecognized key (issue #1721).
1831+
if (!result.IsAlternate && result.Result is { ResultType: null } immediateResult &&
1832+
IsJuly2026OrLaterProtocolRequest(request.JsonRpcRequest))
18121833
{
18131834
immediateResult.ResultType = "complete";
18141835
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace ModelContextProtocol.Extensions.Tasks;
2+
3+
/// <summary>
4+
/// Specifies how a tool call participates in the MCP Tasks extension.
5+
/// </summary>
6+
public enum McpTaskExecutionMode
7+
{
8+
/// <summary>The tool always executes synchronously.</summary>
9+
Synchronous,
10+
11+
/// <summary>The tool executes as a task when the client declares the Tasks extension.</summary>
12+
Optional,
13+
14+
/// <summary>The tool requires the client to declare the Tasks extension.</summary>
15+
Required,
16+
}

0 commit comments

Comments
 (0)