Skip to main content
Glama

getSelectedElement

Retrieve the currently highlighted element from a web page for DOM analysis, enabling AI applications to inspect and interact with browser content.

Instructions

Get the selected element from the browser

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • MCP tool registration and handler for 'getSelectedElement'. Fetches selected element data from the browser connector server via HTTP GET /selected-element and formats it as MCP response.
    server.tool(
      "getSelectedElement",
      "Get the selected element from the browser",
      async () => {
        return await withServerConnection(async () => {
          const response = await fetch(
            `http://${discoveredHost}:${discoveredPort}/selected-element`
          );
          const json = await response.json();
          return {
            content: [
              {
                type: "text",
                text: JSON.stringify(json, null, 2),
              },
            ],
          };
        });
      }
    );
  • HTTP GET endpoint handler that returns the currently stored selected element data from memory or a fallback message if none selected.
    app.get("/selected-element", (req, res) => {
      res.json(selectedElement || { message: "No element selected" });
    });
  • In POST /extension-log handler, updates the global selectedElement when receiving 'selected-element' type data from Chrome DevTools extension.
    case "selected-element":
      console.log("Updating selected element:", {
        tagName: data.element?.tagName,
        id: data.element?.id,
        className: data.element?.className,
      });
      selectedElement = data.element;
      break;
  • Chrome DevTools extension function that captures the selected element ($0) details using eval and sends it to the browser connector server as 'selected-element' log type.
    function captureAndSendElement() {
      chrome.devtools.inspectedWindow.eval(
        `(function() {
          const el = $0;  // $0 is the currently selected element in DevTools
          if (!el) return null;
    
          const rect = el.getBoundingClientRect();
    
          return {
            tagName: el.tagName,
            id: el.id,
            className: el.className,
            textContent: el.textContent?.substring(0, 100),
            attributes: Array.from(el.attributes).map(attr => ({
              name: attr.name,
              value: attr.value
            })),
            dimensions: {
              width: rect.width,
              height: rect.height,
              top: rect.top,
              left: rect.left
            },
            innerHTML: el.innerHTML.substring(0, 500)
          };
        })()`,
        (result, isException) => {
          if (isException || !result) return;
    
          console.log("Element selected:", result);
    
          // Send to browser connector
          sendToBrowserConnector({
            type: "selected-element",
            timestamp: Date.now(),
            element: result,
          });
        }
      );
    }
  • Event listener in Chrome DevTools extension that triggers captureAndSendElement whenever the user selects a new element in the Elements panel.
    chrome.devtools.panels.elements.onSelectionChanged.addListener(() => {
      captureAndSendElement();
    });
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 of behavioral disclosure. It states the action but doesn't explain what 'selected element' means (e.g., UI context, browser state), whether it's read-only or has side effects, or what the output format might be, leaving significant gaps for a tool with no structured safety hints.

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, clear sentence with no wasted words, front-loading the core action. It's appropriately sized for a simple tool with no parameters, making it efficient and easy to parse.

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?

Given the tool's simplicity (0 params, no output schema) and lack of annotations, the description is minimal. It states what the tool does but doesn't provide enough context about behavior, output, or differentiation from siblings, making it incomplete for effective agent use despite the low complexity.

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

Parameters4/5

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

The input schema has 0 parameters with 100% coverage, so no parameter documentation is needed. The description doesn't add param info, but that's acceptable here; it implies no inputs are required, which aligns with the schema, earning a baseline high score for this context.

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 ('Get') and resource ('selected element from the browser'), making the tool's purpose understandable. However, it doesn't explicitly differentiate from sibling tools like 'takeScreenshot' or 'runAccessibilityAudit', which might also involve element selection, so it doesn't reach the highest score.

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?

The description provides no guidance on when to use this tool versus alternatives. With sibling tools like 'runAccessibilityAudit' that might audit selected elements, there's no indication of when 'getSelectedElement' is preferred or what context it's intended for, leaving usage unclear.

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/oenius/browser-tools-mcp'

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