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
feat: Add McpRequestContext support to Complete, Prompt, and Resource methods (spring-ai-community#74)
Add support for McpSyncRequestContext and McpAsyncRequestContext parameters
in Complete, Prompt, and Resource method callbacks, providing a unified
interface for accessing MCP request context.
- Add McpSyncRequestContext and McpAsyncRequestContext parameter support
to AbstractMcpCompleteMethodCallback, AbstractMcpPromptMethodCallback,
and AbstractMcpResourceMethodCallback
- Implement validation to ensure correct context type matches method
synchronicity (sync methods use McpSyncRequestContext, async methods
use McpAsyncRequestContext)
- Add validation to prevent duplicate request context parameters
- Fix @McpProgressToken to properly inject progress token from requests
(previously returned null for CompleteRequest)
- Update README.md documentation to clarify that McpSyncRequestContext,
McpAsyncRequestContext, and @McpProgressToken are supported in Complete,
Prompt, Resource, and Tool methods
- Rename McpProviderUtils into McpPredicates
- Add structured elicit result builder
- Add test coverage for request context parameters in
sync and async Complete, Prompt, and Resource methods
- Add tests for request context with URI variables and arguments
- Add validation tests for duplicate and mismatched context types
- Add tests for non-null progress token handling
Signed-off-by: Christian Tzolov <[email protected]>
-**Filter out** methods with bidirectional parameters (`McpSyncRequestContext`, `McpAsyncRequestContext`, `McpSyncServerExchange`, `McpAsyncServerExchange`)
125
+
- Accept methods with `McpTransportContext` or no context parameter
126
+
- Methods with bidirectional parameters are logged as warnings and skipped
127
+
- Do not support bidirectional operations (roots, elicitation, sampling)
128
+
129
+
101
130
## Key Components
102
131
103
132
### Annotations
@@ -120,15 +149,15 @@ Each operation type has both synchronous and asynchronous implementations, allow
120
149
-**`@McpToolParam`** - Annotates tool method parameters with descriptions and requirement specifications
121
150
122
151
#### Special Parameters and Annotations
123
-
-**`McpSyncRequestContext`** - Special parameter type for synchronous operations that provides a unified interface for accessing MCP request context, including the original request, server exchange (for stateful operations), transport context (for stateless operations), and convenient methods for logging, progress, sampling, and elicitation. This parameter is automatically injected and excluded from JSON schema generation
124
-
-**`McpAsyncRequestContext`** - Special parameter type for asynchronous operations that provides the same unified interface as `McpSyncRequestContext` but with reactive (Mono-based) return types. This parameter is automatically injected and excluded from JSON schema generation
125
-
-**(Deprecated and replaced by `McpSyncRequestContext`) `McpSyncServerExchange`** - Special parameter type for stateful synchronous operations that provides access to server exchange functionality including logging notifications, progress updates, and other server-side operations. This parameter is automatically injected and excluded from JSON schema generation.
126
-
-**(Deprecated and replaced by `McpAsyncRequestContext`) `McpAsyncServerExchange`** - Special parameter type for stateful asynchronous operations that provides access to server exchange functionality with reactive support. This parameter is automatically injected and excluded from JSON schema generation
152
+
-**`McpSyncRequestContext`** - Special parameter type for synchronous operations that provides a unified interface for accessing MCP request context, including the original request, server exchange (for stateful operations), transport context (for stateless operations), and convenient methods for logging, progress, sampling, and elicitation. This parameter is automatically injected and excluded from JSON schema generation. **Supported in Complete, Prompt, Resource, and Tool methods.**
153
+
-**`McpAsyncRequestContext`** - Special parameter type for asynchronous operations that provides the same unified interface as `McpSyncRequestContext` but with reactive (Mono-based) return types. This parameter is automatically injected and excluded from JSON schema generation. **Supported in Complete, Prompt, Resource, and Tool methods.**
154
+
-**(Deprecated and replaced by `McpSyncRequestContext`) `McpSyncServerExchange`** - Legacy parameter type for stateful synchronous operations. Use `McpSyncRequestContext` instead for a unified interface that works with both stateful and stateless operations.
155
+
-**(Deprecated and replaced by `McpAsyncRequestContext`) `McpAsyncServerExchange`** - Legacy parameter type for stateful asynchronous operations. Use `McpAsyncRequestContext` instead for a unified interface that works with both stateful and stateless operations.
127
156
-**`McpTransportContext`** - Special parameter type for stateless operations that provides lightweight access to transport-level context without full server exchange functionality. This parameter is automatically injected and excluded from JSON schema generation
128
-
-**(Deprecated. Handled internally by `McpSyncRequestContext` and `McpAsyncRequestContext`)`@McpProgressToken`** - Marks a method parameter to receive the progress token from the request. This parameter is automatically injected and excluded from the generated JSON schema
129
-
**Note:**if using the `McpSyncRequestContext` or `McpAsyncRequestContext` the progress token is handled internally.
157
+
-**`@McpProgressToken`** - Marks a method parameter to receive the progress token from the request. This parameter is automatically injected and excluded from the generated JSON schema. **Supported in Complete, Prompt, Resource, and Tool methods.**
158
+
**Note:**When using `McpSyncRequestContext` or `McpAsyncRequestContext`, the progress token can be accessed via `ctx.request().progressToken()` instead of using this annotation.
130
159
-**`McpMeta`** - Special parameter type that provides access to metadata from MCP requests, notifications, and results. This parameter is automatically injected and excluded from parameter count limits and JSON schema generation.
131
-
**Note:**if using the McpSyncRequestContext or McpAsyncRequestContext the meta can be obatined via `requestMeta()` instead.
160
+
**Note:**When using `McpSyncRequestContext` or `McpAsyncRequestContext`, metadata can be obtained via `ctx.requestMeta()` instead.
132
161
133
162
### Method Callbacks
134
163
@@ -197,7 +226,7 @@ The modules provide callback implementations for each operation type:
197
226
198
227
The project includes provider classes that scan for annotated methods and create appropriate callbacks:
@@ -2120,7 +2141,7 @@ public class StatelessResourceProvider {
2120
2141
```
2121
2142
2122
2143
**Important Note on Stateless Operations:**
2123
-
Stateless server methods cannot use bidirectional parameters like `McpSyncRequestContext`, `McpAsyncRequestContext`, `McpSyncServerExchange`, or `McpAsyncServerExchange`. These parameters require client capabilities (roots, elicitation, sampling) that are not available in stateless mode. Methods with these parameters will be automatically filtered out and not registered as stateless operations.
2144
+
Stateless server methods can use `McpSyncRequestContext` and `McpAsyncRequestContext`, but bidirectional operations (roots, elicitation, sampling) will not be available. The legacy `McpSyncServerExchange` and `McpAsyncServerExchange` parameters are not supported in stateless mode. Methods using the legacy parameters will be automatically filtered out and not registered as stateless operations.
2124
2145
2125
2146
#### Stateless Tool Example
2126
2147
@@ -2242,7 +2263,7 @@ Override `AbstractMcpToolProvider#doGetToolCallException()` to customize the exc
2242
2263
2243
2264
-**Annotation-based method handling** - Simplifies the creation and registration of MCP methods
2244
2265
-**Support for both synchronous and asynchronous operations** - Flexible integration with different application architectures
2245
-
-**Stateful and stateless implementations** - Choose between full server exchange context (`McpSyncServerExchange`/`McpAsyncServerExchange`) or lightweight transport context (`McpTransportContext`) for all MCP operations
2266
+
-**Stateful and stateless implementations** - Choose between unified request context (`McpSyncRequestContext`/`McpAsyncRequestContext`) or lightweight transport context (`McpTransportContext`) for all MCP operations
2246
2267
-**Comprehensive stateless support** - All MCP operations (Complete, Prompt, Resource, Tool) support stateless implementations for scenarios where full server context is not needed
2247
2268
-**Builder pattern for callback creation** - Clean and fluent API for creating method callbacks
2248
2269
-**Comprehensive validation** - Ensures method signatures are compatible with MCP operations
0 commit comments