Skip to main content
Glama
ESPN Fantasy Basketball MCP Server

ESPN Fantasy Basketball MCP Server

by dylancharris

ESPN Fantasy Basketball MCP Server

Tests Python 3.10+ Code style: ruff Type checked: mypy

An MCP (Model Context Protocol) server that provides access to ESPN Fantasy Basketball APIs. This server enables Claude and other MCP clients to fetch fantasy basketball data including league teams, player rosters, waiver wire players, matchup schedules, and NBA schedules.

Note: This is an unofficial third-party tool and is not affiliated with or endorsed by ESPN.

Features

Available Tools

Core Fantasy Tools

  1. get_league_teams - Get all teams in an ESPN Fantasy Basketball league

  2. get_team_roster - Get roster for a specific team

  3. get_free_agents - Get free agents/waiver wire players

  4. get_matchups - Get league matchup schedule

  5. get_nba_schedule - Get NBA game schedule

Live Draft Assistant Tools

  1. get_draft_status - Get current draft status including all picks and progress

  2. should_i_bid - Get recommendation on whether to bid for the current player being nominated

  3. who_should_i_target_next - Get recommendation on which player to target/nominate next

  4. analyze_my_draft_strategy - Analyze your current draft strategy and spending patterns

  5. get_available_players - Get top available players for the draft with auction values

Related MCP server: nba-mcp

Installation

Prerequisites

  • Python 3.10 or higher

  • uv package manager

Setup

  1. Clone this repository:

    git clone <repository-url>
    cd espn-fantasy-basketball-mcp
  2. Install dependencies using uv:

    uv sync

Usage

Running the Server

uv run espn_fantasy_basketball.py

Configuration for Claude Desktop

Add to your Claude Desktop configuration (~/Library/Application Support/Claude/claude_desktop_config.json):

{
  "mcpServers": {
    "espn-fantasy-basketball": {
      "command": "uv",
      "args": [
        "--directory",
        "/path/to/espn-fantasy-basketball-mcp",
        "run",
        "espn_fantasy_basketball.py"
      ],
      "env": {
        "ESPN_LEAGUE_ID": "your_league_id",
        "ESPN_TEAM_ID": "your_team_id",
        "ESPN_S2": "your_espn_s2_cookie",
        "ESPN_SWID": "your_swid_cookie"
      }
    }
  }
}

Draft Tools Usage

The live draft assistant tools are designed for auction drafts and help answer key questions during your draft:

πŸ’‘ Pro Tip: These tools work seamlessly with Claude Desktop! Ask Claude questions like "Should I bid on this player?" or "Who should I target next?" and it will use these tools automatically to give you expert draft advice.

πŸ” get_draft_status

Get the current state of your draft including all picks made so far.

# Get draft status
draft_status = await get_draft_status(
    league_id=123456,
    year=2025,
    espn_s2="your_espn_s2_cookie",  # for private leagues
    swid="your_swid_cookie"         # for private leagues
)

Returns:

  • inProgress: Whether draft is currently active

  • drafted: Whether draft is completed

  • picks: Array of all picks made so far with player details

  • currentPickNumber: Next pick number

  • currentNominatingTeam: Which team is nominating next

πŸ’° should_i_bid

Get AI-powered recommendation on whether to bid for the current player being nominated.

# Should I bid for this player?
recommendation = await should_i_bid(
    current_player_id=12345,        # Player being nominated
    team_id=1,                      # Your team ID (optional, uses ESPN_TEAM_ID env var)
    league_id=123456,               # Optional, uses ESPN_LEAGUE_ID env var
    year=2025,                      # Optional, uses ESPN_YEAR env var
    espn_s2="your_espn_s2_cookie",  # Optional, uses ESPN_S2 env var
    swid="your_swid_cookie"         # Optional, uses ESPN_SWID env var
)

Returns:

  • action: "bid" or "pass"

  • playerName: Name of the player

  • suggestedBid: Recommended bid amount

  • maxBid: Maximum you should bid

  • reasoning: Explanation of the recommendation

  • priority: Priority score (1-10)

🎯 who_should_i_target_next

Get recommendation on which player to nominate when it's your turn.

