You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This sample demonstrates how to create an MCP client that connects to a protected MCP server using OAuth 2.0 authentication. The client implements a custom OAuth authorization flow with browser-based authentication.
4
+
5
+
## Overview
6
+
7
+
The Protected MCP Client sample shows how to:
8
+
- Connect to an OAuth-protected MCP server
9
+
- Handle OAuth 2.0 authorization code flow
10
+
- Use custom authorization redirect handling
11
+
- Call protected MCP tools with authentication
12
+
13
+
## Prerequisites
14
+
15
+
- .NET 9.0 or later
16
+
- A running TestOAuthServer (for OAuth authentication)
17
+
- A running ProtectedMCPServer (for MCP services)
18
+
19
+
## Setup and Running
20
+
21
+
### Step 1: Start the Test OAuth Server
22
+
23
+
First, you need to start the TestOAuthServer which provides OAuth authentication:
24
+
25
+
```bash
26
+
cd tests\ModelContextProtocol.TestOAuthServer
27
+
dotnet run --framework net9.0
28
+
```
29
+
30
+
The OAuth server will start at `https://localhost:7029`
31
+
32
+
### Step 2: Start the Protected MCP Server
33
+
34
+
Next, start the ProtectedMCPServer which provides the weather tools:
35
+
36
+
```bash
37
+
cd samples\ProtectedMCPServer
38
+
dotnet run
39
+
```
40
+
41
+
The protected server will start at `http://localhost:7071`
42
+
43
+
### Step 3: Run the Protected MCP Client
44
+
45
+
Finally, run this client:
46
+
47
+
```bash
48
+
cd samples\ProtectedMCPClient
49
+
dotnet run
50
+
```
51
+
52
+
## What Happens
53
+
54
+
1. The client attempts to connect to the protected MCP server at `http://localhost:7071`
55
+
2. The server responds with OAuth metadata indicating authentication is required
56
+
3. The client initiates OAuth 2.0 authorization code flow:
57
+
- Opens a browser to the authorization URL at the OAuth server
58
+
- Starts a local HTTP listener on `http://localhost:1179/callback` to receive the authorization code
59
+
- Exchanges the authorization code for an access token
60
+
4. The client uses the access token to authenticate with the MCP server
61
+
5. The client lists available tools and calls the `GetAlerts` tool for Washington state
0 commit comments