Skip to content

Commit 3adff13

Browse files
authored
Merge branch 'main' into nate/fix-pagination-cursor
2 parents 82f35ef + 2ca2de7 commit 3adff13

File tree

20 files changed

+2569
-13
lines changed

20 files changed

+2569
-13
lines changed

README.md

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

799+
### OAuth Authentication for Clients
800+
801+
The SDK includes [authorization support](https://modelcontextprotocol.io/specification/2025-03-26/basic/authorization) for connecting to protected MCP servers:
802+
803+
```python
804+
from mcp.client.auth import OAuthClientProvider, TokenStorage
805+
from mcp.client.session import ClientSession
806+
from mcp.client.streamable_http import streamablehttp_client
807+
from mcp.shared.auth import OAuthClientInformationFull, OAuthClientMetadata, OAuthToken
808+
809+
810+
class CustomTokenStorage(TokenStorage):
811+
"""Simple in-memory token storage implementation."""
812+
813+
async def get_tokens(self) -> OAuthToken | None:
814+
pass
815+
816+
async def set_tokens(self, tokens: OAuthToken) -> None:
817+
pass
818+
819+
async def get_client_info(self) -> OAuthClientInformationFull | None:
820+
pass
821+
822+
async def set_client_info(self, client_info: OAuthClientInformationFull) -> None:
823+
pass
824+
825+
826+
async def main():
827+
# Set up OAuth authentication
828+
oauth_auth = OAuthClientProvider(
829+
server_url="https://api.example.com",
830+
client_metadata=OAuthClientMetadata(
831+
client_name="My Client",
832+
redirect_uris=["http://localhost:3000/callback"],
833+
grant_types=["authorization_code", "refresh_token"],
834+
response_types=["code"],
835+
),
836+
storage=CustomTokenStorage(),
837+
redirect_handler=lambda url: print(f"Visit: {url}"),
838+
callback_handler=lambda: ("auth_code", None),
839+
)
840+
841+
# Use with streamable HTTP client
842+
async with streamablehttp_client(
843+
"https://api.example.com/mcp", auth=oauth_auth
844+
) as (read, write, _):
845+
async with ClientSession(read, write) as session:
846+
await session.initialize()
847+
# Authenticated session ready
848+
```
849+
850+
For a complete working example, see [`examples/clients/simple-auth-client/`](examples/clients/simple-auth-client/).
851+
852+
799853
### MCP Primitives
800854

801855
The MCP protocol defines three core primitives that servers can implement:
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Simple Auth Client Example
2+
3+
A demonstration of how to use the MCP Python SDK with OAuth authentication over streamable HTTP or SSE transport.
4+
5+
## Features
6+
7+
- OAuth 2.0 authentication with PKCE
8+
- Support for both StreamableHTTP and SSE transports
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_PORT=3001 uv run mcp-simple-auth-client
35+
36+
# Use SSE transport
37+
MCP_TRANSPORT_TYPE=sse uv run mcp-simple-auth-client
38+
```
39+
40+
### 3. Complete OAuth flow
41+
42+
The client will open your browser for authentication. After completing OAuth, you can use commands:
43+
44+
- `list` - List available tools
45+
- `call <tool_name> [args]` - Call a tool with optional JSON arguments
46+
- `quit` - Exit
47+
48+
## Example
49+
50+
```
51+
🔐 Simple MCP Auth Client
52+
Connecting to: http://localhost:3001
53+
54+
Please visit the following URL to authorize the application:
55+
http://localhost:3001/authorize?response_type=code&client_id=...
56+
57+
✅ Connected to MCP server at http://localhost:3001
58+
59+
mcp> list
60+
📋 Available tools:
61+
1. echo - Echo back the input text
62+
63+
mcp> call echo {"text": "Hello, world!"}
64+
🔧 Tool 'echo' result:
65+
Hello, world!
66+
67+
mcp> quit
68+
👋 Goodbye!
69+
```
70+
71+
## Configuration
72+
73+
- `MCP_SERVER_PORT` - Server URL (default: 8000)
74+
- `MCP_TRANSPORT_TYPE` - Transport type: `streamable_http` (default) or `sse`
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)