-
Notifications
You must be signed in to change notification settings - Fork 42
feat(cdp-proxy): add /json endpoint for Playwright connectOverCDP support #139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rgarcia
wants to merge
6
commits into
main
Choose a base branch
from
fix/cdp-proxy-json-endpoint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+410
−8
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…port The CDP proxy on port 9222 previously only implemented /json/version, which returned the WebSocket URL but didn't support target discovery. Playwright's connectOverCDP() fetches /json to discover browser targets before establishing a WebSocket connection. Without this endpoint, using `http://127.0.0.1:9222` with agent-browser or Playwright would fail, even though direct WebSocket connections (ws://127.0.0.1:9222) worked fine. This change: - Adds /json and /json/list endpoints that proxy to Chrome's /json - Rewrites webSocketDebuggerUrl and devtoolsFrontendUrl in the response to use the proxy's host instead of Chrome's internal host - Enables `agent-browser --cdp http://127.0.0.1:9222` to work correctly
Add comprehensive e2e tests verifying that agent-browser can connect to Chrome through the CDP proxy on port 9222. Tests validate: - /json endpoint returns targets with URLs rewritten to proxy port - /json/list endpoint works correctly with URL rewriting - /json/version endpoint continues to work - agent-browser works with various --cdp argument formats: - port only (9222) - http URL (http://127.0.0.1:9222) - localhost:port - 127.0.0.1:port - agent-browser snapshot and navigation commands work via proxy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
Rewrite the test to focus on validating the /json and /json/list endpoints added by the PR: - Test that /json returns targets with webSocketDebuggerUrl rewritten from port 9223 (Chrome) to port 9222 (proxy) - Test that /json/list works the same way - Test that /json/version continues to work - Add comparison test showing Chrome's direct /json on 9223 has unrewritten URLs Remove agent-browser tests since they required additional setup and the core functionality is validated by the JSON endpoint tests.
The TestUpstreamManagerDetectsChromiumAndRestart test was flaky in CI because Chromium's stderr output was fully buffered when connected to a file. The "DevTools listening on ws://..." line would sit in a buffer and never be flushed until the buffer filled or the process exited, causing the test to timeout after 20 seconds. Fix by using stdbuf -oL -eL to force line buffering on both stdout and stderr, ensuring each line is flushed immediately after the newline. This also improves test speed from ~17-20s to ~0.5s since we no longer wait for buffered output.
1. Add HTTP status code check before decoding JSON response from Chrome's /json endpoint. Previously, 4xx/5xx responses would result in confusing JSON decode errors like "invalid character '<'". 2. Fix devtoolsFrontendUrl rewriting for URLs with ws= query parameter. Chrome's devtoolsFrontendUrl often has the format: https://chrome-devtools-frontend.appspot.com/.../inspector.html?ws=127.0.0.1:9223/... The previous code only replaced the URL's host field, but in this format the Chrome host appears in the ws= query parameter. Now we handle both cases: direct host replacement and ws= query param replacement.
…2e test Playwright's connectOverCDP requests /json/version/ with a trailing slash, which was falling through to the WebSocket handler and returning a 426 "Upgrade Required" error. This prevented agent-browser (and other Playwright-based tools) from connecting via the CDP proxy on port 9222. - Register trailing-slash variants for /json, /json/, /json/list, /json/list/, /json/version, and /json/version/ endpoints - Add comprehensive e2e test for agent-browser that: - Installs agent-browser globally in the container - Tests CDP connection with port number format (--cdp 9222) - Tests CDP connection with http:// URL format (--cdp http://127.0.0.1:9222) - Verifies navigation, snapshot, get url, and get title commands This enables natural usage of agent-browser within containers: agent-browser --cdp 9222 open https://example.com agent-browser --cdp 9222 snapshot --json
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
The CDP proxy on port 9222 previously only implemented
/json/version, which returned the WebSocket URL but didn't support target discovery.Playwright's
connectOverCDP()fetches/jsonto discover browser targets before establishing a WebSocket connection. Without this endpoint, usinghttp://127.0.0.1:9222with agent-browser or Playwright would fail, even though direct WebSocket connections (ws://127.0.0.1:9222) worked fine.Changes
/jsonand/json/listendpoints that proxy to Chrome's/jsonwebSocketDebuggerUrlanddevtoolsFrontendUrlin the response to use the proxy's host instead of Chrome's internal hostagent-browser --cdp http://127.0.0.1:9222to work correctlyBefore
After
Test plan
go test ./lib/devtoolsproxy/...curl http://127.0.0.1:9222/jsonreturns targets with rewritten URLsagent-browser --cdp http://127.0.0.1:9222 snapshotworks inside a Kernel browser VMNote
Medium Risk
Introduces new HTTP handlers that fetch and transform upstream browser JSON; failures could break CDP client connectivity or add latency, but changes are isolated to the devtools proxy surface.
Overview
Adds HTTP target-discovery support to the DevTools proxy on
:9222by implementing/jsonand/json/list(plus trailing-slash variants) that proxy Chrome’s/jsonresponse and rewritewebSocketDebuggerUrl/devtoolsFrontendUrlhosts back to the proxy viarewriteWSURL.Extends
/json/versionhandling to also accept a trailing slash to avoid Playwright 426s, adds new e2e coverage validating the rewritten endpoints and a realagent-browserflow through the proxy, and hardens thedevtoolsproxyChromium restart test by usingstdbufto reduce CI flakiness when waiting for the “DevTools listening” log line.Written by Cursor Bugbot for commit d2ec4e3. This will update automatically on new commits. Configure here.