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
Bug description
Currently, using this annotation does not work as expected and throws an exception. I believe this issue is related to the use of the @tool annotation.
The MCP Server is protected by OAuth 2.0 [1] and typically acts as an OAuth 2.0 resource server [2]. Therefore, we should be able to use scopes to protect the tool and handle OAuth-related errors appropriately, such as with the OAuth2AccessDeniedHandler.
Add the @PreAuthorize annotation to the Tool method:
@PreAuthorize("hasAuthority('SCOPE_read')")
@Tool(description = "Read users", name = "read_users")
Expected behavior
Using the @PreAuthorize annotation currently throws an error. I need to ensure that this triggers an appropriate OAuth2AccessDeniedHandler error.
The text was updated successfully, but these errors were encountered:
embesozzi
changed the title
Support for the @PreAuthorize annotation in @Tools
Support for the @PreAuthorize annotation in @Tool Components
May 21, 2025
@ThomasVitale, thanks for the response. Yes, it's working in 1.0.0.
Just a quick question - is there a way to customize the OAuth2AccessDeniedHandler error in the case of MCP?
For instance, if I use something like @PreAuthorize("hasAuthority('SCOPE_READ_USERS')") in a controller, I'm able to customize the error message using @ControllerAdvice, for example:
Or in more complex scenarios with a CustomOAuth2AuthenticationEntryPoint or CustomOAuth2AccessDeniedHandler.
In the case of @tools in MCP we are in services (@service or @component), is there a way to intercept the error and customize the response?
Because customization is needed to ensure compliance with the OAuth 2.0 error message format in some cases.
Uh oh!
There was an error while loading. Please reload this page.
Bug description
Currently, using this annotation does not work as expected and throws an exception. I believe this issue is related to the use of the @tool annotation.
The MCP Server is protected by OAuth 2.0 [1] and typically acts as an OAuth 2.0 resource server [2]. Therefore, we should be able to use scopes to protect the tool and handle OAuth-related errors appropriately, such as with the OAuth2AccessDeniedHandler.
[1] https://modelcontextprotocol.io/specification/2025-03-26
[2] https://docs.spring.io/spring-security/reference/servlet/oauth2/resource-server/jwt.html
Environment
Steps to reproduce
Expose an API as an OAuth 2.0 Resource Server using spring.security.oauth2.resourceserver [2] or just follow this example https://spring.io/blog/2025/04/02/mcp-server-oauth2
Add the @PreAuthorize annotation to the Tool method:
Expected behavior
Using the @PreAuthorize annotation currently throws an error. I need to ensure that this triggers an appropriate OAuth2AccessDeniedHandler error.
The text was updated successfully, but these errors were encountered: