Skip to content

fix: update context logging type hints to allow any JSON-serializable data#2445

Closed
cubaseuser123 wants to merge 2 commits intomodelcontextprotocol:mainfrom
cubaseuser123:fix/397-context-logging-any-type
Closed

fix: update context logging type hints to allow any JSON-serializable data#2445
cubaseuser123 wants to merge 2 commits intomodelcontextprotocol:mainfrom
cubaseuser123:fix/397-context-logging-any-type

Conversation

@cubaseuser123
Copy link
Copy Markdown

The issue here comes down to a frustrating type mismatch: the MCP spec clearly states that log message data can be any JSON-serializable type, but the convenience wrappers for ctx.info(), ctx.warning(), etc., were strictly enforcing message: str. So whenever a dev tried to log a simple Python dictionary or list, the type checker would throw a fit.

I traced it down and the good news is the underlying send_log_message method already handles Any properly without trying to blindly stringify things. The bug was entirely superficial.

So I went in and updated the surface-level type hints to bring the API back into alignment with the spec.

Specifically, this updates message: str to message: Any across:

  • ctx.log()
  • ctx.info()
  • ctx.debug()
  • ctx.warning()
  • ctx.error()

Validation:
I ran the full pytest suite against mcpserver/test_server.py and client/test_logging_callback.py — all 90 tests ran cleanly with no regressions to the existing logging behavior.

(Side note: I didn't have to touch any serialization logic, since the transport layer already handles the Any payload exactly as it should).

Let me know if there's anything else you'd like me to tweak before we merge this in! Fixes #397.

@maxisbey
Copy link
Copy Markdown
Contributor

Thanks for the PR, but closing in favour of #2366

@maxisbey maxisbey closed this Apr 14, 2026
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