shopify-mcp-server

RaheesAhmed/shopify-mcp-server Checked 2 days ago

A production-ready MCP server that provides secure, scalable access to Shopify store data for reporting and analytics.

A production-ready MCP server that provides secure, scalable access to Shopify store data for reporting and analytics. Built on FastMCP with enterprise-grade security, role-based access control, and structured reporting. Server runs at http://localhost:8000 with MCP endpoint at /mcp. Sales - total_sales — Total revenue including taxes and shipping - net_sales — Sales minus discounts and returns - average_order_value — Average value per order

Summary extracted from the README.

Shopify Reporting MCP Server

Phase Python FastMCP

Phase 1: Foundation — Core infrastructure for multi-store Shopify reporting and analytics via MCP.

Overview

A production-ready MCP server that provides secure, scalable access to Shopify store data for reporting and analytics. Built on FastMCP with enterprise-grade security, role-based access control, and structured reporting.

Quick Start

Clone the repository:

git clone https://github.com/RaheesAhmed/shopify-mcp-server.git
cd shopify-mcp-server
# Install dependencies
uv sync

# Configure environment
cp .env.example .env
# Edit .env with your Shopify credentials

# Generate encryption key
python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"

# Start server
uv run server.py

Server runs at http://localhost:8000 with MCP endpoint at /mcp.

Features

Feature Implementation
Multi-Store OAuth Shopify OAuth 2.0 with HMAC validation and state management
Token Security AES-256 encryption (Fernet) with per-store key scoping
Access Control Role-based permissions (agency_admin, client_viewer, internal_admin)
Metrics 21 pre-defined Shopify metrics across 7 categories
Reporting JSON and Markdown output formatters
Logging Structured logging with request tracing
Error Handling Standardized error codes with detailed context

Architecture

shopify-reporting-mcp/
├── server.py              # MCP server bootstrap
├── auth/                  # Authentication & authorization
│   ├── encryption.py      # AES-256 token encryption
│   ├── oauth.py           # Shopify OAuth 2.0 flow
│   └── rbac.py            # Role-based access control
├── database/              # Data persistence
│   ├── connection.py      # SQLite connection pool
│   └── migrations.py      # Schema migrations (v1-v5)
├── metrics/               # Metric definitions
│   └── registry.py        # 21 Shopify metrics
├── models/                # Data models
│   ├── entities.py        # Client, Store entities
│   └── audit.py           # Audit log models
├── formatters/            # Report output
│   ├── json_formatter.py  # JSON reports
│   └── markdown_formatter.py  # Markdown reports
├── tools/                 # MCP tools
│   └── shopify.py         # Shopify integration tools
└── utils/                 # Utilities
    ├── logging.py         # Structured logging
    └── errors.py          # Error handling

Configuration

Required Variables

# Shopify App
SHOPIFY_API_KEY=your_api_key
SHOPIFY_API_SECRET=your_api_secret
SHOPIFY_APP_URL=https://your-app.com

# OAuth
OAUTH_REDIRECT_URL=https://your-app.com/auth/callback
OAUTH_SCOPES=read_orders,read_products,read_customers,read_analytics

# Security
ENCRYPTION_KEY=your_fernet_key_here=  # 32-byte base64
JWT_SECRET=your_jwt_secret

# Server
HOST=0.0.0.0
PORT=8000
LOG_LEVEL=INFO

Optional Variables

DATABASE_URL=sqlite+aiosqlite:///data/shopify_mcp.db
JWT_ALGORITHM=HS256
JWT_EXPIRATION_HOURS=24

API Reference

MCP Tools

Tool Description
shopify_validate_store Validate Shopify store domain
shopify_get_auth_url Generate OAuth authorization URL
shopify_list_metrics List all available metrics
shopify_get_metric_definition Get metric details by ID
shopify_search_metrics Search metrics by name
shopify_generate_json_report Generate JSON report
shopify_generate_markdown_report Generate Markdown report
shopify_list_roles List RBAC roles
shopify_server_status Get server status

MCP Resources

URI Description
config://app-settings Server configuration
status://health Health status
metrics://definitions All metric definitions
rbac://roles Role permissions

HTTP Endpoints

Method Endpoint Description
GET /health Health check
GET /metrics List metrics
GET /metrics/{id} Get metric
GET /auth/shopify?shop={domain} OAuth init
GET /auth/callback OAuth callback

Metrics Reference

Sales

  • total_sales — Total revenue including taxes and shipping
  • net_sales — Sales minus discounts and returns
  • average_order_value — Average value per order

Orders

  • total_orders — Order count
  • orders_by_status — Orders grouped by status
  • conversion_rate — Session to order percentage

Products

  • total_products — Active product count
  • top_selling_products — Best performing products
  • product_inventory_value — Total inventory value

Customers

  • total_customers — Unique customers
  • new_customers — First-time customers
  • returning_customers — Repeat customers
  • customer_lifetime_value — Average customer spend

Inventory

  • inventory_quantity — Total units in stock
  • low_stock_products — Below threshold count
  • inventory_turnover — Stock replacement rate

Analytics

  • sessions — Website sessions
  • page_views — Total page views

Finance

  • total_refunds — Refund amount
  • total_discounts — Discount amount
  • gross_profit — Net sales minus COGS

Access Control

Roles

Role Permissions
agency_admin Full store management, report creation, data access
client_viewer Read-only access to assigned stores
internal_admin Full system access

Permission Types

# Store management
STORE_CONNECT, STORE_DISCONNECT, STORE_VIEW, STORE_LIST

# Reports
REPORT_VIEW, REPORT_CREATE, REPORT_EXPORT, REPORT_SCHEDULE

# Data access
DATA_ORDERS, DATA_PRODUCTS, DATA_CUSTOMERS, DATA_ANALYTICS, DATA_INVENTORY

# Admin
ADMIN_MANAGE_USERS, ADMIN_MANAGE_ROLES, ADMIN_AUDIT_LOGS

Report Formats

JSON

{
  "report": {
    "metadata": {
      "title": "Monthly Sales Report",
      "generated_at": "2024-01-15T10:30:00Z",
      "store_name": "My Store"
    },
    "data": {
      "total_sales": 12500.00,
      "total_orders": 150
    }
  }
}

Markdown

# Monthly Sales Report

## Report Information
- **Generated:** 2024-01-15 10:30:00 UTC
- **Store:** My Store

## Summary
| Metric | Value |
|--------|-------|
| Total Sales | $12,500.00 |
| Total Orders | 150 |

Development

Testing

# Health check
curl http://localhost:8000/health

# List metrics
curl http://localhost:8000/metrics

# MCP Inspector
npx @modelcontextprotocol/inspector http://localhost:8000/mcp

Project Roadmap

Phase Status Description
1 Complete Foundation — Auth, RBAC, metrics registry, formatters
2 Planned GraphQL integration — Data ingestion pipelines
3 Planned Report generation — Multi-store aggregation
4 Planned Scheduling — Background job processing
5 Planned NLP interface — LLM-powered queries
6 Planned Analytics — ML predictions
7 Planned Portal UI — Web interface
8 Planned Compliance — Audit trails, PII handling