Skip to content

[prototype] feat(sdk): add chrome-devtools-mcp/sdk programmatic JS/TS client - #3698

Closed
natorion wants to merge 4 commits into
mainfrom
feat/js-sdk-proto
Closed

natorion wants to merge 4 commits into
mainfrom
feat/js-sdk-proto

Conversation

@natorion

@natorion natorion commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Prototype of a thin, strongly typed Node/TypeScript SDK exposed at chrome-devtools-mcp/sdk for programmatic browser inspection alongside Playwright, Puppeteer, or standalone Node scripts, plus a companion chrome-devtools-sdk agent skill.

What changed

  • In-process SDK (src/sdk/index.ts): Built directly on McpServer.from(config, {browserManager, autoInitNewPages}) and server.callTool(name, args), avoiding child-process stdio spawn overhead and spurious MCP roots capability warnings.
  • Subpath export (package.json, scripts/verify-npm-package.js): Adds "exports" exposing "./sdk" (JS entry + self-contained .d.ts types) while preserving "." (./out/Default/gen/src/index.js) for existing McpServer and createMcpServer consumers.
  • Generated TypeScript declarations with JSDoc (scripts/generate-sdk-types.ts -> src/sdk/tools.d.ts): Generates a pure .d.ts interface (DevToolsSdkTools, zero emitted runtime JS) with per-tool JSDoc descriptions directly from createTools() Zod schemas, wired into npm run gen via npm run sdk:generate.
  • Agent skill & docs (skills/chrome-devtools-sdk/SKILL.md, README.md): Adds a concise agent skill covering standalone and Playwright/Puppeteer coexistence patterns, core API reference, and common gotchas, packaged in the npm tarball and listed in README.md.
  • Ergonomic client API:
    • attachDevTools(options) enables experimentalStructuredContent: true by default and exposes all public tools as typed async methods returning {...structuredContent, text} and throwing an Error when isError is true.
    • Exports findUid(snapshot, matcher), close(), and [Symbol.asyncDispose]().
    • Automatically supplies the active pageId on page-scoped tools and evaluate_script (unless serviceWorkerId is set), throws an actionable error when no page is open, and refreshes selectedPageId with a one-time retry if the active tab closes externally.
    • Parses evaluate_script's JSON payload onto result, preserves base64 image blocks on take_screenshot().images when filePath is omitted, and types get_css_styles().matchedStyles.
  • Auto-arming collectors on attach (src/sdk/index.ts, src/McpContext.ts, src/index.ts): When attaching via browserUrl, wsEndpoint, or autoConnect (and autoSelectPage is not false), automatically selects the active page on connect and enables autoInitNewPages so collectors also attach to pages created after attachDevTools() without changing focus. Preserves defaultViewport: null and disconnects without closing the external browser.
  • Empty-array defaults (src/McpResponse.ts): Sets structuredContent.networkRequests, structuredContent.consoleMessages, and structuredContent.pages to [] instead of undefined when empty.

Usage example (Playwright coexistence)

import {chromium} from 'playwright';
import {attachDevTools, findUid} from 'chrome-devtools-mcp/sdk';

const browser = await chromium.launch({
  headless: true,
  args: ['--remote-debugging-port=9222'],
});
const context = await browser.newContext({viewport: {width: 960, height: 640}});
const page = await context.newPage();

await using cd4a = await attachDevTools({browserUrl: 'http://127.0.0.1:9222'});
await page.goto('https://example.com');

const {networkRequests} = await cd4a.list_network_requests();
const {consoleMessages} = await cd4a.list_console_messages({types: ['error']});
const {result} = await cd4a.evaluate_script({function: '() => document.title'});
const snap = await cd4a.take_snapshot();
const uid = findUid(snap, {role: 'button', name: 'Submit'});

Verification

  • npm run sdk:generate && npm run build && npm run check-format && npm run verify-npm-package passed (verifying SDK .d.ts files and skills/chrome-devtools-sdk/SKILL.md in the tarball).
  • Targeted unit/integration tests (tests/sdk.test.ts, tests/McpResponse.test.ts, tests/McpContext.test.ts: 222/222) and full test suite (2303/2303 pass, 0 fail, 4 skipped) passed.
  • Packed npm tarball verified in an isolated external consumer directory against Google Chrome 161.0.8196.0 canary and playwright 1.58.2:
    • tsc --noEmit --strict typecheck against chrome-devtools-mcp/sdk .d.ts passed.
    • Live standalone + Playwright coexistence probes verified: evaluate_script without pageId + parsed .result, inline take_screenshot().images, typed get_css_styles().matchedStyles, list_pages().pages empty-array default + actionable zero-page error, automatic stale-tab recovery when Playwright closes the active tab, viewport preservation (375x812), and post-attach targetcreated collector initialization.

…t ergonomics

- Add skills/chrome-devtools-sdk/SKILL.md and README SDK documentation (verified in verify-npm-package.js).
- Emit per-tool JSDoc descriptions and typed return shapes (evaluate_script.result, get_css_styles.matchedStyles, take_screenshot.images, list_pages.pages) in src/sdk/tools.d.ts.
- Auto-supply pageId for evaluate_script (unless serviceWorkerId is set) and parse its fenced JSON return value onto result.
- Preserve image blocks on take_screenshot() when filePath is omitted.
- Default structuredContent.pages to [] when empty, throw an actionable error when no page is open, refresh and retry once if a cached selectedPageId closes, and auto-initialize collectors on newly created targets (autoInitNewPages) when attaching to external browsers.
@Lightning00Blade

Copy link
Copy Markdown
Collaborator

Closing this due to #3639

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants