@frontmcp/react provides two utility functions for reading DOM elements as MCP-compatible resources. These are useful when AI agents need to inspect page content.
readDomById
Reads a DOM element by itsid attribute and returns its HTML, text content, and tag name.
Return Values
readDomBySelector
Reads all elements matching a CSS selector.Return Values
Use Case: AI Agent Page Inspection
Register DOM readers as MCP resource handlers so AI agents can inspect page content:readResource('dom://byId/login-form') to inspect a form, or readResource('dom://selector/.error-message') to check for error states on the page.
When using
readDomBySelector with complex selectors (e.g., div > .my-class), the selector is embedded in the URI path. Use encodeURIComponent when constructing the URI to avoid parsing issues: dom://selector/${encodeURIComponent('div > .my-class')}.