使用说明
雅虎梦幻MCP服务器
一种模型上下文协议(MCP)服务器,提供对雅虎梦幻体育数据的访问。此服务器充当 yahoo_fantasy_api 图书馆,允许AI助手和其他MCP客户端查询梦幻联赛信息。
特性
- 查询梦幻联赛排名和球队信息
- 检索玩家统计数据和预测
- 访问比赛数据和评分详细信息
- 获取球队的名单信息
- 搜索球员和自由球员
安装
来自PyPI(发布时)
pip install yahoo-fantasy-mcp
来源
git clone https://github.com/yourusername/yahoo_fantasy_mcp.git
cd yahoo_fantasy_mcp
pip install -e .
先决条件
在使用此MCP服务器之前,您需要设置Yahoo Fantasy API凭据:
- 在以下网址注册您的申请 雅虎开发者网络
- 获取您的
consumer_key和consumer_secret - 使用以下方法之一设置OAuth2身份验证
配置
有两种方法可以通过Yahoo的API进行身份验证:
选项1:使用oauth2.json(推荐)
创建一个 oauth2.json 使用您的Yahoo OAuth凭据创建文件。您可以使用 yahoo_fantasy_api 身份验证流程:
from yahoo_oauth import OAuth2
# This will prompt you to authorize via browser
sc = OAuth2(None, None, from_file='oauth2.json')
这 oauth2.json 文件看起来像:
{
"access_token": "...",
"consumer_key": "your_consumer_key",
"consumer_secret": "your_consumer_secret",
"guid": "...",
"refresh_token": "...",
"token_time": 1234567890.123456,
"token_type": "bearer"
}
选项2:使用环境变量
设置以下环境变量:
export YAHOO_CLIENT_ID="your_consumer_key"
export YAHOO_CLIENT_SECRET="your_consumer_secret"
设置您的联盟ID
服务器需要 YAHOO_LEAGUE_ID 环境变量,知道要查询哪个联盟。
查找您的联盟ID:
如果你不知道你的联赛ID,只需运行服务器而不进行设置 YAHOO_LEAGUE_ID:
python -m yahoo_fantasy_mcp
服务器将列出您所属的所有联赛,然后退出。输出示例:
============================================================
YAHOO_LEAGUE_ID not set - Listing available leagues
============================================================
Checking NFL leagues...
Checking MLB leagues...
Found the following leagues:
1. [NFL 2024] My Fantasy League
League ID: 423.l.123456
2. [MLB 2024] Baseball League
League ID: 412.l.789012
To use one of these leagues, set the YAHOO_LEAGUE_ID environment variable:
export YAHOO_LEAGUE_ID=<league_id>
For example:
export YAHOO_LEAGUE_ID=423.l.123456
然后设置环境变量:
export YAHOO_LEAGUE_ID=423.l.123456
用法
作为MCP服务器
此服务器使用 stdio传输 协议,通过标准输入/输出流进行通信。它不监听任何网络端口。
运输类型
- 类型:stdio(标准输入/输出)
- 协议:服务器从stdin读取JSON-RPC消息,并将响应写入stdout
- 连接:由MCP客户端作为子进程调用
所需的环境变量
服务器要求设置以下环境变量:
YAHOO_LEAGUE_ID-您的雅虎幻想联盟的ID(例如,“423.1.123456”)
可选环境变量
根据您的身份验证方法,您可能需要:
YAHOO_CLIENT_ID-您的Yahoo API使用者密钥(如果不使用oauth2.json)YAHOO_CLIENT_SECRET-您的Yahoo API消费者机密(如果不使用oauth2.json)
命令行参数
--oauth2-file <path>-oauth2.json文件的路径(默认:当前目录中的“oauth2.json”)
MCP客户端配置
该服务器应在MCP客户端中配置为stdio服务器。以下是常见MCP客户端的示例配置。
使用oauth2.json文件(推荐)
服务器将自动查找 oauth2.json 在当前工作目录中。
{
"mcpServers": {
"yahoo-fantasy": {
"command": "python",
"args": ["-m", "yahoo_fantasy_mcp"],
"cwd": "/path/to/directory/with/oauth2.json",
"env": {
"YAHOO_LEAGUE_ID": "423.l.123456"
}
}
}
}
或者指定oauth2.json文件的自定义路径:
{
"mcpServers": {
"yahoo-fantasy": {
"command": "python",
"args": ["-m", "yahoo_fantasy_mcp", "--oauth2-file", "/path/to/oauth2.json"],
"env": {
"YAHOO_LEAGUE_ID": "423.l.123456"
}
}
}
}
有环境变量
如果你没有oauth2.json文件,你可以通过环境变量提供凭据:
{
"mcpServers": {
"yahoo-fantasy": {
"command": "python",
"args": ["-m", "yahoo_fantasy_mcp"],
"env": {
"YAHOO_CLIENT_ID": "your_consumer_key",
"YAHOO_CLIENT_SECRET": "your_consumer_secret",
"YAHOO_LEAGUE_ID": "423.l.123456"
}
}
}
}
重要说明
- 服务器必须能够找到
oauth2.json用于身份验证的文件或环境变量。 - 如果使用
oauth2.json,确保cwd(当前工作目录)设置为包含文件的目录,或使用--oauth2-file参数指定完整路径。 - 服务器将根据需要自动刷新OAuth令牌。
- 在首次运行环境变量时,您需要通过浏览器完成OAuth流程。
独立测试
您可以单独测试服务器以验证其是否正常工作:
# Using oauth2.json in current directory (default)
export YAHOO_LEAGUE_ID=423.l.123456
python -m yahoo_fantasy_mcp
# Using oauth2.json from custom path
export YAHOO_LEAGUE_ID=423.l.123456
python -m yahoo_fantasy_mcp --oauth2-file /path/to/oauth2.json
# Using environment variables
export YAHOO_LEAGUE_ID=423.l.123456
YAHOO_CLIENT_ID=your_key YAHOO_CLIENT_SECRET=your_secret python -m yahoo_fantasy_mcp
注意:在没有MCP客户端的情况下独立运行时,服务器将在stdin上等待JSON-RPC消息。这主要用于测试服务器是否正确启动以及身份验证是否正常工作。
可用工具
MCP服务器公开了以下工具,用于只读访问雅虎幻想数据:
联盟信息
get_team_key-获取登录用户所在联赛球队的球队密钥get_current_week-获取联盟的当前周数get_edit_date-获取下一个可以编辑阵容的日期(阵容截止日期)get_end_week-获取联赛赛季的结束周数get_settings-获取全面的联赛设置(得分、季后赛、弃权、交易设置)get_positions-获取联盟中使用的位置及其计数和类型get_stat_categories-获取联盟中跟踪的统计类别get_teams-获取联盟中所有球队的详细信息get_league_standings-获取梦幻联赛的当前排名
配对与评分
get_matchups-获取给定周的匹配数据(默认为当前周)get_matchup_scores-使用球队统计数据获取当前或特定比赛的分数
团队管理
get_team_roster-获取特定团队在特定周或日期的名单get_team_details-获取特定团队的详细信息get_team_matchup-获取给定一周内球队对决的对手队密钥get_team_proposed_trades-获取包括团队在内的拟议交易(包括提供和收到的交易)
玩家信息
search_players-按姓名搜索玩家或按ID获取玩家详细信息get_player_stats-获取指定时间段内一个或多个玩家的统计数据get_free_agents-在联盟中获得特定职位的自由球员get_waivers-获取联盟中目前处于豁免状态的球员
交易
get_transactions-获取联盟交易(添加、删除、交易、混合动作)
注: 此服务器提供只读访问。为了安全起见,不实施任何写入操作(名册更改、交易、阵容修改)。
发展
设置开发环境
# Clone the repository
git clone https://github.com/yourusername/yahoo_fantasy_mcp.git
cd yahoo_fantasy_mcp
# Install development dependencies
pip install -e ".[dev]"
运行测试
pytest
代码质量
# Run linter
flake8 yahoo_fantasy_mcp tests
# Run type checker
mypy yahoo_fantasy_mcp
# Format code
black yahoo_fantasy_mcp tests
项目结构
yahoo_fantasy_mcp/
├── yahoo_fantasy_mcp/
│ ├── __init__.py
│ ├── __main__.py
│ ├── server.py # Main MCP server implementation
│ └── tools.py # Tool implementations
├── tests/
│ ├── __init__.py
│ └── integration/ # Integration tests with real API
│ ├── __init__.py
│ ├── test_tools_integration.py
│ └── test_get_team_roster.py
├── README.md
├── setup.py
├── requirements.txt
├── requirements-dev.txt
└── .gitignore
许可证
MIT许可证-有关详细信息,请参阅许可证文件
贡献
欢迎投稿!请随时提交拉取请求。
鸣谢
建在上面 yahoo_fantasy_api 通过spilchen。
支持
对于问题和疑问:
- 在GitHub上提交问题
- 检查 yahoo_fantasy_api文档