Skip to content

Conversation

@akshaykumarbedre
Copy link

Fixes #397 - FastMCP Context logging methods now accept Any type instead of restricting to str, aligning with MCP specification requirements.

Motivation and Context

The MCP specification requires logging functions to accept any JSON-serializable data (Any type), but FastMCP's Context.log() only accepted strings. This prevented users from logging structured data like dictionaries, lists, numbers, and booleans.

How Has This Been Tested?

  • Added comprehensive test test_context_logging_with_structured_data covering all JSON types (dict, list, number, boolean, string)
  • All existing 84 tests pass
  • Verified backward compatibility with string messages
  • Tested with mock assertions to confirm correct data types passed to underlying session

Breaking Changes

Yes - Removed the extra parameter from all logging methods:

  • Before: ctx.info("message", extra={"key": "value"})
  • After: ctx.info({"message": "message", "key": "value"})

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Changed Context.log() signature from message: str, extra: dict to data: Any. The extra parameter was not part of the MCP spec and created confusion. Updated all convenience methods (debug(), info(), warning(), error()) for consistency.

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