Skip to content

Conversation

@manusa
Copy link
Member

@manusa manusa commented Sep 17, 2025

Prevents protocol issues with some clients.

Kubernetes tooling uses klog to log messages.
Some of these messages end up logged in the stderr or stdout which breaks some of the clients that expect jsonrpc messages.

/cc @ardaguclu

Prevents protocol issues with some clients.

Kubernetes tooling uses klog to log messages.
Some of these messages end up logged in the stderr or stdout which breaks some of the clients that expect jsonrpc messages.

Signed-off-by: Marc Nuri <[email protected]>
@manusa manusa added this to the 0.1.0 milestone Sep 17, 2025
func (m *MCPServerOptions) initializeLogging() {
flagSet := flag.NewFlagSet("klog", flag.ContinueOnError)
klog.InitFlags(flagSet)
if m.StaticConfig.Port == "" {
Copy link
Member

Choose a reason for hiding this comment

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

Why don't we simply by pass klog.InitFlags if m.StaticConfig.Port is empty?

Copy link
Member Author

Choose a reason for hiding this comment

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

because we need to set the other flags (logtostderr, alsologtostderr, stderrthreshold) which have defaults that enable logging of some messages

Copy link
Member Author

Choose a reason for hiding this comment

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

the fix is not for our logs (which there aren't many at the moment) but rather for the upstream kubernetes logs which aren't controlled easily.

Copy link
Member

Choose a reason for hiding this comment

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

Does that mean our logs do not break the jsonrpc?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, they would break the jsonrpc too.
This statement also disables them (as asserted in the provided tests).

Copy link
Member

Choose a reason for hiding this comment

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

Just for clarification; by running klog.InitFlags in SDTIO, which logs that do not break jsonrpc will be logged?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, let me try to summarize.

If you run kubernetes-mcp-server in STDIO mode (i.e. no --port flag) with an unauthorized cluster you can immediately trigger an error that will be logged to STDERR:

E0917 16:47:01.678632   85672 memcache.go:265] "Unhandled Error" err="couldn't get current server API group list: ...

I understand that there might be other scenarios where this might happen.

I've started to see that some MCP Hosts(->Clients) start failing because they've become ore strict and will mark the MCP server as not working.

The goal here is to disable all logging either to stderr or stdout in order not to break the protocol for these more strict clients.

By running klog.InitFlags and then providing the three log configuration flags we prevent klog from logging to stderr which is what happens by default.

Not sure if it's clearer now.

Copy link
Member

Choose a reason for hiding this comment

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

Thank you for explanation. It makes sense.

LGTM

@manusa manusa merged commit 94baad6 into containers:main Sep 17, 2025
6 checks passed
@manusa manusa deleted the fix/stdio-logging branch September 17, 2025 15:03
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.

2 participants