feat(mcp): add FastMCP server, agent tools, and documentation (#561) - #690
feat(mcp): add FastMCP server, agent tools, and documentation (#561)#690ColinTWilliams wants to merge 4 commits into
Conversation
Implements official Model Context Protocol (MCP) server support for nba_api. Exposes 10 specialized tools for LLMs and AI agents covering player stats, game logs, live scoreboards, Synergy play types, Second Spectrum shot tracking, shot mechanics, advanced metrics, hustle stats, and arbitrary raw endpoints. Includes: - Optional dependency 'mcp' in pyproject.toml (pip install 'nba-api[mcp]') - Console script entrypoint ba-mcp - FastMCP / MCPServer backward and forward compatibility - Diacritic/accent normalization for player search - Documentation in docs/mcp.md and Agent Skills standard reference in skills/nba-stats - Unit tests with 100% pass rate in tests/unit/mcp/test_mcp_server.py Follow-up to discussion in Issue swar#561.
- Add mcp to [tool.poetry.group.dev.dependencies] so CI environments install it for pytest
- Synchronize poetry.lock with pyproject.toml changes to satisfy poetry install in CircleCI
- Add pytest.importorskip('mcp') in test_mcp_server.py for environments without mcp
|
full transparency Gemini 3.8 Flash wrote this PR because I wanted to test the model and Antigravity. I did try out the MCP with good success though. here's an example of Gemini 3.8 Flash using the MCP to give an answer: Prompt:
Response:
for Rollins at least, I did a quick check to verify answers here: https://www.nba.com/stats/events?CF=SHOT_TYPE*R*3:ACTION_TYPE*E*step%20back:EVENT_TYPE*E*Made&CFID=&CFPARAMS=&ContextMeasure=FGA&PlayerID=1631157&Season=2025-26&SeasonType=Regular%20Season&TeamID=1610612749&flag=3&sct=plot§ion=playe |
…tice, and endpoint argument mapping
Summary
Hi Randy (@rsforbes) — following up on our discussion in #561 regarding adding Model Context Protocol (MCP) server support to
nba_api!This PR adds an official, lightweight FastMCP server and agent tools to
nba_api, enabling AI coding assistants and LLMs (Claude Desktop, Cursor, Antigravity, Factory Droid, Windsurf, etc.) to query NBA statistics, play-type analytics, and shot tracking directly via the open Model Context Protocol standard.Design & Architecture
nba_apiusers are completely unaffected.[project.optional-dependencies] mcp = ["mcp>=1.2.0"].pip install "nba-api[mcp]"[project.scripts] nba-mcp = "nba_api.mcp.server:main".nba-mcporpython -m nba_api.mcp.server.mcp.server.mcpserver.MCPServer(v2.x) andmcp.server.fastmcp.FastMCP(v1.x) gracefully._get_default_season()) so tools remain current without hardcoded year constants._strip_accents) handles international spellings seamlessly (e.g.,"Luka Doncic"matches"Luka Dončić").FG2A,FG2M,FG2_PCT,FG3A,FG3M, andFG3_PCTbreakdowns inget_shot_chart_actions.query_raw_endpointauto-populates required positional parameters (team_id=0,player_id=0) and maps parameter aliases between standard names and_nullablevariants (e.g.,season->season_nullable)._call_with_retry) absorbs occasional NBA.com read timeouts and high-latency aggregation queries.ScoreboardV3(avoidingScoreboardV2line score deprecations) andcontext_measure_simple='FGA'forShotChartDetail(ensuring misses are captured alongside makes).Exposed MCP Tools (10)
get_player_statsget_player_gamelogget_scoreboardget_team_rosterget_synergy_play_typesget_shot_trackingget_shot_chart_actionsget_advanced_statsget_hustle_statsquery_raw_endpointnba_api.stats.endpointsby class name.Documentation & Skills
docs/mcp.md: Configuration guide for Claude Desktop (claude_desktop_config.json), Cursor (mcp.json), and CLI agents.README.md&docs/table_of_contents.md: Linked to the new MCP documentation.skills/nba-stats/: Included universal Agent Skill definition (SKILL.md) and reference guides (references/endpoints.md,references/recipes.md,references/gotchas.md).Verification & Testing
tests/unit/mcp/test_mcp_server.pyverifying accent normalization, player/team resolution, tool registration, season calculation, shot chart 2PT/3PT breakdowns, truncation notices, and dynamic raw endpoint parameter defaults.pytest tests/unit).ruff checkandruff formatadhering to the project'sline-length = 88andtarget-version = "py310"settings.