Skip to content

Playwright Extension: client name always shows as "unknown" on the token-bypass connect path #41839

Description

@dosxnjos

Problem

When an MCP client connects to the Playwright Extension via the token-bypass path (setting
PLAYWRIGHT_MCP_EXTENSION_TOKEN so the connection dialog doesn't need a manual click), the
connect page and the status page ("Connected to X") always show the client name as "unknown",
regardless of what name the client actually declares. The tab-picker connection path (clicking
"Allow & select") shows the real client name correctly.

Confirmed on upstream/main, unmodified - not specific to any fork or downstream change.

Root cause

In packages/extension/src/ui/connect.tsx, handleConnectToTab is a useCallback that reads the
client name from clientInfo React state:

const handleConnectToTab = useCallback(async (tab?: chrome.tabs.Tab) => {
  ...
  clientName: clientInfo,
  ...
}, [clientInfo]);

On the token-bypass path, it's called synchronously within the same effect run that just called
setClientInfo(info):

const info = `${client.name || 'unknown'}`;
setClientInfo(info);
...
if (token === expectedToken) {
  await handleConnectToTab();  // still closes over the initial 'unknown' clientInfo
  return;
}

setClientInfo only schedules a re-render; it doesn't mutate the clientInfo variable already
captured by the version of handleConnectToTab this effect closed over at mount. So this call
always sees the initial 'unknown' state, never the freshly-parsed info. The tab-picker path
doesn't hit this, since a user click can only happen after at least one render has already landed
with the updated clientInfo.

This is deterministic (not a timing race) and independent of what client name is actually passed -
reproduces the same way regardless.

Proposed fix

Pass the freshly-parsed name explicitly as an argument at the token-bypass call site instead of
relying on the closure - e.g. give handleConnectToTab an optional second parameter and pass
info directly there, leaving the tab-picker call site (which doesn't have this problem) unchanged.
Small, isolated, single-file fix. Happy to open a PR if this approach sounds right.

Metadata

Metadata

Assignees

Labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions