Skip to content

Conversation

@IAmSurajBobade
Copy link
Contributor

@IAmSurajBobade IAmSurajBobade commented Sep 19, 2025

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 #170

mcp/server.go Outdated
opts ServerOptions
impl *Implementation
opts ServerOptions
logger *slog.Logger
Copy link
Contributor

Choose a reason for hiding this comment

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

Use opts.Logger?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated code to use opts.Logger

mcp/server.go Outdated
type ServerOptions struct {
// Optional instructions for connected clients.
Instructions string
// Logger is used for server-side logging. If nil, disable logging.
Copy link
Contributor

Choose a reason for hiding this comment

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

// If non-nil, log server activity.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated comment

mcp/server.go Outdated
})

if !wasInit {
ss.server.logger.Warn("initialized before initialize")
Copy link
Contributor

Choose a reason for hiding this comment

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

If we are returning an error, shouldn't we log these as errors?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed from Warn to Error

type StreamableHTTPHandler struct {
getServer func(*http.Request) *Server
opts StreamableHTTPOptions
logger *slog.Logger
Copy link
Contributor

Choose a reason for hiding this comment

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

use opts.Logger

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated code to use opts.Logger

@IAmSurajBobade
Copy link
Contributor Author

@jba addressed all of your review comments, can you please have a look.

@dmarkhas
Copy link

Could you use a logging interface instead of *slog.Logger ? We want to be able to provide our own logging implementation, and we don't use slog in our codebase.

@jba
Copy link
Contributor

jba commented Sep 25, 2025

@dmarkhas You can write a slog.Handler to wrap your logger.
log/slog is the lingua franca for logging in the Go ecosystem. Adding another interface just for this package would further fragment the logging ecosystem.

@jba jba self-requested a review September 30, 2025 11:35
@jba
Copy link
Contributor

jba commented Sep 30, 2025

This is ready to merge, but I recommend we do it after v1.0.0 to avoid last-minute changes. @findleyr

@jba
Copy link
Contributor

jba commented Oct 1, 2025

@findleyr @samthanawalla just sign off on the public API.

@jba jba merged commit 1dcbf62 into modelcontextprotocol:main Oct 2, 2025
5 checks passed
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.

server-side logging

3 participants