Google Authentication
📌 Overview¶
This tutorial demonstrates building an HTTP-streamable MCP server with FastMCP, with a focus on demonstrating how to authenticate with Google OAuth. It includes:
- Google OAuth authentication using the GoogleProvider
- A
get_user_infotool that retrieves the authenticated Google user's information - Automatic token validation and user data extraction from Google tokens
- A complete uv project structure with
pyproject.tomlfor dependency management
Note: For fundamental concepts about authentication, server setup, CORS, and deployment, see MCP Fundamentals.
See the full example here: https://github.com/Unique-AG/ai/tree/main/tutorials/mcp/google_auth
🚀 Quick Start¶
-
Install dependencies:
-
Configure environment variables: Create a
.envfile with your Google OAuth credentials: -
Run the server:
📌 Implementation¶
This section provides code snippets for the Google authentication-specific implementation. The project follows a standard uv project structure with the main server code in src/google_auth/google_auth_server.py. For setup details on authentication, CORS, and server configuration, refer to MCP Fundamentals.
📁 Project Structure¶
The project is structured as a uv-managed Python package:
🔧 Environment Configuration¶
Load Google OAuth credentials from environment variables:
🔐 Auth: Google Provider¶
This example demonstrates Google OAuth authentication using the GoogleProvider. The GoogleProvider handles Google's token format and validation automatically:
⚙️ FastMCP Server Init¶
Create the MCP server with Google authentication:
🌐 CORS Middleware¶
Enable CORS for browser-based clients:
👤 Get User Info Tool¶
Demonstrates accessing authenticated Google user information from token claims:
Key Points:
- The
get_access_token()dependency provides access to the authenticated token - The GoogleProvider automatically extracts and stores user information in
token.claims - User data includes Google ID, email, name, picture, and locale
🚀 Running the Server¶
The server includes a main() function that can be called programmatically or run directly:
Running the server:
-
Using uv run (recommended):
-
Using the installed script (after
uv sync): -
Direct Python execution:
🔑 Google OAuth Setup¶
To use this example, you need to:
-
Create a Google OAuth Client:
-
Go to the Google Cloud Console
- Create a new project or select an existing one
- Enable the Google+ API
- Go to "Credentials" and create an OAuth 2.0 Client ID
-
Set authorized redirect URIs to match your
BASE_URL(e.g.,http://localhost:8003/auth/callback) -
Configure Environment Variables: Create a
.envfile in the project root with: -
Required Scopes:
-
openid: Required for OpenID Connect authentication https://www.googleapis.com/auth/userinfo.email: Required to access user email information
📦 Dependencies¶
The project uses the following dependencies (managed via pyproject.toml):
- fastapi>=0.120.2 - Web framework
- fastmcp>=2.13.0.2 - FastMCP server framework
- pydantic>=2.12.3 - Data validation
- python-dotenv>=1.0.0 - Environment variable management
Install all dependencies with:
For deployment considerations and configuration details, see MCP Fundamentals.