HMBaytam/GA4-MCP-Sever
If you are the rightful owner of GA4-MCP-Sever and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
A modular, maintainable Google Analytics 4 MCP (Model Context Protocol) server built following Clean Code principles.
GA4 MCP Server
A modular, maintainable Google Analytics 4 MCP (Model Context Protocol) server built following Clean Code principles.
๐โโ๏ธ Getting Started
Prerequisits
To be able to use your own instance of this MCP server you need to create your own Google Credentials and obtain the Client ID and Client Secret. Steps to create Google Cloud Client Credientials
Once you have obtained the Client ID and Client Seceret, add them to the .env.example file then change the name to .env
Quick Start
# Install dependencies
pip install -r requirements.txt
# Set environment variables
export GA4_CLIENT_ID="your_client_id"
export GA4_CLIENT_SECRET="your_client_secret"
# Run the server
fastmcp run app.py:mcp
Install MCP to Claude Desktop
fastmcp install app.py:mcp
๐๏ธ Architecture Overview
The application has been completely refactored from a single 734-line file into a modular architecture:
backend/
โโโ src/ # Main application code
โ โโโ auth/ # Authentication modules
โ โ โโโ oauth_manager.py # OAuth flow management
โ โ โโโ credentials_manager.py # Credential storage/retrieval
โ โโโ analytics/ # GA4 data processing
โ โ โโโ ga4_client.py # GA4 API client wrapper
โ โ โโโ report_builder.py # Request building logic
โ โ โโโ data_formatter.py # Response formatting
โ โโโ config/ # Configuration management
โ โ โโโ settings.py # Environment variable handling
โ โ โโโ constants.py # Application constants
โ โโโ utils/ # Utility modules
โ โ โโโ errors.py # Custom exception classes
โ โ โโโ logging.py # Logging configuration
โ โ โโโ validation.py # Input validation
โ โโโ server.py # FastMCP server setup
โ โโโ main.py # Application entry point
โโโ tests/ # Organized test suite
โ โโโ conftest.py # Shared test fixtures
โ โโโ test_auth/ # Authentication tests
โ โโโ test_analytics/ # Analytics tests
โ โโโ test_integration/ # End-to-end tests
โโโ app_refactored.py # Backward compatibility wrapper
โโโ requirements.txt
๐ Key Features
Authentication Management
# OAuth flow management
oauth_manager = OAuthManager(settings, credentials_manager)
auth_url = oauth_manager.start_oauth_flow()
credentials = oauth_manager.complete_oauth_flow(auth_code)
# Credential persistence
credentials_manager = CredentialsManager()
credentials_manager.save_credentials(creds)
loaded_creds = credentials_manager.load_credentials()
Analytics Data Retrieval
# GA4 client with validation
ga4_client = GA4Client(credentials)
report = ga4_client.get_standard_report(
property_id="123456789",
start_date="7daysAgo",
end_date="today",
metrics="sessions,users",
dimensions="date"
)
Configuration Management
# Centralized settings
settings = Settings()
client_config = settings.get_oauth_client_config()
debug_info = settings.get_debug_info()
๐งช Testing Structure
Organized Test Suite
- Unit Tests: Individual module testing
- Integration Tests: End-to-end workflow testing
- Fixtures: Reusable test data and mocks in
conftest.py
- Coverage: Comprehensive test coverage for all modules
Running Tests
# Run all tests
pytest
# Run specific test module
pytest tests/test_auth/
# Run with coverage
pytest --cov=src tests/
๐ ๏ธ Development Guidelines
Adding New Features
- Identify the Module: Determine which module the feature belongs to
- Follow Patterns: Use existing patterns for error handling, logging, validation
- Write Tests: Add comprehensive tests in appropriate test directory
- Update Documentation: Add docstrings and update README if needed
Code Standards
- Type Hints: All public methods must have type hints
- Docstrings: All public methods must have descriptive docstrings
- Error Handling: Use custom exceptions from
utils.errors
- Logging: Use structured logging from
utils.logging
- Validation: Validate all inputs using
utils.validation
๐ Future Enhancements
The modular architecture enables easy extension:
- New Analytics Features: Add to
analytics/
module - Additional Authentication Methods: Extend
auth/
module - Enhanced Validation: Expand
utils/validation.py
- Better Error Handling: Add specific error types to
utils/errors.py
- Caching: Add caching layer to improve performance
- Rate Limiting: Implement API rate limiting