Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/mcp/server/skills.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,11 @@ async def _handle_read_directory(
) -> HandlerResult:
assert self._read_directory is not None
_require_directory_uri(params.uri)
result = await self._read_directory(ctx, params)
try:
result = await self._read_directory(ctx, params)
ReadDirectoryResult.model_validate(result.model_dump())
validate_directory_result(params.uri, result)
except ValueError:
except (ValidationError, ValueError):
raise MCPError(code=INTERNAL_ERROR, message="Handler returned an invalid result") from None
return _finalize_cacheable(result, ctx.protocol_version)

Expand Down
27 changes: 27 additions & 0 deletions tests/server/test_skills.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,30 @@ async def bad_directory(ctx: ServerRequestContext[Any, Any], params: ReadDirecto
ReadDirectoryRequest(params=ReadDirectoryParams(uri="skill://git-workflow")), ReadDirectoryResult
)
assert exc_info.value.code == INTERNAL_ERROR


async def test_directory_read_validation_error_and_mutation_surface_as_internal_error() -> None:
"""Handler ValidationError and post-construction mutation both return INTERNAL_ERROR (#3882)."""

async def invalid_construction(
ctx: ServerRequestContext[Any, Any], params: ReadDirectoryParams
) -> ReadDirectoryResult:
return ReadDirectoryResult.model_validate({"resources": "not-a-list"})

async def mutated_result(
ctx: ServerRequestContext[Any, Any], params: ReadDirectoryParams
) -> ReadDirectoryResult:
result = ReadDirectoryResult(resources=[Resource(uri="skill://git-workflow/a.md", name="a.md")])
result.cache_scope = "invalid_scope" # type: ignore[assignment]
return result

for handler in (invalid_construction, mutated_result):
server = MCPServer(
"catalog", extensions=[Skills(list_skills=_list_skills, get_skill=_get_skill, read_directory=handler)]
)
async with Client(server) as client:
with pytest.raises(MCPError) as exc_info:
await client.session.send_request(
ReadDirectoryRequest(params=ReadDirectoryParams(uri="skill://git-workflow")), ReadDirectoryResult
)
assert exc_info.value.code == INTERNAL_ERROR
Loading