Skip to content

mcp: map custom slog levels to the nearest lower MCP level in LoggingHandler - #1446

Open
kaankucukx wants to merge 2 commits into
modelcontextprotocol:mainfrom
kaankucukx:mcp/logging-custom-slog-levels
Open

kaankucukx wants to merge 2 commits into
modelcontextprotocol:mainfrom
kaankucukx:mcp/logging-custom-slog-levels

Conversation

@kaankucukx

@kaankucukx kaankucukx commented Oct 24, 2026 •

Copy link
Copy Markdown

Fixes #1445

slogLevelToMCP returned "debug" for any slog.Level that is not one of the eight MCP level constants. LoggingHandler.Enabled compares the raw slog level, so a record at slog.LevelInfo+1 passed it when the client level was info, but ServerSession.Log then compared the mapped "debug" against "info" and discarded the record. Custom levels were silently dropped, and when the client level was debug they were delivered mislabeled as debug.

This maps an unknown level to the highest MCP level that does not exceed it (INFO+1 → info, WARN+1 → warning, ERROR+2 → error, above LevelEmergency → emergency), so both filters agree. Levels below LevelDebug still map to debug. The exact-match path is unchanged.

Tests: TestSlogLevelToMCPCustomLevels covers the mapping table, and TestLoggingHandlerCustomLevel runs a record at INFO+1 through an in-memory client/server with the client level set to info and checks it arrives as info. Both fail on main and pass with this change; go test ./mcp is green.

Logging is deprecated by SEP-2577 but remains functional during the deprecation window, so this keeps the existing behavior correct rather than adding to the API.

…Handler

slogLevelToMCP returned "debug" for any slog.Level that is not one of the
eight MCP level constants. LoggingHandler.Enabled compares the raw slog
level, so a record at slog.LevelInfo+1 passed it when the client level was
"info", but ServerSession.Log then compared the mapped "debug" against
"info" and discarded the record. Custom levels were silently dropped, and
when the client level was "debug" they were delivered mislabeled.

Map an unknown level to the highest MCP level that does not exceed it, so
both filters agree. Levels below LevelDebug still map to "debug".

Fixes modelcontextprotocol#1445
@kaankucukx
kaankucukx force-pushed the mcp/logging-custom-slog-levels branch from dd1f6b0 to e34e9c9 Compare October 24, 2026 13:07
@guglielmo-san

Copy link
Copy Markdown
Contributor

@kaankucukx please fix the tests

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mcp: LoggingHandler drops records logged at custom slog levels

2 participants