# Who should I target next?
recommendation = await who_should_i_target_next(
    team_id=1,                      # Your team ID (optional, uses ESPN_TEAM_ID env var)
    league_id=123456,               # Optional, uses ESPN_LEAGUE_ID env var
    year=2025,                      # Optional, uses ESPN_YEAR env var
    espn_s2="your_espn_s2_cookie",  # Optional, uses ESPN_S2 env var
    swid="your_swid_cookie"         # Optional, uses ESPN_SWID env var
)

Returns:

  • action: "nominate" or "pass"

  • playerId: Recommended player ID

  • playerName: Player name

  • suggestedBid: Recommended opening bid

  • reasoning: Why this player is recommended

  • priority: Priority score (1-10)

πŸ“Š analyze_my_draft_strategy

Analyze your current draft progress, spending patterns, and punt strategy.

# Analyze my draft strategy
analysis = await analyze_my_draft_strategy(
    team_id=1,                      # Your team ID (optional, uses ESPN_TEAM_ID env var)
    league_id=123456,               # Optional, uses ESPN_LEAGUE_ID env var
    year=2025,                      # Optional, uses ESPN_YEAR env var
    espn_s2="your_espn_s2_cookie",  # Optional, uses ESPN_S2 env var
    swid="your_swid_cookie"         # Optional, uses ESPN_SWID env var
)

Returns:

  • team_summary: Your current roster and spending

    • totalSpent: Money spent so far

    • playersCount: Number of players drafted

    • remainingBudget: Money left to spend

  • punt_analysis: Strategy analysis and recommendations

  • budget_per_remaining_player: Average $ per remaining roster spot

πŸ“‹ get_available_players

Get list of top available players with auction values and rankings.

# Get best available players
players = await get_available_players(
    league_id=123456,
    year=2025,
    limit=25,                       # Number of players to return
    espn_s2="your_espn_s2_cookie",
    swid="your_swid_cookie"
)

Returns: Array of available players with:

  • playerId: Player ID

  • player: Player details (name, position, team)

  • auctionValue: Projected auction value

  • rank: Overall ranking

  • isDrafted: False (only undrafted players returned)

πŸ’‘ Draft Assistant Example Workflow

# 1. Check draft status
status = await get_draft_status(league_id, year, espn_s2, swid)
if not status["inProgress"]:
    print("Draft not in progress")

# 2. If someone nominated a player, should you bid?
if current_player_being_nominated:
    advice = await should_i_bid(league_id, year, team_id, player_id, espn_s2, swid)
    print(f"{advice['action'].upper()}: {advice['reasoning']}")
    if advice["action"] == "bid":
        print(f"Suggested bid: ${advice['suggestedBid']}")

# 3. If it's your turn to nominate
if its_your_turn:
    target = await who_should_i_target_next(league_id, year, team_id, espn_s2, swid)
    print(f"Target: {target['playerName']} (${target['suggestedBid']})")
    print(f"Reasoning: {target['reasoning']}")

# 4. Analyze your strategy periodically
strategy = await analyze_my_draft_strategy(league_id, year, team_id, espn_s2, swid)
print(f"Spent: ${strategy['team_summary']['totalSpent']}")
print(f"Budget per remaining player: ${strategy['budget_per_remaining_player']}")

πŸ†” Finding Your Team ID

Most draft and roster tools require your team_id. To find it:

  1. Use the get_league_teams tool to see all teams:

teams = await get_league_teams(league_id, year, espn_s2, swid)
# Look through the results to find your team
  1. Or check the ESPN Fantasy Basketball URL when viewing your team:

    • URL format: https://fantasy.espn.com/basketball/team?leagueId=123456&teamId=1

    • Your team ID is the number after teamId=

  2. Configure it as an environment variable to avoid being asked every time:

    • Add ESPN_TEAM_ID to your Claude Desktop config (see Configuration section above)

    • Once configured, you can omit team_id from tool calls and it will use your configured team automatically

Private League Access

For private leagues, you'll need ESPN authentication cookies:

  • ESPN_S2: ESPN authentication cookie (long string starting with "AE")

  • ESPN_SWID: ESPN SWID cookie (format: {12345678-1234-1234-1234-123456789012})

To get these cookies:

  1. Log into ESPN Fantasy in your browser

  2. Open browser developer tools (F12)

  3. Go to Application/Storage tab β†’ Cookies β†’ espn.com

  4. Find and copy the espn_s2 and SWID cookie values

Development

Running Tests

# Run all tests
uv run pytest

# Run tests with coverage
uv run pytest --cov=espn_fantasy_basketball_mcp

# Run specific test file
uv run pytest tests/test_models.py

# Run tests in verbose mode
uv run pytest -v

Code Quality

# Lint and format code
uv run ruff check --fix .

# Type checking
uv run mypy espn_fantasy_basketball_mcp/

# Run all CI checks locally
./scripts/check.sh

Project Structure

espn-fantasy-basketball-mcp/
β”œβ”€β”€ espn_fantasy_basketball.py      # Main MCP server using FastMCP
β”œβ”€β”€ espn_fantasy_basketball_mcp/    # Core library package
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ client.py                   # ESPN API client
β”‚   β”œβ”€β”€ models.py                   # Pydantic data models
β”‚   └── server.py                   # Legacy MCP server (unused)
β”œβ”€β”€ tests/                          # Test suite
β”‚   β”œβ”€β”€ test_client.py             # Client tests
β”‚   β”œβ”€β”€ test_models.py             # Model tests
β”‚   └── test_server.py             # Server tests
β”œβ”€β”€ conftest.py                    # Pytest configuration
β”œβ”€β”€ pyproject.toml                 # Project configuration
β”œβ”€β”€ requirements.txt               # Dependencies (for pip users)
β”œβ”€β”€ Pipfile                        # Dependencies (for pipenv users)
β”œβ”€β”€ .python-version               # Python version for pyenv
└── README.md                     # This file

API Endpoints Used

This MCP server uses the following ESPN API endpoints:

Fantasy Basketball API

  • Base URL: https://lm-api-reads.fantasy.espn.com/apis/v3/games/fba

  • League Data: /seasons/{year}/segments/0/leagues/{league_id}

    • Teams: ?view=mTeam

    • Rosters: ?view=mRoster

    • Matchups: ?view=mMatchup

    • Free Agents: ?view=kona_player_info

NBA Schedule API

  • Base URL: https://site.api.espn.com/apis/site/v2/sports/basketball/nba

  • Schedule: /scoreboard

API Limitations & Alternatives Needed

Current Limitations

  1. Free Agents Query Limit: ESPN API only returns up to 50 players per request for free agents

  2. Private League Access: Requires ESPN authentication cookies (espn_s2 and SWID)

  3. Rate Limiting: ESPN may rate limit requests (not officially documented)

  4. Undocumented API: ESPN's fantasy API is not officially documented and may change

  5. Data Completeness: Some fields like team location are not provided by ESPN's API

  6. Season Dependency: API behavior may vary between active and inactive seasons

Alternative APIs You May Need

  1. NBA Player Stats & Advanced Metrics

    • NBA Stats API: https://stats.nba.com/stats/ (official but rate limited)

    • Basketball Reference: Web scraping required

    • RapidAPI Sports: Paid API with comprehensive NBA data

  2. Real-time NBA Data

    • ESPN NBA API: https://site.api.espn.com/apis/site/v2/sports/basketball/nba (used for schedule)

    • NBA Data API: https://data.nba.net/prod/ (official but limited)

  3. Advanced Fantasy Analytics

    • FantasyLabs API: Paid service with projections and ownership data

    • DraftKings API: For DFS ownership and salaries

    • Hashtag Basketball: Free projections (web scraping required)

  4. Player News & Injuries

    • ESPN News API: Limited access

    • The Athletic API: Requires subscription

    • Reddit API: r/fantasybball for community insights

Position IDs Reference

ESPN uses the following position IDs:

  • 0: Point Guard (PG)

  • 1: Shooting Guard (SG)

  • 2: Small Forward (SF)

  • 3: Power Forward (PF)

  • 4: Center (C)

  • 5: Guard (G)

  • 6: Forward (F)

  • 12: Bench

  • 13: IR (Injured Reserve)

Example Usage

Via MCP Tools (in Claude Desktop)

