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
    );
Behavior2/5

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

With no annotations provided, the description should disclose behavioral traits like side effects or permissions. It only states the action without noting that it is read-only, nor does it specify whether it waits for the element to exist. The minimal description does not compensate for the lack of annotations.

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?

The description is a single sentence that is front-loaded with the action and resource. No redundant information is present, and every part contributes to the core purpose.

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?

The tool has no output schema, and the description does not specify the return value (e.g., an object with width, height, x, y). Given that sibling tools like get-element-properties likely return different data, this omission makes the description incomplete for an AI agent to know what to expect.

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?

The input schema covers 100% of parameters and describes 'selector' as a CSS selector. The tool description adds no additional meaning beyond the schema, so a baseline score of 3 is appropriate.

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 it retrieves dimension and position information for a specific element, using the verb 'Retrieves' and specifying the resource. This distinguishes it from sibling tools like get-element-html or get-element-styles, though it could be more explicit about the scope (e.g., bounding rect).

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 is provided on when to use this tool versus its siblings (e.g., get-element-properties, get-element-styles). The description does not mention alternatives or pruning conditions, leaving the agent to infer from the tool names alone.

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/ESnark/blowback'

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