create-agent-with-sanity-context by sanity-io/agent-context
npx skills add https://github.com/sanity-io/agent-context --skill create-agent-with-sanity-context为 AI 智能体提供对您 Sanity 内容的智能访问。与仅嵌入的方法不同,Agent Context 具备模式感知能力——智能体可以基于您的内容结构进行推理,使用真实的字段值进行查询,遵循引用关系,并将结构化过滤器与语义搜索相结合。
这实现了以下功能:
Agent Context 为智能体提供您的模式并教授它们 GROQ,但它无法了解您的特定领域。您可以通过 Instructions 字段(数据集特定的查询指导)以及可选的 系统提示(智能体行为和语气)来弥补这一差距。
此工作流程中的三个参与者:
开始之前,请收集以下凭证:
| 凭证 | 获取位置 |
|---|---|
| Sanity 项目 ID | 您的 sanity.config.ts 或 sanity.io/manage |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
通常是 production —— 请检查您的 sanity.config.ts |
| Sanity API 读取令牌 | 在项目目录中运行 npx sanity tokens add "Agent Context" --role=viewer --yes --json(或传递 --project-id=<id>)。或者,在 sanity.io/manage → 项目 → API → 令牌 中创建,并分配 Viewer 角色。 |
| LLM API 密钥 | 来自您的 LLM 提供商(Anthropic、OpenAI 等)—— 任何提供商都适用 |
一个为 AI 智能体提供结构化访问 Sanity 内容的 MCP 服务器。核心集成模式:
MCP URL 格式:
https://api.sanity.io/v2026-03-03/agent-context/:projectId/:dataset —— 基础 URL。 无需文档,通过查询参数配置或直接使用。https://api.sanity.io/v2026-03-03/agent-context/:projectId/:dataset/:slug —— 文档 URL。 应用来自 Agent Context 文档的配置。Agent Context 文档(类型 sanity.agentContext)在 Sanity Studio 中创建,用于配置 MCP 端点。它们包含三个字段:
| 字段 | 模式字段 | 用途 |
|---|---|---|
| Slug | slug | 唯一的 URL 标识符 —— 成为 MCP URL 中的 :slug |
| Instructions | instructions | 针对智能体的领域特定指导,注入到工具描述中 |
| 内容过滤器 | groqFilter | 一个 GROQ 表达式,限定智能体可以访问哪些文档 |
这意味着 Studio 用户无需接触代码即可管理智能体行为 —— 更新 Instructions 或缩小内容过滤器会立即生效。
URL 查询参数 会覆盖文档的配置(对测试和开发很有用):
?instructions=<内容> —— 覆盖 Instructions(使用 ?instructions="" 表示空白状态)?groqFilter=<表达式> —— 覆盖内容过滤器集成很简单:连接到 MCP URL,获取工具,使用它们。参考实现展示了一种方法 —— 请根据您的技术栈和 LLM 提供商进行调整。
| 工具 | 用途 |
|---|---|
initial_context | 获取压缩的模式概览(类型、字段、文档计数) |
groq_query | 执行 GROQ 查询,支持可选的语义搜索 |
schema_explorer | 获取特定文档类型的详细模式 |
用于开发和调试: 通用的 Sanity MCP 提供对您 Sanity 项目更广泛的访问(模式部署、文档管理等)。在开发过程中很有用,但不适用于面向客户的应用程序。
一个完整的集成包含 三个不同的组件,它们可能位于不同的位置:
| 组件 | 是什么 | 示例 |
|---|---|---|
| 1. Studio 设置 | 配置上下文插件并创建 Agent Context 文档 | Sanity Studio(独立仓库或嵌入式) |
| 2. 智能体实现 | 连接到 Agent Context 并处理 LLM 交互的代码 | Next.js API 路由、Express 服务器、Python 服务,或任何 MCP 兼容的客户端 |
| 3. 前端(可选) | 用户与智能体交互的 UI | 聊天小部件、搜索界面、CLI —— 对于后端服务则不需要 |
始终需要一个已部署的 Studio(v5.1.0+)。并非每个集成都需要 Agent Context 插件或文档 —— 基础 MCP URL 无需它们即可工作,因此用户可以仅从智能体实现开始,稍后再添加文档配置 —— 反之亦然。前端取决于具体用例(许多智能体作为后端服务运行或集成到现有 UI 中)。
询问用户他们需要帮助哪个部分:
同时了解:
参考模式使用 Next.js + Vercel AI SDK,但请根据用户正在使用的技术进行调整。
在构建生产智能体之前,验证 MCP 端点是否可达。如果用户还没有读取令牌,可以从终端提供创建 —— 如果可用,从 sanity.config.ts 或 sanity.cli.ts 检测 projectId:
npx sanity tokens add "Agent Context" --role=viewer --yes --json
这将输出包含令牌值的 JSON。如果不在 Sanity 项目目录内,请显式传递 --project-id=<id>。
然后测试端点:
curl -X POST https://api.sanity.io/v2026-03-03/agent-context/:projectId/:dataset \
-H "Authorization: Bearer $SANITY_API_READ_TOKEN" \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "method": "tools/list", "id": 1}'
这确认了令牌有效且端点可达。基础 URL(无 slug)无需 Agent Context 文档即可工作 —— 添加 slug 以应用文档的配置。
用户已经有一个智能体或 MCP 客户端? 他们只需要使用 Bearer 令牌将其连接到他们的 Agent Context URL。工具将自动出现。
从零开始构建? 帮助用户设置 MCP 连接和 LLM 集成。参考实现使用 Vercel AI SDK 和 Anthropic,但该模式适用于任何 LLM 提供商(OpenAI、本地模型等)。从基础开始,根据需要添加高级模式。
特定框架指南:
系统提示(适用于所有框架):参见 references/system-prompts.md 了解结构和领域特定示例(电子商务、文档、支持、内容策划)。
框架指南涵盖:
react-markdown 或 marked 这样的渲染器来显示格式化输出)帮助用户在他们的 Studio 中配置 @sanity/agent-context/studio 插件,并创建一个 Agent Context 文档。此文档控制生产智能体可以看到的内容(通过 groqFilter)以及它接收到的指导(通过 instructions)。
使用 Sanity Functions 跟踪和分析智能体对话。对分析、调试和理解用户交互很有用。
一旦生产智能体正常工作:
调整 Instructions 字段:使用 dial-your-context 技能 —— 这是一个交互式会话,您与用户一起探索他们的数据集,验证发现,并生成简洁的 Instructions,以教授生产智能体那些仅凭模式无法明显了解的内容:反直觉的字段名、二阶引用链、数据质量问题、必需的过滤器以及查询模式。该技能还可以帮助配置 groqFilter 来限定生产智能体可以看到的内容。
塑造系统提示(可选):使用 shape-your-agent 技能 —— 如果用户控制生产智能体的系统提示,这有助于定义语气、边界和防护栏。如果用户不控制系统提示,请跳过此步骤。
Agent Context 支持 text::semanticSimilarity() 进行语义排序:
*[_type == "article" && category == "guides"]
| score(text::semanticSimilarity("getting started tutorial"))
| order(_score desc)
{ _id, title, summary }[0...10]
使用 score() 时,请始终使用 order(_score desc) 以首先获得最佳匹配。
MCP 连接模式与框架和 LLM 无关。无论是 Next.js、Remix、Express 还是 Python FastAPI —— HTTP 传输的工作方式相同。任何支持工具调用的 LLM 提供商都适用。
参见 references/system-prompts.md 了解领域特定示例(电子商务、文档、支持、内容策划)。
_id,以便智能体可以引用文档groqFilter 限定生产智能体可以看到的内容 —— 开始时范围宽泛,然后根据实际需要缩小。过滤器是一个完整的 GROQ 表达式(例如,_type in ["product", "article"])dial-your-context 技能。shape-your-agent 技能。package.json 文件或使用 npm info <package> version,而不是猜测。AI SDK 和 Sanity 包更新频繁,使用过时的版本会导致难以调试的错误。Agent Context 需要一个已部署的 Studio。有关说明,请参见 部署您的 Studio。
SANITY_API_READ_TOKEN 缺失或无效。从终端生成新令牌:
npx sanity tokens add "Agent Context" --role=viewer --yes --json
或者在 sanity.io/manage → 项目 → API → 令牌 中创建,并分配 Viewer 角色。
检查 Agent Context 文档的内容过滤器(groqFilter):
mcpClient.tools() 是否返回工具(记录日志)每周安装次数
180
仓库
GitHub 星标数
4
首次出现
2026年2月4日
安全审计
安装在
opencode178
gemini-cli177
codex177
kimi-cli174
github-copilot174
amp173
Give AI agents intelligent access to your Sanity content. Unlike embedding-only approaches, Agent Context 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:
Agent Context gives agents your schema and teaches them GROQ, but it can't know your domain. You close that gap through the Instructions field (dataset-specific query guidance) and optionally the system prompt (agent behavior and tone).
Three actors in this workflow:
Before starting, gather these credentials:
| Credential | Where to get it |
|---|---|
| Sanity Project ID | Your sanity.config.ts or sanity.io/manage |
| Dataset name | Usually production — check your sanity.config.ts |
| Sanity API read token | Run npx sanity tokens add "Agent Context" --role=viewer --yes --json from the project directory (or pass --project-id=<id>). Alternatively, create at sanity.io/manage → Project → API → Tokens with Viewer role. |
| LLM API key | From your LLM provider (Anthropic, OpenAI, etc.) — any provider works |
An MCP server that gives AI agents structured access to Sanity content. The core integration pattern:
MCP URL formats:
https://api.sanity.io/v2026-03-03/agent-context/:projectId/:dataset — Base URL. No document needed, configure via query params or use as-is.https://api.sanity.io/v2026-03-03/agent-context/:projectId/:dataset/:slug — Document URL. Applies the configuration from an Agent Context document.Agent Context documents (type sanity.agentContext) are created in Sanity Studio and configure the MCP endpoint. They have three fields:
| Field | Schema field | Purpose |
|---|---|---|
| Slug | slug | Unique URL identifier — becomes the :slug in the MCP URL |
| Instructions | instructions | Domain-specific guidance for the agent, injected into tool descriptions |
| Content Filter | groqFilter | A GROQ expression scoping which documents the agent can access |
This means Studio users can manage agent behavior without touching code — updating instructions or narrowing the content filter takes effect immediately.
URL query params override the document's configuration (useful for testing and development):
?instructions=<content> — Override instructions (use ?instructions="" for a blank slate)?groqFilter=<expression> — Override the content filterThe 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.
| 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.
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 Agent Context 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 |
A deployed Studio (v5.1.0+) is always required. Not every integration needs the agent context plugin or document—the base MCP URL works without them, so users can start with just agent implementation and add document configuration later—or vice versa. Frontend depends on the use case (many agents run as backend services or integrate into existing UIs).
Ask the user which part they need help with:
Also understand:
The reference patterns use Next.js + Vercel AI SDK, but adapt to whatever the user is working with.
Before building the production agent, validate that the MCP endpoint is reachable. If the user doesn't have a read token yet, offer to create one from the terminal — detect the projectId from sanity.config.ts or sanity.cli.ts if available:
npx sanity tokens add "Agent Context" --role=viewer --yes --json
This outputs JSON with the token value. If not inside a Sanity project directory, pass --project-id=<id> explicitly.
Then test the endpoint:
curl -X POST https://api.sanity.io/v2026-03-03/agent-context/:projectId/:dataset \
-H "Authorization: Bearer $SANITY_API_READ_TOKEN" \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "method": "tools/list", "id": 1}'
This confirms the token works and the endpoint is reachable. The base URL (no slug) works without an Agent Context document—add a slug to apply a document's configuration.
The user already has an agent or MCP client? They just need to connect it to their Agent Context URL with a Bearer token. The tools will appear automatically.
Building from scratch? Help the user set up the MCP connection and LLM integration. The reference implementations use Vercel AI SDK with Anthropic, but the pattern works with any LLM provider (OpenAI, local models, etc.). Start with the basics and add advanced patterns as needed.
Framework-specific guides:
System prompts (applies to all frameworks): See references/system-prompts.md for structure and domain-specific examples (e-commerce, docs, support, content curation).
The framework guides cover:
react-markdown or marked is needed to display formatted output)Help the user configure the @sanity/agent-context/studio plugin in their Studio and create an Agent Context document. This document controls what the production agent can see (via groqFilter) and what guidance it receives (via instructions).
See references/studio-setup.md
Track and analyze agent conversations using Sanity Functions. Useful for analytics, debugging, and understanding user interactions.
See references/conversation-classification.md.
Once the production agent works:
Tune the Instructions field using the dial-your-context skill — an interactive session where you explore the user's dataset together, verify findings, and produce concise Instructions that teach the production agent what the schema alone doesn't make obvious: counter-intuitive field names, second-order reference chains, data quality issues, required filters, and query patterns. The skill can also help configure a groqFilter to scope what content the production agent sees.
Shape the system prompt (optional) using the shape-your-agent skill — if the user controls the production agent's system prompt, this helps define tone, boundaries, and guardrails. Skip this if the user doesn't control the system prompt.
Agent Context supports text::semanticSimilarity() for semantic ranking:
*[_type == "article" && category == "guides"]
| score(text::semanticSimilarity("getting started tutorial"))
| order(_score desc)
{ _id, title, summary }[0...10]
Always use order(_score desc) when using score() to get best matches first.
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/adapting-to-stacks.md for:
See references/system-prompts.md for domain-specific examples (e-commerce, docs, support, content curation).
_id in projections so agents can reference documentsgroqFilter to scope what the production agent sees — start broad, then narrow based on what it actually needs. The filter is a full GROQ expression (e.g., _type in ["product", "article"])dial-your-context skill.shape-your-agent skill.package.json files or use rather than guessing. AI SDK and Sanity packages update frequently, and using outdated versions will cause errors that are hard to debug.Agent Context requires a deployed Studio. See Deploy Your Studio for instructions.
The SANITY_API_READ_TOKEN is missing or invalid. Generate a new token from the terminal:
npx sanity tokens add "Agent Context" --role=viewer --yes --json
Or create one at sanity.io/manage → Project → API → Tokens with Viewer role.
Check the Agent Context document's content filter (groqFilter):
mcpClient.tools() returns tools (log it)Weekly Installs
180
Repository
GitHub Stars
4
First Seen
Feb 4, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
opencode178
gemini-cli177
codex177
kimi-cli174
github-copilot174
amp173
AI 代码实施计划编写技能 | 自动化开发任务分解与 TDD 流程规划工具
46,800 周安装
Docker安全指南:全面容器安全最佳实践、漏洞扫描与合规性要求
177 周安装
iOS开发专家技能:精通Swift 6、SwiftUI与原生应用开发,涵盖架构、性能与App Store合规
177 周安装
describe技能:AI驱动结构化测试用例生成,提升代码质量与评审效率
2 周安装
专业 README 生成器 | 支持 Rust/TypeScript/Python 项目,自动应用最佳实践
2 周安装
Django 6 升级指南:从 Django 5 迁移的完整步骤与重大变更解析
1 周安装
GitLab DAG与并行处理指南:needs与parallel优化CI/CD流水线速度
2 周安装
npm info <package> version