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
When using the ASP.NET Core integration (`ModelContextProtocol.AspNetCore`), authorization filters are automatically configured to support `[Authorize]` and `[AllowAnonymous]` attributes on MCP server tools, prompts, and resources.
124
+
When using the ASP.NET Core integration (`ModelContextProtocol.AspNetCore`), you can add authorization filters to support `[Authorize]` and `[AllowAnonymous]` attributes on MCP server tools, prompts, and resources by calling `AddAuthorizationFilters()` on your MCP server builder.
125
+
126
+
### Enabling Authorization Filters
127
+
128
+
To enable authorization support, call `AddAuthorizationFilters()` when configuring your MCP server:
129
+
130
+
```csharp
131
+
services.AddMcpServer()
132
+
.WithHttpTransport()
133
+
.AddAuthorizationFilters() // Enable authorization filter support
134
+
.WithTools<WeatherTools>();
135
+
```
136
+
137
+
**Important**: You should always call `AddAuthorizationFilters()` when using ASP.NET Core integration if you want to use authorization attributes like `[Authorize]` on your MCP server tools, prompts, or resources.
125
138
126
139
### Authorization Attributes Support
127
140
@@ -200,9 +213,45 @@ For individual operations, the filters return authorization errors when access i
200
213
-**Prompts**: Throws an `McpException` with "Access forbidden" message
201
214
-**Resources**: Throws an `McpException` with "Access forbidden" message
202
215
216
+
### Filter Execution Order and Authorization
217
+
218
+
Authorization filters are applied automatically when you call `AddAuthorizationFilters()`. These filters run at a specific point in the filter pipeline, which means:
219
+
220
+
**Filters added before authorization filters** can see:
221
+
- Unauthorized requests for operations before they are rejected by the authorization filters
222
+
- Complete listings for unauthorized primitives before they are filtered out by the authorization filters
223
+
224
+
**Filters added after authorization filters** will only see:
225
+
- Authorized requests that passed authorization checks
226
+
- Filtered listings containing only authorized primitives
227
+
228
+
This allows you to implement logging, metrics, or other cross-cutting concerns that need to see all requests, while still maintaining proper authorization:
// This filter runs AFTER authorization - only sees authorized tools
245
+
varresult=awaitnext(context, cancellationToken);
246
+
Console.WriteLine($"Post-auth filter sees {result.Tools?.Count??0} authorized tools");
247
+
returnresult;
248
+
})
249
+
.WithTools<WeatherTools>();
250
+
```
251
+
203
252
### Setup Requirements
204
253
205
-
To use authorization features, you must configure authentication and authorization in your ASP.NET Core application:
254
+
To use authorization features, you must configure authentication and authorization in your ASP.NET Core application and call `AddAuthorizationFilters()`:
thrownewInvalidOperationException("Authorization filter was not invoked for tools/list operation, but authorization metadata was found on the tools. Ensure that AddAuthorizationFilters() is called on the IMcpServerBuilder to configure authorization filters.");
thrownewInvalidOperationException("Authorization filter was not invoked for tools/call operation, but authorization metadata was found on the tool. Ensure that AddAuthorizationFilters() is called on the IMcpServerBuilder to configure authorization filters.");
thrownewInvalidOperationException("Authorization filter was not invoked for resources/list operation, but authorization metadata was found on the resources. Ensure that AddAuthorizationFilters() is called on the IMcpServerBuilder to configure authorization filters.");
thrownewInvalidOperationException("Authorization filter was not invoked for resources/templates/list operation, but authorization metadata was found on the resource templates. Ensure that AddAuthorizationFilters() is called on the IMcpServerBuilder to configure authorization filters.");
thrownewInvalidOperationException("Authorization filter was not invoked for resources/read operation, but authorization metadata was found on the resource. Ensure that AddAuthorizationFilters() is called on the IMcpServerBuilder to configure authorization filters.");
thrownewInvalidOperationException("Authorization filter was not invoked for prompts/list operation, but authorization metadata was found on the prompts. Ensure that AddAuthorizationFilters() is called on the IMcpServerBuilder to configure authorization filters.");
thrownewInvalidOperationException("Authorization filter was not invoked for prompts/get operation, but authorization metadata was found on the prompt. Ensure that AddAuthorizationFilters() is called on the IMcpServerBuilder to configure authorization filters.");
252
+
}
253
+
254
+
returnawaitnext(context,cancellationToken);
255
+
});
256
+
}
257
+
122
258
/// <summary>
123
259
/// Filters a collection of items based on authorization policies in their metadata.
124
260
/// For list operations where we need to filter results by authorization.
0 commit comments