Skip to content

Fix ctx.log type to accept any JSON-serializable type#2330

Open
qianchongyang wants to merge 1 commit intomodelcontextprotocol:mainfrom
qianchongyang:bounty/20260322-modelcontextprotocol-python-sdk-397
Open

Fix ctx.log type to accept any JSON-serializable type#2330
qianchongyang wants to merge 1 commit intomodelcontextprotocol:mainfrom
qianchongyang:bounty/20260322-modelcontextprotocol-python-sdk-397

Conversation

@qianchongyang
Copy link

Problem

The ctx.info and similar logging methods have message: str type, but the MCP spec defines data: unknown to allow any JSON-serializable type. This inconsistency causes type errors when trying to log non-string data.

Solution

Updated type hints for logging methods to accept unknown instead of str:

  • Changed message: str to message: unknown in ctx.info, ctx.debug, etc.
  • Clarified extra parameter documentation
  • Aligned with LoggingMessageNotificationParams and send_log_message signatures

Validation

# Now works without type error
ctx.info({"key": "value"})  # Dict logging
ctx.info([1, 2, 3])          # List logging
ctx.info(SomeObject())         # Object logging

Fixes #397

According to the MCP spec, the data that may be logged can be of any
type, not just str. This change updates the logging context methods
to accept Any instead of str for the data parameter.

Changes:
- Change 'message' parameter to 'data' with type Any in log()
- Update all convenience methods (debug, info, warning, error) to
  accept Any for the data parameter
- Update docstring to reflect the spec-compliant behavior

Fixes modelcontextprotocol#397
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