From 3eee3e6872578dd67e17da950941ce0267f2fab1 Mon Sep 17 00:00:00 2001 From: 5startaek Date: Fri, 4 Apr 2025 23:46:42 +0900 Subject: [PATCH 1/2] feat: modify getChannels method to support fetching channels by IDs from env --- src/slack/index.ts | 58 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/src/slack/index.ts b/src/slack/index.ts index b012466..f2135b3 100644 --- a/src/slack/index.ts +++ b/src/slack/index.ts @@ -53,7 +53,7 @@ interface GetUserProfileArgs { // Tool definitions const listChannelsTool: Tool = { name: "slack_list_channels", - description: "List public channels in the workspace with pagination", + description: "List public or pre-defined channels in the workspace with pagination", inputSchema: { type: "object", properties: { @@ -221,23 +221,51 @@ class SlackClient { } async getChannels(limit: number = 100, cursor?: string): Promise { - const params = new URLSearchParams({ - types: "public_channel", - exclude_archived: "true", - limit: Math.min(limit, 200).toString(), - team_id: process.env.SLACK_TEAM_ID!, - }); - - if (cursor) { - params.append("cursor", cursor); + const predefinedChannelIds = process.env.SLACK_CHANNEL_IDS; + if (!predefinedChannelIds) { + const params = new URLSearchParams({ + types: "public_channel", + exclude_archived: "true", + limit: Math.min(limit, 200).toString(), + team_id: process.env.SLACK_TEAM_ID!, + }); + + if (cursor) { + params.append("cursor", cursor); + } + + const response = await fetch( + `https://slack.com/api/conversations.list?${params}`, + { headers: this.botHeaders }, + ); + + return response.json(); } - const response = await fetch( - `https://slack.com/api/conversations.list?${params}`, - { headers: this.botHeaders }, - ); + const predefinedChannelIdsArray = predefinedChannelIds.split(",").map((id: string) => id.trim()); + const channels = []; - return response.json(); + for (const channelId of predefinedChannelIdsArray) { + const params = new URLSearchParams({ + channel: channelId, + }); + + const response = await fetch( + `https://slack.com/api/conversations.info?${params}`, + { headers: this.botHeaders } + ); + const data = await response.json(); + + if (data.ok && data.channel && !data.channel.is_archived) { + channels.push(data.channel); + } + } + + return { + ok: true, + channels: channels, + response_metadata: { next_cursor: "" }, + }; } async postMessage(channel_id: string, text: string): Promise { From ebfd210e999bc8349743e38764dbb27fcd2301af Mon Sep 17 00:00:00 2001 From: nevergettingold Date: Sat, 5 Apr 2025 00:34:42 +0900 Subject: [PATCH 2/2] docs: update README --- src/slack/README.md | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/slack/README.md b/src/slack/README.md index 970cba6..61bec01 100644 --- a/src/slack/README.md +++ b/src/slack/README.md @@ -5,7 +5,7 @@ MCP Server for the Slack API, enabling Claude to interact with Slack workspaces. ## Tools 1. `slack_list_channels` - - List public channels in the workspace + - List public or pre-defined channels in the workspace - Optional inputs: - `limit` (number, default: 100, max: 200): Maximum number of channels to return - `cursor` (string): Pagination cursor for next page @@ -102,7 +102,8 @@ Add the following to your `claude_desktop_config.json`: ], "env": { "SLACK_BOT_TOKEN": "xoxb-your-bot-token", - "SLACK_TEAM_ID": "T01234567" + "SLACK_TEAM_ID": "T01234567", + "SLACK_CHANNEL_IDS": "C01234567, C76543210" } } } @@ -124,17 +125,26 @@ Add the following to your `claude_desktop_config.json`: "SLACK_BOT_TOKEN", "-e", "SLACK_TEAM_ID", + "-e", + "SLACK_CHANNEL_IDS", "mcp/slack" ], "env": { "SLACK_BOT_TOKEN": "xoxb-your-bot-token", - "SLACK_TEAM_ID": "T01234567" + "SLACK_TEAM_ID": "T01234567", + "SLACK_CHANNEL_IDS": "C01234567, C76543210" } } } } ``` +### Environment Variables + +1. `SLACK_BOT_TOKEN`: Required. The Bot User OAuth Token starting with `xoxb-`. +2. `SLACK_TEAM_ID`: Required. Your Slack workspace ID starting with `T`. +3. `SLACK_CHANNEL_IDS`: Optional. Comma-separated list of channel IDs to limit channel access (e.g., "C01234567, C76543210"). If not set, all public channels will be listed. + ### Troubleshooting If you encounter permission errors, verify that: