Skip to content

Conversation

Copy link

Copilot AI commented Oct 23, 2025

Overview

Integrates PR modelcontextprotocol#1406 from the upstream modelcontextprotocol/python-sdk repository, which adds support for context-only resources in the FastMCP server.

Problem

Previously, it was not possible to define a Resource that takes only the Context parameter as an argument. When attempting to create such a resource, the registration logic incorrectly classified it as a template resource instead of a regular resource.

This limitation prevented developers from creating resources that need access to server session context but don't require dynamic URI components, reducing flexibility in resource design.

Solution

The changes enable proper registration of context-only resources by:

  1. Updating the resource registration logic to distinguish between context parameters and regular function parameters
  2. Adding context parameter support to all resource read() methods
  3. Improving Context type detection to handle generic type aliases like Context[ServerSession, None]

Example Usage

from mcp.server.fastmcp import Context, FastMCP
from mcp.server.session import ServerSession

mcp = FastMCP(name="Context Resource Example")

@mcp.resource("resource://only_context")
def resource_only_context(ctx: Context[ServerSession, None]) -> str:
    """Resource that only receives context."""
    assert ctx is not None
    return "Resource with only context injected"

This resource is now correctly registered as a regular resource, not a template resource, allowing clients to read it directly without providing any URI parameters.

Changes Made

  • Added context parameter to the abstract Resource.read() method signature
  • Updated FunctionResource to track and inject context parameters via context_kwarg field
  • Modified resource registration decorator to exclude context parameters when determining if a resource should be a template
  • Improved find_context_parameter() utility to detect generic Context type aliases
  • Added example demonstrating context-only resource usage
  • Added comprehensive unit and integration tests

Testing

  • All existing tests pass (80 resource-related tests)
  • New unit test in test_server.py::TestContextInjection::test_resource_only_context
  • New integration tests for SSE and StreamableHTTP transports
  • Verified backward compatibility with existing resource implementations

Breaking Changes

None. The context parameter in Resource.read() method is optional with default None. All existing resource implementations continue to work unchanged.


Closes modelcontextprotocol#1405 (upstream issue)
Co-authored-by: 0Delta 12265602+0Delta@users.noreply.github.com

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • astral.sh
    • Triggering command: curl -LsSf REDACTED (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

pull this pr : modelcontextprotocol#1406 into my fork


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.



Co-authored-by: hackthemarket <22823070+hackthemarket@users.noreply.github.com>
Copilot AI changed the title [WIP] Pull changes from PR #1406 into fork Add support for context-only resources (#1406) Oct 23, 2025
Copilot finished work on behalf of hackthemarket October 23, 2025 16:46
Copilot AI requested a review from hackthemarket October 23, 2025 16:46
Copy link
Owner

@hackthemarket hackthemarket left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@hackthemarket hackthemarket marked this pull request as ready for review October 23, 2025 17:11
@hackthemarket hackthemarket merged commit 4bfd423 into main Oct 23, 2025
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.

It is not possible to define a Resource that takes only the Context parameter as an argument.

2 participants