python-sdk
python-sdk copied to clipboard
feat(fastmcp): add include_in_context parameter to resource decorator
Summary
Adds a user-friendly include_in_context boolean parameter to the @resource decorator that automatically sets priority=1.0 in annotations. This simplifies marking resources for context inclusion without requiring users to manually create and manage Annotations objects.
Closes #1657
Motivation
Currently, users must create an Annotations object to set resource priority for context inclusion:
@mcp.resource("resource://data", annotations=Annotations(priority=1.0))
def get_data():
return "important data"
This PR introduces a simpler API:
@mcp.resource("resource://data", include_in_context=True)
def get_data():
return "important data"
Implementation
-
Parameter behavior: When
include_in_context=True, automatically setspriority=1.0 - Override logic: Always overrides explicit priority values when enabled
-
Field preservation: Preserves other annotation fields like
audience - Broad support: Works with both regular and template resources, sync and async functions
Changes
- Modified
FastMCP.resource()decorator to acceptinclude_in_contextparameter - Added logic to create/update annotations with
priority=1.0when enabled - Updated docstring with parameter documentation
- Added 6 comprehensive tests covering various scenarios
Test Plan
- [x]
include_in_context=Truesets priority to 1.0 - [x]
include_in_context=Falsedoesn't affect annotations - [x] Overrides explicit priority values
- [x] Preserves audience annotations
- [x] Works with template resources
- [x] Works with async functions
- [x] All existing annotation tests still pass (backward compatibility)