This guide shows you how to connect Sanity’s Model Context Protocol (MCP) server to n8n, enabling you to validate API responses and data structures directly within your automation workflows. You’ll set up the MCP connection, create a workflow that sends sample data to Sanity for validation, and handle the results—all without writing custom code.
What You’ll Need
- An n8n instance (self-hosted or cloud) with admin access.
- A Sanity project with a dataset and a valid API token (read or write, depending on your validation needs).
- Basic familiarity with n8n’s workflow editor and Sanity’s schema.
- Node.js installed locally if you plan to test the MCP server outside n8n (optional).
Step-by-Step
- Get your Sanity API credentials. In your Sanity project, go to API → Tokens and create a new token with at least
readpermission. Copy the token and note yourprojectIdanddatasetname (usuallyproduction). - Install the Sanity MCP server. The Sanity MCP server is an npm package. In your terminal, run:
npm install -g @sanity/mcp. This gives you thesanity-mcpcommand. - Configure the MCP server. Create a configuration file for the MCP server. On macOS/Linux, create
~/.config/sanity-mcp/config.json. On Windows, use%APPDATA%\sanity-mcp\config.json. The file should look like:{ "projectId": "your-project-id", "dataset": "production", "token": "your-api-token" }Replace the placeholders with your actual values.
- Start the MCP server. Run
sanity-mcpin a terminal. It will start a local server (default port 3000) and print a message likeMCP server running on http://localhost:3000. Keep this terminal open. - Add an MCP node in n8n. In your n8n workflow, add a new node and search for “MCP”. Choose the MCP Client node (or “MCP Tool” if available). In the node configuration, set the Server URL to
http://localhost:3000and leave the authentication blank (or add a header if you secured it). - Authenticate (if needed). If your MCP server requires an API key, add a header in the MCP node’s Headers field, e.g.,
Authorization: Bearer your-mcp-api-key. For local testing, you can skip this. - Test the connection. In the MCP node, click Execute Node to fetch the list of available tools. You should see tools like
validateDocumentorgetDocument. If not, check that the server is running and the URL is correct. - Build a validation workflow. Create a workflow that sends data to the MCP tool. For example:
- Add a Webhook node to receive API requests.
- Add a Set node to define a sample document object (e.g.,
{_type: 'product', name: 'Test', price: 19.99}). - Connect the Set node to the MCP node. In the MCP node, select the
validateDocumenttool and map the document field to the output of the Set node.
- Handle the response. Add a Switch node after the MCP node to check if the validation succeeded. The MCP tool returns a JSON object with a
validboolean or anerrorsarray. Use that to route to success or failure branches. - Log or notify. For failed validations, add an Error Trigger or a Slack node to send a message. For success, you can simply log the output or continue with other steps.
- Test the full workflow. Send a test request to your webhook URL with sample data. Verify that the validation result is correct. Try sending invalid data (e.g., missing required field) to confirm the error handling works.
Common Mistakes
- Wrong port or URL: The MCP server might run on a different port if 3000 is taken. Check the terminal output for the actual URL.
- Missing API token: Without a valid token, the MCP server cannot connect to Sanity. Ensure the token has the right permissions and is correctly pasted in the config file.
- Using the wrong tool name: The MCP server exposes specific tools. If you guess a tool name that doesn’t exist, n8n will show an error. Always list the tools first.
- Not mapping fields correctly: In n8n, the MCP node expects the input as a JSON object. If you pass a string or array, validation will fail. Use a Set node to structure the data properly.
- Firewall blocking localhost: If your n8n is on a remote server, it cannot access
localhost:3000on your local machine. You need to deploy the MCP server on a shared host or use a tunnel.
Tips
- Use environment variables: Store your Sanity token and project ID in n8n’s environment variables to avoid hardcoding them in workflows.
- Test with a small dataset: Validate a single document first before processing large batches to catch schema issues early.
- Handle rate limits: Sanity has API rate limits. If you validate many documents, add a Wait node between requests to avoid hitting them.
- Log everything: Use n8n’s Execute Workflow node to log the raw response from the MCP tool for debugging.
- Secure the MCP server: If you expose it beyond localhost, add an API key and use HTTPS to prevent unauthorized access.
Troubleshooting
MCP node shows “Connection refused” – Ensure the MCP server is running. Check the terminal for errors. If you’re using a remote n8n, use a public URL (e.g., via ngrok) for the server.
Tool list is empty – The MCP server might have failed to connect to Sanity. Verify your config file and token. Run the MCP server in verbose mode (e.g., sanity-mcp --debug) to see logs.
Validation returns unexpected errors – Check the Sanity schema. The MCP tool validates against your project’s schema. If you added new fields, ensure they are in the schema.
n8n cannot parse the response – The MCP node might return a string instead of JSON. Use a Function node to parse it: return JSON.parse($input.first().json.output).
Workflow times out – Increase the timeout in the MCP node settings (default is 30 seconds). Large documents may take longer to validate.
Frequently Asked Questions
Can I use this with Sanity’s GraphQL API? Yes, but the MCP server uses the HTTP API. You can still validate documents that are fetched via GraphQL by passing them to the MCP tool.
Does the MCP server support write operations? It can, if your token has write permissions. But for validation, read-only is sufficient.
How do I deploy the MCP server on a server? Install Node.js, then run npm install -g @sanity/mcp and start it with sanity-mcp. Use a process manager like PM2 to keep it running.
Can I validate multiple documents at once? The MCP tool typically validates one document per call. You can loop over an array in n8n using a Split In Batches node.
What if my Sanity schema changes? The MCP server reads the schema from Sanity at runtime, so it automatically picks up changes. No restart needed.
Is this free? The MCP server is open-source and free. You only pay for n8n and Sanity usage as per their pricing.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Campus Tech Innovation Hubs: What They Are
- Poco C51 Airtel Prepaid Locked Variant India?
- Jenkins vs GitHub Actions: Which CI/CD Fits Your Team?
- Turn Off Backup Encryption in iTunes?
- Shared VPS Hosting: Is It Better?
- Disk Defrag Pro: What It Does and Whether You Need It