Skip to content

Commit b55f28b

Browse files
authored
feat(mcp): events_list returns parseable YAML output (#346)
Signed-off-by: Marc Nuri <[email protected]>
1 parent d372380 commit b55f28b

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

pkg/mcp/events_test.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package mcp
22

33
import (
4+
"strings"
45
"testing"
56

67
"github.com/BurntSushi/toml"
@@ -9,6 +10,7 @@ import (
910
v1 "k8s.io/api/core/v1"
1011
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1112
"k8s.io/client-go/kubernetes"
13+
"sigs.k8s.io/yaml"
1214
)
1315

1416
type EventsSuite struct {
@@ -24,7 +26,7 @@ func (s *EventsSuite) TestEventsList() {
2426
s.Falsef(toolResult.IsError, "call tool failed")
2527
})
2628
s.Run("returns no events message", func() {
27-
s.Equal("No events found", toolResult.Content[0].(mcp.TextContent).Text)
29+
s.Equal("# No events found", toolResult.Content[0].(mcp.TextContent).Text)
2830
})
2931
})
3032
s.Run("events_list (with events)", func() {
@@ -50,8 +52,16 @@ func (s *EventsSuite) TestEventsList() {
5052
s.Nilf(err, "call tool failed %v", err)
5153
s.Falsef(toolResult.IsError, "call tool failed")
5254
})
55+
s.Run("has yaml comment indicating output format", func() {
56+
s.Truef(strings.HasPrefix(toolResult.Content[0].(mcp.TextContent).Text, "# The following events (YAML format) were found:\n"), "unexpected result %v", toolResult.Content[0].(mcp.TextContent).Text)
57+
})
58+
var decoded []v1.Event
59+
err = yaml.Unmarshal([]byte(toolResult.Content[0].(mcp.TextContent).Text), &decoded)
60+
s.Run("has yaml content", func() {
61+
s.Nilf(err, "unmarshal failed %v", err)
62+
})
5363
s.Run("returns all events", func() {
54-
s.Equalf("The following events (YAML format) were found:\n"+
64+
s.YAMLEqf(""+
5565
"- InvolvedObject:\n"+
5666
" Kind: Pod\n"+
5767
" Name: a-pod\n"+
@@ -83,8 +93,16 @@ func (s *EventsSuite) TestEventsList() {
8393
s.Nilf(err, "call tool failed %v", err)
8494
s.Falsef(toolResult.IsError, "call tool failed")
8595
})
96+
s.Run("has yaml comment indicating output format", func() {
97+
s.Truef(strings.HasPrefix(toolResult.Content[0].(mcp.TextContent).Text, "# The following events (YAML format) were found:\n"), "unexpected result %v", toolResult.Content[0].(mcp.TextContent).Text)
98+
})
99+
var decoded []v1.Event
100+
err = yaml.Unmarshal([]byte(toolResult.Content[0].(mcp.TextContent).Text), &decoded)
101+
s.Run("has yaml content", func() {
102+
s.Nilf(err, "unmarshal failed %v", err)
103+
})
86104
s.Run("returns events from namespace", func() {
87-
s.Equalf("The following events (YAML format) were found:\n"+
105+
s.YAMLEqf(""+
88106
"- InvolvedObject:\n"+
89107
" Kind: Pod\n"+
90108
" Name: a-pod\n"+

pkg/toolsets/core/events.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ func eventsList(params api.ToolHandlerParams) (*api.ToolCallResult, error) {
4545
return api.NewToolCallResult("", fmt.Errorf("failed to list events in all namespaces: %v", err)), nil
4646
}
4747
if len(eventMap) == 0 {
48-
return api.NewToolCallResult("No events found", nil), nil
48+
return api.NewToolCallResult("# No events found", nil), nil
4949
}
5050
yamlEvents, err := output.MarshalYaml(eventMap)
5151
if err != nil {
5252
err = fmt.Errorf("failed to list events in all namespaces: %v", err)
5353
}
54-
return api.NewToolCallResult(fmt.Sprintf("The following events (YAML format) were found:\n%s", yamlEvents), err), nil
54+
return api.NewToolCallResult(fmt.Sprintf("# The following events (YAML format) were found:\n%s", yamlEvents), err), nil
5555
}

0 commit comments

Comments
 (0)