Skip to main content
Glama

get-element-dimensions

Retrieve the dimensions and position of any DOM element by specifying a CSS selector, enabling precise layout analysis during development.

Instructions

Retrieves dimension and position information of a specific element

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
selectorYesCSS selector of the element to inspect

Implementation Reference

  • The 'get-element-dimensions' tool handler function. It retrieves dimension and position information (width, height, top, left, bottom, right, x, y, isVisible) of a DOM element using getBoundingClientRect(), registered via server.tool() on the MCP server.
    server.tool(
      'get-element-dimensions',
      'Retrieves dimension and position information of a specific element',
      {
        selector: z.string().describe('CSS selector of the element to inspect')
      },
      async ({ selector }) => {
        try {
          // Check browser status
          const browserStatus = getContextForOperation();
          if (!browserStatus.isStarted) {
            return browserStatus.error;
          }
    
          // Get current checkpoint ID
          const checkpointId = await getCurrentCheckpointId(browserStatus.page);
    
          // Retrieve element dimensions and position information
          const dimensions = await browserStatus.page.evaluate((selector: string) => {
            const element = document.querySelector(selector);
            if (!element) return null;
    
            const rect = element.getBoundingClientRect();
            return {
              width: rect.width,
              height: rect.height,
              top: rect.top,
              left: rect.left,
              bottom: rect.bottom,
              right: rect.right,
              x: rect.x,
              y: rect.y,
              isVisible: !!(
                rect.width &&
                rect.height &&
                window.getComputedStyle(element).display !== 'none' &&
                window.getComputedStyle(element).visibility !== 'hidden'
              )
            };
          }, selector);
    
          if (!dimensions) {
            return {
              content: [
                {
                  type: 'text',
                  text: `Element with selector "${selector}" not found`
                }
              ],
              isError: true
            };
          }
    
          // Result message construction
          const resultMessage = {
            selector,
            dimensions,
            checkpointId
          };
    
          return {
            content: [
              {
                type: 'text',
                text: JSON.stringify(resultMessage, null, 2)
              }
            ]
          };
        } catch (error) {
          const errorMessage = error instanceof Error ? error.message : String(error);
          Logger.error(`Failed to get element dimensions: ${errorMessage}`);
          return {
            content: [
              {
                type: 'text',
                text: `Failed to get element dimensions: ${errorMessage}`
              }
            ],
            isError: true
          };
        }
      }
    );
  • Input schema for 'get-element-dimensions': requires a single 'selector' string parameter (CSS selector of the element to inspect), validated with Zod.
    {
      selector: z.string().describe('CSS selector of the element to inspect')
  • Tool is registered via server.tool('get-element-dimensions', ...) inside the registerBrowserTools() function in src/tools/browser-tools.ts.
    server.tool(
      'get-element-dimensions',
      'Retrieves dimension and position information of a specific element',
      {
        selector: z.string().describe('CSS selector of the element to inspect')
      },
      async ({ selector }) => {
        try {
          // Check browser status
          const browserStatus = getContextForOperation();
          if (!browserStatus.isStarted) {
            return browserStatus.error;
          }
    
          // Get current checkpoint ID
          const checkpointId = await getCurrentCheckpointId(browserStatus.page);
    
          // Retrieve element dimensions and position information
          const dimensions = await browserStatus.page.evaluate((selector: string) => {
            const element = document.querySelector(selector);
            if (!element) return null;
    
            const rect = element.getBoundingClientRect();
            return {
              width: rect.width,
              height: rect.height,
              top: rect.top,
              left: rect.left,
              bottom: rect.bottom,
              right: rect.right,
              x: rect.x,
              y: rect.y,
              isVisible: !!(
                rect.width &&
                rect.height &&
                window.getComputedStyle(element).display !== 'none' &&
                window.getComputedStyle(element).visibility !== 'hidden'
              )
            };
          }, selector);
    
          if (!dimensions) {
            return {
              content: [
                {
                  type: 'text',
                  text: `Element with selector "${selector}" not found`
                }
              ],
              isError: true
            };
          }
    
          // Result message construction
          const resultMessage = {
            selector,
            dimensions,
            checkpointId
          };
    
          return {
            content: [
              {
                type: 'text',
                text: JSON.stringify(resultMessage, null, 2)
              }
            ]
          };
        } catch (error) {
          const errorMessage = error instanceof Error ? error.message : String(error);
          Logger.error(`Failed to get element dimensions: ${errorMessage}`);
          return {
            content: [
              {
                type: 'text',
                text: `Failed to get element dimensions: ${errorMessage}`
              }
            ],
            isError: true
          };
        }
      }
    );
  • src/index.ts:87-92 (registration)
    The registerBrowserTools function is called in main() of src/index.ts, which wires up the 'get-element-dimensions' tool to the MCP server.
    registerBrowserTools(
      server,
      contextManager,
      lastHMREvents,
      screenshotHelpers
    );

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observed

TDQS

B3.1/5.0
Behavior2/5

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

No annotations are provided, so the description carries the full burden. It discloses that the tool reads dimension/position data but omits any detail about coordinate system (viewport/document), units, behavior when selector matches no element, or whether layout must be settled. This is a meaningful ambiguity.

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?

One short sentence with no filler; the core information is front-loaded and concise.

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

Completeness3/5

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

Without an output schema, the description should specify the return structure (e.g., width, height, x/y coordinates and coordinate system). It gives a high-level summary that is minimally viable but leaves important operational details undefined.

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% with a clear description of the selector parameter, so the schema already fully documents the input. The tool description adds no parameter-specific meaning, but the baseline of 3 applies.

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?

Clearly states the action (retrieves) and resource (dimension and position information of a sspecific element). The resource is specific enough to distinguish from sibling getters like get-element-styles or get-element-html, though it doesn't explicitly name an alternative.

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 choose this tool over siblings such as get-element-properties or get-element-styles. The description only states what it does, leaving the agent to infer usage context.

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