Skip to content

Conversation

tzolov
Copy link
Collaborator

@tzolov tzolov commented Oct 5, 2025

Introduces McpSyncRequestContext and McpAsyncRequestContext as unified interfaces for accessing MCP request context across both stateful and stateless operations. These new context types provide:

  • Unified API that works for both stateful and stateless operations
  • Convenient methods for logging, progress updates, sampling, and elicitation
  • Type-safe access to request data and context
  • Automatic injection by the framework

Key changes:

  • Added McpSyncRequestContext and McpAsyncRequestContext interfaces
  • Added default implementations (DefaultMcpSyncRequestContext, DefaultMcpAsyncRequestContext)
  • Updated all tool method callbacks to support new context types
  • Updated JSON schema generator to exclude context parameters
  • Added tests for new context functionality
  • Updated README with detailed documentation and examples
  • Deprecated @McpProgressToken in favor of internal handling by context
  • Marked McpSyncServerExchange and McpAsyncServerExchange as deprecated in favor of McpSyncRequestContext and McpAsyncRequestContext

Resolves #69
Releated to spring-projects/spring-ai#4471

tzolov added 3 commits October 5, 2025 18:52
Introduces McpSyncRequestContext and McpAsyncRequestContext as unified
interfaces for accessing MCP request context across both stateful and
stateless operations. These new context types provide:

- Unified API that works for both stateful and stateless operations
- Convenient methods for logging, progress updates, sampling, and elicitation
- Type-safe access to request data and context
- Automatic injection by the framework

Key changes:
- Added McpSyncRequestContext and McpAsyncRequestContext interfaces
- Added default implementations (DefaultMcpSyncRequestContext, DefaultMcpAsyncRequestContext)
- Updated all tool method callbacks to support new context types
- Updated JSON schema generator to exclude context parameters
- Added tests for new context functionality
- Updated README with detailed documentation and examples
- Deprecated @McpProgressToken in favor of internal handling by context
- Marked McpSyncServerExchange and McpAsyncServerExchange as deprecated
  in favor of McpSyncRequestContext and McpAsyncRequestContext

Resolves spring-ai-community#69
Releated to spring-projects/spring-ai#4471

Signed-off-by: Christian Tzolov <[email protected]>
Replace Consumer<ElicitationSpec> pattern with direct TypeReference parameters
for better type safety and simpler API. Add StructuredElicitResult<T> for
type-safe responses and automatic Map-to-POJO conversion using Jackson.

Signed-off-by: Christian Tzolov <[email protected]>
Signed-off-by: Christian Tzolov <[email protected]>
- Rename `elicitation()` methods to `elicit()` across all context interfaces
- Rename `sampling()` methods to `sample()` across all context interfaces
- Standardize elicit() to always return `StructuredElicitResult<T>` instead of raw type
- Replace positional parameters with builder-style `Consumer<ElicitationSpec>` configuration
- Add support for both `Class<T>` and `TypeReference<T>` type parameters
- Introduce `ElicitationSpec` interface with fluent API for message and metadata
- Update README documentation with new method signatures and examples
- Update all test cases to use new API methods
- Add copyright header to StructuredElicitResult class

Breaking Changes:
- `elicitation(TypeReference<T>)` now returns `Optional<StructuredElicitResult<T>>` instead of `Optional<T>`
- `elicitation(TypeReference<T>, String, Map)` replaced with `elicit(Consumer<ElicitationSpec>, TypeReference<T>)`
- `sampling(...)` renamed to `sample(...)`
- All elicit methods now consistently return structured results with action, content, and metadata

Signed-off-by: Christian Tzolov <[email protected]>
@tzolov tzolov merged commit 08820b3 into spring-ai-community:main Oct 6, 2025
1 check passed
boolean hasOtherParams = Arrays.stream(method.getParameters()).anyMatch(param -> {
Class<?> type = param.getType();
return !CallToolRequest.class.isAssignableFrom(type)
return !McpSyncRequestContext.class.isAssignableFrom(type)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO McpTransportContext should be excluded here too. Currently parameters of type McpTransportContext are included in the generated json schema. Stateless servers may inject McpTransportContext parameters according to docs: https://docs.spring.io/spring-ai/reference/1.1/api/mcp/mcp-annotations-examples.html#_stateless_server_examples

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And also below on line 156.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Unified Request Context API for MCP Operations

2 participants