-
Notifications
You must be signed in to change notification settings - Fork 536
mcp: gate session lifecycle log demotion behind MCPGODEBUG #1208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
anneheartrecord
wants to merge
3
commits into
modelcontextprotocol:main
Choose a base branch
from
anneheartrecord:fix/demote-session-lifecycle-logs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+133
−5
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // Copyright 2026 The Go MCP SDK Authors. All rights reserved. | ||
| // Use of this source code is governed by an MIT-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| package mcp | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "log/slog" | ||
| "strings" | ||
| "sync" | ||
| "testing" | ||
| ) | ||
|
|
||
| // syncBuffer is a bytes.Buffer safe for concurrent use, since session | ||
| // lifecycle events are logged from the connection's read goroutine. | ||
| type syncBuffer struct { | ||
| mu sync.Mutex | ||
| buf bytes.Buffer | ||
| } | ||
|
|
||
| func (b *syncBuffer) Write(p []byte) (int, error) { | ||
| b.mu.Lock() | ||
| defer b.mu.Unlock() | ||
| return b.buf.Write(p) | ||
| } | ||
|
|
||
| func (b *syncBuffer) String() string { | ||
| b.mu.Lock() | ||
| defer b.mu.Unlock() | ||
| return b.buf.String() | ||
| } | ||
|
|
||
| // TestSessionLifecycleLogLevel verifies that routine session bookkeeping | ||
| // (connect, disconnect, initialize, setLevel) logs at info by default, and | ||
| // moves to debug when MCPGODEBUG=demotesessionlifecyclelog=1. On stateless | ||
| // streamable HTTP these events fire on every request, so at info they drown | ||
| // out the server's own logs (#1204); the demotion is gated behind | ||
| // MCPGODEBUG because it changes log output existing users may rely on. | ||
| func TestSessionLifecycleLogLevel(t *testing.T) { | ||
| // "session initialized" is also gated but doesn't fire on this | ||
| // connection path, so it is not asserted here. | ||
| lifecycleMessages := []string{ | ||
| "server connecting", | ||
| "server session connected", | ||
| "client log level set", | ||
| "server session disconnected", | ||
| } | ||
|
|
||
| run := func(t *testing.T, level slog.Level) string { | ||
| var buf syncBuffer | ||
| logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: level})) | ||
| cs, _, cleanup := basicConnection(t, func(s *Server) { | ||
| s.opts.Logger = logger | ||
| }) | ||
| if err := cs.SetLoggingLevel(context.Background(), &SetLoggingLevelParams{Level: "warning"}); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| cleanup() | ||
| return buf.String() | ||
| } | ||
|
|
||
| t.Run("info by default", func(t *testing.T) { | ||
| got := run(t, slog.LevelInfo) | ||
| for _, msg := range lifecycleMessages { | ||
| if !strings.Contains(got, msg) { | ||
| t.Errorf("log output missing %q at info level by default:\n%s", msg, got) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| t.Run("demoted to debug with MCPGODEBUG=demotesessionlifecyclelog=1", func(t *testing.T) { | ||
| old := demotesessionlifecyclelog | ||
| demotesessionlifecyclelog = "1" | ||
| t.Cleanup(func() { demotesessionlifecyclelog = old }) | ||
|
|
||
| if got := run(t, slog.LevelInfo); got != "" { | ||
| t.Errorf("session lifecycle produced log output at info level with demotesessionlifecyclelog=1:\n%s", got) | ||
| } | ||
| got := run(t, slog.LevelDebug) | ||
| for _, msg := range lifecycleMessages { | ||
| if !strings.Contains(got, msg) { | ||
| t.Errorf("log output missing %q at debug level with demotesessionlifecyclelog=1:\n%s", msg, got) | ||
| } | ||
| } | ||
| }) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in
a9b6d0d— an entry under 1.8.0 next toblockingcancelnotify, in bothinternal/docs/mcpgodebug.src.mdand the generateddocs/mcpgodebug.md. Re-rango generate ./internal/docs/so the two agree, andgo test ./internal/docs/passes.One thing worth flagging while you are looking at it: every other option on that page is opt-out (set to
1to get the old behavior back), whereas this one is opt-in — the default keeps logging at info, which is what avoids the behavior change you raised. I said that explicitly in the entry, but it does sit a bit awkwardly next to the section's "will be removed in the 1.9.0 version" line, since dropping the knob would make the demotion unreachable rather than permanent.If you would rather it follow the page convention, I can flip it to
keepsessionlifecycleloginfo: demote by default,1restores info, and removal in 1.9.0 then means the demotion becomes permanent. Your call — happy either way.