Skip to content

Commit 8bc6394

Browse files
committed
2 parents ed11262 + 61756b8 commit 8bc6394

File tree

10 files changed

+2483
-1
lines changed

10 files changed

+2483
-1
lines changed

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,60 @@ async def main():
876876
tool_result = await session.call_tool("echo", {"message": "hello"})
877877
```
878878

879+
### OAuth Authentication for Clients
880+
881+
The SDK includes [authorization support](https://modelcontextprotocol.io/specification/2025-03-26/basic/authorization) for connecting to protected MCP servers:
882+
883+
```python
884+
from mcp.client.auth import OAuthClientProvider, TokenStorage
885+
from mcp.client.session import ClientSession
886+
from mcp.client.streamable_http import streamablehttp_client
887+
from mcp.shared.auth import OAuthClientInformationFull, OAuthClientMetadata, OAuthToken
888+
889+
890+
class CustomTokenStorage(TokenStorage):
891+
"""Simple in-memory token storage implementation."""
892+
893+
async def get_tokens(self) -> OAuthToken | None:
894+
pass
895+
896+
async def set_tokens(self, tokens: OAuthToken) -> None:
897+
pass
898+
899+
async def get_client_info(self) -> OAuthClientInformationFull | None:
900+
pass
901+
902+
async def set_client_info(self, client_info: OAuthClientInformationFull) -> None:
903+
pass
904+
905+
906+
async def main():
907+
# Set up OAuth authentication
908+
oauth_auth = OAuthClientProvider(
909+
server_url="https://api.example.com",
910+
client_metadata=OAuthClientMetadata(
911+
client_name="My Client",
912+
redirect_uris=["http://localhost:3000/callback"],
913+
grant_types=["authorization_code", "refresh_token"],
914+
response_types=["code"],
915+
),
916+
storage=CustomTokenStorage(),
917+
redirect_handler=lambda url: print(f"Visit: {url}"),
918+
callback_handler=lambda: ("auth_code", None),
919+
)
920+
921+
# Use with streamable HTTP client
922+
async with streamablehttp_client(
923+
"https://api.example.com/mcp", auth=oauth_auth
924+
) as (read, write, _):
925+
async with ClientSession(read, write) as session:
926+
await session.initialize()
927+
# Authenticated session ready
928+
```
929+
930+
For a complete working example, see [`examples/clients/simple-auth-client/`](examples/clients/simple-auth-client/).
931+
932+
879933
### MCP Primitives
880934

881935
The MCP protocol defines three core primitives that servers can implement:
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Simple Auth Client Example
2+
3+
A demonstration of how to use the MCP Python SDK with OAuth authentication over streamable HTTP transport.
4+
5+
## Features
6+
7+
- OAuth 2.0 authentication with PKCE
8+
- Streamable HTTP transport
9+
- Interactive command-line interface
10+
11+
## Installation
12+
13+
```bash
14+
cd examples/clients/simple-auth-client
15+
uv sync --reinstall
16+
```
17+
18+
## Usage
19+
20+
### 1. Start an MCP server with OAuth support
21+
22+
```bash
23+
# Example with mcp-simple-auth
24+
cd path/to/mcp-simple-auth
25+
uv run mcp-simple-auth --transport streamable-http --port 3001
26+
```
27+
28+
### 2. Run the client
29+
30+
```bash
31+
uv run mcp-simple-auth-client
32+
33+
# Or with custom server URL
34+
MCP_SERVER_URL=http://localhost:3001 uv run mcp-simple-auth-client
35+
```
36+
37+
### 3. Complete OAuth flow
38+
39+
The client will open your browser for authentication. After completing OAuth, you can use commands:
40+
41+
- `list` - List available tools
42+
- `call <tool_name> [args]` - Call a tool with optional JSON arguments
43+
- `quit` - Exit
44+
45+
## Example
46+
47+
```
48+
🔐 Simple MCP Auth Client
49+
Connecting to: http://localhost:3001
50+
51+
Please visit the following URL to authorize the application:
52+
http://localhost:3001/authorize?response_type=code&client_id=...
53+
54+
✅ Connected to MCP server at http://localhost:3001
55+
56+
mcp> list
57+
📋 Available tools:
58+
1. echo - Echo back the input text
59+
60+
mcp> call echo {"text": "Hello, world!"}
61+
🔧 Tool 'echo' result:
62+
Hello, world!
63+
64+
mcp> quit
65+
👋 Goodbye!
66+
```
67+
68+
## Configuration
69+
70+
- `MCP_SERVER_URL` - Server URL (default: http://localhost:3001)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Simple OAuth client for MCP simple-auth server."""

0 commit comments

Comments
 (0)