spring-projects / spring-projects/spring-ai

Support for the @PreAuthorize annotation in @Tool Components

Open
#3,272 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
Dominant language
Java
Stars
9.5k
Forks
2.9k
Avg merge
1d 10h
Merged PRs (30d)
5

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the failure with a @PreAuthorize-annotated @Tool method in an OAuth 2.0 resource-server setup, then trace how the @Tool component invokes the method and handles authorization failures. Done means the annotation works for tool methods and denied requests reach an appropriate OAuth2AccessDeniedHandler.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, authorization, security
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.