这是一个基于FastMCP的彩票开奖结果解析服务,支持获取中国福利彩票多种彩票类型的开奖结果。
# 使用pip安装
pip install uv
# 或者使用其他方式安装,参考uv官方文档:
# https://github.com/astral-sh/uv
git clone [项目地址]
cd LotteryParser
# 使用uv安装依赖
uv pip install -r requirements.txt
# 或者使用传统pip安装
pip install -r requirements.txt
项目使用FastMCP框架,需要在你的MCP配置文件中添加以下设置:
{
"mcpServers": {
"cnb.cool/cnb/tools/LotterParser-mcp": {
"command": "uv",
"args": [
"--directory",
"C:/my/PyProject/cp/LotteryParser",
"run",
"main.py"
]
}
}
}
command: 使用 uv 作为Python包管理器和运行器args:
--directory: 指定项目目录路径run: 运行Python脚本main.py: 主程序文件注意:
获取指定彩票类型的开奖结果。
参数说明:
lottery_type (str): 彩票类型代码
count (int, 可选): 获取最近几期的数据,默认为1返回值格式:
返回一个包含 LotteryResult 对象的列表。
错误处理:
ValueError@dataclass
class LotteryResult:
name: str # 彩票名称
code: str # 期号
date: str # 开奖日期
numbers: str # 开奖号码
sales: Optional[str] = None # 销售额
content: Optional[str] = None # 中奖情况描述
prize_grades: Optional[List[Dict[str, str]]] = None # 奖金等级详情
@dataclass
class PrizeGrade:
type: str # 奖项类型
num: str # 中奖注数
money: str # 单注奖金
{
"name": "双色球",
"code": "2024001",
"date": "2024-01-01",
"numbers": "红球: 01,15,22,25,30,33, 蓝球: 16",
"sales": "386574322",
"content": "一等奖5注,每注奖金8659521元"
}
{
"name": "快乐8",
"code": "2024001",
"date": "2024-01-01",
"numbers": "开奖号码: 01,05,12,15,22,25,30,33,38,42,45,48,52,55,58,62,65,68,72,75"
}
{
"name": "福彩3D",
"code": "2024001",
"date": "2024-01-01",
"numbers": "开奖号码: 1,2,3"
}
{
"name": "七乐彩",
"code": "2024001",
"date": "2024-01-01",
"numbers": "红球: 01,15,22,25,30,33,38, 特别号: 16"
}
from mcp.client import Client
async def get_latest_ssq():
client = Client()
result = await client.call_tool("cnb.cool/cnb/tools/LotterParser-mcp", "lottery_results", {
"lottery_type": "ssq",
"count": 1
})
print(result[0].format_output())
async def get_multiple_results():
client = Client()
# 获取最近5期双色球开奖结果
results = await client.call_tool("cnb.cool/cnb/tools/LotterParser-mcp", "lottery_results", {
"lottery_type": "ssq",
"count": 5
})
for result in results:
print(result.format_output())
print("-" * 50)
async def handle_errors():
client = Client()
try:
# 尝试获取不存在的彩票类型
result = await client.call_tool("cnb.cool/cnb/tools/LotterParser-mcp", "lottery_results", {
"lottery_type": "invalid_type",
"count": 1
})
except ValueError as e:
print(f"错误: {e}")
except Exception as e:
print(f"未知错误: {e}")
async def format_results():
client = Client()
results = await client.call_tool("cnb.cool/cnb/tools/LotterParser-mcp", "lottery_results", {
"lottery_type": "ssq",
"count": 1
})
if results:
# 获取格式化的开奖信息
formatted_output = results[0].format_output()
print(formatted_output)
# 获取格式化的奖金等级信息
prize_grades = results[0].format_prize_grades()
print("\n奖金等级详情:")
print(prize_grades)
async def use_server_variable():
client = Client()
# 将服务器名称存储为变量,便于维护
server_name = "cnb.cool/cnb/tools/LotterParser-mcp"
# 获取双色球结果
ssq_results = await client.call_tool(server_name, "lottery_results", {
"lottery_type": "ssq",
"count": 1
})
# 获取福彩3D结果
fc3d_results = await client.call_tool(server_name, "lottery_results", {
"lottery_type": "3d",
"count": 1
})
print(f"双色球: {ssq_results[0].format_output()}")
print(f"福彩3D: {fc3d_results[0].format_output()}")
如果遇到网络请求失败:
如果返回空结果:
如果格式化输出异常:
如果遇到API调用错误:
如果遇到uv相关问题: