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
23 changes: 23 additions & 0 deletions tests/server/test_skills.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,26 @@ 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_rejects_handler_validation_error_and_mutation_as_internal_error() -> None:
"""Handler ValidationError and post-construction mutation must surface as INTERNAL_ERROR."""

async def bad_construct(ctx: ServerRequestContext[Any, Any], params: ReadDirectoryParams) -> ReadDirectoryResult:
return ReadDirectoryResult(resources=[], cache_scope="everyone") # type: ignore[arg-type]

async def bad_mutate(ctx: ServerRequestContext[Any, Any], params: ReadDirectoryParams) -> ReadDirectoryResult:
res = ReadDirectoryResult(resources=[Resource(uri="skill://git-workflow/SKILL.md", name="SKILL.md")])
res.cache_scope = "invalid-scope" # type: ignore[assignment]
return res

for handler in (bad_construct, bad_mutate):
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