新 MCP 服务器
一种消息集中器协议(MCP)服务器,为《纽约时报》API提供统一、简单的接口。此服务器通过单个端点简化了与多个NYT API的交互。
概述
此MCP服务器充当各种《纽约时报》API的统一网关,包括:
- 文章检索
- 头条
- Times Wire(实时新闻)
- 最受欢迎
- 档案
- 书籍API
特性
- 单一端点:通过一个一致的接口访问所有NYT API
- 干净的响应:格式化和简化的API响应
- 实时更新:通过Times Wire实时新闻推送
- 灵活搜索:全面的文章搜索功能
- 错误处理:强大的错误管理
- 健康监测:内置健康检查端点
- 易于集成:易于与任何应用程序集成
需求
- Python 3.8+
- 纽约时报API密钥(在 NYT 开发 门户)
- 必需的Python包(见requirements.txt)
快速开始
- 克隆存储库:
git clone https://github.com/your-username/nyt-mcp-server.git
cd nyt-mcp-server- 创建并激活虚拟环境:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- 安装依赖项:
pip install -r requirements.txt- 创建
.env文件:
NYT_API_KEY=your_api_key_here
MCP_PORT=8000
MCP_HOST=0.0.0.0- 运行服务器:
python src/server.py项目结构
nyt-mcp-server/
├── src/
│ ├── __init__.py
│ ├── server.py
│ └── config.py
├── .env
├── .gitignore
├── requirements.txt
└── README.mdAPI文档
基本URL
http://localhost:8000端点
POST/mcp/消息
所有NYT API交互的主要终点。
GET/健康
健康检查端点。
示例用法
import httpx
import asyncio
async def get_news():
async with httpx.AsyncClient() as client:
response = await client.post(
"http://localhost:8000/mcp/message",
json={
"message_type": "times_wire",
"content": {"limit": 5},
"timestamp": "2025-03-05T21:00:00Z"
}
)
return response.json()
# Run the async function
asyncio.run(get_news())消息类型
1.文章搜索
{
"message_type": "article_search",
"content": {
"query": "technology",
"sort": "newest",
"page": 0
},
"timestamp": "2025-03-05T21:00:00Z"
}2.热门故事
{
"message_type": "top_stories",
"content": {
"section": "technology"
},
"timestamp": "2025-03-05T21:00:00Z"
}3.Times Wire
{
"message_type": "times_wire",
"content": {
"limit": 5,
"source": "nyt"
},
"timestamp": "2025-03-05T21:00:00Z"
}4.最受欢迎
{
"message_type": "most_popular",
"content": {
"type": "viewed",
"time_period": "7"
},
"timestamp": "2025-03-05T21:00:00Z"
}5.档案
{
"message_type": "archive",
"content": {
"year": 2025,
"month": 3
},
"timestamp": "2025-03-05T21:00:00Z"
}6.书籍
{
"message_type": "books",
"content": {
"list": "hardcover-fiction"
},
"timestamp": "2025-03-05T21:00:00Z"
}贡献
- 分叉存储库
- 创建功能分支(
git checkout -b feature/AmazingFeature) - 提交您的更改(
git commit -m 'Add some AmazingFeature') - 推到分支(
git push origin feature/AmazingFeature) - 打开拉取请求
许可证
MIT许可证
安全说明
- 永远不要承诺你的
.env文件 - 将您的《纽约时报》API密钥保密
- 对敏感数据使用环境变量
联系
为错误报告或功能请求创建问题。
