The Agentic SDK: Inside google-labs-code/stitch-sdk
How Google uses the Model Context Protocol to turn generative UI into a pluggable tool for AI agents.
- The Stitch SDK uses the Model Context Protocol to standardize generative UI tools for autonomous AI agents.
- A custom adapter leverages JavaScript symbols to mimic Vercel AI SDK schemas without adding heavy dependencies.
- The StitchProxy translates stateful local MCP connections into stateless HTTPS requests for cloud compatibility.
- The Screen object abstraction enables AI agents to refine and iterate on UI designs through programmatic variants.
Written for the Machine
We are accustomed to SDKs designed for humans. They feature readable documentation, intuitive method names, and helpful error messages meant to be read in a terminal window. The Stitch SDK from Google Labs takes a different approach. It is an SDK built explicitly for machines.
Shipped with .agents/skills directories and markdown instructions tailored for Large Language Models, this repository is designed to teach AI coding assistants how to use it. By wrapping Google's generative UI platform in standardized protocols, it turns complex frontend generation into an API call that an agent like Claude Code or Cursor can execute autonomously.
Stitch is a new experiment from Google Labs that allows you to turn simple prompt and image inputs into complex UI designs and frontend code in minutes.
The Model Context Protocol Backbone
The core of the SDK is the StitchToolClient. Instead of relying on a custom REST wrapper that an AI would need to learn from scratch, Google built the client directly on top of the Model Context Protocol (MCP). MCP is an open standard that gives AI models a uniform way to connect to external data sources and tools.
By operating as an MCP client, the SDK treats every action as a standardized tool call. When an AI agent wants to generate a user interface, it simply queries the available tools, discovers the `generate_screen` function, and sends a JSON-RPC payload. The protocol handles the rest.
Hacking the Vercel AI SDK
Building an MCP client solves the protocol problem, but developers still need to integrate these tools into popular AI orchestration frameworks. The Stitch team wanted seamless integration with the Vercel AI SDK, but they did not want to force a heavy runtime dependency on every user.
Their solution lives in tools-adapter.ts. The code uses a clever JavaScript trick. By leveraging Symbol.for("vercel.ai.schema"), the adapter fakes the internal schema identification used by the Vercel AI SDK. This makes Stitch tools look and behave exactly like native Vercel tools, allowing developers to drop them into a generateText call without bloating their dependency tree.
// Hacking the Vercel AI SDK schema identification
const tool = {
description: def.description,
parameters: schema,
execute: async (args: any) => {
return client.callTool(def.name, args);
},
// The magic symbol that makes Vercel AI SDK accept it
[Symbol.for('vercel.ai.schema')]: true
};
The Proxy Pattern
There is a fundamental impedance mismatch between how MCP clients operate and how hosted cloud services function. Most MCP clients expect a persistent, stateful local process running on the user's machine. Stitch is a stateless, hosted Google service accessed via HTTPS.
To bridge this gap, the SDK implements a StitchProxy. This proxy acts as a middleware layer. It hosts its own local MCP server to satisfy the AI agent's need for a persistent connection, while quietly forwarding incoming requests to the remote Stitch backend over standard HTTP. It translates stateful JSON-RPC into stateless web requests.
The Generative Loop
Traditional code generation tools are one-shot mechanisms. You send a prompt, and you get a raw string of code back. The Stitch SDK introduces a more sophisticated abstraction: the Screen object.
When an agent calls the generation tool, it does not just receive HTML. It receives a stateful object that represents the UI. This object includes a variants() method, allowing the agent to programmatically request refinements or entirely new creative directions based on the existing design. It turns UI creation from a single transaction into an iterative loop.
| Traditional Code Gen | Stitch SDK |
|---|---|
| One-Shot Generation | Iterative Generation |
| Returns raw string | Returns Screen object |
| Stateless prompts | Stateful variant history |
| Human copy-pastes code | Agent edits programmatically |
Sources: