该服务器是Telegram API与AI助手之间的桥梁,并基于模型上下文协议。
<a href="https://glama.ai/mcp/servers/484jega1au"> <img width="380" height="200" src="https://gips2.baidu.com/it/u=1132038992,2970626959&fm=3081&app=3081&f=PNG?w=760&h=400" alt="Telegram Server MCP 服务器" /> </a>[!IMPORTANT] 在使用此服务器之前,请确保您已经阅读并理解了Telegram API服务条款。 Telegram API的任何滥用行为可能导致您的账户被暂停。
模型上下文协议(MCP)是一个系统,允许像Claude Desktop这样的AI应用程序连接到外部工具和数据源。它提供了一种清晰且安全的方式,使AI助手能够与本地服务和API进行交互,同时保持用户控制权。
目前,该服务器提供了对Telegram API的只读访问权限。
uv tool install git+https://github.com/sparfenyuk/mcp-telegram
[!NOTE] 如果您已经安装了该服务器,可以使用
uv tool upgrade --reinstall命令进行更新。
[!NOTE] 如果要删除该服务器,可以使用
uv tool uninstall mcp-telegram命令。
在您可以使用该服务器之前,需要连接到Telegram API。
从Telegram API获取API ID和哈希值。
运行以下命令:
mcp-telegram sign-in --api-id <your-api-id> --api-hash <your-api-hash> --phone-number <your-phone-number>
输入您从Telegram收到的验证码以连接到API。
如果启用了双因素身份验证,则可能需要密码。
[!NOTE] 若要从Telegram API注销,请使用
mcp-telegram logout命令。
配置Claude Desktop以识别Exa MCP服务器。
打开Claude Desktop配置文件:
~/Library/Application Support/Claude/claude_desktop_config.json%APPDATA%\Claude\claude_desktop_config.json注意: 您也可以在Claude Desktop应用设置中找到
claude_desktop_config.json
添加服务器配置
{
"mcpServers": {
"mcp-telegram": {
"command": "mcp-server",
"env": {
"TELEGRAM_API_ID": "<your-api-id>",
"TELEGRAM_API_HASH": "<your-api-hash>",
},
}
}
}
}
在使用Telegram的API之前,您需要获取自己的API ID和哈希值:
克隆仓库
安装依赖项
uv sync
运行服务器
uv run mcp-telegram --help
可以在src/mcp_telegram/tools.py文件中添加工具。
如何添加新工具:
创建一个新的类,继承自ToolArgs
class NewTool(ToolArgs):
"""新工具的描述。"""
pass
类的属性将作为工具的参数使用。 类的文档字符串将作为工具的描述使用。
实现新类的tool_runner函数
@tool_runner.register
async def new_tool(args: NewTool) -> t.Sequence[TextContent | ImageContent | EmbeddedResource]:
pass
函数应返回一个TextContent、ImageContent或EmbeddedResource序列。
函数应该是异步的,并接受新类的一个参数。
完成!重启客户端,新的工具应该可用。
验证可以通过Claude Desktop或直接运行工具来完成。
要直接运行工具,使用以下命令:
# 列出所有可用工具
uv run cli.py list-tools
# 运行具体工具
uv run cli.py call-tool --name ListDialogs --arguments '{"unread": true}'
MCP Inspector是一个帮助通过漂亮的UI调试服务器的工具。要运行它,使用以下命令:
npx @modelcontextprotocol/inspector uv run mcp-telegram
[!WARNING] 不要忘记在Inspector中定义环境变量TELEGRAM_API_ID和TELEGRAM_API_HASH。
如果您在Claude Desktop中看到消息“无法连接到MCP服务器mcp-telegram”,这意味着服务器配置不正确。
尝试以下操作:
uv二进制文件的完整路径