新闻获取小部件

一个使用模型上下文协议(MCP)从MongoDB获取新闻并通过交互式小部件在ChatGPT中显示的应用。

  • by vikkysarswat
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 vikkysarswat/chatgpt-mcp-news-widget

使用说明

ChatGPT MCP 新闻小部件

一个使用模型上下文协议(MCP)从MongoDB获取新闻并在ChatGPT中通过交互式小部件展示的OpenAI应用。

特点/特性

  • 🔌 用Python实现的MCP服务器
  • 从MongoDB集合中获取新闻
  • 🎨 在ChatGPT中使用丰富的控件显示新闻(类似于披萨示例)
  • 🔧 可扩展架构,用于添加更多工具

先决条件

  • Python 3.10+
  • MongoDB Atlas 帐户或本地 MongoDB 实例
  • OpenAI平台账户
  • ChatGPT Plus订阅(用于应用连接器)

项目结构

.
├── src/
│   ├── mcp_server.py          # Main MCP server implementation
│   ├── mongodb_client.py      # MongoDB connection and queries
│   └── tools/
│       └── fetch_news.py      # News fetching tool
├── openai_app/
│   ├── app_manifest.json      # OpenAI app manifest
│   └── actions.json           # Action definitions for widgets
├── config/
│   └── config.yaml            # Configuration file
├── tests/
│   └── test_news_tool.py      # Unit tests
├── requirements.txt           # Python dependencies
├── .env.example               # Environment variables template
└── README.md                  # This file

设置

1. 克隆仓库

git clone https://github.com/vikkysarswat/chatgpt-mcp-news-widget.git
cd chatgpt-mcp-news-widget

2. 安装依赖项

pip install -r requirements.txt

3. 配置环境变量

cp .env.example .env

编辑 .env 并添加您的MongoDB连接字符串:

MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/
MONGODB_DATABASE=news_db
MONGODB_COLLECTION=articles
MCP_SERVER_PORT=3000

4. 配置MongoDB

创建一个包含新闻文章的MongoDB集合。示例文档结构:

{
  "_id": "...",
  "title": "Breaking News Title",
  "description": "News article description",
  "content": "Full article content",
  "author": "John Doe",
  "source": "News Source",
  "url": "https://example.com/article",
  "image_url": "https://example.com/image.jpg",
  "published_at": "2025-10-15T10:00:00Z",
  "category": "technology",
  "tags": ["ai", "tech", "innovation"]
}

5. 运行MCP服务器

python src/mcp_server.py

6. 配置OpenAI应用程序

  1. 首选 OpenAI平台
  2. 导航至“应用”部分
  3. 创建一个新应用
  4. 上传该(文件/内容) openai_app/app_manifest.json
  5. 配置MCP服务器端点
  6. 将该应用添加到ChatGPT中

使用方法

一旦配置完成,您可以在ChatGPT中使用以下命令:

  • “获取最新新闻”
  • “给我看看科技新闻”
  • “获取过去24小时的新闻”
  • “查找有关人工智能的新闻”

新闻将以交互式小部件的形式展示,包含:

  • 标题和描述
  • 作者和来源
  • 出版日期
  • 图像缩略图
  • 链接至全文

MongoDB 架构

为提升性能,建议创建以下索引:

db.articles.createIndex({ "published_at": -1 })
db.articles.createIndex({ "category": 1 })
db.articles.createIndex({ "tags": 1 })
db.articles.createIndex({ "title": "text", "content": "text" })

添加更多工具

添加新工具:

  1. 在(某个位置)创建一个新文件 src/tools/
  2. 按照MCP协议实施该工具
  3. 在其中注册它 src/mcp_server.py
  4. 更新 openai_app/actions.json 随着新的动作定义

测试

python -m pytest tests/

参考文献

许可证

麻省理工学院许可证(MIT License)

做出贡献

欢迎贡献!请随时提交拉取请求。

支持

对于问题和疑问,请在GitHub上提交一个议题。