|
1 | 1 | using Microsoft.Extensions.Logging; |
| 2 | +using ModelContextProtocol.Authentication; |
2 | 3 | using ModelContextProtocol.Client; |
3 | 4 | using ModelContextProtocol.Protocol; |
4 | 5 | using System.Diagnostics; |
|
32 | 33 | OAuth = new() |
33 | 34 | { |
34 | 35 | RedirectUri = new Uri("http://localhost:1179/callback"), |
35 | | - AuthorizationRedirectDelegate = HandleAuthorizationUrlAsync, |
| 36 | + AuthorizationCallbackHandler = HandleAuthorizationUrlAsync, |
36 | 37 | DynamicClientRegistration = new() |
37 | 38 | { |
38 | 39 | ClientName = "ProtectedMcpClient", |
|
68 | 69 | /// Handles the OAuth authorization URL by starting a local HTTP server and opening a browser. |
69 | 70 | /// This implementation demonstrates how SDK consumers can provide their own authorization flow. |
70 | 71 | /// </summary> |
71 | | -/// <param name="authorizationUrl">The authorization URL to open in the browser.</param> |
72 | | -/// <param name="redirectUri">The redirect URI where the authorization code will be sent.</param> |
| 72 | +/// <param name="authorizationContext">The context containing the authorization and redirect URIs.</param> |
73 | 73 | /// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param> |
74 | | -/// <returns>The authorization code extracted from the callback, or null if the operation failed.</returns> |
75 | | -static async Task<string?> HandleAuthorizationUrlAsync(Uri authorizationUrl, Uri redirectUri, CancellationToken cancellationToken) |
| 74 | +/// <returns>The authorization result extracted from the callback, or null if the operation failed.</returns> |
| 75 | +static async Task<AuthorizationResult?> HandleAuthorizationUrlAsync( |
| 76 | + AuthorizationCallbackContext authorizationContext, |
| 77 | + CancellationToken cancellationToken) |
76 | 78 | { |
| 79 | + var authorizationUrl = authorizationContext.AuthorizationUri; |
| 80 | + var redirectUri = authorizationContext.RedirectUri; |
| 81 | + |
77 | 82 | Console.WriteLine("Starting OAuth authorization flow..."); |
78 | 83 | Console.WriteLine($"Opening browser to: {authorizationUrl}"); |
79 | 84 |
|
|
93 | 98 | var context = await listener.GetContextAsync(); |
94 | 99 | var query = HttpUtility.ParseQueryString(context.Request.Url?.Query ?? string.Empty); |
95 | 100 | var code = query["code"]; |
| 101 | + var iss = query["iss"]; |
96 | 102 | var error = query["error"]; |
97 | 103 |
|
98 | 104 | string responseHtml = "<html><body><h1>Authentication complete</h1><p>You can close this window now.</p></body></html>"; |
|
115 | 121 | } |
116 | 122 |
|
117 | 123 | Console.WriteLine("Authorization code received successfully."); |
118 | | - return code; |
| 124 | + return new AuthorizationResult { Code = code, Iss = iss }; |
119 | 125 | } |
120 | 126 | catch (Exception ex) |
121 | 127 | { |
|
0 commit comments