Skip to content

Support non-String URI variable parameters for @McpResource methods#6639

Open
OneZero-Y wants to merge 1 commit into
spring-projects:mainfrom
OneZero-Y:fix/6617-mcp-resource-non-string-params
Open

Support non-String URI variable parameters for @McpResource methods#6639
OneZero-Y wants to merge 1 commit into
spring-projects:mainfrom
OneZero-Y:fix/6617-mcp-resource-non-string-params

Conversation

@OneZero-Y

Copy link
Copy Markdown
Contributor

Fixes #6617

Summary

@McpResource methods with URI template variables currently require those parameters to be String. Declaring a primitive or wrapper type (e.g. int id) fails at startup with:

URI variable parameters must be of type String

This forces users to manually parse URI variables, which is inconsistent with Spring MVC-style path variable handling.

This PR removes the String-only restriction and converts URI variable values to the declared parameter type using Spring's DefaultConversionService. Invalid values are surfaced as McpError(INVALID_PARAMS) via the existing resource callback error handling.

Changes

  • Add McpUriVariableValueConverter to convert URI variable strings to the declared parameter type
  • Remove the String-only validation in AbstractMcpResourceMethodCallback
  • Convert URI variable values when building method arguments in buildArgsWithUriVariables
  • Add unit tests for successful int conversion and invalid value handling

Usage

Before (manual parsing required):

@McpResource(uri = "example.com/users/{id}")
public ResourceContents users(String id) {
    int userId = Integer.parseInt(id);
    // ...
}

After:

@McpResource(uri = "example.com/users/{id}")
public ResourceContents users(int id) {
    // id is already converted
}

Test plan

  • ./mvnw -pl mcp/mcp-annotations test -Dtest=SyncMcpResourceMethodCallbackTests#testCallbackWithIntegerUriVariable,SyncMcpResourceMethodCallbackTests#testCallbackWithInvalidIntegerUriVariable,SyncMcpResourceMethodCallbackTests#testCallbackWithUriVariables -Dsurefire.failIfNoSpecifiedTests=false
  • @McpResource with int URI variable parameter registers successfully at startup
  • Valid URI value (42) is converted and passed to the handler method
  • Invalid URI value (not-a-number) returns McpError
  • Existing String URI variable behavior unchanged

@McpResource methods with URI template variables previously required
String parameters. Non-String types such as int failed at startup,
forcing manual parsing inconsistent with Spring MVC path variables.

Add conversion from URI variable strings to the declared parameter
type using DefaultConversionService. Invalid values are reported as
McpError(INVALID_PARAMS) through existing callback error handling.

Closes spring-projects#6617

Signed-off-by: Bishen Yu <aukovyps@163.com>
@OneZero-Y

Copy link
Copy Markdown
Contributor Author

Hey @dimitarproynov — would you mind taking a look when you get a chance? This lets @McpResource URI variables bind to non-String parameter types (e.g. int) via DefaultConversionService, instead of failing at startup. Invalid values surface as McpError(INVALID_PARAMS).

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature request] Support non-String params for resources

2 participants