https://github.com/akshay12390/mcp-weather-oauth2-server
Forked from https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/weather/starter-webmvc-oauth2-server
https://github.com/akshay12390/mcp-weather-oauth2-server
Last synced: 2 months ago
JSON representation
Forked from https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/weather/starter-webmvc-oauth2-server
- Host: GitHub
- URL: https://github.com/akshay12390/mcp-weather-oauth2-server
- Owner: akshay12390
- Created: 2025-05-05T16:25:13.000Z (6 months ago)
- Default Branch: master
- Last Pushed: 2025-05-08T09:25:48.000Z (6 months ago)
- Last Synced: 2025-05-08T10:26:52.652Z (6 months ago)
- Language: Java
- Homepage:
- Size: 742 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-mcp-servers - **mcp-weather-oauth2-server** - Reference https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/weather/starter-webmvc-oauth2-server `java` `server` `web` `ai` `auth` `git clone https://github.com/akshay12390/mcp-weather-starter-webmvc-oauth2-server` (API)
- awesome-mcp-servers - **mcp-weather-oauth2-server** - Reference https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/weather/starter-webmvc-oauth2-server `java` `server` `web` `ai` `auth` `git clone https://github.com/akshay12390/mcp-weather-starter-webmvc-oauth2-server` (API)
README
# MCP with OAuth
This sample demonstrates how to secure an MCP server using OAuth2, as per
the [MCP specification](https://spec.modelcontextprotocol.io/specification/2025-03-26/basic/authorization/).
## Getting started
Run the project with:
```
./mvnw spring-boot:run
```
### Client Credentials
Obtain a token by calling the `/oauth2/token` endpoint:
```shell
curl -XPOST "http://localhost:8080/oauth2/token" \
--data grant_type=client_credentials \
--user "mcp-client:secret"
# And copy-paste the access token
# Or use JQ:
curl -XPOST "http://localhost:8080/oauth2/token" \
--data grant_type=client_credentials \
--user "mcp-client:secret" | jq -r ".access_token"
```
### Auth code with browser flow (PKCE)
Obtain a auth code by first calling authorize endpoint
```shell
python3 pkce_generator.py
```
and then fetch access token by calling the `/oauth2/token` endpoint
```shell
python3 token_request.py
```
## Using MCP inspector
Store the generated token in previous step, and then boot up the MCP inspector:
```shell
npx @modelcontextprotocol/inspector@0.6.0
```
In the MCP inspector, paste your token. Click connect, and voilà!
Note that the token is valid for 15 minutes
## Implementation considerations
### Dependencies
In Spring, OAuth2 Support for MCP server means adding:
1. [Spring Security](https://docs.spring.io/spring-security/) (infrastructure for security)
2. [Spring Authorization Server](https://docs.spring.io/spring-authorization-server/) (issuing tokens)
3. [Spring Security: OAuth2 Resource Server](https://docs.spring.io/spring-security/reference/servlet/oauth2/resource-server/index.html#page-title) (
authentication using tokens)
Note that Spring Auth Server does not support the reactive stack, so issuing tokens only works in Servlet.