Skip to content

mcp: gate session lifecycle log demotion behind MCPGODEBUG - #1208

Open
anneheartrecord wants to merge 2 commits into
modelcontextprotocol:mainfrom
anneheartrecord:fix/demote-session-lifecycle-logs
Open

mcp: gate session lifecycle log demotion behind MCPGODEBUG#1208
anneheartrecord wants to merge 2 commits into
modelcontextprotocol:mainfrom
anneheartrecord:fix/demote-session-lifecycle-logs

Conversation

@anneheartrecord

@anneheartrecord anneheartrecord commented Aug 28, 2026

Copy link
Copy Markdown

Session bookkeeping records (server connecting, server session connected/disconnected, session initialized, client log level set) log at info. On stateless streamable HTTP a session is minted per POST, so every request replays the whole lifecycle — four info lines of bookkeeping per tool call before the server logs anything of its own. The client log level set record additionally fires on every request carrying _meta under the 2026-07-28 spec, on all transports, even when the client set no level.

This gates the demotion of the five lifecycle records to debug behind MCPGODEBUG=demotesessionlifecyclelog=1, so the default behavior is unchanged for existing users (per review). Warn/error records are unchanged. If the demotion should become the default in a future minor, flipping the gate is a one-line change. Adds a test covering both paths: lifecycle visible at info by default; silent at info and visible at debug with the variable set.

Fixes #1204

@guglielmo-san

Copy link
Copy Markdown
Contributor

I agree overall with the change, but this introduces a behavioral change for existing users. We should gate the change behind an MCPGODEBUG variable

Session bookkeeping records (server connecting, server session
connected/disconnected, session initialized, client log level set)
logged at info. On stateless streamable HTTP a session is minted per
POST, so every request replays the whole lifecycle: four info lines of
bookkeeping per tool call before the server logs anything of its own.
The setLevel record additionally fires on every request carrying _meta
under the 2026-07-28 spec, on all transports.

Demote these five records to debug only when
MCPGODEBUG=demotesessionlifecyclelog=1; the default stays at info so
users who rely on the records see no change. If the demotion should
become the default in a future minor, flipping the gate is a one-line
change.

Fixes modelcontextprotocol#1204
@anneheartrecord
anneheartrecord force-pushed the fix/demote-session-lifecycle-logs branch from 9ed4506 to 9eed2ee Compare August 29, 2026 03:03
@anneheartrecord anneheartrecord changed the title mcp: log session lifecycle events at debug instead of info mcp: gate session lifecycle log demotion behind MCPGODEBUG Aug 29, 2026
@anneheartrecord

Copy link
Copy Markdown
Author

@guglielmo-san done — force-pushed as a single commit (9eed2ee). The five lifecycle records now stay at info by default; setting MCPGODEBUG=demotesessionlifecyclelog=1 moves them to debug, so nothing changes for existing users unless they opt in. The knob follows the existing MCPGODEBUG compatibility-parameter pattern (cf. allowsessionsinstateless); if you'd rather have the demotion become the default in a future minor, flipping the gate is a one-line change.

Tests cover both paths (visible at info by default; silent at info and visible at debug with the variable set), -race clean. The only failures in go test ./mcp/ on this machine are the two *LocalhostProtection tests, which fail identically on clean main here (local environment).

Comment thread mcp/server.go
Comment on lines +36 to +43
// demotesessionlifecyclelog, when set to "1" via MCPGODEBUG, demotes routine
// session bookkeeping records ("server connecting", "server session
// connected", "server session disconnected", "session initialized", "client
// log level set") from info to debug. On stateless streamable HTTP a session
// is minted per request, so these fire constantly and drown out the server's
// own logs (#1204). It is gated behind MCPGODEBUG because demoting them
// changes log output that existing users may rely on.
var demotesessionlifecyclelog = mcpgodebug.Value("demotesessionlifecyclelog")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a description should be added in mcpgodebug.rc.md files

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.

Session-lifecycle bookkeeping logs at info, spamming multiple log lines per request on stateless streamable HTTP

2 participants