OAuth Client Example
This example demonstrates how to use the OAuth capabilities of the MCP Go client to authenticate with an MCP server that requires OAuth authentication.
Features
- OAuth 2.1 authentication with PKCE support
- Dynamic client registration
- Authorization code flow
- Token refresh
- Local callback server for handling OAuth redirects
Usage
# Set environment variables (optional)
export MCP_CLIENT_ID=your_client_id
export MCP_CLIENT_SECRET=your_client_secret
# Run the example
go run main.go
How it Works
- The client attempts to initialize a connection to the MCP server
- If the server requires OAuth authentication, it will return a 401 Unauthorized response
- The client detects this and starts the OAuth flow:
- Generates PKCE code verifier and challenge
- Generates a state parameter for security
- Opens a browser to the authorization URL
- Starts a local server to handle the callback
- The user authorizes the application in their browser
- The authorization server redirects back to the local callback server
- The client exchanges the authorization code for an access token
- The client retries the initialization with the access token
- The client can now make authenticated requests to the MCP server
Configuration
Edit the following constants in main.go
to match your environment:
const (
// Replace with your MCP server URL
serverURL = "https://api.example.com/v1/mcp"
// Use a localhost redirect URI for this example
redirectURI = "http://localhost:8085/oauth/callback"
)
OAuth Scopes
The example requests the following scopes:
mcp.read
- Read access to MCP resources
mcp.write
- Write access to MCP resources
You can modify the scopes in the oauthConfig
to match the requirements of your MCP server.