Skip to content

Conversation

@rinaldofesta
Copy link

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

  • include_in_context=True sets priority to 1.0
  • include_in_context=False doesn't affect annotations
  • Overrides explicit priority values
  • Preserves audience annotations
  • Works with template resources
  • Works with async functions
  • All existing annotation tests still pass (backward compatibility)

Add a user-friendly include_in_context boolean parameter to the @resource
decorator that automatically sets priority=1.0 in annotations, making it
easier for users to mark resources for context inclusion without manually
managing annotation objects.

Changes:
- Add include_in_context parameter to resource decorator
- Automatically create Annotations(priority=1.0) when enabled
- Override explicit priority when include_in_context=True
- Preserve other annotation fields (audience)
- Support both regular and template resources
- Update docstring with new parameter documentation
- Add comprehensive test coverage (6 new tests)

The parameter provides a simple API for context inclusion:
  @mcp.resource("resource://data", include_in_context=True)
  def get_data(): return "important data"
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.

[FastMCP] Add 'include_in_context' argument to @resource decorator

1 participant