Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions pkg/server/api/mcp/mcputils/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package mcputils

import (
"context"
"log/slog"
"slices"

"go.gearno.de/kit/log"
)

type (
slogHandler struct {
logger *log.Logger
attrs []slog.Attr
groups []slogGroup
}

slogGroup struct {
name string
attrs []slog.Attr
}
)

// NewSlogLogger adapts the application logger for dependencies that use slog.
func NewSlogLogger(logger *log.Logger) *slog.Logger {
return slog.New(&slogHandler{logger: logger})
}

func (h *slogHandler) Enabled(context.Context, slog.Level) bool {
return true
}

func (h *slogHandler) Handle(ctx context.Context, record slog.Record) error {
attrs := slices.Clone(h.attrs)
groupedAttrs := make([]slog.Attr, 0, record.NumAttrs())
record.Attrs(func(attr slog.Attr) bool {
groupedAttrs = append(groupedAttrs, attr)
return true
})

for i := len(h.groups) - 1; i >= 0; i-- {
groupAttrs := append(slices.Clone(h.groups[i].attrs), groupedAttrs...)
groupedAttrs = []slog.Attr{
slog.Group(h.groups[i].name, attrsToAny(groupAttrs)...),
}
}

h.logger.Log(ctx, record.Level, record.Message, append(attrs, groupedAttrs...)...)

return nil
}

func (h *slogHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
child := h.clone()
if len(child.groups) == 0 {
child.attrs = append(child.attrs, attrs...)
} else {
i := len(child.groups) - 1
child.groups[i].attrs = append(child.groups[i].attrs, attrs...)
}

return child
}

func (h *slogHandler) WithGroup(name string) slog.Handler {
if name == "" {
return h
}

child := h.clone()
child.groups = append(child.groups, slogGroup{name: name})

return child
}

func (h *slogHandler) clone() *slogHandler {
child := &slogHandler{
logger: h.logger,
attrs: slices.Clone(h.attrs),
groups: slices.Clone(h.groups),
}
for i := range child.groups {
child.groups[i].attrs = slices.Clone(child.groups[i].attrs)
}

return child
}

func attrsToAny(attrs []slog.Attr) []any {
values := make([]any, len(attrs))
for i, attr := range attrs {
values[i] = attr
}

return values
}
70 changes: 70 additions & 0 deletions pkg/server/api/mcp/mcputils/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package mcputils_test

import (
"bytes"
"context"
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.gearno.de/kit/log"
"go.probo.inc/probo/pkg/server/api/mcp/mcputils"
)

func TestNewSlogLogger_PreservesRecord(t *testing.T) {
t.Parallel()

var output bytes.Buffer
logger := log.NewLogger(

Check failure on line 39 in pkg/server/api/mcp/mcputils/logger_test.go

View workflow job for this annotation

GitHub Actions / lint-go

missing whitespace above this line (invalid statement above assign) (wsl_v5)
log.WithLevel(log.LevelDebug),
log.WithName("mcp.transport"),
log.WithOutput(&output),
)
slogLogger := mcputils.NewSlogLogger(logger).
With("component", "streamable").
WithGroup("request").
With("method", "POST").
WithGroup("details")

slogLogger.ErrorContext(context.Background(), "transport failed", "retryable", true)

var entry map[string]any
err := json.Unmarshal(output.Bytes(), &entry)

Check failure on line 53 in pkg/server/api/mcp/mcputils/logger_test.go

View workflow job for this annotation

GitHub Actions / lint-go

missing whitespace above this line (invalid statement above assign) (wsl_v5)
require.NoError(t, err)

assert.Equal(t, "ERROR", entry["level"])
assert.Equal(t, "transport failed", entry["msg"])
assert.Equal(t, "mcp.transport", entry["name"])
assert.Equal(t, "streamable", entry["component"])
assert.Equal(
t,
map[string]any{
"method": "POST",
"details": map[string]any{
"retryable": true,
},
},
entry["request"],
)
}
2 changes: 1 addition & 1 deletion pkg/server/api/mcp/v1/v1_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func NewMux(
Stateless: true,
// SessionTimeout: 30 * time.Minute,
EventStore: eventStore,
Logger: nil, // TODO put logger here
Logger: mcputils.NewSlogLogger(logger.Named("transport")),
},
)
protectedHandler := http.NewCrossOriginProtection().Handler(handler)
Expand Down
Loading