Skip to content
Skip to main contentAgent-Native Hackathon winners revealed live Nov 13.RSVP
CONTACT SALESSTART BUILDING

Connect to Sanity

Builder Code

Builder Code can read and write to your Sanity content lake through Sanity's hosted MCP server. Once connected, you can prompt Builder Code to model schemas, query documents with GROQ, render them in your app, and update them within the chat while editors keep working in Sanity Studio.

This guide walks you through connecting the Sanity MCP server to your Builder Code workspace, the patterns that work well for app and site building, and the common pitfalls teams run into.

When to use Builder Code with Sanity

GoalRecommended approach

Build a marketing site whose content editors already work in Sanity Studio

Connect Sanity through MCP, then prompt Builder Code to read from existing document types.

Spin up a new app that needs structured content (blog, docs, product catalog)

Connect Sanity through MCP, then prompt Builder Code to design schemas and seed documents.

Mix Sanity (content) with another backend (e.g. Stripe, Supabase)

Connect multiple MCP servers — Builder Code can call across them in a single prompt.

Render content in a Builder Code-built React/Next.js app with live preview

Use the Sanity MCP server for reads, plus Sanity's @sanity/client SDK with GROQ in the generated code.

Separate prod and staging content cleanly

Use datasets, for example, production, staging. Builder Code can target either per-conversation.

One-off prototype with no real CMS

Skip MCP — let Builder Code generate static content. Add Sanity later when you need editorial control.

You do not need to be a Sanity developer to use this. Builder Code handles the client wiring, GROQ queries, environment variables, and schema definitions once it knows your project exists.

Set up Sanity

Prerequisites

Before connecting, make sure you have:

  • A Builder Code workspace on a plan that includes MCP integrations. The built-in Sanity integration is available on all paid Builder Code plans.
  • Permission to add connectors in Builder. Enabling an integration requires an Admin role or a role with permission to modify MCP servers.
  • A Sanity account with at least one project, and permission to authorize third-party apps against your organization. The OAuth flow uses your existing Sanity login - no API tokens are needed to connect Builder Code to Sanity.
  • (Optional) A schema already defined in your Sanity Studio. If you don't have one, Builder Code can design and deploy document types for you through the MCP server during the build.

What about Sanity API tokens?

There are two separate access paths:

PathWhat it's forWhat you need

Builder Code → Sanity (build time)

Lets Builder Code read your schemas, query documents, and create/update content while you're prompting

OAuth only - handled by the Connect button below. No API tokens. (Sessions expire after about 7 days; reconnect when prompted.)

Your generated app → Sanity (runtime)

Lets the app Builder Code built actually fetch content when users visit it

Sanity API tokens - generated per-project in sanity.io/manage → API → Tokens

When Builder Code writes the runtime fetch code, it'll set up the environment variables and tell you which tokens to paste in. The tokens you may need:

  • A Viewer token — for reading published content from the dataset. Required for nearly every app.
  • A Viewer token with "drafts" permission (or use a public dataset) — for reading draft documents for preview mode. Sanity stores drafts as separate documents with a drafts. ID prefix, so reading them requires a token that can see them.
  • An Editor token — for writing content from the running app. Most apps don't need this; Builder Code uses its own MCP-authorized access for writes during the build.

You can create these now at sanity.io/manage → your project → API → Tokens, or wait until Builder Code prompts you for them — either works.

If you don't have the Integrations tab in your Builder Code workspace, your plan may not include it. Contact your Builder account team.

Integrate Sanity

Builder Code ships with Sanity as a built-in MCP server, so there are no URLs, tokens, or scopes to configure manually. Setup has two parts: a Space admin enables Sanity on the Integrations page, and then each person authorizes their own Sanity account in a Project.

  1. Go to Integrations.
  2. Find the Sanity card and click Enable. A message confirms that Sanity is enabled for your Space.
  3. The card now shows Connected.

Sanity requests these permissions when you authorize it in a Project:

  • Read and write Sanity content
  • Manage Sanity schemas and documents
  • Access Sanity project data

Builder Code connects to Sanity's MCP server at https://mcp.sanity.io.

Heads up: 7-day sessions. Sanity's OAuth sessions expire after roughly a week. If Builder Code suddenly can't reach Sanity, open Integrations in a Project and click Reconnect.

Connect within a Project

To use Sanity in a Project, each person authorizes their own Sanity account. To connect your account:

  1. Go to a Project.
  2. Click the cog icon at the bottom of the prompt window.
  3. Click Integrations.
  4. Next to Sanity, click Connect.
  5. Sign in to Sanity, if needed, and approve the permissions.

Your account is now connected. Use the toggle next to Sanity to turn it on or off for your Projects. If the connection expires, the button changes to Reconnect. Click it to authorize again.

Choosing a default project and dataset

Sanity organizes content into organizations, projects, and datasets. A dataset is an isolated content repository — most teams use them as environments, for example, production and staging, but they can also separate tenants, regions, or experimental content. Most teams want Builder Code to default to one project and a dataset, so Builder Code doesn't have to ask every turn.

  1. Click Manage on the connected Sanity card.
  2. Pick a Default project and Default dataset from the dropdowns. Both are populated from the projects your Sanity user can access.
  3. Save.

You can override these per conversation by telling Builder Code which dataset to use. For example:

Example prompt

Use the staging dataset instead.

Builder Code passes the dataset through to subsequent MCP calls in that chat.

Disconnecting or re-authorizing

You have three options:

  • Disable for your Space: on the Integrations page, click the settings icon on the Sanity card and click Disable. This removes the integration for everyone in your Space. Your Sanity data is untouched; Builder Code loses access, but nothing in Sanity changes.
  • Reconnect your account: in a Project, open Integrations and click Reconnect next to Sanity. Use this if your session has expired or you need to switch to a different Sanity account.
  • Disconnect your account: in a Project, turn off the toggle next to Sanity.

Disconnecting does not modify any code Builder Code has already written. If your app uses @sanity/client with an API token, that keeps working - the MCP connection is for Builder Code's own access during builds.

Use Sanity

What Sanity MCP gives Builder Code

Once connected, Builder Code can call the Sanity MCP server's tools on its own when your prompt needs them. The most useful capabilities, grouped by category:

CapabilityWhat Builder Code can do

Inspect projects & datasets

List organizations, projects, datasets, and deployed schemas. Use this to ground generated UI in real schemas.

Read documents (GROQ)

Execute GROQ queries against published or draft documents, including referenced documents and asset URLs.

Create documents

Seed example content during a build (from JSON or Markdown), or scaffold editorial flows.

Patch documents

Apply field-level patches. For published documents, the MCP creates or updates the corresponding drafts document - the published version stays put until explicitly republished.

Publish, unpublish, discard drafts

Move documents between draft and published states.

Deploy schemas & Studio

Define new document types and deploy them. Builder Code can even deploy a managed Sanity Studio if you don't already have one.

Manage releases

