Stealth browser automation for AI agents over MCP and the Chrome DevTools Protocol: navigation, network hooks, DOM extraction, and pixel-accurate UI cloning.
Python ai-agent-toolsai-toolsanthropicanti-bot-bypassautomation-toolsbrowser-automationchrome-devtoolsclaude-mcpcloudflare-bypassdeveloper-toolsfastmcpmcp-servermodel-context-protocolnetwork-interceptionnodriverpythonstealth-browserundetectable-browserweb-automationweb-scraping
fix(screenshots): return typed MCP image content
bepcyc43e827eef3b9
diff --git a/README.md b/README.mdindex 828caa1..2165dc0 100644--- a/README.md+++ b/README.md@@ -352,6 +352,26 @@ client = Client(| `get_page_content()` | Read page HTML and text || `take_screenshot()` | Capture screenshots |+`take_screenshot` returns an MCP image block by default, with the correct PNG/JPEG+MIME type and structured dimensions, byte count and SHA-256 metadata. Remote MCP+clients can display the image without reading the server's filesystem. Inline+images are complete and limited to 8 MiB after compression (16 MiB capture bytes+and 16 megapixels before encoding); larger captures fail with an explicit export+instruction instead of silently returning an inaccessible local path. For an+explicit server-local export, pass `file_path`. This still saves the requested+file and returns its location; making that exported file accessible to a remote+client remains the caller's responsibility.++The 8 MiB binary limit becomes about 10.67 MiB of base64, plus JSON metadata,+on the wire; client-specific message limits may be lower.++This intentionally replaces the previous inline base64 text and automatic local+file fallback. Metadata is also included as a small text block for clients without+structured-result support. `complete` describes the returned image bytes, not page+readiness or full-page coverage. Limits apply after browser capture and before+Pillow decoding/inline encoding; they are not browser/CDP memory budgets. File+export still captures the image in the browser and is not a low-memory alternative.+</details><details>diff --git a/src/server.py b/src/server.pyindex 35e1df8..8c42f93 100644--- a/src/server.py+++ b/src/server.py@@ -16,6 +16,8 @@ from typing import Any, Dict, List, Optional, Unionimport nodriver as ucfrom fastmcp import FastMCP+from fastmcp.tools.tool import ToolResult+from mcp.types import ImageContent, TextContentfrom pydantic_core import to_jsonfrom browser_manager import BrowserManager@@ -798,7 +800,7 @@ async def take_screenshot(full_page: bool = False,format: str = "png",file_path: Optional[str] = None-) -> Union[str, Dict[str, Any]]:+) -> Union[str, ToolResult]:"""Take a screenshot of the page.@@ -809,9 +811,12 @@ async def take_screenshot(file_path (Optional[str]): Optional file path to save screenshot to.Returns:- Union[str, Dict]: File path if file_path provided, otherwise optimized base64 data or file info dict.+ Union[str, ToolResult]: Explicit server-local file export, or an MCP image+ with format, dimensions and SHA-256 metadata. Inline images are limited+ to 8 MiB; use file_path for larger exports."""from PIL import Image+ import hashlibimport ioformat = format.lower()@@ -828,15 +833,19 @@ async def take_screenshot(save_path = Path(file_path)save_path.parent.mkdir(parents=True, exist_ok=True)await tab.save_screenshot(save_path, format=format, full_page=full_page)- return f"Screenshot saved. AI agents should use the Read tool to view this image: {str(save_path.absolute())}"+ return f"Screenshot saved on the server: {str(save_path.absolute())}"with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp_file:tmp_path = Path(tmp_file.name)try:await tab.save_screenshot(tmp_path, format=format, full_page=full_page)+ if tmp_path.stat().st_size > 16 * 1024 * 1024:+ raise ValueError("Screenshot exceeds inline capture limit; use file_path for export.")with Image.open(tmp_path) as img:+ if img.width * img.height > 16 * 1024 * 1024:+ raise ValueError("Screenshot dimensions exceed inline limit; use file_path.")if img.mode in ('RGBA', 'LA', 'P') and format.lower() == 'jpeg':background = Image.new('RGB', img.size, (255, 255, 255))if img.mode == 'P':@@ -853,28 +862,24 @@ async def take_screenshot(compressed_bytes = output_buffer.getvalue()- base64_size = len(compressed_bytes) * 1.33- estimated_tokens = int(base64_size / 4)-- if estimated_tokens > 20000:- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")- screenshot_filename = f"screenshot_{timestamp}_{instance_id[:8]}.{format.lower()}"- screenshot_path = response_handler.clone_dir / screenshot_filename-- with open(screenshot_path, 'wb') as f:- f.write(compressed_bytes)-- file_size_kb = len(compressed_bytes) / 1024- return {- "file_path": str(screenshot_path),- "filename": screenshot_filename,- "file_size_kb": round(file_size_kb, 2),- "estimated_tokens": estimated_tokens,- "reason": "Screenshot too large, automatically saved to file",- "message": f"Screenshot saved. AI agents should use the Read tool to view this image: {str(screenshot_path)}"- }-- return base64.b64encode(compressed_bytes).decode('utf-8')+ if len(compressed_bytes) > 8 * 1024 * 1024:+ raise ValueError("Screenshot exceeds 8 MiB inline limit; use file_path for export.")+ metadata = {+ "format": format,+ "width": img.width,+ "height": img.height,+ "bytes": len(compressed_bytes),+ "sha256": hashlib.sha256(compressed_bytes).hexdigest(),+ "complete": True,+ }+ return ToolResult(+ content=[ImageContent(+ type="image",+ mimeType=f"image/{format}",+ data=base64.b64encode(compressed_bytes).decode("ascii"),+ ), TextContent(type="text", text=json.dumps(metadata))],+ structured_content=metadata,+ )finally:if tmp_path.exists():diff --git a/tests/test_screenshot_content.py b/tests/test_screenshot_content.pynew file mode 100644index 0000000..539cc19--- /dev/null+++ b/tests/test_screenshot_content.py@@ -0,0 +1,211 @@+"""Screenshot wire-content tests without browser or cleanup side effects."""++import ast+import asyncio+import base64+import hashlib+import io+import json+import os+import sys+import struct+import tempfile+import unittest+import zlib+from pathlib import Path+from typing import Optional, Union+from unittest.mock import patch++from fastmcp import Client, FastMCP+from fastmcp.client.transports import PythonStdioTransport+from fastmcp.tools.tool import ToolResult+from mcp.types import CallToolResult, ImageContent, TextContent+from PIL import Image++ROOT = Path(__file__).resolve().parents[1]+++class ScreenshotContentTests(unittest.TestCase):+ def setUp(self):+ self.directory = tempfile.TemporaryDirectory()+ self.addCleanup(self.directory.cleanup)+ self.captures = []+ buffer = io.BytesIO()+ Image.new("RGBA", (96, 64), (30, 60, 90, 128)).save(buffer, format="PNG")+ self.png = buffer.getvalue()+ self.capture_error = None+ self.capture_started = None++ def server(self):+ # Load the exact production function without importing server.py's global+ # cleanup/browser objects. Registration and client serialization are real.+ tree = ast.parse((ROOT / "src/server.py").read_text())+ function = next(n for n in tree.body if isinstance(n, ast.AsyncFunctionDef)+ and n.name == "take_screenshot")+ mcp = FastMCP("screenshot-wire-test")+ owner = self++ class Tab:+ async def save_screenshot(self, path, **options):+ owner.captures.append((Path(path), options))+ Path(path).write_bytes(owner.png)+ if owner.capture_error:+ raise owner.capture_error+ if owner.capture_started:+ owner.capture_started.set()+ await asyncio.Event().wait()++ class Manager:+ async def get_tab(self, instance_id):+ return Tab() if instance_id == "owned" else None++ namespace = dict(+ __name__="screenshot_test", section_tool=lambda _: mcp.tool,+ browser_manager=Manager(), Path=Path, Optional=Optional, Union=Union,+ ToolResult=ToolResult, ImageContent=ImageContent, TextContent=TextContent,+ tempfile=tempfile, os=os, base64=base64, json=json,+ )+ exec(compile(ast.Module(body=[function], type_ignores=[]), str(ROOT / "src/server.py"), "exec"), namespace)+ return mcp++ def call(self, **kwargs):+ async def run():+ async with Client(self.server()) as client:+ result = await client.call_tool("take_screenshot", {"instance_id": "owned", **kwargs}, raise_on_error=False)+ # Exercise the actual MCP CallToolResult JSON representation.+ wire = CallToolResult(content=result.content, structuredContent=result.structured_content,+ isError=result.is_error).model_dump_json()+ return CallToolResult.model_validate_json(wire)+ return asyncio.run(run())++ def test_png_is_an_image_on_mcp_wire_with_complete_hash_metadata(self):+ result = self.call()+ self.assertFalse(result.isError)+ self.assertEqual(len(result.content), 2)+ image = result.content[0]+ self.assertIsInstance(image, ImageContent)+ self.assertEqual(image.mimeType, "image/png")+ data = base64.b64decode(image.data, validate=True)+ self.assertEqual(Image.open(io.BytesIO(data)).size, (96, 64))+ self.assertEqual(result.structuredContent["sha256"], hashlib.sha256(data).hexdigest())+ self.assertEqual(result.structuredContent["bytes"], len(data))+ self.assertTrue(result.structuredContent["complete"])+ self.assertFalse(self.captures[0][0].exists())+ self.assertNotIn("file_path", result.structuredContent)+ self.assertEqual(json.loads(result.content[1].text), result.structuredContent)++ def test_jpg_alias_has_correct_mime_and_flattens_transparency(self):+ result = self.call(format="jpg", full_page=True)+ self.assertFalse(result.isError)+ self.assertEqual(result.content[0].mimeType, "image/jpeg")+ image = Image.open(io.BytesIO(base64.b64decode(result.content[0].data)))+ self.assertEqual(image.format, "JPEG")+ self.assertEqual(image.mode, "RGB")+ self.assertTrue(all(abs(a - b) < 4 for a, b in zip(image.getpixel((30, 30)), (142, 157, 172))))+ self.assertTrue(self.captures[0][1]["full_page"])++ def test_stdio_client_receives_image_without_server_file_access(self):+ async def run():+ transport = PythonStdioTransport(+ Path(__file__), args=["--serve-fixture"],+ python_cmd=sys.executable, cwd=self.directory.name, keep_alive=False,+ )+ async with Client(transport) as client:+ self.assertIn("take_screenshot", [tool.name for tool in await client.list_tools()])+ return await client.call_tool("take_screenshot", {"instance_id": "owned"})++ result = asyncio.run(run())+ self.assertEqual(len(result.content), 2)+ self.assertIsInstance(result.content[0], ImageContent)+ image = base64.b64decode(result.content[0].data, validate=True)+ self.assertEqual(Image.open(io.BytesIO(image)).size, (96, 64))+ self.assertEqual(result.structured_content["sha256"], hashlib.sha256(image).hexdigest())++ def test_near_limit_image_crosses_stdio_without_truncation(self):+ async def run():+ transport = PythonStdioTransport(+ Path(__file__), args=["--serve-large-fixture"],+ python_cmd=sys.executable, cwd=self.directory.name, keep_alive=False,+ )+ async with Client(transport) as client:+ return await client.call_tool("take_screenshot", {"instance_id": "owned"})+ result = asyncio.run(run())+ image = base64.b64decode(result.content[0].data, validate=True)+ self.assertGreater(len(image), 8 * 1024 * 1024 * .98)+ self.assertLessEqual(len(image), 8 * 1024 * 1024)+ self.assertEqual(Image.open(io.BytesIO(image)).size, (1660, 1660))+ self.assertEqual(result.structured_content["sha256"], hashlib.sha256(image).hexdigest())++ def test_explicit_file_export_stays_available(self):+ target = Path(self.directory.name) / "exports" / "capture.png"+ result = self.call(file_path=str(target))+ self.assertFalse(result.isError)+ self.assertTrue(target.is_file())+ self.assertIn(str(target), result.content[0].text)+ self.assertNotIn("Read tool", result.content[0].text)++ def test_invalid_format_fails_before_capture(self):+ self.assertTrue(self.call(format="gif").isError)+ self.assertEqual(self.captures, [])++ def test_oversized_capture_fails_and_removes_temporary_file(self):+ self.png = b"x" * (16 * 1024 * 1024 + 1)+ result = self.call()+ self.assertTrue(result.isError)+ self.assertIn("inline capture limit", result.content[0].text)+ self.assertFalse(self.captures[0][0].exists())++ def test_excessive_pixels_are_rejected_before_decode(self):+ header = self.png[12:16] + struct.pack(">II", 4097, 4097) + self.png[24:29]+ self.png = self.png[:12] + header + struct.pack(">I", zlib.crc32(header)) + self.png[33:]+ result = self.call()+ self.assertTrue(result.isError)+ self.assertIn("dimensions exceed inline limit", result.content[0].text)+ self.assertFalse(self.captures[0][0].exists())++ def test_capture_failure_removes_temporary_file(self):+ self.capture_error = RuntimeError("Synthetic capture failure")+ self.assertTrue(self.call().isError)+ self.assertFalse(self.captures[0][0].exists())++ def test_cancelled_capture_removes_temporary_file(self):+ async def run():+ self.capture_started = asyncio.Event()+ async with Client(self.server()) as client:+ task = asyncio.create_task(client.call_tool("take_screenshot", {"instance_id": "owned"}))+ await asyncio.wait_for(self.capture_started.wait(), 2)+ task.cancel()+ with self.assertRaises(asyncio.CancelledError):+ await task+ asyncio.run(run())+ self.assertFalse(self.captures[0][0].exists())++ def test_encoded_output_limit_is_not_silently_spilled_or_truncated(self):+ # A synthetic encoder result tests the precise post-compression boundary.+ # Input remains a valid small capture; only output encoding is mocked.+ self.png = ScreenshotContentTests.small_png()+ with patch.object(Image.Image, "save", lambda _, stream, **kw: stream.write(b"x" * (8 * 1024 * 1024 + 1))):+ self.assertTrue(self.call().isError)+ self.assertFalse(self.captures[0][0].exists())++ @staticmethod+ def small_png():+ buffer = io.BytesIO()+ Image.new("RGB", (1, 1)).save(buffer, format="PNG")+ return buffer.getvalue()+++if __name__ == "__main__":+ if sys.argv[1:] in (["--serve-fixture"], ["--serve-large-fixture"]):+ fixture = ScreenshotContentTests()+ fixture.setUp()+ if sys.argv[1] == "--serve-large-fixture":+ buffer = io.BytesIO()+ Image.frombytes("RGB", (1660, 1660), os.urandom(1660 * 1660 * 3)).save(buffer, format="PNG")+ fixture.png = buffer.getvalue()+ try:+ fixture.server().run(show_banner=False)+ finally:+ fixture.doCleanups()+ else:+ unittest.main()