Skip to content

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

Open
paikend wants to merge 2 commits intomodelcontextprotocol:mainfrom
paikend:fix/logging-type-spec-compliance
Open

fix: align Context logging methods with MCP spec data type#2366
paikend wants to merge 2 commits intomodelcontextprotocol:mainfrom
paikend:fix/logging-type-spec-compliance

Conversation

@paikend
Copy link
Copy Markdown

@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 2 commits March 28, 2026 17:30
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>
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

1 participant