Securing the MCP Server
In this example, we will add OAuth 2 support to a sample MCP Server - the “Weather” MCP tool from our Spring AI examples repository. First, we import the required Boot starter inpom.xml:
application.properties:
Authorization header.
If you’d like to learn more about OAuth2 Resource Server support in Spring Security, head over to the reference documentation.
Building an OAuth2 Authorization Server
Our MCP Server now expects an Authorization Server to be running athttp://localhost:9000.
In an enterprise scenario, an authorization server is often already provided, either through cloud services or on-premise deployments of servers such as Keycloak.
For this demo, you can use the Authorization Server we provide with the demo and run it with ./mvnw spring-boot:run.
Alternatively, you can build your own in just a few lines of configuration. First, we need the dependencies:
application.yml:
Building an MCP client
The MCP Server and Authorization Server are straightforward to set up, with simple configuration. We need to put in a little more work to secure the MCP client. To get started building an MCP Client, regardless of authorization, please refer to the reference documentation. ⚠️ Currently, Spring AI only supports adding security for theSYNC MCP clients, using a WebClient.
Ensure your application has the correct dependencies:
application.properties:
client_credentials grant, is used to initialize our client application.
It allows setting up the session with the MCP client, as well as listing available tools, using machine-to-machine communication: no user is involved in that flow.
The second uses the authorization_code grant, and allows our app to obtain tokens on behalf of end-users
That client is used for calling the tools.
While it is not explained here, you will need to add the LLM model of your choice to your application to make it complete.
The next step is to configure MCP clients for Spring AI, by providing a @Bean:
SecurityFilterChain to turn on OAuth2, as well as a custom WebClient.Builder used by the MCP client:
ExchangeFilterFunction that decides which OAuth2 tokens it uses, depending on the context (user interaction or app initialization).
It can look a bit confusing for Spring Security beginners, but feel free to use it as-is:
What’s next?
This is a first step implementing full, end-to-end authorization. By using Spring’s powerful extensibility, we can add OAuth2 to our MCP Clients and Servers, but it requires writing some code. The Spring team is hard at work building a simpler integration, with the delightful configuration-driven Boot user experience. We are also working on fine-grained permissions for MCP Servers. In more advanced use-cases, not all tools/resources/prompts in an MCP Server will require the same permissions: the “thing-reader” tool will be available to every user, but the “thing-writer” is only available to admins.[1]: Model Context Protocol, or MCP for short, is a protocol allow AI models to interact with and access external tools and resources in a structured way. Spring AI provides out-of-the box support for both MCP Servers and MCP Clients.