Skip to content

feat(mcp): implement logging capability with error handling and secret redaction#629

Merged
nader-ziada merged 1 commit intocontainers:mainfrom
nader-ziada:mcp-logging
Jan 14, 2026
Merged

feat(mcp): implement logging capability with error handling and secret redaction#629
nader-ziada merged 1 commit intocontainers:mainfrom
nader-ziada:mcp-logging

Conversation

@nader-ziada
Copy link
Collaborator

Add MCP logging support to enable debug information flow to clients while maintaining security.

  • Enable MCP logging capability in server initialization
  • Add generic K8s error handler with user-friendly messages for all error types
  • Implement logging with dedicated "mcp" logger for client-facing logs
  • Add automatic sanitization for sensitive data (tokens, keys, passwords, cloud credentials)
  • Integrate error handling in all core toolsets (pods, resources, nodes, events, namespaces)
  • Add authentication/authorization failure logging with secure messages

@Cali0707 Cali0707 self-requested a review January 8, 2026 17:11

func sanitizeMessage(msg string) string {
// JSON/YAML field patterns (indices 0-6) - preserve field name
for i := 0; i < 7 && i < len(sensitivePatterns); i++ {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess there hard coded checks seem a bit funky ? 😅

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeah, I feel the same, but couldn't think of something better, will take another pass at it

Copy link
Collaborator

Choose a reason for hiding this comment

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

Would this not be one of the sanitizations that the MCP gateway would handle? (Although I get that that depends on a specific deployment topology, so maybe not the best fix for here)

Copy link
Member

Choose a reason for hiding this comment

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

The spec states that the server should take care of this: https://modelcontextprotocol.io/specification/2025-11-25/server/utilities/logging#implementation-considerations

However, I believe this should be part of the SDK itself, in addition to our own implementation, we might want to open an issue for this.

It's my understanding that most servers will deal with the same kind of logic to remove sensitive information.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

awesome, thx!

@matzew
Copy link
Collaborator

matzew commented Jan 12, 2026

I noticed that for instance on helm.go (e.g. in the helmList) there is no usage of that

Would this be a follow up?

@matzew
Copy link
Collaborator

matzew commented Jan 12, 2026

Overall
LGTM

@nader-ziada
Copy link
Collaborator Author

I noticed that for instance on helm.go (e.g. in the helmList) there is no usage of that

Would this be a follow up?

I probably missed that one, will fix

// SendMCPLog sends a log notification to the MCP client and server logs.
// Uses dedicated "mcp" named logger. Message is automatically sanitized.
// Level: "debug", "info", "notice", "warning", "error", "critical", "alert", "emergency"
func SendMCPLog(ctx context.Context, level, message string) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can use a specific type and some constants to make avoid errors

type Level int


const (
  Error Level = iota
  Critical
  Alert
  Emergency
)

For the levels, maybe reproduce whatever the protocol defines: https://modelcontextprotocol.io/specification/2025-11-25/server/utilities/logging#log-levels

// This provides complete separation from server logs
mcpLogger logr.Logger = klog.NewKlogr().WithName("mcp")

// Patterns for redacting sensitive data from industry-standard secret detection tools
Copy link
Member

Choose a reason for hiding this comment

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

This comment states that these patterns are from industry-standard secret detection tools. It'd be better if we could include references for maintenance and future updates

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

its not coming from one place, I kind of looked in multiple places plus did some research, so don't really have one reference to link here. will remove the comment so its not misleading, but will mention the sdk issue as a future replacement

}
eventMap, err := kubernetes.NewCore(params).EventsList(params, namespace.(string))
if err != nil {
mcplog.HandleK8sError(params.Context, err, "events listing")
Copy link
Member

Choose a reason for hiding this comment

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

This pattern seems brittle.
Implementors need to take care of handling the error which might be eventually forgotten.
As a follow up we should try to find a (decoupled) way of integrating the logging directly in the Kubernetes package or some other alternative that would not require the extra-handling for each tool function

…t redaction

Add comprehensive MCP logging support to enable debug information flow to clients while maintaining security.

Signed-off-by: Nader Ziada <nziada@redhat.com>
Copy link
Member

@manusa manusa left a comment

Choose a reason for hiding this comment

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

LGTM, thx!

@manusa manusa added this to the 0.1.0 milestone Jan 13, 2026
@matzew
Copy link
Collaborator

matzew commented Jan 14, 2026

LGTM!

@nader-ziada nader-ziada merged commit d23fb73 into containers:main Jan 14, 2026
6 checks passed
@nader-ziada nader-ziada deleted the mcp-logging branch January 15, 2026 16:10
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.

4 participants