Create releases for coordinated, scheduled publishing across multiple documents (Sanity's batched-publish primitive).

Manage assets & AI media

Generate or transform images directly into asset fields (note: these consume Sanity AI credits).

Semantic search & docs

Search Sanity documentation, run semantic search over embeddings indices, and surface migration guides.

CORS & project config

Add CORS origins so the app Builder Code builds can call Sanity from the browser without errors.

Builder Code decides which of these to call based on your prompt. You don't invoke them directly. Instead, you describe the outcome. For example:

Example prompt

Show the three most recent blog posts on the homepage.

In this case, Builder Code writes the GROQ, generates the rendering code, and wires @sanity/client.

About AI credits

A handful of tools, such as create_documents_from_markdown, patch_document_from_markdown, generate_image, transform_image, and create_version (when given an instruction), invoke Sanity's AI inference and consume credits from your Sanity plan.

Builder Code uses them when they're the natural fit, for example, ingesting a long Markdown article. If you want to avoid using the credits, send a prompt like this:

Example prompt

Don't use the Markdown ingestion tools — use the JSON ones instead.

Common patterns

1. Build a site against an existing Sanity project

Best for marketing teams that already have content modeled in Sanity Studio and want a new site or landing page experience without rebuilding the CMS.

Example prompt

Connect to my Sanity project and build a marketing homepage. Use the landingPage document type for hero + features, and the caseStudy document type for the testimonial section. Make it responsive.

What Builder Code does behind the scenes:

  • Calls list_workspace_schemas and get_schema to confirm landingPage and caseStudy exist and learn their fields
  • Writes a GROQ query and calls query_documents to pull a few documents as live sample content while you iterate visually
  • Generates a Next.js (or your chosen framework) page that uses @sanity/client at runtime, with environment variables it prompts you to fill in.

For efficiency, tell Builder Code the exact document type names. If you say "use my pages document type" and you have both page and landingPage, Builder Code has to ask you for clarification.

2. Bootstrap schema + Studio + app together

Best when you're starting from a blank Sanity project and want Builder Code to design the schema, deploy a Studio, and build the consuming app in one pass.

Example prompt

I'm building a recipes app. Design and deploy a Sanity schema (Recipe with title, slug, hero image, ingredients list, steps, prep time), deploy a Studio so editors can manage them, seed 5 example recipes, and build a Next.js app that lists them on a /recipes page with detail routes.

Builder Code:

  1. Calls deploy_schema to define Recipe with the fields you described.
  2. Calls deploy_studio to provision a managed Sanity Studio bound to that schema (production URLs end in sanity.studio).
  3. Calls create_documents_from_json five times with plausible seed data.
  4. Generates the app routes, render code, and @sanity/client config.

After the build, your editors can open the Studio URL and start editing - they don't see Builder Code at all.

Tip: Review the schema Fusion proposes before letting it deploy. A 10-second confirmation up front is cheaper than reshaping fields later - and unlike untyped CMSes, Sanity's schema is enforced by the client.

3. Coordinated content launches with releases

Best when multiple documents need to go live together, for example, a product launch page, pricing page, and blog post launching on the same day.

Example prompt

Create a release called summer-launch-2026 and add versioned drafts of the homepage hero copy, the pricing page, and a new blog post announcing the launch. Schedule them all to publish on June 1.

Builder Code:

  1. Calls create_release to create the release container.
  2. Calls create_version to attach each document's draft to that release.
  3. Hands off the actual scheduling to you (you'll set the publish time in Sanity Studio, or Builder Code can stage it using the release tools).

Editors can review the entire release together in Studio before it goes out - cleaner than coordinating individual document publishes.

4. Localized content

Sanity doesn't have a built-in locale system the way some headless CMSes do. Localization is typically modeled in the schema, for example, with per-field language variants, or per-document, with a language field.

Example prompt

My Sanity schema uses internationalized strings via the @sanity/document-internationalization plugin - each landingPage has a language field. Build the homepage so it reads the locale from the URL (/fr or /en) and queries the matching landingPage document.

Tell Builder Code which localization pattern your schema uses (field-level, document-level, or a plugin) and it'll write the right GROQ.

5. Mixing Sanity with other MCP integrations

Builder Code can use multiple MCP servers in one conversation. A typical pattern:

  • Sanity - Marketing pages, blog, product copy.
  • Stripe - Pricing data and checkout.
  • Supabase / Neon - User accounts and per-user state.

Connect each on the Integrations page and prompt across them naturally. For example:

Example prompt

Pull pricing tiers from Stripe and the matching feature copy from Sanity's pricingTier document type.

Best practices

  • Be explicit about project and dataset when it matters. Builder Code uses your defaults from Manage, but on the first turn of a new conversation it's worth saying "using the production dataset of the Marketing project" to remove ambiguity.
  • Drafts have a different document ID. Sanity stores drafts as separate documents prefixed with drafts.. When Builder Code patches a published document, it actually creates/updates the drafts. version - the live document doesn't change until you publish. If you don't see your change on the site, check whether it was published.
  • GROQ is the query language, not REST. When Builder Code generates fetch code, it'll use GROQ via @sanity/client. If you're familiar with SQL or GraphQL, GROQ is the third option - Builder Code handles the syntax, but learning the basics pays off when you read the generated code.
  • Don't paste API tokens into the chat. The MCP connection handles Builder Code's access via OAuth. Runtime tokens for the generated app go into your project's environment variables - Builder Code will tell you which ones to set.
  • Document type names are case-sensitive. Schema type names are stable identifiers (e.g. landingPage, not LandingPage or Landing Page). When in doubt, ask Builder Code to "list document types in my default project" and reference them by the name it returns.
  • CORS for browser-side queries. If your generated app calls Sanity from the browser (not just server-side), the dataset needs your app's origin added to its CORS allowlist. Builder Code can do this automatically via the add_cors_origin tool - just tell it the deployed URL.
  • Image transformations are free; AI image generation isn't. Sanity's standard Image API (resizing, cropping, format conversion) is free and Builder Code will use it by default. The AI tools (generate_image, transform_image) consume Sanity AI credits - opt in deliberately.
  • Revoking access in Sanity. If you revoke the OAuth grant directly in Sanity (sanity.io/manage → your account), Builder Code's connection will silently fail on the next call. Reconnect from the Integrations panel in a Project to fix it.
  • Multiple Sanity organizations. OAuth scopes Builder Code to the Sanity account you signed in with. If you have multiple organizations, sign in with the user that has access to the org you need before clicking Connect.
  • Reconnect every ~7 days. Sanity's OAuth sessions don't last forever. If Builder Code suddenly can't see your projects, the session expired - click Reconnect next to Sanity in a Project's Integrations panel.

Common fixes

Either your plan doesn't include MCP integrations, the integration is rolling out, your role doesn't have permission to add connectors, or an admin has hidden the built-in connectors. Check the Integrations page. If other built-in connectors appear but not Sanity, contact Builder Support. If you see no built-in servers at all, your plan likely doesn't include the feature.

The most common cause is a browser blocking third-party cookies for the popup. Try:

  1. Disabling popup blockers for app.builder.io.
  2. Allowing third-party cookies for sanity.io during the flow.
  3. Using a non-incognito window.

If it still fails, copy the URL the popup tried to open and share it with support.

Three things to check:

  1. You're in the right project and dataset (open Manage on the Sanity card to confirm defaults).
  2. The schema type name matches exactly - case sensitive, camelCase by convention.
  3. The schema is deployed. Sanity supports schemas deployed via Studio, via the CLI, or via the MCP server itself - if it lives only on a developer's laptop in an undeployed schemas/ folder, the MCP can't see it.

Ask Builder Code to list the deployed schemas:

Example prompt

List deployed schemas in my default Sanity project.

If it comes back empty, deploy your Studio or have Builder Code call deploy_schema first.

Builder Code uses your default dataset unless you override it mid-conversation. Either change the default under Manage, or include the dataset in your prompt: "…in the staging dataset." If you just wrote to the wrong dataset, Sanity's history view lets you inspect and revert recent changes - but discarded drafts can't be restored.

Sanity separates drafts from published documents. Builder Code's patches go to drafts by default - that's the safe behavior. To publish, either:

  • Ask Builder Code to "publish those changes" (it'll call publish_documents), or
  • Publish from Sanity Studio yourself.

If your site is showing stale content even after publishing, your runtime client is probably caching - check whether you're using @sanity/client with useCdn: true (which caches for ~60 seconds) versus useCdn: false (always fresh, lower rate limit).

Sanity's API rejects cross-origin requests unless your dataset's CORS allowlist includes the origin. Tell Builder Code to add the origin:

Example prompt

Add https://my-app.vercel.app as a CORS origin to my Sanity project.

Builder Code handles the rest. You can also do this manually at Sanity.io Manage → your project → API → CORS Origins.

They will, on next reload of Sanity Studio. If Studio is showing the old schema, force a rebuild - or, if Builder Code deployed a managed Studio for you, give it a minute and refresh. The OAuth user (the person who connected Builder Code) is the recorded author of the deploy, and they need permission to deploy schemas in that project.

Visit Sanity's MCP server documentation. The same OAuth flow works; you just point your client at https://mcp.sanity.io. There's also a npx sanity@latest mcp configure CLI command that auto-configures common editors using your existing Sanity CLI credentials.

What's next

  • Sanity MCP server documentation—the full list of ~48 tools the MCP server exposes, including ones Builder Code can reach for on your behalf.
  • Builder Code integrations for the broader catalog of MCP servers Builder Code supports, plus the custom-server flow for enterprise plans.
  • Sanity's How queries work — GROQ cheat sheet. This is a helpful resource before your first build, so you can read the queries Builder Code generates.
  • Sanity's Content Modeling: What It Is and How to Get Started. Especially useful if your team is new to schema-first CMSes. Sanity enforces structure more strictly than untyped CMSes; well-planned schemas save time later.
Was this article helpful?