使用说明
Slack MCP服务器
用于Slack工作空间集成的模型上下文协议(MCP)服务器,使用具有HTTP传输和多用户支持的FastMCP。
特性
工具
核心消息传递工具
- 对话_历史 -通过增强的用户详细信息从渠道/DM获取消息
- bulk_对话_历史 -高效地从多个通道获取消息(避免N个API调用)
- 对话回复 -获取带分页的线程消息
- 对话_添加_消息 -将消息发布到通道/DM(默认情况下禁用安全功能)
- 对话_搜索_消息 -使用高级过滤器搜索邮件
- message_permalink -获取特定消息的永久链接
渠道和用户管理
- 频道列表 -列出所有具有排序选项的频道
- 频道_详细 -高效获取全面的渠道信息(避免多余的API调用)
- channel_info -获取详细的频道信息(主题、目的、成员数量)
- channel_members -列出频道成员及其用户详细信息
- set_channel_topic -更新频道主题
- set_channel_用途 -更新渠道用途
- 用户信息 -获取详细的用户信息和个人资料(支持多个用户)
- 用户列表 -列出所有具有筛选选项的用户(仅缓存以提高性能)
- 用户呈现 -检查用户的在线/外出状态
工作区和分析
- workspace_info -获取工作区详细信息(名称、域、计划)
- 分析_总结 -基于缓存数据的工作空间分析
- 文件列表 -按频道/用户/类型列出带有过滤器的文件
- 初始化缓存 -强制创建两个缓存文件
- cache_info -显示缓存文件的位置、大小和状态
- clear_cache -清除缓存文件以强制刷新
- 检查权限 -测试哪些Slack API作用域可用
交互式功能
- add_reaction -在消息中添加表情符号反应
资源
- slack://workspace/channels -包含元数据的所有频道的CSV目录
- slack://workspace/users -包含元数据的所有用户的CSV目录
安装
pip install -r requirements.txt
配置
所需的环境变量
SLACK_MCP_XOXP_TOKEN-Slack机器人令牌(xoxb-)或用户令牌(xoxp-)
可选环境变量
SLACK_MCP_ADD_MESSAGE_TOOL-启用邮件发布:- 未设置=禁用(安全默认)
true或1=已为所有通道启用- 逗号分隔的通道ID=仅对特定通道启用
SLACK_MCP_USERS_CACHE-用户缓存文件的路径(默认:~/slack-cache/users_cache.json)SLACK_MCP_CHANNELS_CACHE-通道缓存文件的路径(默认值:~/slack-cache/channels_cache_v2.json)
用法
启动HTTP服务器
python slack_mcp_server.py
服务器在上运行 http://0.0.0.0:8000/mcp 默认情况下。
认证
对于多用户支持,请在请求标头中传递Slack令牌:
SLACK_MCP_XOXP_TOKEN: xoxb-your-slack-token
通过邮件头进行可选邮件发布控制:
SLACK_MCP_ADD_MESSAGE_TOOL: true
或者,设置 SLACK_MCP_XOXP_TOKEN 单用户模式的环境变量。
API示例
获取频道历史记录
{
"method": "conversations_history",
"params": {
"channel_id": "#general",
"limit": "1d",
"include_activity_messages": false
}
}
搜索邮件
{
"method": "conversations_search_messages",
"params": {
"search_query": "project update",
"filter_in_channel": "#general",
"filter_users_from": "@john",
"limit": 50
}
}
获取频道目录
{
"method": "resource",
"params": {
"uri": "slack://myworkspace/channels"
}
}
松弛权限
Slack应用程序所需的范围:
- 频道:历史 -阅读公共频道消息
- 组:历史 -阅读私人频道消息
- im:历史 -阅读直接消息
- MPI:历史 -阅读群组私信
- 频道:阅读 -列出公共频道
- 组:阅读 -列出私人频道
- 用户:阅读 -列出工作区用户
- 聊天:写 -发布消息(如果启用)
增强功能
智能高速缓存
- 用户缓存:自动缓存用户信息以避免重复的API调用
- 通道缓存:缓存具有可配置刷新间隔的通道元数据
- 演出:显著减少API速率限制的使用,并改进响应时间
- 缓存优先:
user_info和channel_info工具默认缓存即时响应 - 后备方案:如果缓存未命中或明确请求,则优雅地回退到API
智能名称解析
除了ID之外,服务器现在还接受用户友好的名称:
渠道参考:
#general→ 解析为通道ID(C1234567890)#project-alpha→ 解析为通道ID@john_dm→ 使用用户“john”打开/查找DM
用户参考:
@john→ 解析为用户ID(U1234567890)john.doe→ 使用显示名称或真实名称解析John Doe→ 使用真实姓名解析
示例:
// Get messages from #general channel with user details
{
"method": "conversations_history",
"params": {
"channel_id": "#general",
"limit": "1d",
"include_user_details": true
}
}
// Get detailed info about a user (cache-first, instant)
{
"method": "user_info",
"params": {
"user_id": "@gongrzhe"
}
}
// Get all channels efficiently (1 API call instead of N channel_info calls)
{
"method": "channels_detailed",
"params": {
"channel_types": "public_channel,private_channel",
"sort": "popularity",
"limit": 50
}
}
// Get messages from multiple channels efficiently (instead of N conversations_history calls)
{
"method": "bulk_conversations_history",
"params": {
"channel_ids": "#general, #random, #project-alpha",
"limit": "1d",
"filter_user": "@chris"
}
}
// Get fresh user info from API (slower)
{
"method": "user_info",
"params": {
"user_id": "@gongrzhe",
"use_cache": false
}
}
// List members of #general channel
{
"method": "channel_members",
"params": {
"channel_id": "#general"
}
}
// Get workspace analytics
{
"method": "analytics_summary",
"params": {
"date_range": "30d"
}
}
// Add reaction to a message
{
"method": "add_reaction",
"params": {
"channel_id": "#general",
"message_ts": "1699123456.123456",
"emoji_name": "thumbsup"
}
}
// Set channel topic
{
"method": "set_channel_topic",
"params": {
"channel_id": "#general",
"topic": "Welcome to our main discussion channel!"
}
}
// Search messages from @john in #project-alpha
{
"method": "conversations_search_messages",
"params": {
"search_query": "deployment",
"filter_in_channel": "#project-alpha",
"filter_users_from": "@john"
}
}
// List files shared by @chris
{
"method": "files_list",
"params": {
"user_id": "@chris",
"count": 20,
"types": "images"
}
}
// Check what permissions are available
{
"method": "check_permissions"
}
// Initialize both cache files
{
"method": "initialize_cache"
}
// Check cache file locations and status
{
"method": "cache_info"
}
// Clear all cache files
{
"method": "clear_cache",
"params": {
"cache_type": "both"
}
}
安全
- 为了安全起见,默认情况下禁用消息发布
- 基于令牌的身份验证,支持多用户
- 没有记录或提交到存储库的秘密
- 遵循Slack API费率限制和最佳实践