Skip to content

Commit 1ba2678

Browse files
authored
Merge pull request #1260 from ohmont/feat/optimize-slack-get-channels
Modify getChannels method in server-slack to support fetching channels by IDs from env
2 parents 3958be3 + 3344768 commit 1ba2678

File tree

2 files changed

+56
-18
lines changed

2 files changed

+56
-18
lines changed

src/slack/README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ MCP Server for the Slack API, enabling Claude to interact with Slack workspaces.
55
## Tools
66

77
1. `slack_list_channels`
8-
- List public channels in the workspace
8+
- List public or pre-defined channels in the workspace
99
- Optional inputs:
1010
- `limit` (number, default: 100, max: 200): Maximum number of channels to return
1111
- `cursor` (string): Pagination cursor for next page
@@ -102,7 +102,8 @@ Add the following to your `claude_desktop_config.json`:
102102
],
103103
"env": {
104104
"SLACK_BOT_TOKEN": "xoxb-your-bot-token",
105-
"SLACK_TEAM_ID": "T01234567"
105+
"SLACK_TEAM_ID": "T01234567",
106+
"SLACK_CHANNEL_IDS": "C01234567, C76543210"
106107
}
107108
}
108109
}
@@ -124,17 +125,26 @@ Add the following to your `claude_desktop_config.json`:
124125
"SLACK_BOT_TOKEN",
125126
"-e",
126127
"SLACK_TEAM_ID",
128+
"-e",
129+
"SLACK_CHANNEL_IDS",
127130
"mcp/slack"
128131
],
129132
"env": {
130133
"SLACK_BOT_TOKEN": "xoxb-your-bot-token",
131-
"SLACK_TEAM_ID": "T01234567"
134+
"SLACK_TEAM_ID": "T01234567",
135+
"SLACK_CHANNEL_IDS": "C01234567, C76543210"
132136
}
133137
}
134138
}
135139
}
136140
```
137141

142+
### Environment Variables
143+
144+
1. `SLACK_BOT_TOKEN`: Required. The Bot User OAuth Token starting with `xoxb-`.
145+
2. `SLACK_TEAM_ID`: Required. Your Slack workspace ID starting with `T`.
146+
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.
147+
138148
### Troubleshooting
139149

140150
If you encounter permission errors, verify that:

src/slack/index.ts

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ interface GetUserProfileArgs {
5353
// Tool definitions
5454
const listChannelsTool: Tool = {
5555
name: "slack_list_channels",
56-
description: "List public channels in the workspace with pagination",
56+
description: "List public or pre-defined channels in the workspace with pagination",
5757
inputSchema: {
5858
type: "object",
5959
properties: {
@@ -221,23 +221,51 @@ class SlackClient {
221221
}
222222

223223
async getChannels(limit: number = 100, cursor?: string): Promise<any> {
224-
const params = new URLSearchParams({
225-
types: "public_channel",
226-
exclude_archived: "true",
227-
limit: Math.min(limit, 200).toString(),
228-
team_id: process.env.SLACK_TEAM_ID!,
229-
});
230-
231-
if (cursor) {
232-
params.append("cursor", cursor);
224+
const predefinedChannelIds = process.env.SLACK_CHANNEL_IDS;
225+
if (!predefinedChannelIds) {
226+
const params = new URLSearchParams({
227+
types: "public_channel",
228+
exclude_archived: "true",
229+
limit: Math.min(limit, 200).toString(),
230+
team_id: process.env.SLACK_TEAM_ID!,
231+
});
232+
233+
if (cursor) {
234+
params.append("cursor", cursor);
235+
}
236+
237+
const response = await fetch(
238+
`https://slack.com/api/conversations.list?${params}`,
239+
{ headers: this.botHeaders },
240+
);
241+
242+
return response.json();
233243
}
234244

235-
const response = await fetch(
236-
`https://slack.com/api/conversations.list?${params}`,
237-
{ headers: this.botHeaders },
238-
);
245+
const predefinedChannelIdsArray = predefinedChannelIds.split(",").map((id: string) => id.trim());
246+
const channels = [];
239247

240-
return response.json();
248+
for (const channelId of predefinedChannelIdsArray) {
249+
const params = new URLSearchParams({
250+
channel: channelId,
251+
});
252+
253+
const response = await fetch(
254+
`https://slack.com/api/conversations.info?${params}`,
255+
{ headers: this.botHeaders }
256+
);
257+
const data = await response.json();
258+
259+
if (data.ok && data.channel && !data.channel.is_archived) {
260+
channels.push(data.channel);
261+
}
262+
}
263+
264+
return {
265+
ok: true,
266+
channels: channels,
267+
response_metadata: { next_cursor: "" },
268+
};
241269
}
242270

243271
async postMessage(channel_id: string, text: string): Promise<any> {

0 commit comments

Comments
 (0)