GA4-MCP-Sever

HMBaytam/GA4-MCP-Sever

3.2

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

  1. Identify the Module: Determine which module the feature belongs to
  2. Follow Patterns: Use existing patterns for error handling, logging, validation
  3. Write Tests: Add comprehensive tests in appropriate test directory
  4. 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