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.
Best use case
create-agent-with-sanity-context is best used when you need a repeatable AI agent workflow instead of a one-off prompt.
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.
Teams using create-agent-with-sanity-context should expect a more consistent output, faster repeated execution, less prompt rewriting.
When to use this skill
- You want a reusable workflow that can be run more than once with consistent structure.
When not to use this skill
- You only need a quick one-off answer and do not need a reusable workflow.
- You cannot install or maintain the underlying files, dependencies, or repository context.
Installation
Claude Code / Cursor / Codex
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/create-agent-with-sanity-context/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How create-agent-with-sanity-context Compares
| Feature / Agent | create-agent-with-sanity-context | Standard Approach |
|---|---|---|
| Platform Support | Not specified | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | Unknown | N/A |
Frequently Asked Questions
What does this skill do?
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.
Where can I find the source code?
You can find the source code on GitHub using the link provided at the top of the page.
SKILL.md Source
# 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:
| Credential | Where to get it |
| ------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Sanity Project ID** | Your `sanity.config.ts` or [sanity.io/manage](https://sanity.io/manage) |
| **Dataset name** | Usually `production` — check your `sanity.config.ts` |
| **Sanity API read token** | Create at [sanity.io/manage](https://sanity.io/manage) → Project → API → Tokens. See [HTTP Auth docs](https://www.sanity.io/docs/content-lake/http-auth#k967e449638bc) |
| **LLM API key** | From 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
| Tool | Purpose |
| ----------------- | --------------------------------------------------------------- |
| `initial_context` | Get compressed schema overview (types, fields, document counts) |
| `groq_query` | Execute GROQ queries with optional semantic search |
| `schema_explorer` | Get 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:
| Component | What it is | Examples |
| --------------------------- | --------------------------------------------------------------- | ------------------------------------------------------------------------------- |
| **1. Studio Setup** | Configure the context plugin and create agent context documents | Sanity Studio (separate repo or embedded) |
| **2. Agent Implementation** | Code that connects to Context MCP and handles LLM interactions | Next.js API route, Express server, Python service, or any MCP-compatible client |
| **3. Frontend (Optional)** | UI for users to interact with the agent | Chat 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):
```bash
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](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](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:
```groq
*[_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](references/nextjs-agent.md#adapting-to-other-stacks) 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](references/nextjs-agent.md#troubleshooting) for:
- Token authentication errors
- Empty results / no documents found
- Tools not appearingRelated Skills
kitt-create-slash-commands
Expert guidance for creating slash commands. Use when working with slash commands, creating custom commands, understanding command structure, or learning YAML configuration.
kitt-create-plans
Create hierarchical project plans optimized for solo agentic development. Use when planning projects, phases, or tasks that the AI agent will execute. Produces agent-executable plans with verification criteria, not enterprise documentation. Handles briefs, roadmaps, phase plans, and context handoffs.
extracting-ai-context
Extracts and manages AI context (skills, AGENTS.md) from workflow-kotlin library JARs. Use when setting up AI tooling for a workflow-kotlin project, updating skills after a library version change, or configuring agent-specific directories.
create-workflow
Create Jazz workflow automation files (WORKFLOW.md). Use this for scheduling Jazz agents to run recurring tasks. For OS-level scripts/commands, use create-system-routine.
create-rule
Create persistent AI agent rules and instructions. Use when you want to create a rule, add coding standards, set up project conventions, configure file-specific patterns, or create AGENTS.md/GEMINI.md rule files across Cursor, Gemini CLI, or Codex.
create-prompt
Expert prompt engineering for creating effective prompts for Claude, GPT, and other LLMs. Use when writing system prompts, user prompts, few-shot examples, or optimizing existing prompts for better performance.
create-expert-skill
Create production-ready skills from expert knowledge. Extracts domain expertise and system ontologies, uses scripts for deterministic work, loads knowledge progressively. Use when building skills that must work reliably in production.
create-event-handlers
Sets up RabbitMQ event publishers and consumers following ModuleImplementationGuide.md Section 9. RabbitMQ only (no Azure Service Bus). Creates publishers with DomainEvent (tenantId preferred), consumers with handlers, naming {domain}.{entity}.{action}, required fields (id, type, version, timestamp, tenantId, source, data). Use when adding event-driven communication, async workflows, or integrating via events.
create-custom-prompt
Prompt for creating custom prompt files
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.
create-agents-md
Create or rewrite AGENTS.md files for Open Mercato packages and modules. Use this skill when adding a new package, creating a new module, or when an existing AGENTS.md needs to be created or refactored. Ensures prescriptive tone, MUST rules, checklists, and consistent structure across all agent guidelines.
context-optimizer
Analyzes Copilot Chat debug logs, agent definitions, skills, and instruction files to audit context window utilization. Provides log parsing, turn-cost profiling, redundancy detection, hand-off gap analysis, and optimization recommendations. Use when optimizing agent context efficiency, identifying where to add subagent hand-offs, or reducing token waste across agent systems.