Google Authentication & Security
Relevant source files
The following files were used as context for generating this wiki page:
- packages/google-workspace-mcp/README.md
- packages/google-workspace-mcp/src/google_workspace_mcp/auth/init.py
- packages/google-workspace-mcp/src/google_workspace_mcp/services/base.py
- packages/google-workspace-mcp/src/google_workspace_mcp/services/docs_service.py
- packages/google-workspace-mcp/pyproject.toml
- packages/markdowndeck/README.md
Introduction
Google Authentication & Security within the google-workspace-mcp package provides a standardized, secure bridge for AI assistants to interact with Google Workspace services via the Model Context Protocol (MCP). The system centralizes OAuth 2.0 credential management, ensuring that sensitive API keys and secrets remain isolated on the server rather than being exposed to AI models. This architecture allows AI models to perform authorized actions on behalf of a user across Drive, Docs, Gmail, Calendar, Sheets, and Slides while maintaining strict security boundaries.
The implementation relies on the google-auth library suite to manage token flows and provides a unified BaseGoogleService that handles lazy-loading of authenticated API clients. This ensures that every tool call, from searching files in Drive to sending emails in Gmail, is backed by valid, scoped credentials.
Sources: packages/google-workspace-mcp/README.md, packages/google-workspace-mcp/src/google_workspace_mcp/services/base.py
Authentication Architecture
The authentication system is built on a multi-tier architecture that separates credential acquisition from service execution. It supports various credential types, including Service Accounts for automated workflows and OAuth 2.0 user credentials for interactive applications.
Credential Management Flow
The package utilizes a centralized utility, get_credentials, to retrieve valid Google API credentials. This utility is the single entry point for all service classes requiring authorization.
The diagram shows how environment variables flow into the authentication utility to build the API clients used by the various services.
Sources: packages/google-workspace-mcp/src/google_workspace_mcp/auth/init.py, packages/google-workspace-mcp/src/google_workspace_mcp/services/base.py:28-34
Supported Authentication Methods
The system prioritizes different credential sources to provide flexibility across development and production environments:
| Method | Source | Use Case |
|---|---|---|
| Service Account | GOOGLE_APPLICATION_CREDENTIALS | Server-side automated workflows. |
| OAuth Env Variables | CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN | Cloud deployments and MCP worker environments. |
| Local Token Files | ~/.markdowndeck/token.json | Local development and interactive CLI usage. |
Sources: packages/markdowndeck/README.md, packages/google-workspace-mcp/README.md
Service Security & Error Handling
Security is enforced at the service level through the BaseGoogleService class, which implements standardized error handling and lazy credential loading.
BaseGoogleService Logic
The BaseGoogleService ensures that the Google API client is only initialized when needed. It also provides a robust handle_api_error method to translate raw HttpError responses from Google into structured, safe error messages for the MCP server.
This sequence illustrates the lazy-loading pattern used to ensure credentials are valid at the moment of API execution.
Sources: packages/google-workspace-mcp/src/google_workspace_mcp/services/base.py:16-64
Standardized Error Responses
When an API call fails (e.g., due to insufficient permissions or expired tokens), the system captures the HttpError and extracts the status code and reason to prevent sensitive stack traces from leaking to the AI model.
def handle_api_error(self, operation: str, error: Exception) -> dict[str, Any]:
if isinstance(error, HttpError):
error_details = {
"error": True,
"operation": operation,
"status_code": error.resp.status,
"reason": error.resp.reason,
"message": str(error),
}
# ... logs error and returns structured dict
Sources: packages/google-workspace-mcp/src/google_workspace_mcp/services/base.py:44-55
Required API Scopes
To function correctly, the OAuth 2.0 refresh token must be generated with specific scopes corresponding to the enabled capabilities. The following scopes are essential for full functionality:
https://www.googleapis.com/auth/drive(Drive & Docs export)https://www.googleapis.com/auth/gmail.modify(Read, send, and delete emails)https://www.googleapis.com/auth/calendar(Manage events)https://www.googleapis.com/auth/docs(Edit documents)https://www.googleapis.com/auth/spreadsheets(Edit sheets)https://www.googleapis.com/auth/presentations(Create slides)
Sources: packages/google-workspace-mcp/README.md
Configuration and Environment Variables
The authentication behavior is primarily controlled via environment variables. These variables are consumed by the gauth module to authenticate the server session.
| Variable | Description | Requirement |
|---|---|---|
GOOGLE_WORKSPACE_CLIENT_ID | OAuth 2.0 Client ID from Google Cloud Console. | Required for OAuth flow. |
GOOGLE_WORKSPACE_CLIENT_SECRET | OAuth 2.0 Client Secret. | Required for OAuth flow. |
GOOGLE_WORKSPACE_REFRESH_TOKEN | Long-lived token used to generate access tokens. | Required for non-interactive use. |
GOOGLE_APPLICATION_CREDENTIALS | Path to service account JSON key file. | Optional (alternative to OAuth). |
Sources: packages/google-workspace-mcp/README.md, packages/markdowndeck/README.md
Conclusion
Google Authentication & Security in this project provides a robust framework for managing enterprise-grade access to Workspace data. By leveraging a lazy-loading service architecture and centralized credential management, the system ensures that AI-driven interactions remain secure, authorized, and transparently handled through standard Google API protocols.