Skip to content

fix: align Context logging methods with MCP spec data type#2366

Merged
maxisbey merged 5 commits intomodelcontextprotocol:mainfrom
paikend:fix/logging-type-spec-compliance
Apr 14, 2026
Merged

fix: align Context logging methods with MCP spec data type#2366
maxisbey merged 5 commits intomodelcontextprotocol:mainfrom
paikend:fix/logging-type-spec-compliance

Conversation

@paikend
Copy link
Copy Markdown
Contributor

@paikend paikend commented Mar 28, 2026

Summary

  • Change message: strdata: Any in Context.log() and convenience methods (debug, info, warning, error)
  • Remove the extra parameter — callers can now pass structured data directly as data
  • Aligns with ServerSession.send_log_message(data: Any) which already accepts Any

Motivation

The MCP specification defines log data as unknown (any JSON-serializable type), but Context.info() and friends only accepted str.

Before:

# Type error — spec allows this but SDK didn't
await ctx.info({"key": "value", "count": 42})

# Workaround via extra parameter
await ctx.log(level="info", message="msg", extra={"key": "value"})

After:

# Now works — any JSON-serializable type
await ctx.info({"key": "value", "count": 42})
await ctx.info("plain string still works")
await ctx.info([1, 2, 3])

Breaking Change

  • message parameter renamed to data in log(), debug(), info(), warning(), error()
  • extra parameter removed from log() and convenience methods

Callers using positional args (ctx.info("msg")) are unaffected. Callers using message= keyword or extra= need to update.

Test plan

  • All 1143 existing tests pass (0 failures)
  • Updated test_logging_callback.py to use new API
  • test_context_logging in test_server.py passes (string data still works)

Fixes #397

🤖 Generated with Claude Code

paikend and others added 5 commits April 14, 2026 21:33
Change `message: str` to `data: Any` in `Context.log()` and convenience
methods (debug, info, warning, error) to match the MCP specification
which allows any JSON-serializable type for log data.

Remove the `extra` parameter since callers can now pass structured data
directly (e.g., `ctx.info({"key": "value"})`).

Fixes modelcontextprotocol#397

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The MCP spec defines 8 RFC-5424 syslog severity levels (debug, info,
notice, warning, error, critical, alert, emergency). Context.log
previously only accepted 4 of them via a local Literal, even though
ServerSession.send_log_message and LoggingMessageNotificationParams
already accept the full LoggingLevel type.

The debug/info/warning/error convenience methods are unchanged; they
cover the common cases. Callers needing the other levels go through
ctx.log() directly.

Github-Issue: modelcontextprotocol#397
@maxisbey maxisbey force-pushed the fix/logging-type-spec-compliance branch from f314001 to 9adddc0 Compare April 14, 2026 21:37
Copy link
Copy Markdown
Contributor

@maxisbey maxisbey left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! I added changed the log level to use the actual literal type with all the levels and added migration docs. Other than that looked good to me. Approved :)

@maxisbey maxisbey enabled auto-merge (squash) April 14, 2026 21:39
@maxisbey maxisbey merged commit 3d7b311 into modelcontextprotocol:main Apr 14, 2026
26 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.

Context logging function types are not spec compliant

2 participants