MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / NewLoggingHandler

Function NewLoggingHandler

mcp/logging.go:102–123  ·  view source on GitHub ↗

NewLoggingHandler creates a [LoggingHandler] that logs to the given [ServerSession] using a [slog.JSONHandler].

(ss *ServerSession, opts *LoggingHandlerOptions)

Source from the content-addressed store, hash-verified

100// NewLoggingHandler creates a [LoggingHandler] that logs to the given [ServerSession] using a
101// [slog.JSONHandler].
102func NewLoggingHandler(ss *ServerSession, opts *LoggingHandlerOptions) *LoggingHandler {
103 var buf bytes.Buffer
104 jsonHandler := slog.NewJSONHandler(&buf, &slog.HandlerOptions{
105 ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr {
106 // Remove level: it appears in LoggingMessageParams.
107 if a.Key == slog.LevelKey {
108 return slog.Attr{}
109 }
110 return a
111 },
112 })
113 lh := &LoggingHandler{
114 ss: ss,
115 mu: new(sync.Mutex),
116 buf: &buf,
117 handler: jsonHandler,
118 }
119 if opts != nil {
120 lh.opts = *opts
121 }
122 return lh
123}
124
125// Enabled implements [slog.Handler.Enabled] by comparing level to the [ServerSession]'s level.
126func (h *LoggingHandler) Enabled(ctx context.Context, level slog.Level) bool {

Callers 2

Example_loggingFunction · 0.92
TestEndToEndFunction · 0.85

Calls

no outgoing calls

Tested by 2

Example_loggingFunction · 0.74
TestEndToEndFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…