Skip to content

Commit 2304c3e

Browse files
k4cper-gclaude
andcommitted
Fix screenshot tool return type for FastMCP compatibility
FastMCP/Pydantic can't serialize Image | str union type. Use Image return type and raise exceptions for errors instead. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 4a5a3dd commit 2304c3e

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

examples/cross-os/mcp_server.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ def screenshot(
439439
region_y: int | None = None,
440440
region_w: int | None = None,
441441
region_h: int | None = None,
442-
) -> Image | str:
442+
) -> Image:
443443
"""Capture a screenshot from a specific screen and return it as a PNG image.
444444
445445
By default captures the full primary monitor on the target machine.
@@ -455,31 +455,23 @@ def screenshot(
455455
region_w: Width of capture region in pixels.
456456
region_h: Height of capture region in pixels.
457457
"""
458-
try:
459-
session = _resolve_screen(screen)
460-
except ValueError as e:
461-
return json.dumps({"success": False, "error": str(e)})
458+
session = _resolve_screen(screen)
462459

463460
region_params = [region_x, region_y, region_w, region_h]
464461
has_any = any(v is not None for v in region_params)
465462
has_all = all(v is not None for v in region_params)
466463

467464
if has_any and not has_all:
468-
return json.dumps({
469-
"success": False,
470-
"error": "All region parameters (region_x, region_y, region_w, region_h) "
471-
"must be provided together, or none at all.",
472-
})
465+
raise ValueError(
466+
"All region parameters (region_x, region_y, region_w, region_h) "
467+
"must be provided together, or none at all."
468+
)
473469

474470
region = None
475471
if has_all:
476472
region = {"x": region_x, "y": region_y, "w": region_w, "h": region_h}
477473

478-
try:
479-
png_bytes = session.screenshot(region=region)
480-
except (ImportError, RuntimeError) as e:
481-
return json.dumps({"success": False, "error": str(e)})
482-
474+
png_bytes = session.screenshot(region=region)
483475
return Image(data=png_bytes, format="png")
484476

485477

0 commit comments

Comments
 (0)