|
1 | 1 | package mcp |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "flag" |
4 | 6 | "regexp" |
5 | | - "strings" |
| 7 | + "strconv" |
6 | 8 | "testing" |
7 | 9 |
|
8 | 10 | "github.com/mark3labs/mcp-go/client/transport" |
| 11 | + "github.com/stretchr/testify/suite" |
| 12 | + "k8s.io/klog/v2" |
| 13 | + "k8s.io/klog/v2/textlogger" |
9 | 14 | ) |
10 | 15 |
|
11 | | -func TestToolCallLogging(t *testing.T) { |
12 | | - testCaseWithContext(t, &mcpContext{logLevel: 5}, func(c *mcpContext) { |
13 | | - _, _ = c.callTool("configuration_view", map[string]interface{}{ |
14 | | - "minified": false, |
15 | | - }) |
16 | | - t.Run("Logs tool name", func(t *testing.T) { |
17 | | - expectedLog := "mcp tool call: configuration_view(" |
18 | | - if !strings.Contains(c.logBuffer.String(), expectedLog) { |
19 | | - t.Errorf("Expected log to contain '%s', got: %s", expectedLog, c.logBuffer.String()) |
20 | | - } |
21 | | - }) |
22 | | - t.Run("Logs tool call arguments", func(t *testing.T) { |
23 | | - expected := `"mcp tool call: configuration_view\((.+)\)"` |
24 | | - m := regexp.MustCompile(expected).FindStringSubmatch(c.logBuffer.String()) |
25 | | - if len(m) != 2 { |
26 | | - t.Fatalf("Expected log entry to contain arguments, got %s", c.logBuffer.String()) |
27 | | - } |
28 | | - if m[1] != "map[minified:false]" { |
29 | | - t.Errorf("Expected log arguments to be 'map[minified:false]', got %s", m[1]) |
30 | | - } |
31 | | - }) |
| 16 | +type McpLoggingSuite struct { |
| 17 | + BaseMcpSuite |
| 18 | + klogState klog.State |
| 19 | + logBuffer bytes.Buffer |
| 20 | +} |
| 21 | + |
| 22 | +func (s *McpLoggingSuite) SetupTest() { |
| 23 | + s.BaseMcpSuite.SetupTest() |
| 24 | + s.klogState = klog.CaptureState() |
| 25 | +} |
| 26 | + |
| 27 | +func (s *McpLoggingSuite) TearDownTest() { |
| 28 | + s.BaseMcpSuite.TearDownTest() |
| 29 | + s.klogState.Restore() |
| 30 | +} |
| 31 | + |
| 32 | +func (s *McpLoggingSuite) SetLogLevel(level int) { |
| 33 | + flags := flag.NewFlagSet("test", flag.ContinueOnError) |
| 34 | + klog.InitFlags(flags) |
| 35 | + _ = flags.Set("v", strconv.Itoa(level)) |
| 36 | + klog.SetLogger(textlogger.NewLogger(textlogger.NewConfig(textlogger.Verbosity(level), textlogger.Output(&s.logBuffer)))) |
| 37 | +} |
| 38 | + |
| 39 | +func (s *McpLoggingSuite) TestLogsToolCall() { |
| 40 | + s.SetLogLevel(5) |
| 41 | + s.InitMcpClient() |
| 42 | + _, err := s.CallTool("configuration_view", map[string]interface{}{"minified": false}) |
| 43 | + s.Require().NoError(err, "call to tool configuration_view failed") |
| 44 | + |
| 45 | + s.Run("Logs tool name", func() { |
| 46 | + s.Contains(s.logBuffer.String(), "mcp tool call: configuration_view(") |
32 | 47 | }) |
33 | | - before := func(c *mcpContext) { |
34 | | - c.clientOptions = append(c.clientOptions, transport.WithHeaders(map[string]string{ |
35 | | - "Accept-Encoding": "gzip", |
36 | | - "Authorization": "Bearer should-not-be-logged", |
37 | | - "authorization": "Bearer should-not-be-logged", |
38 | | - "a-loggable-header": "should-be-logged", |
39 | | - })) |
| 48 | + s.Run("Logs tool call arguments", func() { |
| 49 | + expected := `"mcp tool call: configuration_view\((.+)\)"` |
| 50 | + m := regexp.MustCompile(expected).FindStringSubmatch(s.logBuffer.String()) |
| 51 | + s.Len(m, 2, "Expected log entry to contain arguments") |
| 52 | + s.Equal("map[minified:false]", m[1], "Expected log arguments to be 'map[minified:false]'") |
| 53 | + }) |
| 54 | +} |
| 55 | + |
| 56 | +func (s *McpLoggingSuite) TestLogsToolCallHeaders() { |
| 57 | + s.SetLogLevel(7) |
| 58 | + s.InitMcpClient(transport.WithHTTPHeaders(map[string]string{ |
| 59 | + "Accept-Encoding": "gzip", |
| 60 | + "Authorization": "Bearer should-not-be-logged", |
| 61 | + "authorization": "Bearer should-not-be-logged", |
| 62 | + "a-loggable-header": "should-be-logged", |
| 63 | + })) |
| 64 | + _, err := s.CallTool("configuration_view", map[string]interface{}{"minified": false}) |
| 65 | + s.Require().NoError(err, "call to tool configuration_view failed") |
| 66 | + |
| 67 | + s.Run("Logs tool call headers", func() { |
| 68 | + expectedLog := "mcp tool call headers: A-Loggable-Header: should-be-logged" |
| 69 | + s.Contains(s.logBuffer.String(), expectedLog, "Expected log to contain loggable header") |
| 70 | + }) |
| 71 | + sensitiveHeaders := []string{ |
| 72 | + "Authorization:", |
| 73 | + // TODO: Add more sensitive headers as needed |
40 | 74 | } |
41 | | - testCaseWithContext(t, &mcpContext{logLevel: 7, before: before}, func(c *mcpContext) { |
42 | | - _, _ = c.callTool("configuration_view", map[string]interface{}{ |
43 | | - "minified": false, |
44 | | - }) |
45 | | - t.Run("Logs tool call headers", func(t *testing.T) { |
46 | | - expectedLog := "mcp tool call headers: A-Loggable-Header: should-be-logged" |
47 | | - if !strings.Contains(c.logBuffer.String(), expectedLog) { |
48 | | - t.Errorf("Expected log to contain '%s', got: %s", expectedLog, c.logBuffer.String()) |
49 | | - } |
50 | | - }) |
51 | | - sensitiveHeaders := []string{ |
52 | | - "Authorization:", |
53 | | - // TODO: Add more sensitive headers as needed |
| 75 | + s.Run("Does not log sensitive headers", func() { |
| 76 | + for _, header := range sensitiveHeaders { |
| 77 | + s.NotContains(s.logBuffer.String(), header, "Log should not contain sensitive header") |
54 | 78 | } |
55 | | - t.Run("Does not log sensitive headers", func(t *testing.T) { |
56 | | - for _, header := range sensitiveHeaders { |
57 | | - if strings.Contains(c.logBuffer.String(), header) { |
58 | | - t.Errorf("Log should not contain sensitive header '%s', got: %s", header, c.logBuffer.String()) |
59 | | - } |
60 | | - } |
61 | | - }) |
62 | | - t.Run("Does not log sensitive header values", func(t *testing.T) { |
63 | | - if strings.Contains(c.logBuffer.String(), "should-not-be-logged") { |
64 | | - t.Errorf("Log should not contain sensitive header value 'should-not-be-logged', got: %s", c.logBuffer.String()) |
65 | | - } |
66 | | - }) |
67 | 79 | }) |
| 80 | + s.Run("Does not log sensitive header values", func() { |
| 81 | + s.NotContains(s.logBuffer.String(), "should-not-be-logged", "Log should not contain sensitive header value") |
| 82 | + }) |
| 83 | +} |
| 84 | + |
| 85 | +func TestMcpLogging(t *testing.T) { |
| 86 | + suite.Run(t, new(McpLoggingSuite)) |
68 | 87 | } |
0 commit comments