Skip to content

Commit 4dc1fdd

Browse files
committed
Implement MCP Auth Protocol
1 parent f69d1e9 commit 4dc1fdd

File tree

4 files changed

+71
-27
lines changed

4 files changed

+71
-27
lines changed

README.md

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -91,45 +91,44 @@ flowchart LR
9191

9292
## Getting Started - Local Deployment
9393

94-
### 1. **Prepare Local Development Environment**
94+
### 1. Prepare Local Development Environment
9595
- [Install .NET 8 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/8.0)
9696
- [Install Docker Desktop](https://docs.docker.com/desktop/)
9797
- [Install and turn on Kubernetes](https://docs.docker.com/desktop/features/kubernetes/#install-and-turn-on-kubernetes)
9898

99-
### 2. **Run Local Docker Registry**
99+
### 2. Run Local Docker Registry
100100
```sh
101101
docker run -d -p 5000:5000 --name registry registry:2.7
102102
```
103103

104-
### 3. **Build & Publish MCP Server Images**
104+
### 3. Build & Publish MCP Server Images
105105
Build and push the MCP server images to your local registry (`localhost:5000`).
106106
```sh
107107
docker build -f mcp-example-server/Dockerfile mcp-example-server -t localhost:5000/mcp-example:1.0.0
108108
docker push localhost:5000/mcp-example:1.0.0
109109
```
110110

111-
### 4. **Build & Publish MCP Gateway**
111+
### 4. Build & Publish MCP Gateway
112112
(Optional) Open `dotnet/Microsoft.McpGateway.sln` with Visual Studio.
113113

114114
Publish the MCP Gateway image by right-clicking `Publish` on `Microsoft.McpGateway.Service` in Visual Studio, or run:
115115
```sh
116116
dotnet publish dotnet/Microsoft.McpGateway.Service/src/Microsoft.McpGateway.Service.csproj -c Release /p:PublishProfile=localhost_5000.pubxml
117117
```
118118

119-
### 5. **Deploy MCP Gateway to Kubernetes Cluster**
119+
### 5. Deploy MCP Gateway to Kubernetes Cluster
120120
Apply the deployment manifests:
121121
```sh
122122
kubectl apply -f deployment/k8s/local-deployment.yml
123123
```
124124

125-
### 6. **Enable Port Forwarding**
125+
### 6. Enable Port Forwarding
126126
Forward the gateway service port:
127127
```sh
128128
kubectl port-forward -n adapter svc/mcpgateway-service 8000:8000
129129
```
130130

131-
### 7. **Test the API**
132-
131+
### 7. Test the API - MCP Server Management
133132
- Import the OpenAPI definition from `openapi/mcp-gateway.openapi.json` into tools like [Postman](https://www.postman.com/), [Bruno](https://www.usebruno.com/), or [Swagger Editor](https://editor.swagger.io/).
134133

135134
- Send a request to create a new adapter resource:
@@ -146,16 +145,29 @@ kubectl port-forward -n adapter svc/mcpgateway-service 8000:8000
146145
}
147146
```
148147

149-
- After deploying the MCP server, use a client like [MCP Inspector](https://modelcontextprotocol.io/docs/tools/inspector) to test the connection.
148+
### 8. Test the API - MCP Server Access
149+
- After deploying the MCP server, use a client like [VS Code](https://code.visualstudio.com/) to test the connection. Refer to the guide: [Use MCP servers in VS Code (Preview)](https://code.visualstudio.com/docs/copilot/chat/mcp-servers).
150+
> **Note:** Ensure VSCode is up to date to access the latest MCP features.
151+
152+
- To connect to the deployed `mcp-example` server, use:
153+
- `http://localhost:8000/adapters/mcp-example/mcp` (Streamable HTTP)
150154

151-
To connect to the deployed `mcp-example` server, use:
152-
- `http://localhost:8000/adapters/mcp-example/mcp` (Streamable HTTP)
155+
Sample `.vscode/mcp.json` that connects to the `mcp-example` server
156+
```json
157+
{
158+
"servers": {
159+
"mcp-example": {
160+
"url": "http://localhost:8000/adapters/mcp-example/mcp",
161+
}
162+
}
163+
}
164+
```
153165

154-
For other servers:
155-
- `http://localhost:8000/adapters/{name}/mcp` (Streamable HTTP)
156-
- `http://localhost:8000/adapters/{name}/sse` (SSE)
166+
- For other servers:
167+
- `http://localhost:8000/adapters/{name}/mcp` (Streamable HTTP)
168+
- `http://localhost:8000/adapters/{name}/sse` (SSE)
157169

158-
### 8. **Clean the Environment**
170+
### 9. Clean the Environment
159171
To remove all deployed resources, delete the Kubernetes namespace:
160172
```sh
161173
kubectl delete namespace adapter
@@ -199,13 +211,14 @@ The cloud-deployed service requires bearer token authentication using Azure Entr
199211
- **Admin consent Description**: Any brief description
200212
- Click **Add scope**
201213

202-
#### Authorize Azure CLI as a Client Application
214+
#### Authorize Azure CLI & VS Code as a Client Application
203215

204-
To allow Azure CLI to work as the client for token acquisition.
216+
To allow Azure CLI & VS Code to work as the client for token acquisition.
205217

206218
1. Still in **Expose an API**, scroll down to **Authorized client applications**
207219
2. Click **+ Add a client application**
208220
- **Client ID**: `04b07795-8ddb-461a-bbee-02f9e1bf7b46` (Azure CLI)
221+
- **Client ID**: `aebc6443-996d-45c2-90f0-388ff96faa56` (VS Code)
209222
- In Authorized scopes, select the scope `access`
210223
- Click **Add**
211224

@@ -249,7 +262,7 @@ Build the MCP server image in ACR:
249262
az acr build -r "mgreg$resourceLabel" -f mcp-example-server/Dockerfile mcp-example-server -t "mgreg$resourceLabel.azurecr.io/mcp-example:1.0.0"
250263
```
251264

252-
### 5. Test the API
265+
### 5. Test the API - MCP Server Management
253266

254267
- Import the OpenAPI spec from `openapi/mcp-gateway.openapi.json` into [Postman](https://www.postman.com/), [Bruno](https://www.usebruno.com/), or [Swagger Editor](https://editor.swagger.io/)
255268

@@ -273,23 +286,37 @@ az acr build -r "mgreg$resourceLabel" -f mcp-example-server/Dockerfile mcp-examp
273286
}
274287
```
275288

276-
- After deploying the MCP server, use a client like [MCP Inspector](https://modelcontextprotocol.io/docs/tools/inspector) to test the connection.
277-
> **Note:** A valid bearer token is still required in the Authorization header when connecting to the server.
289+
### 6. Test the API - MCP Server Access
290+
291+
- After deploying the MCP server, use a client like [VS Code](https://code.visualstudio.com/) to test the connection. Refer to the guide: [Use MCP servers in VS Code (Preview)](https://code.visualstudio.com/docs/copilot/chat/mcp-servers).
292+
> **Note:** Ensure VSCode is up to date to access the latest MCP features.
278293
279294
- To connect to the deployed `mcp-example` server, use:
280295
- `http://<resourceLabel>.<location>.cloudapp.azure.com/adapters/mcp-example/mcp` (Streamable HTTP)
281296

282-
- For other servers:
283-
- `http://<resourceLabel>.<location>.cloudapp.azure.com/adapters/{name}/mcp` (Streamable HTTP)
284-
- `http://<resourceLabel>.<location>.cloudapp.azure.com/adapters/{name}/sse` (SSE)
297+
Sample `.vscode/mcp.json` that connects to the `mcp-example` server
298+
```json
299+
{
300+
"servers": {
301+
"mcp-example": {
302+
"url": "http://<resourceLabel>.<location>.cloudapp.azure.com/adapters/mcp-example/mcp",
303+
}
304+
}
305+
}
306+
```
307+
> **Note:** Authentication is still required to access the MCP server, VS Code will help handle the authentication process.
308+
309+
- For other servers:
310+
- `http://<resourceLabel>.<location>.cloudapp.azure.com/adapters/{name}/mcp` (Streamable HTTP)
311+
- `http://<resourceLabel>.<location>.cloudapp.azure.com/adapters/{name}/sse` (SSE)
285312

286-
### 6. Clean the Environment
313+
### 7. Clean the Environment
287314
To remove all deployed resources, delete the resource group from Azure portal or run:
288315
```sh
289316
az group delete --name <resourceGroupName> --yes
290317
```
291318

292-
### 7. Production Onboarding
319+
### 8. Production Onboarding
293320

294321
- **TLS Configuration**
295322
Set up HTTPS on Azure Application Gateway (AAG) listener using valid TLS certificates.

dotnet/Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
<PackageVersion Include="Moq" Version="4.20.72" />
1515
<PackageVersion Include="MSTest" Version="3.6.4" />
1616
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
17+
<PackageVersion Include="ModelContextProtocol.AspNetCore" Version="0.3.0-preview.2" />
1718
</ItemGroup>
1819
</Project>

dotnet/Microsoft.McpGateway.Service/src/Microsoft.McpGateway.Service.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" />
1313
<PackageReference Include="Microsoft.Extensions.Caching.Cosmos" />
1414
<PackageReference Include="Microsoft.Identity.Web" />
15+
<PackageReference Include="ModelContextProtocol.AspNetCore" />
1516
</ItemGroup>
1617

1718
<ItemGroup>

dotnet/Microsoft.McpGateway.Service/src/Program.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.McpGateway.Management.Store;
1313
using Microsoft.McpGateway.Service.Routing;
1414
using Microsoft.McpGateway.Service.Session;
15+
using ModelContextProtocol.AspNetCore.Authentication;
1516

1617
var builder = WebApplication.CreateBuilder(args);
1718
var credential = new DefaultAzureCredential();
@@ -31,8 +32,22 @@
3132
}
3233
else
3334
{
34-
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
35-
.AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));
35+
var azureAdConfig = builder.Configuration.GetSection("AzureAd");
36+
builder.Services.AddAuthentication(options =>
37+
{
38+
options.DefaultChallengeScheme = McpAuthenticationDefaults.AuthenticationScheme;
39+
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
40+
})
41+
.AddMcp(options =>
42+
{
43+
options.ResourceMetadata = new()
44+
{
45+
Resource = new Uri($"api://{azureAdConfig["ClientId"]}"),
46+
AuthorizationServers = { new Uri($"https://login.microsoftonline.com/{azureAdConfig["tenantId"]}/v2.0") },
47+
ScopesSupported = [$"api://{azureAdConfig["ClientId"]}/.default"]
48+
};
49+
})
50+
.AddMicrosoftIdentityWebApi(azureAdConfig);
3651

3752
builder.Services.AddSingleton<IAdapterResourceStore>(c =>
3853
{

0 commit comments

Comments
 (0)