Once configured, you can ask Claude to:

  • "Get all teams in my fantasy basketball league"

  • "Show me the roster for the Lakers team"

  • "What free agents are available?"

  • "Show me this week's matchups"

  • "What NBA games are today?"

Direct Python Usage

from espn_fantasy_basketball_mcp.client import ESPNFantasyBasketballClient

# Initialize client
client = ESPNFantasyBasketballClient(
    league_id=12345,
    year=2025,
    espn_s2="your_espn_s2_cookie",  # For private leagues
    swid="your_swid_cookie"         # For private leagues
)

# Get all teams in league
teams = await client.get_league_teams()

# Get roster for team ID 1
roster = await client.get_team_roster(team_id=1)

# Get top 25 free agent guards
free_agents = await client.get_free_agents(size=25, position_id=5)

# Get current week matchups
matchups = await client.get_matchups()

# Get today's NBA games
nba_games = await client.get_nba_schedule()

Troubleshooting

Common Issues

  1. "Team not found" errors: Verify your league ID and ensure you have access to the league

  2. Authentication errors: Check that your espn_s2 and SWID cookies are correct and not expired

  3. Empty results: Some data may not be available during off-season or for certain league settings

  4. Rate limiting: ESPN may temporarily block requests if you make too many in quick succession

Debug Mode

The client includes error handling and logging. For debugging, check the server logs when running:

uv run espn_fantasy_basketball.py

Contributing

  1. Fork the repository

  2. Create a feature branch: git checkout -b feature-name

  3. Make your changes and add tests

  4. Run tests: uv run pytest

  5. Run code quality checks: uv run black . && uv run ruff .

  6. Commit your changes: git commit -am 'Add feature'

  7. Push to the branch: git push origin feature-name

  8. Create a Pull Request

License

MIT License - see LICENSE file for details.

Available Tools

14 tools
analyze_my_draft_strategyA

Analyze your current draft strategy and spending patterns.

Args:
    team_id: Your team ID (optional, uses ESPN_TEAM_ID env var)
    league_id: ESPN Fantasy Basketball league ID (optional, uses ESPN_LEAGUE_ID env var)
    year: Season year (e.g., 2025) (optional, uses ESPN_YEAR env var or defaults to 2025)
    espn_s2: ESPN authentication cookie for private leagues (optional, uses ESPN_S2 env var)
    swid: ESPN SWID cookie for private leagues (optional, uses ESPN_SWID env var)

Returns:
    Dictionary with punt strategy analysis, spending summary, and recommendations

Inputs: swid, year, espn_s2, team_id, +1 more.

analyze_trade_proposalA

Analyze a trade proposal using comprehensive statistical analysis.

Args:
    your_player_ids: List of player IDs you would trade away
    their_player_ids: List of player IDs you would receive
    league_id: ESPN Fantasy Basketball league ID (optional, uses ESPN_LEAGUE_ID env var)
    year: Season year (e.g., 2025) (optional, uses ESPN_YEAR env var or defaults to 2025)
    espn_s2: ESPN authentication cookie for private leagues (optional, uses ESPN_S2 env var)
    swid: ESPN SWID cookie for private leagues (optional, uses ESPN_SWID env var)

Returns:
    Dictionary with trade analysis, recommendation, and category impact

Inputs: your_player_ids (required), their_player_ids (required), swid, year, +2 more.

compare_playersA

Compare multiple players across statistical categories.

Args:
    player_ids: List of ESPN player IDs to compare
    league_id: ESPN Fantasy Basketball league ID (optional, uses ESPN_LEAGUE_ID env var)
    year: Season year (e.g., 2025) (optional, uses ESPN_YEAR env var or defaults to 2025)
    categories: List of categories to compare (optional, defaults to 9-cat)
    espn_s2: ESPN authentication cookie for private leagues (optional, uses ESPN_S2 env var)
    swid: ESPN SWID cookie for private leagues (optional, uses ESPN_SWID env var)

Returns:
    Dictionary with detailed player comparison and winner by category

Inputs: player_ids (required), swid, year, espn_s2, +2 more.

get_available_playersA

Get top available players for the draft with auction values.

