modelcontextprotocol / modelcontextprotocol/python-sdk

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

未关闭
#1,405 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

bug improves spec compliance P2 ready for work
主要语言
Python
星标
24.3k
派生
4k
平均合并
1 天 1 小时
30 天内合并 PR
31

描述

Initial Checks
Description

Problem

Currently, it is 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 classifies it as a template resource instead of a regular resource.

Expected Behavior

Resources with only a Context parameter should be registered as regular resources, not template resources.

Current Behavior

Resources with only a Context parameter are incorrectly registered as template resources, causing them to not appear in the resource list and fail when accessed.

Root Cause

The parameter validation logic in the FastMCP.resource() decorator doesn't properly distinguish between effective function parameters and Context parameters. It treats any function with parameters as a potential template resource, even when the only parameter is a Context.

Solution

  1. Fix the parameter validation logic to exclude Context parameters when determining if a resource should be a template
  2. Update the Resource.read() method signature to accept an optional context parameter
  3. Update all resource implementations to support the new signature

This change maintains full backward compatibility while enabling context-only resources.

Example Code
@mcp.resource("resource://user_profile")
def get_user_profile(ctx: Context) -> str:
    # Something Code ...
Python & MCP Python SDK
Python 3.13.5
MCP Python SDK v1.15.0

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 FastMCP.resource() 装饰器的参数验证开始,检查 Context-only 函数是如何分类的。然后跟踪 Resource.read() 和现有的资源实现,以一致地更新调用签名;当 Context-only 资源出现在资源列表中,并且可以在不破坏现有资源的情况下访问它们时,即视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
api
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
42/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。