fix(mcp): avoid inline screenshot blobs in tool results
browser_get_state and browser_screenshot were returning inline PNG blobs in MCP tool results. Clients that replay tool history back through Anthropic can trip Could not process image on every later turn once that screenshot payload lands in the conversation.
This stores MCP screenshots as temporary PNG files and returns the local path in the JSON payload instead of inlining base64 image data. I also taught the in-repo MCP client to lift image blocks into ActionResult.images so MCP image results still flow through the browser-use message pipeline without getting stringified into prompt text.
Tests:
pytest tests/ci/test_mcp_screenshot_results.py -qpytest tests/ci/test_file_system_llm_integration.py -q -k "image_stored_in_message_manager or agent_message_prompt_includes_images or image_end_to_end"
Fixes #4742
Summary by cubic
Stop inlining screenshot base64 in MCP tool results. Screenshots are saved to temp files, returned as local paths, and cleaned up on successful session close to avoid Anthropic โCould not process imageโ errors when replaying tool history. Fixes #4742.
- Bug Fixes
browser_get_stateandbrowser_screenshotwrite PNGs to a temp dir and returnscreenshot_pathin JSON; no base64 inlined.- Server stores screenshots in a dedicated temp dir, deletes them on successful close (warns if a file canโt be removed), and preserves files/state if close fails for retry.
- Updated tool descriptions to reflect path-based screenshot results.
- MCP client lifts
types.ImageContentintoActionResult.images(with correct extensions) and keeps text inextracted_content. - Added tests for file-backed screenshots, cleanup on close, preservation on close failure, and client image extraction.
Written for commit f094a5bdad05c861249133c1ddabb9aaf82af253. Summary will update on new commits. Review in cubic
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.
I'll get the CLA signed โ will follow up once it's done.
I'll get the CLA signed and follow up here once it's done.
I'll get the CLA signed โ will follow up once it's done.
For the wrong element being clicked:
The fix should use more specific selectors:
def find_element(page, description):
# Build a confidence score for each element
candidates = page.query_selector_all('*')
best_match = None
best_score = 0
for elem in candidates:
score = 0
# Tag match
if description.tag and elem.tag == description.tag:
score += 1
# Text match (fuzzy)
if description.text:
if description.text.lower() in elem.text.lower():
score += 3
# ID/class match
if description.id and description.id in (elem.id or ''):
score += 2
# Position match (prefer visible elements)
if elem.is_visible():
score += 1
if score > best_score:
best_score = score
best_match = elem
return best_match if best_score > 0 else None
For the MCP inline screenshot issue:
Return URL reference instead of inline base64:
import tempfile, base64
def format_screenshot_for_mcp(screenshot_b64: str) -> dict:
return {
'type': 'image',
'data': f'data:image/jpeg;base64,{screenshot_b64}',
'mime_type': 'image/jpeg'
}
๐ This PR has been automatically marked as stale because it hasn't had activity for 45 days.
To keep this PR open:
- Rebase against the latest main branch
- Address any review feedback or merge conflicts
- Add a comment explaining the current status
- Add the
work-in-progresslabel if you're still actively working on this
This will be automatically closed in 14 days if no further activity occurs.
Thanks for contributing to browser-use! ๐ค
๐ This PR was automatically closed because it was stale for 14 days with no activity.
Don't worry! If you'd like to continue this work:
- Reopen this PR and rebase against main
- Create a fresh PR with updated changes
- Join our Discord if you need help
Thanks for contributing to browser-use! ๐ค