browser-use icon indicating copy to clipboard operation
browser-use copied to clipboard

fix(mcp): avoid inline screenshot blobs in tool results

Open onthebed opened this issue 7 months ago โ€ข 7 comments

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 -q
  • pytest 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_state and browser_screenshot write PNGs to a temp dir and return screenshot_path in 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.ImageContent into ActionResult.images (with correct extensions) and keeps text in extracted_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

onthebed avatar Apr 25 '26 18:04 onthebed

CLA assistant check
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.

CLAassistant avatar Apr 25 '26 18:04 CLAassistant

I'll get the CLA signed โ€” will follow up once it's done.

onthebed avatar Apr 27 '26 08:04 onthebed

I'll get the CLA signed and follow up here once it's done.

onthebed avatar Apr 27 '26 09:04 onthebed

I'll get the CLA signed โ€” will follow up once it's done.

onthebed avatar Apr 27 '26 13:04 onthebed

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

PHclaw avatar Apr 29 '26 00:04 PHclaw

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'
    }

PHclaw avatar Apr 29 '26 01:04 PHclaw

๐Ÿ‘‹ 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-progress label 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! ๐Ÿค–

github-actions[bot] avatar Jun 14 '26 03:06 github-actions[bot]

๐Ÿ”’ 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! ๐Ÿค–

github-actions[bot] avatar Jun 29 '26 03:06 github-actions[bot]