Skip to content

Commit 2bf5586

Browse files
authored
Merge branch 'main' into custom-http-client
2 parents 048c968 + 2ca2de7 commit 2bf5586

File tree

20 files changed

+2570
-14
lines changed

20 files changed

+2570
-14
lines changed

README.md

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

799-
## Configuring the HTTP client
799+
### Configuring the HTTP client
800800

801801
It is possible to override the httpx client to customize for your needs.
802802

@@ -819,6 +819,60 @@ http_client = httpx.AsyncClient(
819819
streamable_client = streamablehttp_client("/v1/mcp", http_client=http_client)
820820
```
821821

822+
### OAuth Authentication for Clients
823+
824+
The SDK includes [authorization support](https://modelcontextprotocol.io/specification/2025-03-26/basic/authorization) for connecting to protected MCP servers:
825+
826+
```python
827+
from mcp.client.auth import OAuthClientProvider, TokenStorage
828+
from mcp.client.session import ClientSession
829+
from mcp.client.streamable_http import streamablehttp_client
830+
from mcp.shared.auth import OAuthClientInformationFull, OAuthClientMetadata, OAuthToken
831+
832+
833+
class CustomTokenStorage(TokenStorage):
834+
"""Simple in-memory token storage implementation."""
835+
836+
async def get_tokens(self) -> OAuthToken | None:
837+
pass
838+
839+
async def set_tokens(self, tokens: OAuthToken) -> None:
840+
pass
841+
842+
async def get_client_info(self) -> OAuthClientInformationFull | None:
843+
pass
844+
845+
async def set_client_info(self, client_info: OAuthClientInformationFull) -> None:
846+
pass
847+
848+
849+
async def main():
850+
# Set up OAuth authentication
851+
oauth_auth = OAuthClientProvider(
852+
server_url="https://api.example.com",
853+
client_metadata=OAuthClientMetadata(
854+
client_name="My Client",
855+
redirect_uris=["http://localhost:3000/callback"],
856+
grant_types=["authorization_code", "refresh_token"],
857+
response_types=["code"],
858+
),
859+
storage=CustomTokenStorage(),
860+
redirect_handler=lambda url: print(f"Visit: {url}"),
861+
callback_handler=lambda: ("auth_code", None),
862+
)
863+
864+
# Use with streamable HTTP client
865+
async with streamablehttp_client(
866+
"https://api.example.com/mcp", auth=oauth_auth
867+
) as (read, write, _):
868+
async with ClientSession(read, write) as session:
869+
await session.initialize()
870+
# Authenticated session ready
871+
```
872+
873+
For a complete working example, see [`examples/clients/simple-auth-client/`](examples/clients/simple-auth-client/).
874+
875+
822876
### MCP Primitives
823877

824878
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)