01
Why Traditional AI Agents Fail in Production
Standard LLM agents executing web or infrastructure tasks frequently suffer from two fatal flaws: hallucinated parameter structures when calling tools, and the lack of structured feedback loops when an external web page or API changes. To build reliable systems, we must decouple the reasoning model from the tool execution runtime using standardized protocol boundaries.
02
The Interface Problem
Ad-hoc JSON function calling implementations vary widely across model providers, leading to vendor lock-in and brittle prompt engineering. Furthermore, headless browser automation scripts written dynamically by an LLM often fail due to dynamic DOM mutations, shadow roots, or timing race conditions.
03
Model Context Protocol (MCP) + Structured DOM Extraction
By implementing an MCP server that exposes strongly typed tools (e.g. 'navigate', 'inspect_accessibility_tree', 'click_element_by_role'), the LLM interacts with a deterministic API rather than generating arbitrary code. The MCP server validates every input parameter against a strict schema before invoking Playwright.
mcp-tool-definition.tstypescript
server.setRequestHandler(CallToolRequestSchema, async (request) => {
if (request.params.name === "click_element") {
const { selector, timeoutMs = 5000 } = request.params.arguments as {
selector: string;
timeoutMs?: number;
};
await page.waitForSelector(selector, { timeout: timeoutMs });
await page.click(selector);
return { content: [{ type: "text", text: `Successfully clicked ${selector}` }] };
}
throw new Error("Unknown tool requested");
});04
Field Observations
1. Prefer accessibility trees over raw HTML: Passing an accessibility tree snapshot to the model reduces token usage by 85% and significantly improves tool accuracy.
2. Always enforce hard timeouts: External websites hang and network sockets stall; every Playwright action must have an unyielding timeout boundary.
3. Fail gracefully: When a step fails, return structured diagnostic context to the model so it can formulate an alternative path rather than crashing the workflow.