sanity-best-practices: An Agent Skill Guide for Full-Stack Development with Sanity

Introduction

When building content sites with Headless CMS, common challenges often lie not in “whether the API can be connected,” but in how to model schemas reliably, write stable GROQ queries, align TypeGen with frontend types, and determine which documentation to follow for Visual Editing and framework integration. When allowing AI to modify Sanity code directly, it tends to rely on outdated examples, fabricate document _ids, or stuff production-grade videos into file fields—all of which can lead to unexpected production bills or high maintenance costs.

Below is the sanity-best-practices Skill maintained by Sanity within the Sanity Agent Toolkit. It organizes conventions and topic-specific reference documents related to workflows such as schema, GROQ, TypeGen, Visual Editing, and framework integration into instructions that Agents can load on a task-by-task basis, thereby reducing the aforementioned deviations.

What is this?

sanity-best-practices is an Agent Skill maintained by the Sanity team, designed for schema design, GROQ queries, TypeGen, Visual Editing, images and Portable Text, Studio structure, localization, migrations, Sanity Functions, webhooks, Blueprints, and integration scenarios involving Next.js, Nuxt, Astro, Remix, SvelteKit, Angular, Hydrogen, App SDK, and Dashboard-embedded Studios (defineApplication).

The Skill itself is located at the repository path skills/sanity-best-practices. Unlike MCP-directly connected projects or bundled installations of other Skills or plugins, it focuses on “reading guides by task + comparing correct vs. incorrect examples.” It is suitable for allowing Agents to align with official practices when writing schemas, checking GROQ, setting up previews, or reviewing Sanity codebases.

The official documentation specifies that this Skill should be cited preferentially when keywords such as defineType / defineField, defineQuery, content modeling, Presentation / previews, frontend integration, documentEventHandler, defineDocumentFunction, @sanity/functions, @sanity/blueprints, or sanity.blueprint.ts are involved.

Core Features

When to Apply

The documentation lists typical scenarios including:

  • Setting up new Sanity projects or team onboarding
  • Integration with frontend frameworks (Next.js, Nuxt, Astro, Remix, SvelteKit, Hydrogen, etc.)
  • Writing or optimizing GROQ
  • Designing content schemas
  • Implementing Visual Editing and live preview
  • Images, Portable Text, and page builders
  • Configuring Studio structure, TypeGen, and localization
  • Migrating content from other systems
  • Building custom applications with the Sanity App SDK
  • Developing or deploying Studios / apps within the Sanity Dashboard
  • Managing infrastructure with Blueprints
  • Content automation via Sanity Functions or webhooks

Global Rules

Before covering cross-topic subjects, the Skill emphasizes several hard conventions, such as:

  1. Let Sanity generate _ids for regular documents; do not create deterministic UUIDs, IDs derived from slugs, or legacy system IDs yourself.
  2. Model relationships using reference, then resolve associated documents via GROQ lookups, source-key fields, or the _id returned by the create interface.
  3. Explicit document IDs should primarily be used for singletons controlled by Studio Structure (including localized singletons, such as homePage-en).

Video and Assets

Do not use Sanity file assets for large-scale video playback in production: file delivers raw downloads without transcoding or adaptive streaming, and traffic can easily lead to high bandwidth bills. For Enterprise users with the video add-on enabled, the documentation recommends using the Sanity Media Library (transcoded and adaptive streaming via Mux), using defineVideoField() from sanity/media-library for fields, and @mux/mux-player-react with the asset’s playback ID for playback. Other options include the Studio-side sanity-plugin-mux-input, or storing only embed URLs for YouTube / Vimeo. While small files for short previews can be placed in file fields, large-scale user-facing video must go through the Media Library or a dedicated streaming service.

Quick Reference: Integrations and Topics

The Skill adopts a design of “loading only 1-2 reference files relevant to the current task.” Integration-focused guides include: get-started, nextjs, nuxt, angular, astro, remix, svelte, hydrogen, project-structure, app-sdk, dashboard, blueprints, functions.

Topic-focused guides include: groq, schema, visual-editing, page-builder, portable-text, image, studio-structure, typegen, seo, localization, migration, migration-html-import.

Each reference file (e.g., references/groq.md, references/schema.md, references/nextjs.md) typically contains: comprehensive explanations of the topic, incorrect vs. correct code comparisons, decision matrices and workflow descriptions, as well as framework-specific patterns.

The Toolkit overview also mentions: the complete set of Agent resources includes approximately 25 integration/topic guides and 26 focused best practice rules (subject to the current repository description).

Installation and Activation

This Skill belongs to sanity-io/agent-toolkit. The installation method depends on whether you also require MCP or plugins.

Install Agent Skills Only (Including sanity-best-practices)

Suitable for clients compatible with Agent Skills such as Cursor and Claude Code:

npx skills add sanity-io/agent-toolkit

This installs the collection of Skills within the toolkit; sanity-best-practices is one of them. You can use npx skills list to view installed items and npx skills update to upgrade.

Relationship with MCP and Plugins (Optional)

If you want the Agent to directly access Sanity project content, datasets, schemas, etc., the official recommendation is MCP rather than relying solely on local Skill files:

npx sanity@latest mcp configure

This command detects and configures MCP for Cursor, Claude Code, VS Code, etc.; authentication can use the logged-in Sanity CLI user.

Cursor users can also manually add the HTTP service in MCP settings:

{
  "mcpServers": {
    "Sanity": {
      "type": "http",
      "url": "https://mcp.sanity.io"
    }
  }
}

Codex example:

codex mcp add Sanity --url https://mcp.sanity.io

Claude Code users can install the Sanity plugin (including MCP, skills, commands) via the official plugin marketplace, for example:

/plugin install sanity@claude-plugins-official

Cursor users can also install similar capabilities via one click from the Sanity plugin on the Cursor Marketplace. Skills and MCP can coexist: MCP handles real-time project status and rule synchronization, while Skills handle loading long-form best practices by topic.

Manual Copy (Offline)

Official documentation supports copying skill reference files locally for offline use, but you will need to track repository updates yourself.

Typical Usage

The official recommended workflow can be summarized in three steps:

  1. Choose the right entry point: Start with the framework or topic guide that best matches the request (e.g., if only working on Next.js App Router + Live Content API, read nextjs first; if only optimizing queries, read groq first).
  2. Expand as needed: Read a second reference only when the task spans domains (e.g., after schema changes requiring TypeGen updates, open typegen).
  3. Modify code based on examples: Implement changes in the corresponding references/*.md according to incorrect/correct examples and decision matrices, rather than loading all 20+ files at once.

When submitting requests to an Agent, try to include specific technical points to help trigger the scope defined in the Skill description, for example:

  • “Check this defineField and reference usage against sanity-best-practices.”
  • “Configure Presentation / live preview based on the nextjs reference in the Skill.”
  • “Migrate HTML to Portable Text using the @portabletext/block-tools workflow from migration-html-import.”

Scenarios and Notes

Who it is for: Full-stack/frontend teams already using or planning to use Sanity; developers using Agents like Cursor, Claude Code, or Codex to write schemas, GROQ, framework integrations, or review PRs.

Advantages: Maintained officially by Sanity; covers topics ranging from modeling to SEO, localization, Functions, and Blueprints; originates from the same source as the MCP and plugins in the Agent Toolkit, facilitating coordination with real-time project status.

Notes:

  • Skills are instructions and reference documents; they do not replace your actual schema and dataset in your Sanity project. Sensitive operations should still go through normal permissions and CI.
  • There are clear “do not do” items regarding video, _id, references, etc. If the Agent hasn’t loaded this Skill, it is prone to pitfalls; for complex tasks, explicitly request adherence to sanity-best-practices.
  • When installing only the Skill, content updates depend on npx skills update or re-running add; MCP-side rules can be kept relatively up-to-date by the server. The two can be combined as needed.

Conclusion

sanity-best-practices consolidates conventions scattered across multiple documents in Sanity development into an Agent Skill that can be loaded by task: from schema and GROQ to TypeGen, Visual Editing, and mainstream framework integrations, accompanied by global rules (such as document IDs and video assets) to reduce common mistakes.

Official Skill Directory:

https://github.com/sanity-io/agent-toolkit/tree/main/skills/sanity-best-practices

Full Toolkit Installation and MCP Instructions:

https://github.com/sanity-io/agent-toolkit

羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

Xiaoye