An implementation of the Model Context Protocol (MCP) server for Sanity.io.
This server implements the MCP protocol using stdio transport, making it suitable for direct integration with LLM assistants that support the MCP protocol. The server provides tools for interacting with Sanity's content management system.
- Built with TypeScript for type safety and better developer experience
- Uses Vitest for testing, with better ES Modules support
- Implements controllers for various Sanity.io features:
actions.ts
: Document publishing and unpublishing operationsembeddings.ts
: Embeddings and semantic searchgroq.ts
: GROQ queries and real-time updatesmutate.ts
: Document mutations and modificationsreleases.ts
: Content release managementschema.ts
: Schema type informationtools.ts
: Tool definitions for MCP
The server provides the following tools:
-
GROQ Queries
query
: Executes GROQ queries (formerlysearchContent
)subscribeToUpdates
: Subscribes to real-time updates for documentsgetGroqSpecification
: Gets the GROQ query language specification
-
Document Retrieval
getDocument
: Gets a document by ID or multiple documents by their IDsgetDocuments
: Gets multiple documents by their IDs (alternative to usinggetDocument
with an array)
-
Document Mutations
createDocument
: Creates a new documentupdateDocument
: Updates one or more existing documentsmutateDocument
: Performs multiple operations on a single documentdeleteDocument
: Deletes one or more documentsbatchMutations
: Performs multiple mutations across different documentsupdatePortableText
: Updates Portable Text fields (formerlymodifyPortableTextField
)
-
Document Actions
publishDocument
: Publishes one or more documentsunpublishDocument
: Unpublishes one or more documentscreateRelease
: Creates a new content releaseaddDocumentToRelease
: Adds a document to a content releaseremoveDocumentFromRelease
: Removes one or more documents from a content releaselistReleaseDocuments
: Lists documents in a releasecreateDocumentVersion
: Creates a version of one or more documents in a specific releaseunpublishDocumentWithRelease
: Marks one or more documents for unpublishing when a release is published
-
Schema Management
listSchemaTypes
: Lists available schema typesgetTypeSchema
: Gets detailed schema for a specific type
-
Embeddings and Semantic Search
semanticSearch
: Performs semantic search on embeddings indiceslistEmbeddingsIndices
: Lists available embeddings indices
-
Project Management
listOrganizationsAndProjects
: Lists all organizations and their projectslistStudios
: Lists all studios for a specific project
npm install
This project is built with TypeScript. To compile the TypeScript files to JavaScript:
npm run build
Important: Before running the server, make sure to set up your environment variables as described in the Environment Variables section below. The server requires Sanity API credentials to function properly.
Start the development server with automatic recompilation and restart on changes:
npm run dev
This uses tsc -w
to watch TypeScript files and recompile them when they change, plus node --watch
to restart the server when the compiled JavaScript changes.
For production or regular usage:
npm run build
npm start
npm start
Or after building:
node dist/index.js
The server can be integrated with AI assistants that support the MCP protocol. It uses stdio transport (standard input/output) to communicate.
To see an example of how to integrate with the MCP client, check out the usage-example.ts
file.
For Anthropic's Claude AI, you can configure it to use this MCP server by adding the following to your Claude configuration:
{
"tools": [
{
"name": "sanity-mcp",
"type": "mcp",
"path": "/path/to/sanity-mcp-server/dist/index.js",
"env": {
"SANITY_TOKEN": "your_sanity_api_token",
"SANITY_PROJECT_ID": "your_sanity_project_id",
"SANITY_DATASET": "your_sanity_dataset",
"SANITY_API_VERSION": "2025-03-15"
}
}
]
}
To configure the server, you need to set up environment variables:
-
Copy the
.env.sample
file to create your own.env
file:cp .env.sample .env
-
Edit the
.env
file and fill in your Sanity credentials:SANITY_TOKEN=your_sanity_api_token SANITY_PROJECT_ID=your_sanity_project_id SANITY_DATASET=your_sanity_dataset SANITY_API_VERSION=2024-05-23 # Must be 2024-05-23 or later to support releases PORT=3000 # Server port
You can get these values from your Sanity project settings:
SANITY_TOKEN
: Create an API token in the Sanity management console with appropriate permissionsSANITY_PROJECT_ID
: Your Sanity project ID (found in project settings)SANITY_DATASET
: The dataset name you want to work with (typically "production")
The project is organized as follows:
src/
: Source codecontrollers/
: Controller modules for different Sanity featurestypes/
: TypeScript type definitionsutils/
: Utility functionsconfig/
: Configuration files
config/
: Configuration files for development tools.eslintrc.json
: ESLint configuration.eslintignore
: Files to ignore in ESLinttsconfig.test.json
: TypeScript configuration for tests
scripts/
: Development and build scriptstest/
: Test filesunit/
: Unit testsintegration/
: Integration tests
schemas/
: Sanity schema files (not tracked in version control)
MIT