通信服务连接器

MCP服务器用于通信服务连接器,目前支持Telegram多账号配置,设计具有可扩展性,未来可添加更多服务(如Slack、Discord、电子邮件等)。

  • by AI-Guru
  • Python
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 AI-Guru/mmxxvi_straight_connect

使用说明

直接连接

通信服务连接器的MCP服务器。目前支持Telegram多账户配置。专为可扩展性而设计——稍后可以添加更多服务(Slack、Discord、电子邮件等)。

建筑

mcp/
├── server.py            # FastAPI + FastMCP entry point
├── telegram_service.py  # Shared Telegram service layer
├── telegram_api.py      # REST API routes
├── telegram.py          # MCP tool definitions + config
├── requirements.txt     # Server dependencies
└── Dockerfile

src/
├── config.py          # Generates mcp.json from .env
└── chat.py            # Gradio + LangChain ReAct chat app

docker-compose.yaml
pyproject.toml
.env                   # Your configuration (not committed)
.env.example           # Configuration template

服务器公开了两个接口:

  • 主控程序 用于AI代理工具
  • REST API 用于直接HTTP访问

两个接口使用相同的底层 TelegramService 层,确保行为一致。

设置

  1. 复制环境模板并填写您的值:
cp .env.example .env
  1. 从获取机器人令牌 @植物学家 在Telegram上添加它们 .env.

  2. 启动MCP服务器:

docker compose up --build -d
  1. 验证它是否正在运行:
docker compose logs mcp

配置

所有配置都存在 .env。参见 .env.example 对于完整模板。

电报账户

# Comma-separated account labels
TELEGRAM_ACCOUNTS=nietzsche,aurelia

# Per-account bot tokens (label uppercased)
TELEGRAM_NIETZSCHE_BOT_TOKEN=your-token-here
TELEGRAM_AURELIA_BOT_TOKEN=your-token-here

工具级别

控制每个帐户可用的Telegram工具。设置全局默认值,并可选择覆盖每个帐户:

# Global default
TELEGRAM_LEVEL=basic

# Per-account override
TELEGRAM_NIETZSCHE_LEVEL=standard
TELEGRAM_AURELIA_LEVEL=full
级别 工具(累积)
基本的 get_me, send_message, get_updates
标准 + forward_message, edit_message_text, delete_message, send_photo, send_document
先进的 + get_chat, send_location, send_poll, pin_chat_message, unpin_chat_message, get_chat_member_count, get_chat_member
满的 + send_audio, send_video, send_voice, send_sticker, copy_message, set_message_reaction, leave_chat, send_contact, send_venue

允许聊天

可选择限制机器人帐户可以与哪些聊天进行交互。如果设置,则任何针对不在列表中的聊天的工具调用都将被拒绝。如果未设置,则允许所有聊天。

# Comma-separated chat IDs (user IDs, group IDs, @channel_usernames)
TELEGRAM_NIETZSCHE_ALLOWED_CHATS=123456789,-1001234567890
TELEGRAM_AURELIA_ALLOWED_CHATS=987654321,@mychannel

这适用于所有需要 chat_id 参数。对于 forward_message 和 copy_message,检查源聊天和目标聊天。

允许的用户ID

限制机器人帐户将与哪些Telegram用户进行交互。来自未列出用户的传入更新将通过以下方式自动过滤掉 get_updates。如果未设置,则允许所有用户使用,并在启动时记录警告。

TELEGRAM_NIETZSCHE_ALLOWED_USER_IDS=123456789
TELEGRAM_AURELIA_ALLOWED_USER_IDS=5138099108,987654321

MCP端口

MCP_PORT=9831

聊天模型

被...使用 uv run chat 对于Gradio测试界面:

OPENAI_API_BASE=http://localhost:8000/v1
OPENAI_API_KEY=not-needed
CHAT_MODEL=openai:your-model-name

MCP端点

每个帐户都有自己的端点,由服务和帐户标签限定:

http://localhost:9831/mcp/telegram/<account>

示例:

  • http://localhost:9831/mcp/telegram/nietzsche
  • http://localhost:9831/mcp/telegram/aurelia

没有共享 /mcp endpoint——每个URL只对一个帐户开放。这使得MCP客户端能够为每个帐户连接不同的工具命名空间。

REST API

REST API与MCP一起提供,用于直接HTTP访问。这两个接口使用相同的底层服务层。

