Skip to main content
Glama
yashpreetbathla

MCP Accessibility Bridge

Get Focused Element

get_focused_element

Retrieve the currently keyboard-focused element's accessibility details and suggested selectors to verify focus management in accessible user interfaces.

Instructions

Returns the currently keyboard-focused element's accessibility info and suggested selectors. Useful for checking focus management in accessible UIs.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • Main handler function that retrieves the currently keyboard-focused element's accessibility information. Takes no arguments, uses Puppeteer's accessibility snapshot to find the focused node, then retrieves additional details via CDP including DOM attributes, tag name, and suggested selectors.
    export async function getFocusedElementHandler(
      _args: Record<string, never>
    ): Promise<ReturnType<typeof toolSuccess | typeof toolError>> {
      try {
        const { page, cdpSession } = browserManager.requireConnection();
    
        // Snapshot the full AX tree (unfiltered) to find focused node
        const snapshot = await page.accessibility.snapshot({ interestingOnly: false });
    
        if (!snapshot) {
          return toolSuccess({
            focused: null,
            message: 'No accessibility snapshot available.',
          });
        }
    
        // Find the focused node via the snapshot
        // Puppeteer's snapshot returns plain objects; we need to walk the tree
        const focusedNode = findFocusedInSnapshot(snapshot as unknown as Record<string, unknown>);
    
        if (!focusedNode) {
          return toolSuccess({
            focused: null,
            message: 'No element is currently focused.',
          });
        }
    
        // Try to get additional selector info via CDP
        // Use DOM.getFocusedNodeDetails if available, otherwise use JS evaluation
        let suggestedSelectors = null;
        let domAttributes: Record<string, string> | null = null;
        let tagName: string | null = null;
    
        try {
          // Get focused element via JavaScript
          const focusedHandle = await page.evaluateHandle(() => document.activeElement);
          const remoteObj = focusedHandle.remoteObject();
    
          if (remoteObj.objectId) {
            const describeResult = await cdpSession.send('DOM.describeNode', {
              objectId: remoteObj.objectId,
              depth: 0,
            }) as DomDescribeNodeResponse;
    
            const domNode = describeResult.node;
            tagName = domNode.localName;
            domAttributes = parseAttributes(domNode.attributes);
    
            // Get AX tree for this specific node
            const axResult = await cdpSession.send('Accessibility.getPartialAXTree', {
              backendNodeId: domNode.backendNodeId,
              fetchRelatives: false,
            }) as GetPartialAXTreeResponse;
    
            const primaryAXNode = (axResult.nodes ?? []).find((n) => !n.ignored);
            if (primaryAXNode) {
              const name = (primaryAXNode.name?.value as string) ?? '';
              const role = (primaryAXNode.role?.value as string) ?? '';
              suggestedSelectors = buildSelectorFromRawNode(
                name,
                role,
                tagName,
                domNode.attributes
              );
            }
          }
        } catch {
          // DOM info unavailable — still return what we have from the snapshot
        }
    
        return toolSuccess({
          focused: {
            role: focusedNode['role'] as string,
            name: focusedNode['name'] as string,
            ...(focusedNode['value'] !== undefined && { value: focusedNode['value'] }),
            ...(focusedNode['description'] !== undefined && { description: focusedNode['description'] }),
            ...(tagName && { tagName }),
            ...(domAttributes && { domAttributes }),
            ...(suggestedSelectors && { suggestedSelectors }),
          },
        });
      } catch (error) {
        return toolError(error);
      }
    }
  • Schema definition for get_focused_element tool - an empty object since the tool takes no arguments.
    export const getFocusedElementSchema = {};
  • src/index.ts:118-129 (registration)
    Tool registration in the MCP server, registering 'get_focused_element' with its metadata and handler function.
    // ── get_focused_element ──────────────────────────────────────────────────────
    server.registerTool(
      'get_focused_element',
      {
        title: 'Get Focused Element',
        description:
          'Returns the currently keyboard-focused element\'s accessibility info ' +
          'and suggested selectors. Useful for checking focus management in accessible UIs.',
        inputSchema: getFocusedElementSchema,
      },
      getFocusedElementHandler
    );
  • Helper functions: findFocusedInSnapshot recursively walks the accessibility tree to find the node with focused=true; parseAttributes converts CDP's attributes array format into an object map.
    function findFocusedInSnapshot(
      node: Record<string, unknown>
    ): Record<string, unknown> | null {
      if (node['focused'] === true) return node;
    
      const children = node['children'] as Record<string, unknown>[] | undefined;
      if (!children) return null;
    
      for (const child of children) {
        const found = findFocusedInSnapshot(child);
        if (found) return found;
      }
      return null;
    }
    
    function parseAttributes(attrs: string[] | undefined): Record<string, string> {
      const map: Record<string, string> = {};
      if (!attrs) return map;
      for (let i = 0; i + 1 < attrs.length; i += 2) {
        map[attrs[i]] = attrs[i + 1];
      }
      return map;
    }
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 what the tool returns but doesn't describe behavioral traits such as whether it requires a browser connection (implied by sibling tools), potential errors if no element is focused, performance characteristics, or output format details. The description adds minimal context beyond the basic purpose.

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 concise and well-structured with two sentences. The first sentence states the core functionality, and the second provides usage context. Every sentence earns its place without redundancy or unnecessary details.

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?

Given the tool's moderate complexity (retrieving focused element data) and lack of annotations or output schema, the description is adequate but has gaps. It explains the purpose and usage context but doesn't cover behavioral aspects like dependencies on browser state or output structure. For a tool with no structured metadata, more completeness would be beneficial.

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 parameter information, which is appropriate here. A baseline of 4 is applied for tools with no parameters, as there's nothing to compensate for.

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 tool's purpose: 'Returns the currently keyboard-focused element's accessibility info and suggested selectors.' It specifies the verb ('returns'), resource ('currently keyboard-focused element'), and output type ('accessibility info and suggested selectors'). However, it doesn't explicitly differentiate from sibling tools like 'get_element_properties' or 'get_accessibility_tree', which also retrieve accessibility-related information.

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

Usage Guidelines3/5

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

The description provides implied usage guidance: 'Useful for checking focus management in accessible UIs.' This suggests a context (accessibility testing) but doesn't explicitly state when to use this tool versus alternatives like 'get_interactive_elements' or 'query_accessibility_tree'. No exclusions or prerequisites are mentioned.

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/yashpreetbathla/mcp-accessibility-bridge'

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