Skip to main content
Glama
BrowserGenie

BrowserGenie MCP Server

by BrowserGenie

query_shadow_dom

Query elements inside a shadow DOM by providing the host element selector and the inner selector within the shadow root.

Instructions

Query an element inside a shadow DOM root. Provide the host element selector and the inner selector within the shadow root.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
hostSelectorYesCSS selector for the shadow host element
innerSelectorYesCSS selector within the shadow root
tabIdNoTarget tab ID (defaults to currently active tab)
apiKeyNoAPI key for authentication if enabled

Implementation Reference

  • The registerElementTools function registers all element-related MCP tools including query_shadow_dom on the server via server.tool().
    export function registerElementTools(server: McpServer, bridge: WebSocketBridge) {
      server.tool(
        'find_element',
        'Find an element by visible text, ARIA role, ARIA label, CSS selector, or XPath. Returns a unique CSS selector and element details. Use this when you need to locate an element but don\'t know its exact selector. Role matching supports both explicit role="..." attributes AND semantic HTML (e.g., <button> matches role "button", <a> matches role "link"). When multiple filters are provided (e.g., role + text), they are combined with AND logic.',
        {
          text: z.string().optional().describe('Find by visible text content (substring match). Can be combined with role for precise targeting.'),
          role: z.string().optional().describe('Find by ARIA role (e.g., "button", "link", "navigation", "main"). Falls back to semantic HTML roles if no explicit role attribute is present.'),
          ariaLabel: z.string().optional().describe('Find by aria-label attribute (substring match)'),
          css: z.string().optional().describe('CSS selector (most direct — bypasses all other filters)'),
          xpath: z.string().optional().describe('XPath expression (bypasses all other filters)'),
          nth: z.number().optional().describe('Select the Nth match (0-indexed), default: 0'),
          tabId: z.number().optional().describe('Target tab ID (defaults to currently active tab)'),
          apiKey: z.string().optional().describe('API key for authentication if enabled'),
        },
        async ({ text, role, ariaLabel, css, xpath, nth, tabId, apiKey }) => {
          const result = await bridge.sendCommand({
            command: 'find_element',
            params: { text, role, ariaLabel, css, xpath, nth },
            tabId,
            apiKey,
            timeout: LONG_TIMEOUT,
          });
          if (!result.success) {
            return { content: [{ type: 'text', text: `Error: ${result.error?.message}` }], isError: true };
          }
          return { content: [{ type: 'text', text: JSON.stringify(result.data, null, 2) }] };
        }
      );
    
      server.tool(
        'get_element_state',
        'Get the state of an element: exists, visible, enabled, focused, checked, selected, readOnly, required, valid, and more. Essential for verifying element conditions during testing.',
        {
          selector: z.string().describe('CSS selector or XPath expression'),
          selectorType: z.enum(['css', 'xpath']).optional().default('css').describe('Selector type'),
          tabId: z.number().optional().describe('Target tab ID (defaults to currently active tab)'),
          apiKey: z.string().optional().describe('API key for authentication if enabled'),
        },
        async ({ selector, selectorType, tabId, apiKey }) => {
          const result = await bridge.sendCommand({
            command: 'get_element_state',
            params: { selector, selectorType },
            tabId,
            apiKey,
            timeout: LONG_TIMEOUT,
          });
          if (!result.success) {
            return { content: [{ type: 'text', text: `Error: ${result.error?.message}` }], isError: true };
          }
          return { content: [{ type: 'text', text: JSON.stringify(result.data, null, 2) }] };
        }
      );
    
      server.tool(
        'query_shadow_dom',
        'Query an element inside a shadow DOM root. Provide the host element selector and the inner selector within the shadow root.',
        {
          hostSelector: z.string().describe('CSS selector for the shadow host element'),
          innerSelector: z.string().describe('CSS selector within the shadow root'),
          tabId: z.number().optional().describe('Target tab ID (defaults to currently active tab)'),
          apiKey: z.string().optional().describe('API key for authentication if enabled'),
        },
        async ({ hostSelector, innerSelector, tabId, apiKey }) => {
          const result = await bridge.sendCommand({
            command: 'query_shadow_dom',
            params: { hostSelector, innerSelector },
            tabId,
            apiKey,
            timeout: LONG_TIMEOUT,
          });
          if (!result.success) {
            return { content: [{ type: 'text', text: `Error: ${result.error?.message}` }], isError: true };
          }
          return { content: [{ type: 'text', text: JSON.stringify(result.data, null, 2) }] };
        }
      );
    
      server.tool(
        'get_computed_styles',
        'Get computed CSS styles for an element. Optionally filter to specific properties or query pseudo-elements like ::before and ::after.',
        {
          selector: z.string().describe('CSS selector'),
          properties: z.array(z.string()).optional().describe('Specific CSS properties to query (default: all computed styles)'),
          pseudoElement: z.string().optional().describe('Pseudo-element (e.g., "::before", "::after")'),
          tabId: z.number().optional().describe('Target tab ID (defaults to currently active tab)'),
          apiKey: z.string().optional().describe('API key for authentication if enabled'),
        },
        async ({ selector, properties, pseudoElement, tabId, apiKey }) => {
          const result = await bridge.sendCommand({
            command: 'get_computed_styles',
            params: { selector, properties, pseudoElement },
            tabId,
            apiKey,
            timeout: LONG_TIMEOUT,
          });
          if (!result.success) {
            return { content: [{ type: 'text', text: `Error: ${result.error?.message}` }], isError: true };
          }
          return { content: [{ type: 'text', text: JSON.stringify(result.data, null, 2) }] };
        }
      );
    
      server.tool(
        'deep_query_shadow_dom',
        'Query an element inside a deep shadow DOM tree by providing a path of host selectors.',
        {
          hostPath: z.array(z.string()).describe('Ordered array of shadow host CSS selectors to traverse'),
          innerSelector: z.string().describe('CSS selector within the deepest shadow root'),
          tabId: z.number().optional().describe('Target tab ID (defaults to currently active tab)'),
          apiKey: z.string().optional().describe('API key for authentication if enabled'),
        },
        async ({ hostPath, innerSelector, tabId, apiKey }) => {
          const result = await bridge.sendCommand({
            command: 'deep_query_shadow_dom',
            params: { hostPath, innerSelector },
            tabId,
            apiKey,
            timeout: LONG_TIMEOUT,
          });
          if (!result.success) {
            return { content: [{ type: 'text', text: `Error: ${result.error?.message}` }], isError: true };
          }
          return { content: [{ type: 'text', text: JSON.stringify(result.data, null, 2) }] };
        }
      );
    
      server.tool(
        'get_shadow_dom_tree',
        'Return the full shadow DOM tree structure as JSON for a given host element.',
        {
          hostSelector: z.string().describe('CSS selector for the shadow host element'),
          maxDepth: z.number().optional().default(5).describe('Maximum depth to traverse'),
          tabId: z.number().optional().describe('Target tab ID (defaults to currently active tab)'),
          apiKey: z.string().optional().describe('API key for authentication if enabled'),
        },
        async ({ hostSelector, maxDepth, tabId, apiKey }) => {
          const result = await bridge.sendCommand({
            command: 'get_shadow_dom_tree',
            params: { hostSelector, maxDepth },
            tabId,
            apiKey,
            timeout: LONG_TIMEOUT,
          });
          if (!result.success) {
            return { content: [{ type: 'text', text: `Error: ${result.error?.message}` }], isError: true };
          }
          return { content: [{ type: 'text', text: JSON.stringify(result.data, null, 2) }] };
        }
      );
    }
  • Input schema for query_shadow_dom tool: hostSelector (string), innerSelector (string), optional tabId (number), optional apiKey (string).
    {
      hostSelector: z.string().describe('CSS selector for the shadow host element'),
      innerSelector: z.string().describe('CSS selector within the shadow root'),
      tabId: z.number().optional().describe('Target tab ID (defaults to currently active tab)'),
      apiKey: z.string().optional().describe('API key for authentication if enabled'),
    },
  • Handler function for query_shadow_dom tool. Sends a 'query_shadow_dom' command via WebSocket bridge with hostSelector and innerSelector params. Returns the result or an error.
      async ({ hostSelector, innerSelector, tabId, apiKey }) => {
        const result = await bridge.sendCommand({
          command: 'query_shadow_dom',
          params: { hostSelector, innerSelector },
          tabId,
          apiKey,
          timeout: LONG_TIMEOUT,
        });
        if (!result.success) {
          return { content: [{ type: 'text', text: `Error: ${result.error?.message}` }], isError: true };
        }
        return { content: [{ type: 'text', text: JSON.stringify(result.data, null, 2) }] };
      }
    );
  • WebSocketBridge.sendCommand() is the helper that forwards the command to the Chrome extension over WebSocket and resolves the response.
    async sendCommand(cmd: BridgeCommand): Promise<BridgeResponse> {
      if (!this.isConnected()) {
        return {
          success: false,
          error: {
            code: 'NOT_CONNECTED',
            message: 'Chrome extension is not connected. Ensure the extension is installed, enabled, and the browser is running.',
          },
        };
      }
    
      const id = crypto.randomUUID();
      const timeout = cmd.timeout ?? DEFAULT_TIMEOUT;
    
      return new Promise<BridgeResponse>((resolve, reject) => {
        const timer = setTimeout(() => {
          this.pending.delete(id);
          resolve({
            success: false,
            error: {
              code: 'TIMEOUT',
              message: `Command '${cmd.command}' timed out after ${timeout}ms`,
            },
          });
        }, timeout);
    
        this.pending.set(id, { resolve, reject, timer });
    
        const message = {
          id,
          type: 'request',
          command: cmd.command,
          params: cmd.params,
          tabId: cmd.tabId,
          apiKey: cmd.apiKey,
          timestamp: Date.now(),
        };
    
        this.client!.send(JSON.stringify(message));
      });
    }
  • Entry point creating the WebSocketBridge and server, which ultimately registers the query_shadow_dom tool.
    const bridge = new WebSocketBridge(WEBSOCKET_PORT);
    const server = createServer(bridge);
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations are absent, so the description should disclose behavioral traits. It does not state whether the operation is read-only, has side effects, or requires permissions. Only implies a query without specifying effects.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Two short sentences, front-loaded with purpose. No wasted words; each sentence adds value.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Lacks information on return value, error handling, and scope (e.g., depth of shadow root query) especially given the absence of an output schema and presence of similar sibling tools.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 100%, so baseline is 3. The description adds minimal extra meaning beyond the schema's parameter descriptions, primarily rephrasing the required inputs.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('Query an element inside a shadow DOM root') and specifies the required inputs, but does not differentiate from the sibling tool 'deep_query_shadow_dom'.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No guidance on when to use this tool vs. alternatives like deep_query_shadow_dom or get_shadow_dom_tree. The description only instructs what to provide, not the context of use.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/BrowserGenie/mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server