Skip to content

Commit 49dcff3

Browse files
authored
feat(mcp): log tool call (HTTP headers) (#221)
Signed-off-by: Marc Nuri <marc@marcnuri.com>
1 parent 94f7055 commit 49dcff3

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

pkg/mcp/common_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"encoding/json"
77
"flag"
88
"fmt"
9-
"k8s.io/klog/v2"
10-
"k8s.io/klog/v2/textlogger"
119
"net/http/httptest"
1210
"os"
1311
"path/filepath"
@@ -36,6 +34,8 @@ import (
3634
"k8s.io/client-go/tools/clientcmd"
3735
"k8s.io/client-go/tools/clientcmd/api"
3836
toolswatch "k8s.io/client-go/tools/watch"
37+
"k8s.io/klog/v2"
38+
"k8s.io/klog/v2/textlogger"
3939
"k8s.io/utils/ptr"
4040
"sigs.k8s.io/controller-runtime/pkg/envtest"
4141
"sigs.k8s.io/controller-runtime/tools/setup-envtest/env"

pkg/mcp/mcp.go

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

33
import (
4+
"bytes"
45
"context"
56
"fmt"
67
"k8s.io/klog/v2"
@@ -171,6 +172,12 @@ func contextFunc(ctx context.Context, r *http.Request) context.Context {
171172
func toolCallLoggingMiddleware(next server.ToolHandlerFunc) server.ToolHandlerFunc {
172173
return func(ctx context.Context, ctr mcp.CallToolRequest) (*mcp.CallToolResult, error) {
173174
klog.V(5).Infof("mcp tool call: %s(%v)", ctr.Params.Name, ctr.Params.Arguments)
175+
if ctr.Header != nil {
176+
buffer := bytes.NewBuffer(make([]byte, 0))
177+
if err := ctr.Header.Write(buffer); err == nil {
178+
klog.V(7).Infof("mcp tool call headers: %s", buffer)
179+
}
180+
}
174181
return next(ctx, ctr)
175182
}
176183
}

pkg/mcp/mcp_tools_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,16 @@ func TestToolCallLogging(t *testing.T) {
140140
}
141141
})
142142
})
143+
testCaseWithContext(t, &mcpContext{logLevel: 7}, func(c *mcpContext) {
144+
_, _ = c.callTool("configuration_view", map[string]interface{}{
145+
"minified": false,
146+
})
147+
t.Run("Logs tool call headers", func(t *testing.T) {
148+
expectedLog := "mcp tool call headers: Accept-Encoding: gzip"
149+
if !strings.Contains(c.logBuffer.String(), expectedLog) {
150+
t.Errorf("Expected log to contain '%s', got: %s", expectedLog, c.logBuffer.String())
151+
}
152+
})
153+
154+
})
143155
}

0 commit comments

Comments
 (0)