松弛mpm
用于Slack工作空间集成的Python MCP(模型上下文协议)服务器和API库。将40多个Slack操作作为MCP工具与Claude Desktop一起使用,并为构建Slack集成提供一个干净的异步Python API。
它是什么
- Python API库:
from slack_mpm.api import messages; await messages.send_message(...) - MCP服务器:通过包装Claude Desktop的API
slack-mpm mcp - 代理脚本:中的独立自动化脚本
agents/
先决条件
- Python 3.10+ 和 紫外线
- A. Slack应用程序 使用bot令牌
创建Slack应用程序
- 首选 https://api.slack.com/apps 点击“创建新应用”
- 选择“从头开始”,为其命名并选择您的工作区
- 转到“OAuth和权限”并添加这些Bot令牌范围:
- channels:read, channels:write, channels:manage - chat:write, chat:write.public - users:read - files:read, files:write - reactions:write - pins:write - bookmarks:read, bookmarks:write - emoji:read - groups:read, groups:write - im:read, im:write - mpim:read, mpim:write
- 将应用程序安装到您的工作区
- 复制“Bot用户OAuth令牌”(以开头
xoxb-)
对于 search_messages 以及提醒,还可以创建一个用户令牌 search:read, reminders:read, reminders:write.
快速开始
git clone
cd slack-mpm
cp .env.local.example .env.local
# Edit .env.local and add your SLACK_BOT_TOKEN=xoxb-...
uv sync
uv run slack-mpm setup # Verify your token works
uv run slack-mpm doctor # Health checkClaude桌面配置
添加 ~/Library/Application Support/Claude/claude_desktop_config.json (macOS):
{
"mcpServers": {
"slack": {
"command": "uv",
"args": ["run", "--directory", "/absolute/path/to/slack-mpm", "slack-mpm", "mcp"]
}
}
}重新启动克劳德桌面。您应该看到可用的Slack工具。
可用工具
渠道工具(8)
| 工具 | 说明 |
|---|---|
list_channels | 列出所有频道(公共+私人) |
get_channel_info | 获取频道的详细信息 |
create_channel | 创建新频道 |
archive_channel | 存档频道 |
invite_to_channel | 邀请用户加入频道 |
kick_from_channel | 从频道中删除用户 |
join_channel | 加入频道 |
set_channel_topic | 设置频道主题 |
消息工具(13)
| 工具 | 说明 |
|---|---|
send_message | 发送消息(支持块+线程) |
send_ephemeral | 发送仅对一个用户可见的消息 |
update_message | 编辑现有邮件 |
delete_message | 删除消息 |
get_permalink | 获取消息的永久链接 |
search_messages | 搜索邮件(需要用户令牌) |
list_history | 获取频道消息历史记录 |
add_reaction | 添加表情符号反应 |
remove_reaction | 删除表情符号反应 |
pin_message | 固定消息 |
unpin_message | 不愉快的消息 |
reply_in_thread | 在帖子中回复 |
get_thread_replies | 获取所有线程回复 |
用户工具(5)
| 工具 | 说明 |
|---|---|
list_users | 列出所有工作区用户 |
get_user_info | 获取用户详细信息 |
get_user_by_email | 通过电子邮件查找用户 |
open_dm | 与用户打开DM通道 |
list_user_channels | 列出用户所属的频道 |
文件工具(5)
| 工具 | 说明 |
|---|---|
upload_file | 将文件上传到频道 |
list_files | 列出工作区文件 |
get_file_info | 获取文件详细信息 |
delete_file | 删除文件 |
share_file | 将现有文件共享到频道 |
工作空间工具(4)
| 工具 | 说明 |
|---|---|
get_workspace_info | 获取工作区/团队信息 |
list_emojis | 列出自定义表情符号 |
get_bot_info | 获取机器人详细信息 |
auth_test | 验证令牌 |
提醒工具(4)
| 工具 | 说明 |
|---|---|
add_reminder | 创建提醒 |
list_reminders | 列出提醒 |
complete_reminder | 标记提醒完成 |
delete_reminder | 删除提醒 |
书签工具(3)
| 工具 | 说明 |
|---|---|
list_bookmarks | 列出频道书签 |
add_bookmark | 向频道添加书签 |
remove_bookmark | 删除书签 |
定时消息工具(3)
| 工具 | 说明 |
|---|---|
schedule_message | 安排未来的消息 |
list_scheduled_messages | 列出待处理的计划邮件 |
delete_scheduled_message | 取消已安排的消息 |
直接使用Python API
import asyncio
from slack_mpm.api import messages, channels, users
from slack_mpm.auth.token_manager import TokenManager
async def main():
token = TokenManager().get_token()
# Send a message
await messages.send_message(token, "#general", "Hello from Python!")
# List channels
data = await channels.list_channels(token)
for ch in data["channels"]:
print(ch["name"])
# Get user info
user = await users.get_user_by_email(token, "person@example.com")
print(user["user"]["real_name"])
asyncio.run(main())代理脚本
中的独立自动化脚本 agents/ 目录。
slack_listener.py --实时通道监视器
轮询一个频道,并在新消息到达时打印出来。
uv run agents/slack_listener.py --channel C1234567890
uv run agents/slack_listener.py --channel C1234567890 --interval 10
uv run agents/slack_listener.py --channel C1234567890 --no-historyslack_notifier.py --发送通知
从命令行或stdin向Slack频道发送消息或文件上传。
uv run agents/slack_notifier.py --channel C1234567890 --message "Deploy complete"
echo "alert!" | uv run agents/slack_notifier.py --channel C1234567890
cat report.txt | uv run agents/slack_notifier.py --channel C1234567890 --as-file --filename report.txtslack_responder.py --自动应答机器人
监控@提及或DM,并自动回复配置的消息。
uv run agents/slack_responder.py --response "Thanks, I'll get back to you!"
uv run agents/slack_responder.py --channel C1234567890 --response "Got it!" --interval 60
uv run agents/slack_responder.py --response "Out of office" --dry-runslack_digest.py --活动摘要
生成最近频道活动的摘要:消息计数、活动用户、热门话题。
uv run agents/slack_digest.py --channel C1234567890
uv run agents/slack_digest.py --channel C1234567890 --hours 168 # 1 week
uv run agents/slack_digest.py --channel C1234567890 --hours 24 --top-users 10slack_archiver.py --频道历史导出
将完整的频道消息历史导出为JSON或Markdown文件,并支持线程。
uv run agents/slack_archiver.py --channel C1234567890 --output ./archive/
uv run agents/slack_archiver.py --channel C1234567890 --output ./archive/ --format markdown
uv run agents/slack_archiver.py --channel C1234567890 --output ./archive/ --days 30发展
# Install dev dependencies
uv sync
# Run tests
uv run pytest
uv run pytest --cov=src --cov-report=html
# Type checking
uv run mypy --strict src/
# Linting
uv run ruff check src/ agents/
uv run ruff format src/ agents/项目结构
src/slack_mpm/
├── api/
│ ├── _client.py # Shared httpx client + SlackAPIError
│ ├── channels.py # Channel operations
│ ├── messages.py # Message operations
│ ├── users.py # User operations
│ ├── files.py # File operations
│ ├── workspace.py # Workspace operations
│ ├── reminders.py # Reminder operations
│ ├── bookmarks.py # Bookmark operations
│ └── scheduled.py # Scheduled message operations
├── auth/
│ ├── models.py # SlackToken, TokenStatus, WorkspaceInfo
│ └── token_manager.py # TokenManager (loads from .env.local)
├── cli/
│ └── main.py # CLI: setup, doctor, mcp commands
└── server/
└── slack_mpm_server.py # SlackMCPServer (MCP adapter)
agents/
├── slack_listener.py # Channel message poller
├── slack_notifier.py # Send notifications
├── slack_responder.py # Auto-responder bot
├── slack_digest.py # Activity digest
└── slack_archiver.py # History export