get_mlb_team_info
Retrieve detailed team information for MLB, including stats, season data, and player details, by specifying team ID, name, or additional criteria. Ideal for integrating baseball data into applications.
Instructions
Get information about a specific team by ID or name.
Args: team (str): Team ID or team name as a string. Can be numeric string, full name, abbreviation, or location. season (Optional[int]): Season year. sport_id (Optional[int]): Sport ID. hydrate (Optional[str]): Additional data to hydrate. fields (Optional[str]): Comma-separated list of fields to include.
Returns: dict: Team information.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| fields | No | ||
| hydrate | No | ||
| season | No | ||
| sport_id | No | ||
| team | Yes |
Implementation Reference
- mlb_api.py:314-352 (handler)The handler function for the 'get_mlb_team_info' tool, decorated with @mcp.tool(). It resolves the team ID using a helper function and fetches team information via the MLB API.@mcp.tool() def get_mlb_team_info( team: str, season: Optional[int] = None, sport_id: Optional[int] = None, hydrate: Optional[str] = None, fields: Optional[str] = None, ) -> dict: """ Get information about a specific team by ID or name. Args: team (str): Team ID or team name as a string. Can be numeric string, full name, abbreviation, or location. season (Optional[int]): Season year. sport_id (Optional[int]): Sport ID. hydrate (Optional[str]): Additional data to hydrate. fields (Optional[str]): Comma-separated list of fields to include. Returns: dict: Team information. """ try: params = {} if season is not None: params["season"] = season if sport_id is not None: params["sportId"] = sport_id if hydrate is not None: params["hydrate"] = hydrate if fields is not None: params["fields"] = fields team_id = get_team_id_from_name(team) if team_id is None: return {"error": f"Could not find team ID for '{team}'"} team_info = mlb.get_team(team_id, **params) return {"team_info": team_info} except Exception as e: return {"error": str(e)}
- mlb_api.py:152-175 (helper)Helper function that resolves a team name, partial name, or string ID to the numeric team ID by checking a local CSV file with exact and substring matching.def get_team_id_from_name(team: str) -> Optional[int]: """Helper to get team ID from team name, partial name, or stringified ID.""" # Accept stringified integer as ID try: return int(team) except (ValueError, TypeError): pass import csv team_lower = team.lower().strip() with open("current_mlb_teams.csv", "r") as f: reader = csv.DictReader(f) # First, try exact match for row in reader: if team_lower == row["team_name"].lower().strip(): return int(row["team_id"]) f.seek(0) next(reader) # skip header # Then, try substring match for row in reader: if team_lower in row["team_name"].lower(): return int(row["team_id"]) return None
- main.py:22-22 (registration)The call to setup_mlb_tools(mcp) in the main server file, which defines and registers all MLB tools, including get_mlb_team_info, to the MCP server instance.setup_mlb_tools(mcp)