Skip to content

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

@rinaldofesta

Description

@rinaldofesta

Is your feature request related to a problem? Please describe.

Currently, in FastMCP, if a developer wants to hint that a resource is critical and should be included in the model's context window by default (e.g., operational guidelines or system prompts), they need to manually understand and set the annotations field with a priority score.

While the protocol spec defines priority (0.0 - 1.0), it is not immediately obvious to a Python developer how to assert "Always include this." The current method is slightly verbose and less discoverable:

# Current way
@mcp.resource("uri", annotations=Annotations(priority=1.0)) 
def my_resource(): 
    return "..."

Describe the solution you'd like

I propose adding a boolean argument include_in_context (defaulting to False) to the FastMCP.resource decorator.

This would provide a high-level, Pythonic abstraction over the priority annotation. Setting this to True would automatically set the underlying resource priority to 1.0 (indicating maximum priority for context inclusion).

Proposed Usage

from mcp.server.fastmcp import FastMCP

mcp = FastMCP("Demo")

@mcp.resource(
    "guidelines://ops", 
    include_in_context=True  # Developer intent is clear here
)
def operational_guidelines():
    return "..."

Implementation Details

When include_in_context=True:

  • Automatically creates Annotations(priority=1.0) if no annotations exist
  • Overrides the priority field to 1.0 if annotations are explicitly provided
  • Preserves other annotation fields (like audience)

Why this is valuable

  1. Developer Experience (DX): It reduces cognitive load. Developers don't need to look up the specific float values for priority; they can just use a boolean flag.

  2. Readability: include_in_context=True explicitly states the intent of the resource code-side.

  3. Discoverability: It makes the concept of "default context" more accessible to new MCP users.

  4. Consistency: Similar to how other frameworks provide high-level abstractions for common patterns.

Additional context

I have reviewed src/mcp/server/fastmcp/server.py and believe this can be implemented by modifying the resource decorator to check this boolean and update the annotations before registration.

I am happy to submit a PR for this feature. As this would be my first contribution to the project, I wanted to validate the naming convention (include_in_context vs auto_load vs priority_high) and approach before pushing code.

This complements the work being done in #1475 for dynamic annotations and #1468 for annotation support.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions