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 unified request context interfaces for MCP operations (#70)
feat: Add unified request context interfaces for MCP operations
Introduce McpSyncRequestContext and McpAsyncRequestContext as unified interfaces
that provide convenient access to MCP request context, server exchange, transport
context, and helper methods for common operations.
Key Changes:
Core Context Interfaces:
- Add McpSyncRequestContext interface for synchronous operations
- Add McpAsyncRequestContext interface for asynchronous operations (Mono-based)
- Add McpRequestContextTypes interface with shared type definitions
- Add StructuredElicitResult<T> record for typed elicitation responses
Default Implementations:
- Add DefaultMcpSyncRequestContext with stateful and stateless support
- Add DefaultMcpAsyncRequestContext with stateful and stateless support
- Implement builder pattern for context creation
- Support automatic context injection in tool/resource/prompt methods
Specification Classes:
- Add DefaultElicitationSpec for elicitation configuration
- Add DefaultLoggingSpec for logging configuration
- Add DefaultProgressSpec for progress notification configuration
- Add DefaultSamplingSpec for sampling request configuration
- Add DefaultSamplingSpec.DefaultModelPreferenceSpec for model preferences
Context Features:
- Unified API for both stateful and stateless operations
- Convenience methods: debug(), info(), warn(), error() for logging
- Progress tracking: progress(int), progress(Consumer<ProgressSpec>)
- Elicitation support with typed responses and custom configuration
- Sampling support with flexible message and model configuration
- Access to roots, ping, and request metadata
Method Callback Updates:
- Update AbstractMcpToolMethodCallback to support context injection
- Add createRequestContext() abstract method for context creation
- Update all tool callback implementations (Sync/Async, Stateful/Stateless)
- Add context type checking in isExchangeOrContextType()
- Update JSON schema generation to exclude context parameters
Documentation:
- Update README.md with context usage examples
- Add synchronous and asynchronous context examples
- Document all available context methods and their return types
- Mark @McpProgressToken and McpServerExchange as deprecated
- Add migration notes for using new context interfaces
Testing:
- Add DefaultMcpSyncRequestContextTests with test coverage
- Add DefaultMcpAsyncRequestContextTests with reactive test coverage
- Add DefaultLoggingSpecTests for logging specification
- Add DefaultProgressSpecTests for progress specification
- Add DefaultSamplingSpecTests for sampling specification
- Update tool callback tests to verify context parameter support
Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
Copy file name to clipboardExpand all lines: README.md
+210-8Lines changed: 210 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -120,11 +120,15 @@ Each operation type has both synchronous and asynchronous implementations, allow
120
120
-**`@McpToolParam`** - Annotates tool method parameters with descriptions and requirement specifications
121
121
122
122
#### Special Parameters and Annotations
123
-
-**`@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
124
-
-**`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
125
-
-**`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
-
-**`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
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
127
127
-**`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.
130
+
-**`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.
128
132
129
133
### Method Callbacks
130
134
@@ -870,6 +874,204 @@ public List<String> smartComplete(
870
874
871
875
This feature enables context-aware MCP operations where the behavior can be customized based on client-provided metadata such as user identity, preferences, session information, or any other contextual data.
872
876
877
+
#### McpRequestContext Support
878
+
879
+
The library provides unified request context interfaces (`McpSyncRequestContext` and `McpAsyncRequestContext`) that offer a higher-level abstraction over the underlying MCP infrastructure. These context objects provide convenient access to:
880
+
881
+
- The original request (CallToolRequest, ReadResourceRequest, etc.)
882
+
- Server exchange (for stateful operations) or transport context (for stateless operations)
883
+
- Convenient methods for logging, progress updates, sampling, elicitation, and more
884
+
885
+
**Key Benefits:**
886
+
-**Unified API**: Single parameter type works for both stateful and stateless operations
887
+
-**Convenience Methods**: Built-in helpers for common operations like logging and progress tracking
888
+
-**Type Safety**: Strongly-typed access to request data and context
889
+
-**Automatic Injection**: Context is automatically created and injected by the framework
890
+
891
+
When a method parameter is of type `McpSyncRequestContext` or `McpAsyncRequestContext`:
892
+
- The parameter is automatically injected with the appropriate context implementation
893
+
- The parameter is excluded from JSON schema generation
894
+
- For stateful operations, the context provides access to `McpSyncServerExchange` or `McpAsyncServerExchange`
895
+
- For stateless operations, the context provides access to `McpTransportContext`
896
+
897
+
**Synchronous Context Example:**
898
+
899
+
```java
900
+
public record UserInfo(String name, String email, Number age) {}
901
+
902
+
@McpTool(name="process-with-context", description="Process data with unified context")
903
+
publicString processWithContext(
904
+
McpSyncRequestContext context,
905
+
@McpToolParam(description="Data to process", required=true) String data) {
`McpAsyncRequestContext` provides the same methods but with reactive return types (`Mono<T>` instead of `T` or `Optional<T>`).
1072
+
1073
+
This unified context approach simplifies method signatures and provides a consistent API across different operation types and execution modes (stateful vs stateless, sync vs async).
1074
+
873
1075
### Async Tool Example
874
1076
875
1077
```java
@@ -1771,30 +1973,30 @@ public class AsyncElicitationHandler {
0 commit comments