Content-Length: 566433 | pFad | http://github.com/modelcontextprotocol/python-sdk/commit/e33cd41c7a0e9a8efe3aac4c0e1d5bdbb849cd15

D7 Add OAuth authentication client for HTTPX (#751) · modelcontextprotocol/python-sdk@e33cd41 · GitHub
Skip to content

Commit e33cd41

Browse files
ihrprpcarleton
andauthored
Add OAuth authentication client for HTTPX (#751)
Co-authored-by: Paul Carleton <paulc@anthropic.com>
1 parent 6353dd1 commit e33cd41

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
@@ -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: 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)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/modelcontextprotocol/python-sdk/commit/e33cd41c7a0e9a8efe3aac4c0e1d5bdbb849cd15

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy