Skip to content

fix: allow Any type for logging message parameter#2367

Open
mac-agent wants to merge 1 commit intomodelcontextprotocol:mainfrom
mac-agent:fix/logging-data-type
Open

fix: allow Any type for logging message parameter#2367
mac-agent wants to merge 1 commit intomodelcontextprotocol:mainfrom
mac-agent:fix/logging-data-type

Conversation

@mac-agent
Copy link
Copy Markdown

Summary

Change the type annotation of the message parameter in Context.log(), Context.debug(), Context.info(), Context.warning(), and Context.error() from str to Any.

Problem

The MCP spec defines the log data field as:

data: unknown — "Any JSON serializable type is allowed here"

But the Python SDK types the message as str, preventing rich structured logging like:

ctx.info({"event": "progress", "percent": 50})

Solution

  • Change message: str to message: Any in all 5 logging methods
  • Update docstring to document the expanded type
  • Keep parameter name as message for backward compatibility
  • No behavioral changes — existing string calls continue to work identically

Changes

  • src/mcp/server/mcpserver/context.py: Type annotation changes only

Fixes #397

Change the type annotation of the message parameter in Context.log(),
Context.debug(), Context.info(), Context.warning(), and Context.error()
from str to Any.

The MCP spec defines the log data field as:
  data: unknown - "Any JSON serializable type is allowed here"

This allows MCP servers to send rich structured notifications:
  ctx.info({"event": "progress", "percent": 50})

The parameter name is kept as message for backward compatibility.

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

2 participants