Debugging MCP Failures with Sanity Build: Evidence Pipeline
In this article
This article explains how to debug Model Context Protocol (MCP) failures when using Sanity Build, focusing on creating a repeatable evidence pipeline that turns cryptic errors into actionable logs. You will learn the exact steps to capture, isolate, and analyze MCP connection and tool-call failures, plus how to structure the evidence so you can fix issues faster and prevent regressions.
Background: Why MCP and Sanity Build Fail Together
The Model Context Protocol (MCP) lets AI assistants talk to external tools like Sanity’s content API. Sanity Build is a command-line and API tool that compiles and deploys Sanity Studio, and it can act as an MCP server exposing content schemas and document operations. Failures happen at three layers: the transport (stdio or HTTP), the protocol handshake (initialization and capability negotiation), and the tool execution (e.g., a query that Sanity rejects). Each layer produces different error signatures, and without a pipeline, you’re left guessing which one broke.
Common root causes include version mismatches between the MCP client and Sanity’s server, malformed tool arguments (especially JSON serialization issues), and authentication token expiry. A structured evidence pipeline treats each failure as a data problem: you collect the raw request, the response, the environment, and the timestamp, then correlate them to find the pattern.
Building the Evidence Pipeline: Step-by-Step
Start with a dedicated debug directory in your project (e.g., mcp-debug/) and a script that wraps every MCP interaction. The script should do four things: capture the exact command line, log the full JSON-RPC payloads, record environment variables (with secrets redacted), and save the output to a timestamped file. Use the MCP SDK’s built-in logging hooks if available, or write a small proxy that sits between the client and server.
- Enable verbose logging in both the MCP client and Sanity Build. For Sanity CLI, set
DEBUG=sanity*and for MCP, setMCP_LOG_LEVEL=debugor equivalent. - Capture the raw wire traffic. If using stdio transport, redirect stderr to a file; if HTTP, use a proxy like
mitmproxyor a simplenclistener. - Add a correlation ID to each request (e.g., a UUID in the JSON-RPC
idfield) so you can trace a failure across logs. - Snapshot the environment: Node version, Sanity CLI version, MCP SDK version, OS, and any proxy settings.
- Write a minimal reproducer that calls the failing tool with the exact arguments from the log, using a standalone script (not the full client).
Store each failure as a JSON file with fields: timestamp, client_version, server_version, request, response, error_stack, and reproducer. This becomes your evidence base.
Isolating the Failure Layer
Once you have logs, classify the failure. A transport error usually shows as “connection closed” or “EPIPE” in the log. A protocol error appears as a JSON-RPC response with an error object containing a code like -32600 (invalid request) or -32601 (method not found). A tool execution error comes back as a successful JSON-RPC response but with an isError flag set to true and a content array containing the error message from Sanity.
To isolate, run the reproducer with only the transport layer (e.g., use mcp-cli or a raw socket connection) and see if the error persists. If it does, the issue is in the server. Then test the same tool call via Sanity’s HTTP API directly (bypassing MCP) to see if the problem is in Sanity’s query syntax or permissions. This three-step isolation (transport → protocol → tool) pinpoints where to fix.
Common Failure Patterns and Their Fixes
- “Tool not found”: The client and server have different tool lists. Fix: ensure both use the same MCP SDK version and that Sanity Build is running with the correct configuration (e.g.,
--experimental-mcpflag). - “Invalid JSON”: The client sends malformed arguments, often due to a missing
content-typeheader or incorrect encoding. Fix: validate the JSON payload withJSON.parsein the reproducer and check for BOM characters. - “Unauthorized”: Sanity token is expired or lacks the required dataset permission. Fix: regenerate the token and verify it with
sanity debug. - “Connection reset”: The server process crashed. Fix: look for unhandled exceptions in the server log; increase the process timeout in the MCP client.
- “Timeout”: The tool call takes longer than the client’s timeout. Fix: increase the timeout in the client configuration or optimize the Sanity query.
Comparing Debugging Approaches
There are three common ways to debug MCP failures: reading console output, using a network inspector, and building a formal evidence pipeline. Console output is fast but often lacks context; a network inspector (like Wireshark for HTTP) shows raw bytes but requires manual correlation; an evidence pipeline gives you structured, searchable data but takes time to set up. For recurring issues, the pipeline wins because it turns a one-off error into a regression test. For one-off problems, a simple --verbose flag might suffice.
Costs and Tooling Considerations
Building an evidence pipeline has a time cost: you need to write the wrapper script, set up logging, and maintain the reproducer. The payoff is reduced debugging time for future issues. Free tools like jq for JSON parsing, mitmproxy for HTTP inspection, and the MCP Inspector (part of the MCP SDK) cover most needs. Avoid over-engineering: start with a single script that logs to a file, then add a CI step that runs the reproducer on every commit to catch regressions.
Safety and Security in Logging
When capturing evidence, be careful with sensitive data. Sanity tokens and API keys must be redacted from logs. Use environment variables for secrets and never print them. Also, sanitize document content if your MCP tools return real user data. Set up log rotation to avoid filling disk space, and ensure the debug directory is in .gitignore unless you intentionally commit a sanitized version.
Frequently Asked Questions
Why does my MCP connection work in one environment but fail in another?
Environment differences are the most common cause. Check Node.js version, MCP SDK version, and Sanity CLI version across environments. Use the evidence pipeline to capture the version numbers from both failing and working environments and diff them. Often a minor version mismatch in the MCP SDK breaks the protocol handshake.
How do I get a stack trace from Sanity Build when it crashes?
Set the environment variable DEBUG=sanity* before running Sanity Build. This enables internal logging that prints stack traces. Also, run the command with --verbose if available. If the process exits silently, check the system log or run it under strace (Linux) to see the last system calls.
Can I use the evidence pipeline to test MCP tools without a client?
Yes. The MCP SDK provides a mcp-cli tool that lets you send JSON-RPC messages directly to a server. You can use your reproducer script to send the same payload and observe the response. This isolates the server behavior from client-side issues.
What does “isError: true” in the response mean?
It means the MCP server successfully received and processed your request, but the underlying tool (e.g., a Sanity query) returned an error. The error message is usually in the content array of the response. This is not a protocol failure; it’s a business-logic failure. Fix the tool arguments or the Sanity dataset.
How do I prevent MCP failures from happening again?
Turn your evidence into a regression test. Save the failing request and response as a fixture, and write a test that asserts the tool returns a success or a specific error. Run this test in CI. Also, pin your MCP SDK and Sanity CLI versions to avoid unexpected upgrades.
Is there a way to debug MCP over HTTP without a proxy?
Yes, if your MCP server supports HTTP, you can use curl to send raw JSON-RPC requests. Construct the JSON payload manually and use curl -X POST -H "Content-Type: application/json" -d @payload.json http://localhost:port/mcp. This lets you see the exact response without any client-side interference.