python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

feat(fastmcp): add include_in_context parameter to resource decorator

Open rinaldofesta opened this issue 2 months ago • 0 comments

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 sets priority=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 accept include_in_context parameter
  • Added logic to create/update annotations with priority=1.0 when enabled
  • Updated docstring with parameter documentation
  • Added 6 comprehensive tests covering various scenarios

Test Plan

  • [x] include_in_context=True sets priority to 1.0
  • [x] include_in_context=False doesn'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)

rinaldofesta avatar Nov 27 '25 11:11 rinaldofesta