Skip to main content
Glama
currency-bot-db

currency-bot-db

Currency MCP Bot(教学项目)

一个简单的 Telegram Bot + 公共 API + Docker/PostgreSQL + MCP + Cursor 集成示例。

架构图

Telegram
   ↓
Python Bot
   ↓
API курса валют

Python Bot
   ↓
PostgreSQL
   ↑
   MCP
   ↑
Cursor

Related MCP server: postgres-mcp

Telegram Bot 在这里做什么

接收 /start、/усд、/еur、/цну 命令,并返回这些货币兑卢布的汇率。

API 在这里做什么

机器人向免费公共 API(open.er-api.com)请求实时汇率——无需密钥,也无需注册。

Docker 在这里做什么

docker compose 在容器中启动 PostgreSQL。机器人本身在本地用 Python 运行。

MCP 在这里做什么

MCP 服务器为 Cursor 提供两个访问同一数据库的工具:

  1. get_recent_requests — 用户最近的请求

  2. get_currency_stats — USD / EUR / CNY 被询问的次数

文件

文件

用途

bot.py

Telegram 机器人(aiogram)

db.py

不带 ORM 的 PostgreSQL

mcp_server.py

用于 Cursor 的 MCP 服务器

docker-compose.yml

Docker 中的 PostgreSQL

.cursor/mcp.json

本项目中的 MCP 连接

.env

密钥和配置(不纳入 Git)

快速开始

1. Python 依赖

python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -r requirements.txt

2. 环境变量

复制示例并粘贴机器人令牌:

copy .env.example .env

在 .env 中填写:

TELEGRAM_BOT_TOKEN=ваш_токен_от_BotFather

3. 通过 Docker 使用 PostgreSQL

docker compose up -d

验证:

docker compose ps

4. 启动机器人

python bot.py

在 Telegram 中向机器人发送 /start,然后发送 /usd、/eur、/cyn。

5. Cursor 中的 MCP

项目中有 .cursor/mcp.json。在 Windows 上,服务器也已添加到用户级 ~/.cursor/mcp.json,确保它一定出现在 Connected 列表中。

  1. 先创建 venv 并安装依赖(见上面的第 1 步)

  2. Ctrl+Shift+P → Developer: Reload Window

  3. 打开 Settings → MCP

  4. 找到 currency-bot-db — 状态应为绿色,tools: get_recent_requests、get_currency_stats

如果服务器不存在或状态为红色:请检查 PostgreSQL(docker compose ps)以及 .venv 是否已安装。

如何获取 Telegram Bot Token

  1. 打开 Telegram,找到 @BotFather

  2. 发送 /newbot

  3. 为机器人想一个名称和 username

  4. BotFather 会发送一个形如 123456:ABC... 的令牌

  5. 将其作为 TELEGRAM_BOT_TOKEN=... 粘贴到 .env 中

不要将令牌提交到 Git — .env 文件已在 .gitignore 中。

在 Cursor 中向 MCP 发送测试请求

向机器人发送几条命令后,在 Cursor 聊天中写下:

  • “显示用户的最近请求”

  • “用户最常查询哪些货币?”

  • “用 limit=5 调用 get_recent_requests”

  • “调用 get_currency_stats”

Cursor 应使用 MCP 工具 get_recent_requests 和 get_currency_stats。

Related MCP Connectors

Related MCP Servers

  • A
    license
    C
    quality
    D
    maintenance
    Enables querying PostgreSQL database metadata including table lists and schema information through the MCP protocol. Supports stdio transport for Cursor/IDE integration as well as HTTP transport with health check endpoints.
    2
    MIT
  • A
    license
    Not graded
    quality
    D
    maintenance
    MCP server for PostgreSQL that enables Cursor and other MCP clients to execute SQL queries, list tables, and explore database schemas via HTTP.
    1,139 npm
    MIT
  • F
    license
    Not graded
    quality
    F
    maintenance
    Provides read-only SQL query access to Postgres and DuckDB databases via MCP tools, with extensive security hardening for public endpoints.
    1
    -
  • A
    license
    A
    quality
    D
    maintenance
    Enables querying and interacting with PostgreSQL databases using natural language through MCP. Supports configuration via environment variables and Cursor IDE integration.
    3
    MIT