|
| 1 | +# Simple Auth Client Example |
| 2 | + |
| 3 | +This example demonstrates how to use the MCP Python SDK to create a client that connects to an MCP server using 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 | +- Tool listing and execution |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +1. Python 3.9 or higher |
| 15 | +2. An MCP server that supports OAuth authentication (like `mcp-simple-auth`) |
| 16 | +3. uv for dependency management |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +```bash |
| 21 | +cd examples/clients/simple-auth-client |
| 22 | +uv install |
| 23 | +``` |
| 24 | + |
| 25 | +## Usage |
| 26 | + |
| 27 | +### 1. Start the Auth Server |
| 28 | + |
| 29 | +First, start the MCP auth server in another terminal: |
| 30 | + |
| 31 | +```bash |
| 32 | +cd path/to/mcp-simple-auth |
| 33 | +uv run mcp-simple-auth --transport streamable-http --port 3001 |
| 34 | +``` |
| 35 | + |
| 36 | +### 2. Run the Client |
| 37 | + |
| 38 | +```bash |
| 39 | +# Run the client |
| 40 | +uv run mcp-simple-auth-client |
| 41 | + |
| 42 | +# Or with custom server URL |
| 43 | +MCP_SERVER_URL=http://localhost:3001 uv run mcp-simple-auth-client |
| 44 | +``` |
| 45 | + |
| 46 | +### 3. Authentication Flow |
| 47 | + |
| 48 | +1. The client will attempt to connect to the server |
| 49 | +2. If authentication is required, the client will open your default browser |
| 50 | +3. Complete the OAuth flow in the browser |
| 51 | +4. Return to the client - it should now be connected |
| 52 | + |
| 53 | +### 4. Interactive Commands |
| 54 | + |
| 55 | +Once connected, you can use these commands: |
| 56 | + |
| 57 | +- `list` - List available tools from the server |
| 58 | +- `call <tool_name> [args]` - Call a tool with optional JSON arguments |
| 59 | +- `quit` - Exit the client |
| 60 | + |
| 61 | +### Example Session |
| 62 | + |
| 63 | +``` |
| 64 | +=� Simple MCP Auth Client |
| 65 | +Connecting to: http://localhost:3001 |
| 66 | +
|
| 67 | +Please visit the following URL to authorize the application: |
| 68 | +http://localhost:3001/authorize?response_type=code&client_id=... |
| 69 | +
|
| 70 | + Connected to MCP server at http://localhost:3001 |
| 71 | +Session ID: abc123 |
| 72 | +
|
| 73 | +<� Interactive MCP Client |
| 74 | +Commands: |
| 75 | + list - List available tools |
| 76 | + call <tool_name> [args] - Call a tool |
| 77 | + quit - Exit the client |
| 78 | +
|
| 79 | +mcp> list |
| 80 | +
|
| 81 | +=� Available tools: |
| 82 | +1. echo |
| 83 | + Description: Echo back the input text |
| 84 | +
|
| 85 | +mcp> call echo {"text": "Hello, world!"} |
| 86 | +
|
| 87 | +=' Tool 'echo' result: |
| 88 | +Hello, world! |
| 89 | +
|
| 90 | +mcp> quit |
| 91 | +=K Goodbye! |
| 92 | +``` |
| 93 | + |
| 94 | +## Configuration |
| 95 | + |
| 96 | +You can customize the client behavior with environment variables: |
| 97 | + |
| 98 | +- `MCP_SERVER_URL` - Server URL (default: http://localhost:3001) |
| 99 | +- `AUTH_CODE` - Authorization code for completing OAuth flow |
| 100 | + |
| 101 | +## Implementation Details |
| 102 | + |
| 103 | +This example shows how to: |
| 104 | + |
| 105 | +1. **Create an OAuth provider** - Implement the `OAuthClientProvider` protocol |
| 106 | +2. **Use streamable HTTP transport** - Connect using the `streamablehttp_client` context manager |
| 107 | +3. **Handle authentication** - Manage OAuth flow including browser redirect |
| 108 | +4. **Interactive tool usage** - List and call tools from the command line |
| 109 | + |
| 110 | +The key components are: |
| 111 | + |
| 112 | +- `SimpleOAuthProvider` - Minimal OAuth provider implementation |
| 113 | +- `SimpleAuthClient` - Main client class that handles connection and tool operations |
| 114 | +- Interactive loop for user commands |
| 115 | + |
| 116 | +## Error Handling |
| 117 | + |
| 118 | +The client handles common error scenarios: |
| 119 | + |
| 120 | +- Server connection failures |
| 121 | +- Authentication errors |
| 122 | +- Invalid tool calls |
| 123 | +- Network timeouts |
| 124 | + |
| 125 | +All errors are displayed with helpful messages to guide debugging. |
0 commit comments