Skip to main content
Glama

Sanity MCP Server

Official
by sanity-io
document.ts1.21 kB
import type {SanityClient} from '@sanity/client' import {type DocumentId, getDraftId, getPublishedId, isVersionId, isDraftId} from '@sanity/id-utils' /** * Retrieves a document from Sanity by document ID. * * If the document ID is a version ID, returns that specific version. * Otherwise, fetches both draft and published versions and returns the appropriate one * based on the original document ID type - prioritizing draft for draft IDs and * published for published IDs. */ export async function getDocument(documentId: DocumentId, client: SanityClient) { if (isVersionId(documentId)) { const document = await client.getDocument(documentId) if (!document) { throw new Error(`Could not find document: "${documentId}"`) } return document } const [draft, published] = await Promise.all([ client.getDocument(getDraftId(documentId)), client.getDocument(getPublishedId(documentId)), ]) if (!draft && !published) { throw new Error(`Could not find document: "${documentId}"`) } // Prioritize draft over published depending on the original document ID if (isDraftId(documentId)) { return (draft || published)! } return (published || draft)! }

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/sanity-io/sanity-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server