用于 Slack 工作区集成的模型上下文协议(MCP)服务器,使用 FastMCP 和 HTTP 传输,并支持多用户。
pip install -r requirements.txt
SLACK_MCP_XOXP_TOKEN - Slack 机器人令牌(xoxb-)或用户令牌(xoxp-)SLACK_M_ MCP_ADD_MESSAGE_TOOL - 启用消息发布:
true 或 1 = 对所有频道启用SLACK_MCP_USERS_CACHE - 用户缓存文件路径(默认:~/slack-cache/users_cache.json)SLACK_MCP_CHANNELS_CACHE - 频道缓存文件路径(默认:~/slack-cache/channels_cache_v2.json)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 环境变量以单用户模式运行。
{
"method": "conversations_history",
"params": {
"channel_id": "#general",
"limit": "1d",
"include_activity_messages": false
}
}
{
"method": "conversations_search_messages",
"params": {
"search_query": "项目更新",
"filter_in_channel": "#general",
"filter_users_from": "@john",
"limit": 50
}
}
{
"method": "resource",
"params": {
"uri": "slack://myworkspace/channels"
}
}
您的 Slack 应用所需的范围:
user_info 和 channel_info 工具默认使用缓存以实现即时响应服务器现在接受用户友好的名称以及 ID:
频道引用:
#general → 解析为频道 ID (C1234567890)#project-alpha → 解析为频道 ID@john_dm → 打开/查找与用户“john”的私信用户引用:
@john → 解析为用户 ID (U1234567890)john.doe → 使用显示名或真实名解析John Doe → 使用真实名解析示例:
// 从 #general 频道获取带有用户详情的消息
{
"method": "conversations_history",
"params": {
"channel_id": "#general",
"limit": "1d",
"include_user_details": true
}
}
// 获取用户详细信息(缓存优先,即时响应)
{
"method": "user_info",
"params": {
"user_id": "@gongrzhe"
}
}
// 高效地获取所有频道(1次 API 调用代替 N 次 channel_info 调用)
{
"method": "channels_detailed",
"params": {
"channel_types": "public_channel,private_channel",
"sort": "popularity",
"limit": 50
}
}
// 高效地从多个频道获取消息(代替 N 次 conversations_history 调用)
{
"method": "bulk_conversations_history",
"params": {
"channel_ids": "#general, #random, #project-alpha",
"limit": "1d",
"filter_user": "@chris"
}
}
// 从 API 获取最新的用户信息(较慢)
{
"method": "user_info",
"params": {
"user_id": "@gongrzhe",
"use_cache": false
}
}
// 列出 #general 频道的成员
{
"method": "channel_members",
"params": {
"channel_id": "#general"
}
}
// 获取工作区分析
{
"method": "analytics_summary",
"params": {
"date_range": "30d"
}
}
// 向消息添加反应
{
"method": "add_reaction",
"params": {
"channel_id": "#general",
"message_ts": "1699123456.123456",
"emoji_name": "thumbsup"
}
}
// 设置频道主题
{
"method": "set_channel_topic",
"params": {
"channel_id": "#general",
"topic": "欢迎来到我们的主要讨论频道!"
}
}
// 在 #project-alpha 中搜索 @john 的消息
{
"method": "conversations_search_messages",
"params": {
"search_query": "部署",
"filter_in_channel": "#project-alpha",
"filter_users_from": "@john"
}
}
// 列出 @chris 分享的文件
{
"method": "files_list",
"params": {
"user_id": "@chris",
"count": 20,
"types": "images"
}
}
// 检查可用的权限
{
"method": "check_permissions"
}
// 初始化两个缓存文件
{
"method": "initialize_cache"
}
// 检查缓存文件的位置和状态
{
"method": "cache_info"
}
// 清除所有缓存文件
{
"method": "clear_cache",
"params": {
"cache_type": "both"
}
}