Args:
    league_id: ESPN Fantasy Basketball league ID (optional, uses ESPN_LEAGUE_ID env var)
    year: Season year (e.g., 2025) (optional, uses ESPN_YEAR env var or defaults to 2025)
    limit: Number of players to return (default 50)
    espn_s2: ESPN authentication cookie for private leagues (optional, uses ESPN_S2 env var)
    swid: ESPN SWID cookie for private leagues (optional, uses ESPN_SWID env var)

Returns:
    List of available player dictionaries with auction values and rankings

Inputs: swid, year, limit, espn_s2, +1 more. Returns: result

get_draft_statusA

Get current draft status including all picks and progress.

Args:
    league_id: ESPN Fantasy Basketball league ID (optional, uses ESPN_LEAGUE_ID env var)
    year: Season year (e.g., 2025) (optional, uses ESPN_YEAR env var or defaults to 2025)
    espn_s2: ESPN authentication cookie for private leagues (optional, uses ESPN_S2 env var)
    swid: ESPN SWID cookie for private leagues (optional, uses ESPN_SWID env var)

Returns:
    Dictionary with draft status, picks, and current state

Inputs: swid, year, espn_s2, league_id.

get_free_agentsA

Get free agents/waiver wire players from an ESPN Fantasy Basketball league.

Args:
    league_id: ESPN Fantasy Basketball league ID (optional, uses ESPN_LEAGUE_ID env var)
    year: Season year (e.g., 2025) (optional, uses ESPN_YEAR env var or defaults to 2025)
    size: Number of players to return (max 50, default 50)
    position_id: Filter by position ID (optional)
    espn_s2: ESPN authentication cookie for private leagues (optional, uses ESPN_S2 env var)
    swid: ESPN SWID cookie for private leagues (optional, uses ESPN_SWID env var)

Returns:
    List of available free agent player dictionaries

Inputs: size, swid, year, espn_s2, +2 more. Returns: result

get_league_teamsA

Get all teams in an ESPN Fantasy Basketball league.

Args:
    league_id: ESPN Fantasy Basketball league ID (optional, uses ESPN_LEAGUE_ID env var)
    year: Season year (e.g., 2025) (optional, uses ESPN_YEAR env var or defaults to 2025)
    espn_s2: ESPN authentication cookie for private leagues (optional, uses ESPN_S2 env var)
    swid: ESPN SWID cookie for private leagues (optional, uses ESPN_SWID env var)

Returns:
    List of team dictionaries with id, name, location, record, etc.

Inputs: swid, year, espn_s2, league_id. Returns: result

get_matchupsA

Get matchups/schedule for an ESPN Fantasy Basketball league.

Args:
    league_id: ESPN Fantasy Basketball league ID (optional, uses ESPN_LEAGUE_ID env var)
    year: Season year (e.g., 2025) (optional, uses ESPN_YEAR env var or defaults to 2025)
    scoring_period: Specific scoring period to get matchups for (optional)
    espn_s2: ESPN authentication cookie for private leagues (optional, uses ESPN_S2 env var)
    swid: ESPN SWID cookie for private leagues (optional, uses ESPN_SWID env var)

Returns:
    List of matchup dictionaries with home/away teams and scores

Inputs: swid, year, espn_s2, league_id, +1 more. Returns: result

get_nba_scheduleA

Get NBA schedule for a specific date.

Args:
    date: Date in YYYY-MM-DD format (optional, defaults to today)

Returns:
    List of NBA game dictionaries with teams, times, and details

Inputs: date. Returns: result

get_player_statsA

Get comprehensive player statistics for specified timeframe.

Args:
    player_id: ESPN player ID
    league_id: ESPN Fantasy Basketball league ID (optional, uses ESPN_LEAGUE_ID env var)
    year: Season year (e.g., 2025) (optional, uses ESPN_YEAR env var or defaults to 2025)
    timeframe: Time period - "season", "projections", "last_7", "last_30"
    espn_s2: ESPN authentication cookie for private leagues (optional, uses ESPN_S2 env var)
    swid: ESPN SWID cookie for private leagues (optional, uses ESPN_SWID env var)

Returns:
    Dictionary with comprehensive player statistics across all categories

Inputs: player_id (required), swid, year, espn_s2, +2 more.

get_team_rosterA

Get roster for a specific team in an ESPN Fantasy Basketball league.

