Skip to content

fix: enables context injection into resources #413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

fubuloubu
Copy link

@fubuloubu fubuloubu commented Apr 2, 2025

rebase of #248 (copied below)


closes #244

Motivation and Context

According to the readme it should be possible to pass a Context to a resource (like it is for tools), but that's currently throwing an error (#244)

How Has This Been Tested?

How Has This Been Tested?

Added tests, and confirmed with inspector that the below demo server works as intended:

from mcp.server.fastmcp import FastMCP, Context

mcp = FastMCP("Demo")


@mcp.resource("identification://{name}")
def get_ids(name: str, ctx: Context) -> str:
    request_id = ctx.request_id
    client_id = ctx.client_id
    return str(f"{request_id},{client_id}")

Breaking Changes

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

I tried to follow a similar structure to the way context is injected in tools

@fubuloubu fubuloubu force-pushed the fix/resources-with-context branch 4 times, most recently from 29732d5 to 7a3ea56 Compare April 2, 2025 17:16
@fubuloubu fubuloubu force-pushed the fix/resources-with-context branch from 7a3ea56 to c78d1ac Compare April 2, 2025 17:18
@stuzero
Copy link

stuzero commented Apr 9, 2025

context injection into prompts would be great also

@ChristianHinge
Copy link

It would be fantastic with this feature! Since most of my endpoints use context, I currently have to wrap them all as tools.

@Kludex Kludex force-pushed the fix/resources-with-context branch from 4445683 to 0790305 Compare May 7, 2025 08:53
@Kludex
Copy link
Member

Kludex commented May 7, 2025

This PR doesn't seem to be enough. We can't read from the ctx on the resource yet... All the tests are receiving the Context, but they are actually not using it. 😅

@ihrpr ihrpr added this to the r-05-25 milestone May 13, 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.

@app.resource() does not accept ctx: Context
6 participants