From e9afd5473a401bffd3b3ec76078a2e839658d80b Mon Sep 17 00:00:00 2001 From: Max Isbey <224885523+maxisbey@users.noreply.github.com> Date: Thu, 13 Nov 2025 11:41:48 +0000 Subject: [PATCH] Replace environment variables with CLI arguments in simple-auth-client The simple-auth-client example previously used environment variables (MCP_SERVER_PORT and MCP_TRANSPORT_TYPE) to configure the server URL and transport type. This approach was inflexible and couldn't handle arbitrary server URLs. This change replaces the environment variables with command-line arguments using argparse: - --url: Specifies the full URL of the MCP server - --transport: Specifies the transport type (streamable-http or sse) Benefits: - Users can now specify any server URL, not just localhost ports - More intuitive and discoverable interface with --help - Better follows CLI conventions All documentation (both client and server READMEs) has been updated to reflect the new command-line interface. --- examples/clients/simple-auth-client/README.md | 29 +++++++++------- .../mcp_simple_auth_client/main.py | 34 ++++++++++++------- examples/servers/simple-auth/README.md | 4 +-- 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/examples/clients/simple-auth-client/README.md b/examples/clients/simple-auth-client/README.md index 3e92f2947..1ede1c475 100644 --- a/examples/clients/simple-auth-client/README.md +++ b/examples/clients/simple-auth-client/README.md @@ -28,13 +28,17 @@ uv run mcp-simple-auth --transport streamable-http --port 3001 ### 2. Run the client ```bash +# Connect to default server (http://localhost:8000/mcp) uv run mcp-simple-auth-client -# Or with custom server URL -MCP_SERVER_PORT=3001 uv run mcp-simple-auth-client +# Connect to a custom server URL +uv run mcp-simple-auth-client --url http://localhost:3001/mcp # Use SSE transport -MCP_TRANSPORT_TYPE=sse uv run mcp-simple-auth-client +uv run mcp-simple-auth-client --url http://localhost:3001/sse --transport sse + +# View all options +uv run mcp-simple-auth-client --help ``` ### 3. Complete OAuth flow @@ -42,19 +46,20 @@ MCP_TRANSPORT_TYPE=sse uv run mcp-simple-auth-client The client will open your browser for authentication. After completing OAuth, you can use commands: - `list` - List available tools -- `call [args]` - Call a tool with optional JSON arguments +- `call [args]` - Call a tool with optional JSON arguments - `quit` - Exit ## Example ```markdown -🔐 Simple MCP Auth Client -Connecting to: http://localhost:3001 +🚀 Simple MCP Auth Client +Connecting to: http://localhost:8001/mcp +Transport type: streamable-http -Please visit the following URL to authorize the application: -http://localhost:3001/authorize?response_type=code&client_id=... +🔗 Attempting to connect to http://localhost:8001/mcp... +Opening browser for authorization: http://localhost:9000/authorize?... -✅ Connected to MCP server at http://localhost:3001 +✅ Connected to MCP server at http://localhost:8001/mcp mcp> list 📋 Available tools: @@ -68,7 +73,7 @@ mcp> quit 👋 Goodbye! ``` -## Configuration +## Command Line Options -- `MCP_SERVER_PORT` - Server URL (default: 8000) -- `MCP_TRANSPORT_TYPE` - Transport type: `streamable-http` (default) or `sse` +- `--url` - Full URL of the MCP server (default: `http://localhost:8000/mcp`) +- `--transport` - Transport type: `streamable-http` (default) or `sse` diff --git a/examples/clients/simple-auth-client/mcp_simple_auth_client/main.py b/examples/clients/simple-auth-client/mcp_simple_auth_client/main.py index 5987a878e..9f0a68ecf 100644 --- a/examples/clients/simple-auth-client/mcp_simple_auth_client/main.py +++ b/examples/clients/simple-auth-client/mcp_simple_auth_client/main.py @@ -6,8 +6,8 @@ """ +import argparse import asyncio -import os import threading import time import webbrowser @@ -331,22 +331,32 @@ async def interactive_loop(self): async def main(): """Main entry point.""" - # Default server URL - can be overridden with environment variable - # Most MCP streamable HTTP servers use /mcp as the endpoint - server_url = os.getenv("MCP_SERVER_PORT", 8000) - transport_type = os.getenv("MCP_TRANSPORT_TYPE", "streamable-http") - server_url = ( - f"http://localhost:{server_url}/mcp" - if transport_type == "streamable-http" - else f"http://localhost:{server_url}/sse" + parser = argparse.ArgumentParser( + description="Simple MCP client with OAuth authentication support", + formatter_class=argparse.ArgumentDefaultsHelpFormatter, ) + parser.add_argument( + "--url", + type=str, + default="http://localhost:8000/mcp", + help="Full URL of the MCP server", + ) + parser.add_argument( + "--transport", + type=str, + choices=["streamable-http", "sse"], + default="streamable-http", + help="Transport type to use", + ) + + args = parser.parse_args() print("🚀 Simple MCP Auth Client") - print(f"Connecting to: {server_url}") - print(f"Transport type: {transport_type}") + print(f"Connecting to: {args.url}") + print(f"Transport type: {args.transport}") # Start connection flow - OAuth will be handled automatically - client = SimpleAuthClient(server_url, transport_type) + client = SimpleAuthClient(args.url, args.transport) await client.connect() diff --git a/examples/servers/simple-auth/README.md b/examples/servers/simple-auth/README.md index b80e98a04..c307b5bde 100644 --- a/examples/servers/simple-auth/README.md +++ b/examples/servers/simple-auth/README.md @@ -43,7 +43,7 @@ uv run mcp-simple-auth-rs --port=8001 --auth-server=http://localhost:9000 --tra ```bash cd examples/clients/simple-auth-client # Start client with streamable HTTP -MCP_SERVER_PORT=8001 MCP_TRANSPORT_TYPE=streamable-http uv run mcp-simple-auth-client +uv run mcp-simple-auth-client --url http://localhost:8001/mcp --transport streamable-http ``` ## How It Works @@ -101,7 +101,7 @@ uv run mcp-simple-auth-legacy --port=8002 ```bash # Test with client (will automatically fall back to legacy discovery) cd examples/clients/simple-auth-client -MCP_SERVER_PORT=8002 MCP_TRANSPORT_TYPE=streamable-http uv run mcp-simple-auth-client +uv run mcp-simple-auth-client --url http://localhost:8002/mcp --transport streamable-http ``` The client will: