Skip to main content
Glama
lxman

Safari MCP Server

by lxman

safari_inspect_element

Inspect DOM elements in Safari to retrieve their properties using CSS selectors, enabling browser automation and developer tool access for AI assistants.

Instructions

Inspect a DOM element and get its properties

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sessionIdYesSession identifier
selectorYesCSS selector for the element

Implementation Reference

  • Core tool handler implementation: Finds DOM element by CSS selector using Selenium WebDriver, retrieves tag name, truncated text, attributes (via executeScript), and bounding rectangle.
    async inspectElement(sessionId: string, selector: string): Promise<ElementInspectionResult> {
      const session = this.getSession(sessionId);
      if (!session) {
        throw new Error(`Session ${sessionId} not found`);
      }
    
      try {
        const element = await session.driver.findElement(By.css(selector));
        
        const [tagName, text, attributes, boundingRect] = await Promise.all([
          element.getTagName(),
          element.getText(),
          session.driver.executeScript(`
            const el = arguments[0];
            const attrs = {};
            for (let attr of el.attributes) {
              attrs[attr.name] = attr.value;
            }
            return attrs;
          `, element),
          session.driver.executeScript(`
            const rect = arguments[0].getBoundingClientRect();
            return {
              x: rect.x,
              y: rect.y,
              width: rect.width,
              height: rect.height
            };
          `, element)
        ]);
    
        return {
          tagName,
          text: text.substring(0, 500), // Limit text length
          attributes,
          boundingRect
        };
      } catch (error: unknown) {
        const errorMessage = error instanceof Error ? error.message : String(error);
        throw new Error(`Element inspection failed: ${errorMessage}`);
      }
    }
  • MCP server tool handler wrapper: Delegates to SafariDriverManager.inspectElement and formats the result as JSON text content.
    private async inspectElement(args: Record<string, any>): Promise<Array<{ type: string; text: string }>> {
      const { sessionId, selector } = args;
      
      const elementInfo = await this.driverManager.inspectElement(sessionId, selector);
      
      return [
        {
          type: 'text',
          text: `Element inspection for selector '${selector}':\n\n${JSON.stringify(elementInfo, null, 2)}`
        }
      ];
    }
  • Tool registration in the ListTools response, defining name, description, and input schema (requires sessionId and selector).
    {
      name: 'safari_inspect_element',
      description: 'Inspect a DOM element and get its properties',
      inputSchema: {
        type: 'object',
        properties: {
          sessionId: { type: 'string', description: 'Session identifier' },
          selector: { type: 'string', description: 'CSS selector for the element' }
        },
        required: ['sessionId', 'selector']
      }
    },
  • Input schema definition for the tool: object with sessionId (string) and selector (string), both required.
    inputSchema: {
      type: 'object',
      properties: {
        sessionId: { type: 'string', description: 'Session identifier' },
        selector: { type: 'string', description: 'CSS selector for the element' }
      },
      required: ['sessionId', 'selector']
  • Dispatch case in handleToolCall switch statement routing to the inspectElement handler.
    case 'safari_inspect_element':
      return await this.inspectElement(args);

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/lxman/safari-mcp-server'

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