DEV Community

Cover image for Midnight MCP - AI-assisted development for Compact smart contracts
Idris Olubisiđź’ˇ for Devs of Midnight

Posted on • Originally published at docs.midnight.network

Midnight MCP - AI-assisted development for Compact smart contracts

AI coding assistants like Claude, GitHub Copilot, and Cursor have transformed how developers write code. But they have a fundamental limitation: they only know what was in their training data.

Compact, Midnight's smart contract language, isn't in that training data. When you ask an AI assistant to write a Compact contract, it hallucinates. It invents syntax that doesn't exist, references functions that were never defined, and produces code that fails at compile time.

Midnight MCP solves this problem.

What is MCP?

The Model Context Protocol (MCP) is an open standard that allows AI assistants to access external tools and data sources. Instead of relying solely on training data, an AI assistant with MCP can query live documentation, search codebases, and call APIs.

Midnight MCP is an MCP server purpose-built for Midnight development. It gives AI assistants:

  • Indexed knowledge of 102 Midnight repositories
  • Real compiler validation before showing you code
  • Semantic search across documentation and examples
  • Version-aware syntax references for Compact

When you ask Claude to write a Compact contract, it queries Midnight MCP for the correct syntax, generates the code, validates it against the real compiler, and only shows you working code.

The problem with AI-generated Compact code

Consider this prompt:

"Write a simple counter contract in Compact"

Without Midnight MCP, an AI assistant might generate:

contract Counter {
    state count: Int = 0;

    function increment(): Void {
        count = count + 1;
    }
}
Enter fullscreen mode Exit fullscreen mode

This looks plausible. It's also completely wrong:

  • Compact uses ledger for state, not state.
  • There is no Int type in Compact. It uses Uint<32>, Field, and other specific types.
  • Void doesn't exist. Compact uses [] for the unit type.
  • State mutations require witness functions, not direct assignment.

The AI hallucinated a language that resembles Solidity but isn't Compact. A developer unfamiliar with Compact might spend hours debugging code that was never valid.

Install Midnight MCP

Midnight MCP requires no API keys and installs in under 60 seconds. Add the appropriate configuration to your AI assistant based on the tool you use.

Claude Desktop

On macOS, edit ~/Library/Application Support/Claude/claude_desktop_config.json. Add the following configuration to enable Midnight MCP:

{
  "mcpServers": {
    "midnight": {
      "command": "npx",
      "args": ["-y", "midnight-mcp@latest"]
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Cursor

Create or edit .cursor/mcp.json in your project root. You can also configure it globally at ~/.cursor/mcp.json on macOS/Linux or %USERPROFILE%\.cursor\mcp.json on Windows. Add the following configuration:

{
  "mcpServers": {
    "midnight": {
      "command": "npx",
      "args": ["-y", "midnight-mcp@latest"]
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

VS Code with GitHub Copilot

Create or edit .vscode/mcp.json in your project. Add the following configuration to connect Copilot to Midnight MCP:

{
  "servers": {
    "midnight": {
      "command": "npx",
      "args": ["-y", "midnight-mcp@latest"]
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

After adding the configuration, restart your AI assistant. You now have access to 29 Midnight-specific tools.

How Midnight MCP works

Midnight MCP operates as a local server that connects your AI assistant to Midnight's ecosystem.

Midnight MCP Architecture

When you ask for a Compact contract:

  1. The AI assistant calls midnight-get-latest-syntax to retrieve current Compact syntax.
  2. It generates code using the correct patterns.
  3. It calls midnight-compile-contract to validate against the real compiler.
  4. If compilation fails, it reads the error, fixes the code, and retries.
  5. You receive verified, working code.

This compile-validate-fix loop happens automatically. You never see the broken intermediate versions.

Real compiler integration

Most code-generation tools rely on pattern matching and hope. Midnight MCP validates code against the actual Compact compiler hosted.

The compiler catches errors that static analysis cannot, including:

  • Type mismatches: Using Field where Uint<64> is expected
  • Sealed field violations: Attempting to access sealed state incorrectly
  • Disclose rule errors: Missing or malformed privacy annotations
  • Unbound identifiers: References to undefined variables or types

When the compiler returns an error, the response includes the exact line number and column:

success: false
message: "Line 12:8 - unbound identifier 'totalSupply'"
errorDetails:
  line: 12
  column: 8
  errorType: error
Enter fullscreen mode Exit fullscreen mode

The AI assistant uses this information to fix the code and try again.

Graceful fallback

If the hosted compiler is unavailable, Midnight MCP falls back to static analysis. The response indicates which validation method was used:

validationType: "compiler"           # Real compiler validation
validationType: "static-analysis-fallback"  # Compiler unavailable
Enter fullscreen mode Exit fullscreen mode

You always receive validation. The tool never fails silently.

Semantic search across 102 repositories

Today, Midnight MCP indexes every non-archived repository in the Midnight ecosystem:

  • All 88 repositories from midnightntwrk.
  • 14 community and partner repositories, including OpenZeppelin contracts and hackathon winners.

The search is semantic, not keyword-based. For example, a prompt like "find code that handles shielded transactions" returns relevant results even if those exact words don't appear in the code.

Prompt: "How do I implement a token with transfer limits?"

midnight-search-compact returns:
- Token contract examples from midnight-examples
- Rate limiting patterns from community repos
- Relevant documentation sections
Enter fullscreen mode Exit fullscreen mode

29+ tools for Midnight development

Midnight MCP provides 29 tools organized by function.

Search tools

Use these tools to find code, documentation, and examples across the Midnight ecosystem.

Tool Purpose
midnight-search-compact Search Compact language code across indexed repos
midnight-search-docs Search official Midnight documentation
midnight-search-typescript Search TypeScript SDK implementations
midnight-fetch-docs Fetch live documentation from docs.midnight.network

Analysis tools

Use these tools to validate, analyze, and review Compact contracts.

Tool Purpose
midnight-compile-contract Validate code against the real Compact compiler
midnight-analyze-contract Run 15 static security checks
midnight-review-contract AI-powered security review
midnight-extract-contract-structure Parse contract structure and exports

Generation tools

Use these tools to create new contracts and documentation.

Tool Purpose
midnight-generate-contract Generate contracts from natural language descriptions
midnight-document-contract Generate documentation in Markdown or JSDoc format

Repository tools

Use these tools to access files and syntax references from Midnight repositories.

Tool Purpose
midnight-get-file Retrieve files from any indexed Midnight repository
midnight-get-file-at-version Get file content at a specific version
midnight-compare-syntax Compare syntax between Compact versions
midnight-get-latest-syntax Current Compact syntax reference
midnight-get-repo-context Everything needed to start coding (compound tool)
midnight-list-examples List available example contracts

Version management tools

Use these tools to manage upgrades and track changes between Compact versions.

Tool Purpose
midnight-upgrade-check Full upgrade analysis (compound tool)
midnight-check-breaking-changes Identify breaking changes between versions
midnight-get-migration-guide Step-by-step migration instructions

Resources and prompts

Beyond tools, Midnight MCP provides nine built-in resources and five interactive prompts.

Resources are always-available references that provide quick access to syntax and examples:

midnight://syntax/latest      Current Compact syntax
midnight://examples/counter   Counter contract example
midnight://examples/token     Token contract example
midnight://docs/compact       Compact language reference
Enter fullscreen mode Exit fullscreen mode

Prompts are templates for common tasks that guide you through specific workflows:

create-compact-contract      Start a new contract
debug-compact-error          Fix compilation errors
security-review              Full security audit
compare-compact-versions     Migration assistance
Enter fullscreen mode Exit fullscreen mode

Architecture

Midnight MCP is built for reliability:

  • Token efficiency: Outputs YAML by default (20-30% fewer tokens than JSON)
  • Compound tools: Single calls that combine multiple operations
  • Graceful degradation: Falls back to cached data when services are unavailable
  • Progress notifications: Real-time updates during long operations

The codebase is fully tested with 206 tests across 10 test suites.

What's next

Midnight MCP is open source and actively developed. The roadmap includes:

  • Full ZK circuit output parsing from compiler results.
  • Contract deployment directly from AI chat.
  • TypeScript SDK integration for automatic prover code generation.
  • Local devnet interaction for querying balances and submitting transactions.

Learn more

Explore the source code and contribute:

→ GitHub repository

→ npm package

→ API documentation

Midnight MCP is a community project. Contributions, issues, and feature requests are welcome.

Top comments (0)