Skip to content
create-agent-with-sanity-context logo

Build an Agent with Sanity Context

create-agent-with-sanity-context

Build AI agents with structured access to Sanity content via Context MCP. Covers Studio setup, agent implementation, and advanced patterns like client-side tools and custom rendering.

SKILL.md

Full skill instructions

Build an Agent with Sanity Context

Give AI agents intelligent access to your Sanity content. Unlike embedding-only approaches, Context MCP is schema-aware—agents can reason over your content structure, query with real field values, follow references, and combine structural filters with semantic search.

What this enables:

  • Agents understand the relationships between your content types
  • Queries use actual schema fields, not just text similarity
  • Results respect your content model (categories, tags, references)
  • Semantic search is available when needed, layered on structure

Note: Context MCP understands your schema structure but not your domain. You'll provide domain context (what your content is for, how to use it) through the agent's system prompt.

What You'll Need

Before starting, gather these credentials:

CredentialWhere to get it
Sanity Project IDYour sanity.config.ts or sanity.io/​manage
Dataset nameUsually production — check your sanity.config.ts
Sanity API read tokenCreate at sanity.io/​manage → Project → API → Tokens. See HTTP Auth docs
LLM API keyFrom your LLM provider (Anthropic, OpenAI, etc.) — any provider works

How Context MCP Works

An MCP server that gives AI agents structured access to Sanity content. The core integration pattern:

  1. MCP Connection: HTTP transport to the Context MCP URL
  2. Authentication: Bearer token using Sanity API read token
  3. Tool Discovery: Get available tools from MCP client, pass to LLM
  4. System Prompt: Domain-specific instructions that shape agent behavior

MCP URL formats:

  • https://api.sanity.io/:apiVersion/​agent-context/:projectId/:dataset — Access all content in the dataset
  • https://api.sanity.io/:apiVersion/​agent-context/:projectId/:dataset/:slug — Access filtered content (requires agent context document with that slug)

The slug-based URL uses the GROQ filter defined in your agent context document to scope what content the agent can access. Use this for production agents that should only see specific content types.

The integration is simple: Connect to the MCP URL, get tools, use them. The reference implementation shows one way to do this—adapt to your stack and LLM provider.

Available MCP Tools

ToolPurpose
initial_contextGet compressed schema overview (types, fields, document counts)
groq_queryExecute GROQ queries with optional semantic search
schema_explorerGet detailed schema for a specific document type

For development and debugging: The general Sanity MCP provides broader access to your Sanity project (schema deployment, document management, etc.). Useful during development but not intended for customer-facing applications.

Before You Start: Understand the User's Situation

A complete integration has three distinct components that may live in different places:

ComponentWhat it isExamples
1. Studio SetupConfigure the context plugin and create agent context documentsSanity Studio (separate repo or embedded)
2. Agent ImplementationCode that connects to Context MCP and handles LLM interactionsNext.js API route, Express server, Python service, or any MCP-compatible client
3. Frontend (Optional)UI for users to interact with the agentChat widget, search interface, CLI—or none for backend services

Studio setup and agent implementation are required. Frontend is optional—many agents run as backend services or integrate into existing UIs.

Ask the user which part they need help with:

  • Components in different repos (most common): You may only have access to one component. Complete what you can, then tell the user what steps remain for the other repos.
  • Co-located components: All three in the same project—work through them one at a time (Studio → Agent → Frontend).
  • Already on step 2 or 3: If you can't find a Studio in the codebase, ask the user if Studio setup is complete.

Also understand:

  1. Their stack: What framework/​runtime? (Next.js, Remix, Node server, Python, etc.)
  2. Their AI library: Vercel AI SDK, LangChain, direct API calls, etc.
  3. Their domain: What will the agent help with? (Shopping, docs, support, search, etc.)

The reference patterns use Next.js + Vercel AI SDK, but adapt to whatever the user is working with.

Workflow

Quick Validation (Optional)

Before building an agent, you can validate MCP access directly using the base URL (no slug required):

curl -X POST https://api.sanity.io/​YOUR_API_VERSION/​agent-context/​YOUR_PROJECT_ID/​YOUR_DATASET \
  -H "Authorization: Bearer $SANITY_API_READ_TOKEN" \
  -H "Content-Type: application/​json" \
  -d '{"jsonrpc": "2.0", "method": "tools/​list", "id": 1}'

This confirms your token works and the MCP endpoint is reachable. The base URL gives access to all content—useful for testing before setting up content filters via agent context documents.

Step 1: Set up Sanity Studio

Configure the context plugin and create agent context documents to scope what content the agent can access.

See references/​studio-setup.md

Step 2: Build the Agent (Adapt to user's stack)

Already have an agent or MCP client? You just need to connect it to your Context MCP URL with a Bearer token. The tools will appear automatically.

Building from scratch? The reference implementation uses Next.js + Vercel AI SDK with Anthropic, but the pattern works with any LLM provider (OpenAI, local models, etc.). It's comprehensive—covering everything from basic chat to advanced patterns. Start with the basics and add advanced patterns as needed.

See references/​nextjs-agent.md

The reference covers:

  • Core setup (required): MCP connection, authentication, basic chat route
  • System prompts (required): Domain-specific instructions for your agent
  • Frontend (optional): React chat component
  • Advanced patterns (optional): Client-side tools, auto-continuation, custom rendering

GROQ with Semantic Search

Context MCP supports text::embedding() for semantic ranking:

*[_type == "article" && category == "guides"]
  | score(text::embedding("getting started tutorial"))
  | order(_score desc)
  { _id, title, summary }[0...10]

Always use order(_score desc) when using score() to get best matches first.

Adapting to Different Stacks

The MCP connection pattern is framework and LLM-agnostic. Whether Next.js, Remix, Express, or Python FastAPI—the HTTP transport works the same. Any LLM provider that supports tool calling will work.

See references/​nextjs-agent.md for:

  • Framework-specific route patterns (Express, Remix, Python)
  • AI library integrations (LangChain, direct API calls)
  • System prompt examples for different domains (e-commerce, docs, support)

Best Practices

  • Start simple: Build the basic integration first, then add advanced patterns as needed
  • Schema design: Use descriptive field names—agents rely on schema understanding
  • GROQ queries: Always include _id in projections so agents can reference documents
  • Content filters: Start broad, then narrow based on what the agent actually needs
  • System prompts: Be explicit about forbidden behaviors and formatting rules

Troubleshooting

Context MCP returns errors or no schema

Context MCP requires your schema to be available server-side. This happens automatically when your Studio runs, but if it's not working:

  1. Check Studio version: Ensure you're on Sanity Studio v5.1.0 or later
  2. Open your Studio: Simply opening the Studio in a browser triggers schema deployment
  3. Verify deployment: After opening Studio, retry the MCP connection

Other common issues

See references/​nextjs-agent.md for:

  • Token authentication errors
  • Empty results / no documents found
  • Tools not appearing

More skills from diegosouzapw

accessibility logo
diegosouzapw/awesome-omni-skill

accessibility

Accessibility guidelines for VS Code features — covers accessibility help dialogs, accessible views, verbosity settings, accessibility signals, ARIA alerts/status announcements, keyboard navigation, and ARIA labels/roles. Applies to both new interactive UI surfaces and updates to existing feature...

62 1
View
mermaid-diagrams logo
diegosouzapw/awesome-omni-skill

mermaid-diagrams

Mermaid diagram creation for flowcharts, sequences, ERDs, and more. Generate diagrams from text in markdown files. Use for documentation, architecture diagrams, and visual representations. Triggers on mermaid, flowchart, sequence diagram, ERD, entity relationship, gantt chart, pie chart, class di...

62 1
View
tauri2-react-rust logo
diegosouzapw/awesome-omni-skill

tauri2-react-rust

Guides development of cross-platform desktop apps with Tauri 2, TypeScript, React, and Rust. Use when building Tauri apps, implementing IPC, designing Rust backend or TypeScript/React frontend, when researching or cloning a website (open site, snapshot elements), when verifying local dev or built...

62 1
View
fp-ts-errors logo
diegosouzapw/awesome-omni-skill

fp-ts-errors

Handle errors as values using fp-ts Either and TaskEither for cleaner, more predictable TypeScript code. Use when implementing error handling patterns with fp-ts.

62 0
View
ring:pre-dev-research logo
diegosouzapw/awesome-omni-skill

ring:pre-dev-research

Gate 0 research phase for pre-dev workflow. Dispatches 3 parallel research agents to gather codebase patterns, external best practices, and framework documentation BEFORE creating PRD/TRD. Outputs research.md with file:line references.

62 0
View
create-assistant logo
diegosouzapw/awesome-omni-skill

create-assistant

Create and configure Vapi voice AI assistants with models, voices, transcribers, tools, hooks, and advanced settings. Use when building voice agents, phone bots, customer support assistants, or any conversational AI that handles phone or web calls.

62 0
View
codex-headless logo
diegosouzapw/awesome-omni-skill

codex-headless

Delegiere Aufgaben an OpenAI Codex CLI im Headless-Mode. Nutzt ChatGPT Subscription (KEIN API Key). Nur Workspace-Zugriff, KEIN System-Zugriff. Auto-Accept mit --full-auto. Web-Recherche mit --search.

62 0
View
social-media-scheduler logo
diegosouzapw/awesome-omni-skill

social-media-scheduler

Generate a full week of social media content for any topic. Outputs platform-optimized posts for Twitter/X, LinkedIn, and Instagram with hashtags and posting times.

62 0
View
Data Privacy Compliance logo
diegosouzapw/awesome-omni-skill

Data Privacy Compliance

Data privacy and regulatory compliance specialist for GDPR, CCPA, HIPAA, and international data protection laws. Use when implementing privacy controls, conducting data protection impact assessments, ensuring regulatory compliance, or managing data subject rights. Expert in consent management, da...

62 0
View
triple-review logo
diegosouzapw/awesome-omni-skill

triple-review

Reviews a task using the TRIPLE protocol, as the review agent.

62 0
View
Tech Stack & Architecture Decision logo
diegosouzapw/awesome-omni-skill

Tech Stack & Architecture Decision

Define and document the technology stack and architecture decisions for a project. Use when the user needs to choose a tech stack, make architecture decisions, define infrastructure choices, or document technology selections. Triggers on requests like "define the tech stack", "choose technologies...

62 0
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View