Skip to content

Commit 252a378

Browse files
committed
🤖 fix: paginate get_events output
1 parent d88c856 commit 252a378

1 file changed

Lines changed: 27 additions & 4 deletions

File tree

‎internal/app/mcpapp/tools.go‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const (
2020
defaultPodLogTailLines int64 = 2000
2121
maxPodLogBytes int64 = 1 << 20
2222
podLogTruncatedSuffix = "\n(truncated)"
23+
defaultEventListLimit int64 = 200
24+
maxEventListLimit int64 = 1000
2325
)
2426

2527
type listControlPlanesInput struct {
@@ -82,9 +84,11 @@ type listTemplatesOutput struct {
8284
}
8385

8486
type getEventsInput struct {
85-
Namespace string `json:"namespace,omitempty"`
87+
Namespace string `json:"namespace"`
8688
Name string `json:"name,omitempty"`
8789
Kind string `json:"kind,omitempty"`
90+
Limit int64 `json:"limit,omitempty"`
91+
Continue string `json:"continue,omitempty"`
8892
}
8993

9094
type eventSummary struct {
@@ -96,7 +100,8 @@ type eventSummary struct {
96100
}
97101

98102
type getEventsOutput struct {
99-
Items []eventSummary `json:"items"`
103+
Items []eventSummary `json:"items"`
104+
Continue string `json:"continue,omitempty"`
100105
}
101106

102107
type getPodLogsInput struct {
@@ -232,6 +237,10 @@ func registerTools(server *mcp.Server, k8sClient client.Client, clientset kubern
232237
Name: "get_events",
233238
Description: "Get Kubernetes events for an object.",
234239
}, func(ctx context.Context, _ *mcp.CallToolRequest, input getEventsInput) (*mcp.CallToolResult, getEventsOutput, error) {
240+
if input.Namespace == "" {
241+
return nil, getEventsOutput{}, fmt.Errorf("namespace is required")
242+
}
243+
235244
selector := fields.Set{}
236245
if input.Name != "" {
237246
selector["involvedObject.name"] = input.Name
@@ -240,7 +249,18 @@ func registerTools(server *mcp.Server, k8sClient client.Client, clientset kubern
240249
selector["involvedObject.kind"] = input.Kind
241250
}
242251

243-
listOptions := metav1.ListOptions{}
252+
limit := input.Limit
253+
if limit <= 0 {
254+
limit = defaultEventListLimit
255+
}
256+
if limit > maxEventListLimit {
257+
limit = maxEventListLimit
258+
}
259+
260+
listOptions := metav1.ListOptions{
261+
Limit: limit,
262+
Continue: input.Continue,
263+
}
244264
if len(selector) > 0 {
245265
listOptions.FieldSelector = selector.String()
246266
}
@@ -250,7 +270,10 @@ func registerTools(server *mcp.Server, k8sClient client.Client, clientset kubern
250270
return nil, getEventsOutput{}, fmt.Errorf("list events in namespace %q: %w", input.Namespace, err)
251271
}
252272

253-
output := getEventsOutput{Items: make([]eventSummary, 0, len(eventList.Items))}
273+
output := getEventsOutput{
274+
Items: make([]eventSummary, 0, len(eventList.Items)),
275+
Continue: eventList.Continue,
276+
}
254277
for _, event := range eventList.Items {
255278
output.Items = append(output.Items, eventSummary{
256279
Type: event.Type,

0 commit comments

Comments
 (0)