Skip to content

Support for the @PreAuthorize annotation in @Tool Components #3272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
embesozzi opened this issue May 21, 2025 · 2 comments
Open

Support for the @PreAuthorize annotation in @Tool Components #3272

embesozzi opened this issue May 21, 2025 · 2 comments

Comments

@embesozzi
Copy link

embesozzi commented May 21, 2025

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

<properties>
	<java.version>21</java.version>
	<spring-ai.version>1.0.0-M7</spring-ai.version>
</properties>

Steps to reproduce

  1. 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

  2. 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.

@embesozzi embesozzi changed the title Support for the @PreAuthorize annotation in @Tools Support for the @PreAuthorize annotation in @Tool Components May 21, 2025
@ThomasVitale
Copy link
Contributor

@embesozzi thanks for reporting this issue. Is this still problem in version 1.0.0?

@embesozzi
Copy link
Author

embesozzi commented May 23, 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:

@ControllerAdvice
public class OAuth2ExceptionHandler { ... }

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.

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

No branches or pull requests

2 participants