Skip to content

Commit cb55a8c

Browse files
authored
Merge pull request #10 from harrycis/list_channels
more flexible params for channels_list
2 parents 39e5d7f + 478cd16 commit cb55a8c

File tree

3 files changed

+57
-23
lines changed

3 files changed

+57
-23
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Model Context Protocol (MCP) server for Slack Workspaces. This integration suppo
2121
- Required inputs:
2222
- `channel_types` (string): Comma-separated channel types. Allowed values: 'mpim', 'im', 'public_channel', 'private_channel'. Example: 'public_channel,private_channel,im'.
2323
- `sort` (string): Type of sorting. Allowed values: 'popularity' - sort by number of members/participants in each channel.
24+
- `limit` (number, default: 100): Limit of channels to fetch.
25+
- `cursor` (string): Cursor for pagination. Use the value of the last row and column in the response as next_cursor field returned from the previous request.
2426
- Returns: List of channels
2527

2628
## Setup Guide

pkg/handler/channels.go

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package handler
22

33
import (
44
"context"
5+
"fmt"
56
"log"
67
"sort"
78
"strings"
@@ -21,6 +22,7 @@ type Channel struct {
2122
Topic string `json:"topic"`
2223
Purpose string `json:"purpose"`
2324
MemberCount int `json:"memberCount"`
25+
Cursor string `json:"cursor"`
2426
}
2527

2628
type ChannelsHandler struct {
@@ -54,46 +56,65 @@ func (ch *ChannelsHandler) ChannelsHandler(ctx context.Context, request mcp.Call
5456
}
5557
}
5658

59+
cursor := request.GetString("cursor", "")
60+
limit := request.GetInt("limit", 0)
61+
if limit == 0 && cursor == "" {
62+
return nil, fmt.Errorf("One of limit or cursor needs to be provided")
63+
}
64+
if limit == 0 {
65+
limit = 100
66+
}
67+
5768
api, err := ch.apiProvider.Provide()
5869
if err != nil {
5970
return nil, err
6071
}
6172

62-
var channels []slack.Channel
73+
var channelList []Channel
6374

6475
params := &slack.GetConversationsParameters{
6576
Types: channelTypes,
66-
Limit: 100,
77+
Limit: limit,
6778
ExcludeArchived: true,
79+
Cursor: cursor,
6880
}
69-
var total int
70-
for i := 1; ; i++ {
71-
var (
72-
chans []slack.Channel
73-
nextcur string
74-
)
81+
var (
82+
total int
83+
nextcur string
84+
)
85+
for {
86+
var chans []slack.Channel
7587

7688
chans, nextcur, err = api.GetConversationsContext(ctx, params)
77-
78-
if nextcur == "" {
79-
log.Printf("channels fetch complete %v", total)
89+
if err != nil {
8090
break
8191
}
8292

83-
params.Cursor = nextcur
93+
if l := len(chans); l > 0 {
94+
for _, channel := range chans {
95+
channelList = append(channelList, Channel{
96+
ID: channel.ID,
97+
Name: "#" + channel.Name,
98+
Topic: channel.Topic.Value,
99+
Purpose: channel.Purpose.Value,
100+
MemberCount: channel.NumMembers,
101+
})
102+
}
103+
104+
total += l
105+
params.Limit -= l
106+
}
84107

85-
channels = append(channels, chans...)
86-
}
108+
if total >= limit {
109+
log.Printf("channels fetch limit reached %v", total)
110+
break
111+
}
87112

88-
var channelList []Channel
89-
for _, channel := range channels {
90-
channelList = append(channelList, Channel{
91-
ID: channel.ID,
92-
Name: "#" + channel.Name,
93-
Topic: channel.Topic.Value,
94-
Purpose: channel.Purpose.Value,
95-
MemberCount: channel.NumMembers,
96-
})
113+
if nextcur == "" {
114+
log.Printf("channels fetch exhausted")
115+
break
116+
}
117+
params.Cursor = nextcur
97118
}
98119

99120
switch sortType {
@@ -105,6 +126,10 @@ func (ch *ChannelsHandler) ChannelsHandler(ctx context.Context, request mcp.Call
105126
// pass
106127
}
107128

129+
if len(channelList) > 0 && nextcur != "" {
130+
channelList[len(channelList)-1].Cursor = nextcur
131+
}
132+
108133
csvBytes, err := gocsv.MarshalBytes(&channelList)
109134
if err != nil {
110135
return nil, err

pkg/server/server.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ func NewMCPServer(provider *provider.ApiProvider) *MCPServer {
4949
mcp.WithString("sort",
5050
mcp.Description("Type of sorting. Allowed values: 'popularity' - sort by number of members/participants in each channel."),
5151
),
52+
mcp.WithNumber("limit",
53+
mcp.DefaultNumber(100),
54+
mcp.Description("The maximum number of items to return. Must be an integer under 1000."),
55+
),
56+
mcp.WithString("cursor",
57+
mcp.Description("Cursor for pagination. Use the value of the last row and column in the response as next_cursor field returned from the previous request."),
58+
),
5259
), channelsHandler.ChannelsHandler)
5360

5461
return &MCPServer{

0 commit comments

Comments
 (0)