Tetrad
Quadruple Consensus MCP Server for Claude Code
Tetrad is a high-performance MCP (Model Context Protocol) server written in Rust that orchestrates three AI-powered CLI code evaluation tools (Codex, Gemini CLI, Qwen) to validate all code produced by Claude Code.
The system implements a quadruple consensus protocol where no code or plan is accepted without approval from four intelligences: the three external evaluators + Claude Code itself.
Features
- Quadruple Consensus: 4 AI models must agree to approve code
- ReasoningBank: Continuous learning system with RETRIEVEβJUDGEβDISTILLβCONSOLIDATE cycle
- High Performance: Written in Rust with parallel execution via Tokio
- MCP Server: JSON-RPC 2.0 server over stdio for Claude Code integration
- Full CLI: Intuitive commands (
init,serve,status,doctor,config, etc.) - LRU Cache: Result caching with configurable TTL
- Hook System: Pre/post evaluation callbacks for customization
- Extensible: Plugin system for custom executors
- Cross-session: SQLite persistence for patterns and history
Quick Start
1. Install Tetrad
# Via npm (recommended - works seamlessly with Claude Code)
# Or via cargo (requires additional setup)
1.1 Initialize in Your Project (Optional)
This will:
- Create
tetrad.tomlconfiguration file - Create
.tetrad/directory for the database - Add
.tetrad/to your.gitignore
2. Install and Configure External CLI Tools
Tetrad requires at least one of the following AI CLI tools. Good news: you don't need separate API keys if you have existing subscriptions!
Codex CLI (OpenAI)
Authentication options:
| Method | Requirements | API Key Needed? |
|---|---|---|
| ChatGPT Login | ChatGPT Plus, Pro, Business, Edu, or Enterprise subscription | No |
| API Key | OpenAI API account with credits | Yes |
# Option 1: Login with your ChatGPT subscription (recommended)
# Opens browser for OAuth - follow the prompts
# Option 2: Use API key
Note: For headless/remote servers, use
codex login --device-authand follow the device code flow.
π Codex CLI Authentication Docs
Gemini CLI (Google)
Authentication options:
| Method | Requirements | API Key Needed? | Free Tier |
|---|---|---|---|
| Google Login | Personal Google account | No | 60 req/min, 1,000 req/day |
| API Key | Google AI Studio account | Yes | Same limits |
# Option 1: Login with Google account (recommended)
# Opens browser for OAuth - follow the prompts
# Option 2: Use API key from Google AI Studio
Free Tier: With a personal Google account, you get access to Gemini 2.5 Pro with 1M token context window at no cost!
π Gemini CLI Authentication Docs
Qwen CLI (Alibaba)
Authentication options:
| Method | Requirements | API Key Needed? | Free Tier |
|---|---|---|---|
| DashScope API | Alibaba Cloud account | Yes | 2,000 req/day |
| Coding Plan | Qwen Coding subscription | Yes (different format) | Included |
# Get your API key from DashScope console
# For international users (outside China)
Regional Endpoints:
- Singapore:
dashscope-intl.aliyuncs.com- Virginia:
dashscope-us.aliyuncs.com- Beijing:
dashscope.aliyuncs.com
π Qwen API Docs
3. Verify Installation
# Check Tetrad version
# Check CLI availability
# Diagnose any issues
4. Add to Claude Code CLI
# Add Tetrad as MCP server (all projects)
# Verify it's configured
# Current project only
# If installed via cargo
Note: Tetrad is published on the MCP Registry for discoverability and documentation.
5. Alternative: Manual Configuration
Create or edit .mcp.json in your project root:
Or for global user configuration in ~/.claude.json (mcpServers section):
How It Works
When you ask Claude Code to write code, Tetrad automatically validates it:
You: "Create a function in Rust that calculates the average of a vector"
Claude Code:
1. Writes the code
2. Calls tetrad_review_code automatically
3. Tetrad sends to Codex, Gemini and Qwen for evaluation
4. Returns consolidated consensus from all 3 evaluators
Tetrad Response:
βββββββββββββββββββββββββββββββββββββββββββββββ
β DECISION: PASS β β
β Score: 92/100 β
β Consensus: Yes (3/3 approved) β
β β
β Votes: β
β β’ Codex: Pass (95) β
β β’ Gemini: Pass (90) β
β β’ Qwen: Pass (92) β
β β
β Suggestions: β
β - Consider handling empty vector case β
βββββββββββββββββββββββββββββββββββββββββββββββ
Claude Code: Saves the approved code
When Issues Are Found:
Tetrad Response:
βββββββββββββββββββββββββββββββββββββββββββββββ
β DECISION: BLOCK β β
β Score: 26/100 β
β Consensus: Yes (3/3 rejected) β
β β
β Votes: β
β β’ Codex: Fail (30) - division by zero β
β β’ Gemini: Fail (25) - no error handling β
β β’ Qwen: Fail (25) - unsafe operation β
β β
β Issues: β
β - Division by zero not handled β
β - Missing input validation β
β - No Result/Option return type β
βββββββββββββββββββββββββββββββββββββββββββββββ
Claude Code: Fixes the code and resubmits
CLI Commands
tetrad - Quadruple Consensus CLI for Claude Code
COMMANDS:
init Initialize configuration in current directory
serve Start the MCP server (used by Claude Code)
status Show CLI status (codex, gemini, qwen)
config Configure options interactively
doctor Diagnose configuration issues
version Show version
evaluate Evaluate code manually (without MCP)
history Show evaluation history from ReasoningBank
export Export patterns from ReasoningBank
import Import patterns into ReasoningBank
OPTIONS:
-c, --config <FILE> Configuration file (default: tetrad.toml)
-v, --verbose Verbose mode
-q, --quiet Quiet mode
-h, --help Show help
MCP Tools
When running as MCP server, Tetrad exposes 6 tools:
| Tool | Description |
|---|---|
tetrad_review_plan |
Review implementation plans before coding |
tetrad_review_code |
Review code before saving |
tetrad_review_tests |
Review tests before finalizing |
tetrad_confirm |
Confirm agreement with received feedback |
tetrad_final_check |
Final verification before commit |
tetrad_status |
Check health of evaluators |
Workflow Example
1. Claude Code generates plan β tetrad_review_plan β Feedback
2. Claude Code implements β tetrad_review_code β Feedback
3. Claude Code adjusts β tetrad_confirm β Confirmation
4. Claude Code finalizes β tetrad_final_check β Certificate
Architecture
Claude Code β MCP Protocol (stdio) β Tetrad Server (Rust)
β
βββββββββββββββββββββββΌββββββββββββββββββββββ
βΌ βΌ βΌ
Codex CLI Gemini CLI Qwen CLI
(syntax) (architecture) (logic)
β β β
βββββββββββββββββββββββΌββββββββββββββββββββββ
βΌ
Consensus Engine
β
βββββββββββββββ΄ββββββββββββββ
βΌ βΌ
LRU Cache ReasoningBank
(results) (SQLite)
RETRIEVEβJUDGEβDISTILLβCONSOLIDATE
Executor Specializations
| Executor | CLI | Specialization |
|---|---|---|
| Codex | codex exec --json |
Syntax and code conventions |
| Gemini | gemini -o json |
Architecture and design |
| Qwen | qwen |
Logic bugs and correctness |
Consensus Rules
| Rule | Requirement | Use Case |
|---|---|---|
| Golden | Unanimity (3/3) | Critical code, security |
| Strong | 3/3 or 2/3 with high confidence | Default |
| Weak | Simple majority (2/3) | Rapid prototyping |
ReasoningBank
The ReasoningBank is a continuous learning system that stores and consolidates code patterns:
Learning Cycle
RETRIEVE β JUDGE β DISTILL β CONSOLIDATE
β β β β
β β β ββ Merge similar patterns
β β ββ Extract new patterns
β ββ Evaluate code with context
ββ Search for relevant patterns
Pattern Types
- AntiPattern: Patterns to avoid (bugs, vulnerabilities, code smells)
- GoodPattern: Patterns to follow (best practices, idiomatic patterns)
- Ambiguous: Patterns with uncertain classification (needs more data)
ReasoningBank Commands
# View evaluation history
# Export patterns to share
# Import patterns from another ReasoningBank
Configuration
The tetrad.toml file is created automatically with tetrad init:
[]
= "info"
= 60
[]
= true
= "codex"
= ["exec", "--json"]
= 30
[]
= true
= "gemini"
= ["-o", "json"]
= 30
[]
= true
= "qwen"
= []
= 30
[]
= "strong"
= 70
= 3
[]
= true
= ".tetrad/tetrad.db"
= 10
= 100
[]
= true
= 1000
= 300
Interactive Configuration
Use tetrad config for interactive configuration:
π§ Tetrad Interactive Configuration
What would you like to configure?
β― General Settings
Executors (Codex, Gemini, Qwen)
Consensus
ReasoningBank
Save and Exit
Exit without Saving
LRU Cache
The system includes an LRU cache to avoid unnecessary re-evaluations:
- Capacity: Configurable (default: 1000 entries)
- TTL: Configurable time-to-live (default: 5 minutes)
- Key: Hash of code + language + evaluation type
- Invalidation: Automatic by TTL or manual
Hook System
Hooks allow customizing behavior at specific points:
| Hook | When | Use |
|---|---|---|
pre_evaluate |
Before evaluation | Modify request, skip evaluation |
post_evaluate |
After evaluation | Logging, metrics, notifications |
on_consensus |
When consensus reached | Automatic actions on approval |
on_block |
When code blocked | Alerts, automatic rollback |
Built-in Hooks
- LoggingHook: Records all evaluations
- MetricsHook: Collects usage statistics
Project Structure
tetrad/
βββ Cargo.toml # Crate manifest
βββ CLAUDE.md # Documentation for Claude Code
βββ README.md # This file
βββ Tetrad.md # Complete specification
βββ src/
β βββ main.rs # Entry point (CLI)
β βββ lib.rs # Exportable library
β βββ cli/
β β βββ mod.rs # CLI definition with clap
β β βββ commands.rs # Command implementations
β β βββ interactive.rs # Interactive configuration (dialoguer)
β βββ executors/
β β βββ mod.rs
β β βββ base.rs # CliExecutor trait
β β βββ codex.rs # Codex executor
β β βββ gemini.rs # Gemini executor
β β βββ qwen.rs # Qwen executor
β βββ types/
β β βββ mod.rs
β β βββ config.rs # TOML configuration
β β βββ errors.rs # TetradError/TetradResult
β β βββ requests.rs # EvaluationRequest
β β βββ responses.rs # EvaluationResult, ModelVote
β βββ consensus/
β β βββ mod.rs # Exports
β β βββ engine.rs # ConsensusEngine
β β βββ aggregator.rs # Vote aggregation
β β βββ rules.rs # Voting rules
β βββ reasoning/
β β βββ mod.rs # Exports
β β βββ bank.rs # ReasoningBank
β β βββ patterns.rs # Pattern types
β β βββ sqlite.rs # SQLite storage
β β βββ export.rs # Import/Export
β βββ mcp/
β β βββ mod.rs # Exports
β β βββ server.rs # MCP server
β β βββ protocol.rs # JSON-RPC types
β β βββ tools.rs # Tool handlers
β β βββ transport.rs # Stdio transport
β βββ cache/
β β βββ mod.rs # Exports
β β βββ lru.rs # LRU cache
β βββ hooks/
β βββ mod.rs # Hook trait and HookSystem
β βββ builtin.rs # Default hooks
βββ tests/
βββ cli_integration.rs
βββ consensus_integration.rs
βββ mcp_integration.rs
βββ reasoning_integration.rs
Development
# Build
# Tests
# Lint
# Format
# Documentation
# Run CLI
Troubleshooting
"CLI not found"
# Check if CLIs are in PATH
# Check configuration
"stdin is not a terminal" (Codex)
Make sure your config uses exec --json:
[]
= ["exec", "--json"]
"Response does not contain valid JSON" (Gemini)
Make sure your config uses -o json:
[]
= ["-o", "json"]
Check MCP status in Claude Code
Inside Claude Code, run:
/mcp
Prerequisites
To use Tetrad, you need at least one of the AI CLIs installed and authenticated:
| CLI | Installation | Auth Without API Key? | Free Tier |
|---|---|---|---|
| Codex CLI | npm i -g @openai/codex |
β ChatGPT Plus/Pro/Business | Via subscription |
| Gemini CLI | npm i -g @google/gemini-cli |
β Google account | 1,000 req/day |
| Qwen CLI | npm i -g qwen-code |
β DashScope key required | 2,000 req/day |
Check availability with:
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
MIT
Author
SamoraDC
Links: