Skip to main content

Strava integration for MCP

Project description

Strava MCP Server

CI/CD Pipeline codecov

A Model Context Protocol (MCP) server for interacting with the Strava API.

Features

This MCP server provides tools to access data from the Strava API:

  • Get user's activities
  • Get a specific activity's details
  • Get the segments of an activity
  • Get the leaderboard of a segment

Installation

Prerequisites

  • Python 3.13 or later
  • Strava API credentials (client ID and client secret)

Setup

  1. Clone the repository:
git clone <repository-url>
cd strava
  1. Install dependencies with uv:
uv install
  1. Set up environment variables with your Strava API credentials:

Create a Strava app at https://www.strava.com/settings/api and obtain your client ID and client secret.

You can set environment variables directly:

export STRAVA_CLIENT_ID=your_client_id  # from https://www.strava.com/settings/api
export STRAVA_CLIENT_SECRET=your_client_secret  # from https://www.strava.com/settings/api

Alternatively, you can create a .env file in the root directory with these variables:

STRAVA_CLIENT_ID=your_client_id
STRAVA_CLIENT_SECRET=your_client_secret

The application will automatically load variables from the .env file if it exists.

Authentication

The server includes an automatic OAuth flow using a separate local web server:

  1. The first time you make a request to the Strava API, the system checks if you have a refresh token.
  2. If no refresh token is found, it automatically starts a standalone OAuth server and opens your browser to the Strava authorization page.
  3. After authorizing the application in your browser, you'll be redirected to a local callback page.
  4. The server automatically obtains and stores the refresh token for future use.

You can also get a refresh token manually by running:

python get_token.py

Or set the refresh token directly if you already have one:

export STRAVA_REFRESH_TOKEN=your_refresh_token

You can also add the refresh token to your .env file:

STRAVA_REFRESH_TOKEN=your_refresh_token

This approach eliminates the need to manually go through the authorization flow and copy/paste tokens. The OAuth flow uses your STRAVA_CLIENT_ID and STRAVA_CLIENT_SECRET environment variables.

Usage

Running the Server

To run the server:

# If installed as a package:
strava-mcp

# Or run the module directly:
python -m strava_mcp.main

Development Mode

You can run the server in development mode with the MCP CLI:

mcp dev strava_mcp/main.py

Installing in Claude Desktop

To install the server in Claude Desktop:

mcp install --env-file ~/.ssh/env-strava.sh strava_mcp/main.py

Where ~/.ssh/strava.sh contains your Strava credentials:

export STRAVA_CLIENT_ID=your_client_id
export STRAVA_CLIENT_SECRET=your_client_secret

Note that this will actually copy your credentials to your Claude Desktop configuration (/Users/<username>/Library/Application Support/Claude/claude_desktop_config.json).

Alternatively, you can add the following to your Claude Desktop configuration:

"strava": {
    "command": "bash",
    "args": [
        "-c",
        "source ~/.ssh/strava.sh && uv --directory /Users/yorrickjansen/work/mcp/strava run -m strava_mcp.main"
    ]
}

Where ~/.ssh/strava.sh contains your Strava credentials:

export STRAVA_CLIENT_ID=your_client_id
export STRAVA_CLIENT_SECRET=your_client_secret

This option is more secure as it doesn't expose your credentials in your configuration file.

Tools

Get User Activities

Retrieves a list of activities for the authenticated user.

Parameters:

  • before (optional): Epoch timestamp to filter activities before a certain time
  • after (optional): Epoch timestamp to filter activities after a certain time
  • page (optional): Page number (default: 1)
  • per_page (optional): Number of items per page (default: 30)

Get Activity

Gets detailed information about a specific activity.

Parameters:

  • activity_id: The ID of the activity
  • include_all_efforts (optional): Whether to include all segment efforts (default: false)

Get Activity Segments

Retrieves the segments from a specific activity.

Parameters:

  • activity_id: The ID of the activity

Get Segment Leaderboard

Gets the leaderboard for a specific segment.

Parameters:

  • segment_id: The ID of the segment
  • gender (optional): Filter by gender ('M' or 'F')
  • age_group (optional): Filter by age group
  • weight_class (optional): Filter by weight class
  • following (optional): Filter by friends of the authenticated athlete
  • club_id (optional): Filter by club
  • date_range (optional): Filter by date range
  • context_entries (optional): Number of context entries
  • page (optional): Page number (default: 1)
  • per_page (optional): Number of items per page (default: 30)

Development

Project Structure

  • strava_mcp/: Main package directory
    • __init__.py: Package initialization
    • config.py: Configuration settings using pydantic-settings
    • models.py: Pydantic models for Strava API entities
    • api.py: Low-level API client for Strava
    • auth.py: Strava OAuth authentication implementation
    • oauth_server.py: Standalone OAuth server implementation
    • service.py: Service layer for business logic
    • server.py: MCP server implementation
  • tests/: Unit tests
  • strava_mcp/main.py: Main entry point to run the server
  • get_token.py: Utility script to get a refresh token manually

Running Tests

To run tests:

pytest

Publishing to PyPI

To build and publish the package to PyPI, use uv:

Building the package

# Build both sdist and wheel
uv build

# Build only wheel
uv build --wheel

The built packages will be in the dist/ directory.

Publishing to PyPI

# Publish to Test PyPI first
uv publish --index testpypi

# Publish to PyPI
uv publish

Installing from PyPI

After publishing, your package can be installed with:

# From Test PyPI
pip install --index-url https://test.pypi.org/simple/ strava-mcp

# From PyPI
pip install strava-mcp

License

MIT License

Acknowledgements

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

strava_mcp-0.3.0.tar.gz (120.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

strava_mcp-0.3.0-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file strava_mcp-0.3.0.tar.gz.

File metadata

  • Download URL: strava_mcp-0.3.0.tar.gz
  • Upload date:
  • Size: 120.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.9

File hashes

Hashes for strava_mcp-0.3.0.tar.gz
Algorithm Hash digest
SHA256 a59c6300168010a726b2ec185fee2fc693b88a3ec850fd45c79ee826f8dad495
MD5 f04dbc3ea05aa4bc6654b8cdf037a5aa
BLAKE2b-256 d025941924f10968b72db8101ec2037088c60e4fb973e93dac04eab07b791b77

See more details on using hashes here.

File details

Details for the file strava_mcp-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: strava_mcp-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.9

File hashes

Hashes for strava_mcp-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 607bf07ed7593093732062d8f34e7a2b8a82d83d6716c93fa3b69e78f64783df
MD5 137ae3c158ffd6764f43035b972b3aec
BLAKE2b-256 cd521a34f68fed86aa63f4ce4311d8ca5ccadf836cc9e73fc1b3584ae224232b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page