Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions 70 examples/auth-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# MCP Authentication Example

This example demonstrates how to implement OAuth 2.0 authentication with the Model Context Protocol (MCP) Java SDK.

## Overview

The example consists of:

1. A simple MCP server with OAuth authentication
2. A simple MCP client that authenticates using OAuth
3. A tool that requires authentication to access

## Running the Example

### 1. Build the Project

```bash
cd examples/auth-example
mvn clean package
```

### 2. Run the Server

In one terminal window:

```bash
cd examples/auth-example
mvn exec:java -Dexec.mainClass="io.modelcontextprotocol.examples.auth.server.SimpleAuthServer"
```

### 3. Run the Client

In another terminal window:

```bash
cd examples/auth-example
mvn exec:java -Dexec.mainClass="io.modelcontextprotocol.examples.auth.client.SimpleAuthClient"
```

## Using the Client

Once the client is running, you can use these commands:
- `list` - List available tools
- `call get_user_profile` - Call the user profile tool
- `quit` - Exit the client

## Authentication Flow

1. Client initiates the OAuth flow
2. Server redirects to the authorization page
3. User approves the authorization
4. Server redirects back to the client with an authorization code
5. Client exchanges the code for access and refresh tokens
6. Client uses the access token for authenticated MCP requests

## Implementation Details

### Server

The server implements the `OAuthAuthorizationServerProvider` interface to provide OAuth authentication. It uses in-memory storage for clients, tokens, and authorization codes.

### Client

The client uses the `OAuthClientProvider` class to handle OAuth authentication. It opens a browser for the authorization flow and starts a local server to receive the OAuth callback.

## Code Structure

- `SimpleAuthServer.java` - Server implementation
- `SimpleAuthClient.java` - Client implementation
- `Constants.java` - Shared constants
58 changes: 58 additions & 0 deletions 58 examples/auth-example/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.modelcontextprotocol.sdk</groupId>
<artifactId>mcp-parent</artifactId>
<version>0.11.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>auth-example</artifactId>
<name>MCP Auth Example</name>
<description>Example of MCP authentication using OAuth 2.0</description>

<dependencies>
<!-- MCP SDK -->
<dependency>
<groupId>io.modelcontextprotocol.sdk</groupId>
<artifactId>mcp</artifactId>
<version>${project.version}</version>
</dependency>

<!-- Spring Web for HTTP server -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.2.3</version>
</dependency>

<!-- Logging -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.2.3</version>
</plugin>
</plugins>
</build>
</project>
3 changes: 3 additions & 0 deletions 3 examples/auth-example/run-client.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
cd "$(dirname "$0")"
mvn exec:java -Dexec.mainClass="io.modelcontextprotocol.examples.auth.client.SimpleAuthClient"
3 changes: 3 additions & 0 deletions 3 examples/auth-example/run-server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
cd "$(dirname "$0")"
mvn exec:java -Dexec.mainClass="io.modelcontextprotocol.examples.auth.server.SimpleAuthServer"
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.