mcp/server: enable server logging (#501)
mcp/server.go: implement server‑side logging - Thread a *slog.Logger through server paths to improve debuggability - Introduce no‑op discard handler + ensureLogger so loggers are never nil - Use ensured loggers in Server - address todo comments related to adding logging support All tests are passing and no functionality should change with this PR Fixes https://github.com/modelcontextprotocol/go-sdk/issues/170
This commit is contained in:
parent
49047aebe8
commit
1dcbf62661
1 changed files with 72 additions and 3 deletions
|
|
@ -88,6 +88,23 @@ type LoggingHandler struct {
|
|||
handler slog.Handler
|
||||
}
|
||||
|
||||
// discardHandler is a slog.Handler that drops all logs.
|
||||
// TODO: use slog.DiscardHandler when we require Go 1.24+.
|
||||
type discardHandler struct{}
|
||||
|
||||
func (discardHandler) Enabled(context.Context, slog.Level) bool { return false }
|
||||
func (discardHandler) Handle(context.Context, slog.Record) error { return nil }
|
||||
func (discardHandler) WithAttrs([]slog.Attr) slog.Handler { return discardHandler{} }
|
||||
func (discardHandler) WithGroup(string) slog.Handler { return discardHandler{} }
|
||||
|
||||
// ensureLogger returns l if non-nil, otherwise a discard logger.
|
||||
func ensureLogger(l *slog.Logger) *slog.Logger {
|
||||
if l != nil {
|
||||
return l
|
||||
}
|
||||
return slog.New(discardHandler{})
|
||||
}
|
||||
|
||||
// NewLoggingHandler creates a [LoggingHandler] that logs to the given [ServerSession] using a
|
||||
// [slog.JSONHandler].
|
||||
func NewLoggingHandler(ss *ServerSession, opts *LoggingHandlerOptions) *LoggingHandler {
|
||||
|
|
|
|||
Loading…
Reference in a new issue