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
Copy file name to clipboardExpand all lines: docs/concepts/filters.md
+213-2Lines changed: 213 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,9 +7,14 @@ uid: filters
7
7
8
8
# MCP Server Handler Filters
9
9
10
-
For each handler type in the MCP Server, there are corresponding `AddXXXFilter` methods in `McpServerBuilderExtensions.cs` that allow you to add filters to the handler pipeline. The filters are stored in `McpServerOptions.Filters`and applied during server configuration.
10
+
The MCP Server provides two levels of filters for intercepting and modifying request processing:
11
11
12
-
## Available Filter Methods
12
+
1.**Message Filters** - Low-level filters (`AddIncomingMessageFilter`, `AddOutgoingMessageFilter`) that intercept all JSON-RPC messages before routing
13
+
2.**Request-Specific Filters** - Handler-level filters (e.g., `AddListToolsFilter`, `AddCallToolFilter`) that target specific MCP operations
14
+
15
+
The filters are stored in `McpServerOptions.Filters` and applied during server configuration.
16
+
17
+
## Available Request-Specific Filter Methods
13
18
14
19
The following filter methods are available:
15
20
@@ -25,6 +30,212 @@ The following filter methods are available:
25
30
-`AddUnsubscribeFromResourcesFilter` - Filter for resource unsubscription handlers
26
31
-`AddSetLoggingLevelFilter` - Filter for logging level handlers
27
32
33
+
## Message Filters
34
+
35
+
In addition to the request-specific filters above, there are low-level message filters that intercept all JSON-RPC messages before they are routed to specific handlers:
36
+
37
+
-`AddIncomingMessageFilter` - Filter for all incoming JSON-RPC messages (requests and notifications)
38
+
-`AddOutgoingMessageFilter` - Filter for all outgoing JSON-RPC messages (responses and notifications)
39
+
40
+
### When to Use Message Filters
41
+
42
+
Message filters operate at a lower level than request-specific filters and are useful when you need to:
43
+
44
+
- Intercept all messages regardless of type
45
+
- Implement custom protocol extensions or handle custom JSON-RPC methods
46
+
- Log or monitor all traffic between client and server
47
+
- Modify or skip messages before they reach handlers
48
+
- Send additional messages in response to specific events
49
+
50
+
### Incoming Message Filter
51
+
52
+
`AddIncomingMessageFilter` intercepts all incoming JSON-RPC messages before they are dispatched to request-specific handlers:
**Important**: Incoming message filters always run before request-specific filters, and outgoing message filters run when responses or notifications are sent. The complete execution flow for a request/response cycle is:
191
+
192
+
```
193
+
Request arrives
194
+
↓
195
+
IncomingFilter1 (before next)
196
+
↓
197
+
IncomingFilter2 (before next)
198
+
↓
199
+
Request Routing → ListToolsFilter → Handler
200
+
↓
201
+
IncomingFilter2 (after next)
202
+
↓
203
+
IncomingFilter1 (after next)
204
+
↓
205
+
Response sent via OutgoingFilter1 (before next)
206
+
↓
207
+
OutgoingFilter2 (before next)
208
+
↓
209
+
Transport sends message
210
+
↓
211
+
OutgoingFilter2 (after next)
212
+
↓
213
+
OutgoingFilter1 (after next)
214
+
```
215
+
216
+
### Passing Data Between Filters
217
+
218
+
The `Items` dictionary allows you to pass data between filters processing the same message:
0 commit comments