Skills._handle_read_directory reports handler ValidationError as INVALID_PARAMS and skips outbound re-validation
维护者通常 1 天内回复
评估
调研方向
阅读 src/mcp/server/skills.py 中第 132-142 行附近,并将 _handle_read_directory 与 _handle_list(112-116)和 _handle_get(123-127)进行比较,后两者已经将 handler 调用以及 model_validate(result.model_dump()) 包裹在 try ... except ValidationError 中。照此模式修改,使两条失败路径都抛出 MCPError(code=INTERNAL_ERROR, message="Handler returned an invalid result")。用 issue 中的两案例脚本复现,然后运行现有的 skills server 测试,确认其他 handler 仍能正常工作。注意报告者已有本地修复并请求被指派,因此开始前请与维护者确认。
由索引模型根据 Issue 内容生成。
描述
Initial Checks
- I confirm that I'm using the newest release of my line (the latest 2.x, or the latest 1.x if I'm still on v1)
- I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue
Release line
2.x (current stable)
Description
In src/mcp/server/skills.py, Skills documents that a non-conformant handler result is a server fault reported as INTERNAL_ERROR, not the caller's INVALID_PARAMS.
_handle_list (lines 112-116) and _handle_get (lines 123-127) enforce this by wrapping the handler call and outbound re-validation (model_validate(result.model_dump())) in try ... except ValidationError.
_handle_read_directory (lines 132-142) omits both:
await self._read_directory(ctx, params)runs outsidetry ... except ValidationError, so aValidationErrorraised while buildingReadDirectoryResultescapes to the framework's default handler and returns-32602(INVALID_PARAMS) instead of-32603(INTERNAL_ERROR).- It never calls
ReadDirectoryResult.model_validate(result.model_dump()), so a handler that mutates a field after construction (e.g. setting an invalidcache_scope) ships a non-conformant payload over the wire.
Example Code
import anyio
from mcp.client import Client
from mcp.server.mcpserver import MCPServer
from mcp.server.skills import Skills
from mcp.shared.exceptions import MCPError
from mcp.shared.skills import (
ListSkillsResult,
ReadDirectoryParams,
ReadDirectoryRequest,
ReadDirectoryResult,
)
from mcp_types import Resource
from mcp_types.jsonrpc import INTERNAL_ERROR, INVALID_PARAMS
from pydantic import BaseModel, ConfigDict
class _RawResult(BaseModel):
model_config = ConfigDict(extra="allow")
async def list_skills(ctx, params):
return ListSkillsResult(skills=[])
async def get_skill(ctx, params):
raise MCPError(code=INVALID_PARAMS, message="unknown")
async def bad_construct(ctx, params):
return ReadDirectoryResult(resources=[], cache_scope="everyone") # type: ignore[arg-type]
async def bad_mutate(ctx, params):
res = ReadDirectoryResult(resources=[Resource(uri="skill://pdf/a.txt", name="a.txt")])
res.cache_scope = "invalid-scope" # type: ignore[assignment]
return res
async def main():
s1 = MCPServer("s1", extensions=[Skills(list_skills=list_skills, get_skill=get_skill, read_directory=bad_construct)])
async with Client(s1) as client:
try:
await client.session.send_request(ReadDirectoryRequest(params=ReadDirectoryParams(uri="skill://pdf")), _RawResult)
except MCPError as exc:
print(f"Case 1: code={exc.error.code} (expected {INTERNAL_ERROR}), message={exc.error.message!r}")
s2 = MCPServer("s2", extensions=[Skills(list_skills=list_skills, get_skill=get_skill, read_directory=bad_mutate)])
async with Client(s2) as client:
raw = await client.session.send_request(ReadDirectoryRequest(params=ReadDirectoryParams(uri="skill://pdf")), _RawResult)
print(f"Case 2: leaked invalid payload -> cacheScope={raw.model_extra.get('cacheScope')!r}")
anyio.run(main)
Observed output:
Case 1: code=-32602 (expected -32603), message='Invalid request parameters'
Case 2: leaked invalid payload -> cacheScope='invalid-scope'
Expected behavior: both cases should raise MCPError(code=INTERNAL_ERROR, message="Handler returned an invalid result"), matching _handle_list and _handle_get.
I have a minimal fix (wrapping await self._read_directory(ctx, params) and ReadDirectoryResult.model_validate(result.model_dump()) in try ... except ValidationError) and regression tests ready locally, and would be happy to submit a PR if assigned.
Python & MCP Python SDK
- Python: 3.14.8
- mcp: 2.3.1.dev20+77001c96 (main @ 77001c96)
AI disclosure: AI assistance was used to help investigate and draft this issue; the reproduction and observed output were verified locally.
- 主要语言
- Python
- 星标
- 25k
- 派生
- 4.2k
- 平均合并
- 10 分钟
- 30 天内合并 PR
- 1
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
modelcontextprotocol/python-sdk 的其他 Issue
-
v2
难度 1/5 1 小时以内 新手友好度 86/100
modelcontextprotocol/python-sdk#3978 · 2 条评论 ·
维护者通常 1 天内回复
-
Client serves cached tools after its context exits with an exception可能已有人在做 @jonxsong 于 1 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 65/100
modelcontextprotocol/python-sdk#3968 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 85/100
modelcontextprotocol/python-sdk#3957 ·
维护者通常 1 天内回复
-
v2
难度 1/5 1 小时以内 新手友好度 91/100
modelcontextprotocol/python-sdk#3934 · 2 条评论 ·
维护者通常 1 天内回复
-
bug v1
难度 2/5 1-3 小时 新手友好度 79/100
modelcontextprotocol/python-sdk#3929 · 2 条评论 ·
维护者通常 1 天内回复
查看 modelcontextprotocol/python-sdk 的全部 Issue
相似的 Issue
-
auto-reported bug
难度 2/5 1-3 小时 新手友好度 62/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
-
backend bug
难度 2/5 1-3 小时 新手友好度 86/100
nolte/kamerplanter#3193 ·
维护者通常 1 天内回复
-
soup prune-prompt exits 1 after writing its output on a file over 100,000 rows, and reports a capped row count otherwise可能已有人在做 @abdulwaarith0 今天认领。 未关闭needs-triage
难度 2/5 1-3 小时 新手友好度 84/100
MakazhanAlpamys/Soup#3690 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复