基础URL: http://localhost:9831/api/telegram/{account}/...

Swagger文档: http://localhost:9831/docs

端点

方法 端点 描述 级别
得到 /{account}/me 获取机器人信息 基本
职位 /{account}/chats/{chat_id}/messages 发送消息 基本
得到 /{account}/updates 获取更新(自动确认) 基本
职位 /{account}/chats/{chat_id}/forward 转发消息 标准
PUT /{account}/chats/{chat_id}/messages 编辑消息 标准
删除 /{account}/chats/{chat_id}/messages/{message_id} 删除消息 标准
职位 /{account}/chats/{chat_id}/photos 发送照片 标准
职位 /{account}/chats/{chat_id}/documents 发送文档 标准
得到 /{account}/chats/{chat_id} 获取聊天信息 高级
职位 /{account}/chats/{chat_id}/location 发送位置 高级
职位 /{account}/chats/{chat_id}/polls 发送投票 高级
职位 /{account}/chats/{chat_id}/pin 固定消息 高级
职位 /{account}/chats/{chat_id}/unpin 打开邮件 高级
得到 /{account}/chats/{chat_id}/member-count 获取会员数 高级
得到 /{account}/chats/{chat_id}/members/{user_id} 获取会员信息 高级
职位 /{account}/chats/{chat_id}/audio 发送音频 已满
职位 /{account}/chats/{chat_id}/video 发送视频 完整
职位 /{account}/chats/{chat_id}/voice 发送语音信息 已满
职位 /{account}/chats/{chat_id}/stickers 发送贴纸 完整
职位 /{account}/chats/{chat_id}/copy 复制邮件 完整
职位 /{account}/chats/{chat_id}/reactions 设置反应 满
职位 /{account}/chats/{chat_id}/leave 离开聊天 已满
职位 /{account}/chats/{chat_id}/contacts 发送联系人 已满
职位 /{account}/chats/{chat_id}/venues 发送地点 已满

示例

# Get bot info
curl http://localhost:9831/api/telegram/aurelia/me

# Send a message
curl -X POST http://localhost:9831/api/telegram/aurelia/chats/123456789/messages \
  -H "Content-Type: application/json" \
  -d '{"text": "Hello!"}'

# Get updates (auto-acknowledged)
curl "http://localhost:9831/api/telegram/aurelia/updates?limit=10"

# Get updates without acknowledging
curl "http://localhost:9831/api/telegram/aurelia/updates?auto_acknowledge=false"

客户端工具

生成mcp.json

打印从您的 .env:

uv run config

输出:

{
  "mcpServers": {
    "nietzsche": {
      "url": "http://localhost:9831/mcp/telegram/nietzsche"
    },
    "aurelia": {
      "url": "http://localhost:9831/mcp/telegram/aurelia"
    }
  }
}

聊天界面

启动Gradio web UI,使用LangChain ReAct代理连接到所有配置的MCP服务器:

uv run chat

启动时,它将:

  1. 连接到每个配置的MCP服务器
  2. 列出每个帐户的所有可用工具
  3. 测试配置的聊天模型
  4. 启动Gradio界面

工具名称以帐户标签作为前缀以消除歧义(例如。, nietzsche_telegram_send_message, aurelia_telegram_get_updates).

运作原理

  • 共享服务层:MCP工具和REST API都使用 TelegramService 对于所有Telegram操作,确保行为一致
  • URL 路由:ASGI中间件重写 /mcp/<service>/<account> 连接到内部FastMCP端点,并通过以下方式设置帐户上下文 contextvars
  • 动态注册:只有已配置的服务才注册其工具。如果 TELEGRAM_ACCOUNTS 为空,不存在Telegram工具
  • 电平门控:工具根据所有帐户的最高级别进行注册。在调用时进行每个帐户级别的检查,如果帐户级别不足,则返回错误
  • 聊天白名单:每个帐户可选 ALLOWED_CHATS 限制机器人可以与哪些聊天进行交互
  • 用户筛选:每个帐户可选 ALLOWED_USER_IDS 筛选传入更新,使其仅包含来自指定用户的消息
  • 自动确认更新:默认情况下, get_updates 自动确认检索到的更新,因此在后续调用中不会再次返回
  • 可扩展性:新服务遵循相同的模式——添加配置类,有条件地注册工具,中间件通过自动路由 /mcp/<service>/<account>