Args:
    team_id: Team ID to get roster for (optional, uses ESPN_TEAM_ID env var)
    league_id: ESPN Fantasy Basketball league ID (optional, uses ESPN_LEAGUE_ID env var)
    year: Season year (e.g., 2025) (optional, uses ESPN_YEAR env var or defaults to 2025)
    scoring_period: Specific scoring period (optional)
    espn_s2: ESPN authentication cookie for private leagues (optional, uses ESPN_S2 env var)
    swid: ESPN SWID cookie for private leagues (optional, uses ESPN_SWID env var)

Returns:
    Dictionary with team roster including all players and their positions

Inputs: swid, year, espn_s2, team_id, +2 more.

should_i_bidA

Get recommendation on whether to bid for the current player being nominated.

Args:
    current_player_id: ID of player currently being nominated
    team_id: Your team ID (optional, uses ESPN_TEAM_ID env var)
    league_id: ESPN Fantasy Basketball league ID (optional, uses ESPN_LEAGUE_ID env var)
    year: Season year (e.g., 2025) (optional, uses ESPN_YEAR env var or defaults to 2025)
    espn_s2: ESPN authentication cookie for private leagues (optional, uses ESPN_S2 env var)
    swid: ESPN SWID cookie for private leagues (optional, uses ESPN_SWID env var)

Returns:
    Dictionary with bid recommendation, suggested amount, and reasoning

Inputs: current_player_id (required), swid, year, espn_s2, +2 more.

who_should_i_target_nextA

Get recommendation on which player to target/nominate next.

Args:
    team_id: Your team ID (optional, uses ESPN_TEAM_ID env var)
    league_id: ESPN Fantasy Basketball league ID (optional, uses ESPN_LEAGUE_ID env var)
    year: Season year (e.g., 2025) (optional, uses ESPN_YEAR env var or defaults to 2025)
    espn_s2: ESPN authentication cookie for private leagues (optional, uses ESPN_S2 env var)
    swid: ESPN SWID cookie for private leagues (optional, uses ESPN_SWID env var)

Returns:
    Dictionary with player recommendation and reasoning

Inputs: swid, year, espn_s2, team_id, +1 more.

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    A4/5.0

    Scored across 14 tools

    Disambiguation4/5

    Most tools have distinct purposes: schedule, teams, rosters, free agents, matchups, draft status, player stats, comparisons, trade analysis, and trends. However, 'get_free_agents' and 'get_available_players' could be confused (one is for waiver wire, the other for draft), and the three draft advisory tools ('should_i_bid', 'who_should_i_target_next', 'analyze_my_draft_strategy') overlap in context though they produce different outputs.

    Naming Consistency4/5

    The majority follow a clear 'get_' or 'analyze_' + noun pattern, such as get_league_teams, get_player_stats, and analyze_trade_proposal. A few deviating question-style names (should_i_bid, who_should_i_target_next) stand out but are still readable and follow a similar snake_case style.

    Tool Count5/5

    14 tools is a well-scoped set for a fantasy basketball analytics server. Each tool addresses a meaningful aspect of the domain without redundancy, and the count falls comfortably in the ideal range.

    Completeness4/5

    The server covers most core read and analysis workflows: schedule, rosters, matchups, draft, free agents, player stats, comparisons, trends, and trade evaluations. It lacks write operations like adding players or setting lineups, but for an advisory/analytics assistant this is a reasonable scope with only minor gaps.

    Maintenance

    ActivityInactive
    ResponsivenessNo recent community issues

    Related MCP Connectors

    Related MCP Servers

    • F
      license
      Not graded
      quality
      D
      maintenance
      An MCP server for accessing college basketball statistics through the SportsData.io CBB v3 Stats API, enabling AI agents to retrieve and analyze college basketball data through natural language interactions.
      -
    • A
      license
      A
      quality
      D
      maintenance
      MCP server for NBA live data and stats, providing read-only tools to query live scores, box scores, player info, standings, and more from NBA.com.
      15
      15 npm
      1
      MIT
    • F
      license
      B
      quality
      D
      maintenance
      MCP server that enables Claude Desktop to access real-time sports data including live scores, fixtures, standings, and NBA statistics using free APIs.
      10
      -