Stitch SDK

repository·main·Indexed 23 days ago

https://github.com/google-labs-code/stitch-sdk

A toolset for programmatically generating UI screens from text prompts and managing them through projects. The SDK supports direct programmatic use, integration with AI agent frameworks like Vercel AI SDK and Google ADK, and provides a Stitch MCP Proxy container for use with Claude Code, Cursor, and VS Code.

Tokens
9.5K
Snippets
29
Records
62
Agent score
82%

What's inside @google/stitch-sdk

  1. Use the Tool Client for Agentic workflows

    main

    For agents or orchestration scripts requiring direct MCP tool access, use the stitch object or instantiate StitchToolClient for explicit configuration (like custom API keys or base URLs). The client auto-connects on the first tool call.

    import { StitchToolClient } from "@google/stitch-sdk";
    
    const client = new StitchToolClient({ apiKey: "your-api-key" });
    const result = await client.callTool("create_project", {
      title: "Agent Project",
    });
    await client.close();
  2. Configure the Stitch SDK with explicit options

    main

    If you need to provide credentials manually or override default settings, you can instantiate StitchToolClient and pass it to the Stitch constructor.

    Authentication requires either an apiKey or both an accessToken and projectId.

    import { Stitch, StitchToolClient } from "@google/stitch-sdk";
    
    const client = new StitchToolClient({
      apiKey: "your-api-key",
      baseUrl: "https://stitch.googleapis.com/mcp",
      timeout: 300_000,
    });
    
    const sdk = new Stitch(client);
    const projects = await sdk.projects();
  3. Integrate with Google ADK

    main

    To use Stitch with the Google Agent Development Kit (ADK) for TypeScript, use stitchAdkTools() to get tools formatted as FunctionTools. These can then be passed to an LlmAgent.

    import { stitchAdkTools } from "@google/stitch-sdk/adk";
    import { LlmAgent } from "@google/adk";
    
    // Get all Stitch tools formatted as ADK FunctionTools
    const tools = stitchAdkTools();
    
    // Or filter specific tools:
    // const tools = stitchAdkTools({
    //   include: ["create_project", "generate_screen_from_text", "get_screen"],
    // });
    
    const designerAgent = new LlmAgent({
      name: "Designer",
      model: "gemini-2.5-pro",
      instruction: "You are a designer. Create a project and generate a screen.",
      tools,
    });
  4. Create the Stitch API key secret

    main

    To ensure security, the Stitch API key is injected via a container secret rather than environment variables or command-line arguments. Use the following commands to create the stitch-api-key secret in your container engine.

    # Podman
    printf '%s' "your-stitch-api-key" | podman secret create stitch-api-key -
    
    # Docker
    printf '%s' "your-stitch-api-key" | docker secret create stitch-api-key -
  5. Quick Start: Generate a screen from a prompt

    main

    To generate a UI screen, reference a project by ID and use the generate method with a text prompt. Ensure STITCH_API_KEY is set in your environment. You can then retrieve the screen's HTML and image via download URLs.

    import { stitch } from "@google/stitch-sdk";
    
    // STITCH_API_KEY must be set in the environment
    const project = stitch.project("your-project-id");
    const screen = await project.generate(
      "A login page with email and password fields",
    );
    const html = await screen.getHtml();
    const imageUrl = await screen.getImage();
  6. Integrate with Vercel AI SDK

    main

    You can pass Stitch tools directly into the Vercel AI SDK using stitchTools(). This allows LLMs to autonomously create projects, generate screens, and retrieve screen data. You can filter the available tools using the include option.

    import { generateText, stepCountIs } from "ai";
    import { google } from "@ai-sdk/google";
    import { stitchTools } from "@google/stitch-sdk/ai";
    
    const { text, steps } = await generateText({
      model: google("gemini-2.5-flash"),
      tools: stitchTools(),
      prompt: "Create a project and generate a modern dashboard with a stat card",
      stopWhen: stepCountIs(5),
    });
    
    // To filter tools:
    // const tools = stitchTools({
    //   include: ["create_project", "generate_screen_from_text", "get_screen"],
    // });
  7. Integrate Stitch with Vercel AI SDK

    main

    The stitchTools() function returns all Stitch MCP tools as Vercel AI SDK Tool objects. You can pass these directly into generateText() or streamText() to allow a model to use Stitch tools autonomously.

    Options:

    • apiKey: string (Default: STITCH_API_KEY) - Override the environment variable.
    • include: string[] (Default: all tools) - Only expose specific tool names.
    import { generateText, stepCountIs } from "ai";
    import { stitchTools } from "@google/stitch-sdk/ai";
    
    const { text } = await generateText({
      model: yourModel,
      tools: stitchTools(),
      prompt: "Create a login page",
      stopWhen: stepCountIs(5),
    });
  8. Configure Stitch via environment variables

    main

    The SDK can be configured using the following environment variables:

    VariableRequiredDescription
    STITCH_API_KEYYes (or use OAuth)API key for authentication
    STITCH_ACCESS_TOKENNoOAuth access token (alternative to API key)
    GOOGLE_CLOUD_PROJECTWith OAuthGoogle Cloud project ID
    STITCH_HOSTNoOverride the MCP server URL