You can close this window now.
"; + byte[] buffer = Encoding.UTF8.GetBytes(responseHtml); + context.Response.ContentLength64 = buffer.Length; + context.Response.ContentType = "text/html"; + context.Response.OutputStream.Write(buffer, 0, buffer.Length); + context.Response.Close(); + + if (!string.IsNullOrEmpty(error)) + { + Console.WriteLine($"Auth error: {error}"); + return null; + } + + if (string.IsNullOrEmpty(code)) + { + Console.WriteLine("No authorization code received"); + return null; + } + + Console.WriteLine("Authorization code received successfully."); + return new AuthorizationResult + { + Code = code, + State = state, + Iss = iss + }; + } + catch (Exception ex) + { + Console.WriteLine($"Error getting auth code: {ex.Message}"); + return null; + } + finally + { + if (listener.IsListening) listener.Stop(); + } +} + +static void OpenBrowser(Uri url) +{ + // Validate the URI scheme - only allow safe protocols + if (url.Scheme != Uri.UriSchemeHttp && url.Scheme != Uri.UriSchemeHttps) + { + Console.WriteLine($"Error: Only HTTP and HTTPS URLs are allowed."); + return; + } + + try + { + var psi = new ProcessStartInfo + { + FileName = url.ToString(), + UseShellExecute = true + }; + Process.Start(psi); + } + catch (Exception ex) + { + Console.WriteLine($"Error opening browser: {ex.Message}"); + Console.WriteLine($"Please manually open this URL: {url}"); + } +} +``` + +### Run the Samples + +Start the IdentityServer and MCP Server applications, then run the console client. When prompted to sign in for the console client, use username `bob` with password `bob` to sign in. The console will output the response from the MCP Server after it self-registers. + + +#### Sample Client Output + +```text +Protected MCP Client +Connecting to server at https://localhost:7141... + +Starting OAuth authorization flow... +Opening browser to: https://localhost:5001/connect/authorize?client_id=AZwWIaA8ApNcB5jptVQrSaNO4hF-nbRtBz19QnKEGOI&redirect_uri=http%3a%2f%2flocalhost%3a1279%2fcallback&response_type=code&code_challenge=mfZCS1wY7EHZwUkIb50SD6dmzReczXjyDMC_GFKEHvM&code_challenge_method=S256&state=EAx8LsLDnK0gVNY8Oxw8wF0Jv6TPCu9-YlyHL7XfFHE&resource=https%3a%2f%2flocalhost%3a7141&scope=mcp%3atools+offline_access +Listening for OAuth callback on: http://localhost:1279/ +Authorization code received successfully. +Found 2 tools on the server. + +Calling get_alerts tool... +Result: Event: Coastal Flood Statement +Area: Southern Queens; Southern Nassau +Severity: Minor +Description: * WHAT...Up to one half foot of inundation above ground level +expected in vulnerable areas near the waterfront and shoreline. + +* WHERE...Southern Queens and Southern Nassau Counties. + +* WHEN...This evening. + +* IMPACTS...Brief minor flooding of the most vulnerable locations near the +waterfront and shoreline. + +* ADDITIONAL DETAILS...Additional rounds of localized minor +flooding are likely with the Wednesday Night and Thursday Night +high tides. Minor coastal flooding could be a bit more +widespread with the Wednesday night high tide. +Instruction: Do not drive through flooded roadways. +-- +Event: Coastal Flood Statement +Area: Southern Fairfield; Southern Westchester +Severity: Minor +Description: * WHAT...Up to one half foot of inundation above ground level +expected in vulnerable areas near the waterfront and shoreline. + +* WHERE...In Connecticut, Southern Fairfield County. In New +York, Southern Westchester County. + +* WHEN...This evening. + +* IMPACTS...Brief minor flooding of the most vulnerable locations near the +waterfront and shoreline + +* ADDITIONAL DETAILS...Additional rounds of localized minor +flooding are likely with the Wednesday Night and Thursday Night +high tides. Minor coastal flooding could be a bit more +widespread with the Wednesday night high tide. +Instruction: Do not drive through flooded roadways. +``` + +## Source Code + +The finished source code is available in the Samples repository, and a reference implementation of this quickstart is available [here](/identityserver/samples/mcp-server). diff --git a/astro/src/content/docs/identityserver/samples/mcp-server.mdx b/astro/src/content/docs/identityserver/samples/mcp-server.mdx new file mode 100644 index 000000000..98c839511 --- /dev/null +++ b/astro/src/content/docs/identityserver/samples/mcp-server.mdx @@ -0,0 +1,22 @@ +--- +title: "AI and MCP Server and Client" +description: "Samples demonstrating how IdentityServer fits in an AI architecture, including as the authorization server for MCP." +date: 2026-08-07 +sidebar: + order: 70 +--- + +import {LinkCard} from "@astrojs/starlight/components"; + +This section contains samples on how to protect an MCP server with Duende IdentityServer. It uses [Dynamic Client Registration](/identityserver/configuration/dcr.mdx) so any MCP-compatible client can connect without pre-configuration." + +### Demo Sample + +This sample contains the finished source code for the [MCP Server sample](/identityserver/quickstarts/8-mcp.mdx). + +