Labsco
dshills logo

Second Opinion

โ˜… 12

from dshills

Review commits and codebases using external LLMs like OpenAI, Google Gemini, and Mistral.

๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅโœ“ VerifiedAccount requiredAdvanced setup

Second Opinion ๐Ÿ”

An MCP (Model Context Protocol) server that assists Claude Code in reviewing commits and code bases. This tool leverages external LLMs (OpenAI, Google Gemini, Ollama, Mistral) to provide intelligent code review capabilities, git diff analysis, commit quality assessment, and uncommitted work analysis.

Features

  • Git Diff Analysis: Analyze git diff output to understand code changes using LLMs
  • Code Review: Review code for quality, security, and best practices with AI assistance
  • Commit Analysis: Analyze git commits for quality and adherence to best practices
  • Uncommitted Work Analysis: Analyze all uncommitted changes or just staged changes
  • Repository Information: Get information about git repositories
  • Multiple LLM Support: Works with OpenAI, Google Gemini, Ollama (local), and Mistral AI
  • ๐Ÿš€ Smart Optimization: Dynamic token allocation and task-specific temperature tuning
  • โšก Performance Tuning: Provider-specific optimizations and memory-aware chunking
  • Security: Input validation, secure path handling, and API key protection
  • Memory Safety: Configurable memory limits and streaming support for large diffs

Setting up with Claude Code

1. Locate Claude Code Configuration

The configuration file location depends on your operating system:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

2. Edit Configuration

Open the configuration file and add the Second Opinion server:

Option 1: Using JSON Configuration (Recommended)

{
  "mcpServers": {
    "second-opinion": {
      "command": "/path/to/second-opinion/bin/second-opinion"
    }
  }
}

Replace /path/to/second-opinion with the actual path where you cloned the repository.

Option 2: Using Environment Variables

{
  "mcpServers": {
    "second-opinion": {
      "command": "/path/to/second-opinion/bin/second-opinion",
      "env": {
        "DEFAULT_PROVIDER": "openai",
        "OPENAI_API_KEY": "your-openai-api-key",
        "OPENAI_MODEL": "gpt-5-mini",
        "LLM_TEMPERATURE": "0.3",
        "LLM_MAX_TOKENS": "4096"
      }
    }
  }
}

3. Restart Claude Code

After saving the configuration, restart Claude Code for the changes to take effect.

4. Verify Installation

In Claude Code, you should see "second-opinion" in the MCP servers list. You can test it by asking:

"What git repository information can you get from the current directory?"

Available Tools

1. analyze_git_diff ๐Ÿš€ Optimized

Analyzes git diff output to understand code changes using the configured LLM with automatic optimization.

Parameters:

  • diff_content (required): Git diff output to analyze
  • summarize (optional): Whether to provide a summary of changes
  • provider (optional): LLM provider to use (overrides default)
  • model (optional): Model to use (overrides provider default)

Smart Optimizations:

  • Dynamic Token Allocation: 4096-32768 tokens based on diff size
  • Temperature Tuning: 0.25 optimized for diff analysis
  • Chunking: Automatic chunking for large diffs (>10MB or >1000 files)
  • Provider-Specific: Custom parameters per LLM provider

Example in Claude Code:

"Analyze this git diff and tell me what changed: [paste diff here]"

2. review_code ๐Ÿš€ Optimized

Reviews code for quality, security, and best practices using the configured LLM with task-specific optimization.

Parameters:

  • code (required): Code to review
  • language (optional): Programming language of the code
  • focus (optional): Specific focus area - security, performance, style, or all
  • provider (optional): LLM provider to use (overrides default)
  • model (optional): Model to use (overrides provider default)

Smart Optimizations:

  • Task-Specific Temperature: 0.1 for security focus (high precision), 0.2 for general code review
  • Dynamic Token Allocation: Scales with code size for comprehensive analysis
  • Focus-Aware Analysis: Specialized prompts and parameters per focus area

Example in Claude Code:

"Review this Python code for security issues: [paste code here]"

3. analyze_commit ๐Ÿš€ Optimized

Analyzes a git commit for quality and adherence to best practices using the configured LLM with commit-specific optimization.

Parameters:

  • commit_sha (optional): Git commit SHA to analyze (default: HEAD)
  • repo_path (optional): Path to the git repository (default: current directory)
  • provider (optional): LLM provider to use (overrides default)
  • model (optional): Model to use (overrides provider default)

Smart Optimizations:

  • Commit Analysis Temperature: 0.2 for consistent, deterministic commit analysis
  • Memory-Safe Diff Processing: Handles large commits with automatic truncation
  • Combined Analysis: Includes commit message quality, diff analysis, and best practices

Example in Claude Code:

"Analyze the latest commit in this repository"
"Analyze commit abc123 and tell me if it follows best practices"

4. analyze_uncommitted_work ๐Ÿš€ Optimized

Analyzes uncommitted changes in a git repository to help prepare for commits with intelligent optimization.

Parameters:

  • repo_path (optional): Path to the git repository (default: current directory)
  • staged_only (optional): Analyze only staged changes (default: false, analyzes all uncommitted changes)
  • provider (optional): LLM provider to use (overrides default)
  • model (optional): Model to use (overrides provider default)

Smart Optimizations:

  • Code Review Temperature: 0.2 for balanced analysis of uncommitted changes
  • Large Changeset Handling: Automatic chunking for extensive modifications
  • Context-Aware Analysis: Tailored analysis for staged vs. all uncommitted work

LLM Analysis Includes:

  • Summary of all changes (files modified, added, deleted)
  • Type and nature of changes (feature, bugfix, refactor, etc.)
  • Completeness and readiness for commit
  • Potential issues or concerns
  • Suggested commit message(s) if changes are ready
  • Recommendations for organizing commits if changes should be split

Example in Claude Code:

"Analyze my uncommitted changes and suggest a commit message"
"Review only my staged changes before I commit"
"Should I split my current changes into multiple commits?"

5. get_repo_info

Gets information about a git repository (no LLM analysis).

Parameters:

  • repo_path (optional): Path to the git repository (default: current directory)

Example in Claude Code:

"Show me information about this git repository"

Security Features

  • Input Validation: All repository paths and commit SHAs are validated to prevent command injection
  • Path Restrictions: Repository paths must be within the current working directory
  • API Key Protection: API keys are never exposed in error messages or logs
  • HTTP Timeouts: All LLM API calls have 30-second timeouts to prevent hanging
  • Concurrent Access: Thread-safe provider management for concurrent requests

Optimization System ๐Ÿš€

Second Opinion includes a comprehensive optimization system that automatically tunes performance based on content and context:

Dynamic Token Allocation

  • 4096 tokens: Very small diffs (<5KB)
  • 6144 tokens: Small diffs (5-20KB)
  • 8192 tokens: Medium diffs (20-50KB)
  • 12288 tokens: Large diffs (50-150KB)
  • 16384 tokens: Very large diffs (150-500KB)
  • 32768 tokens: Huge diffs (>500KB)

Task-Specific Temperature Settings

  • 0.1: Security reviews (maximum precision)
  • 0.2: Code reviews and commit analysis (mostly deterministic)
  • 0.25: Diff analysis (slightly flexible)
  • 0.3: Architecture reviews (allows creativity)

Provider-Specific Optimizations

  • OpenAI: Full token allocation with top_p=0.9
  • Google: Capped at 8192 tokens with focused sampling (top_k=20, top_p=0.8)
  • Mistral: Conservative allocation with top_p=0.8
  • Ollama: Local model optimization with repeat_penalty=1.05

Memory Management

  • Automatic Chunking: Large diffs (>10MB or >1000 files) are intelligently split
  • Smart Chunk Sizing: Adapts chunk size based on file count
  • Memory-Aware Streaming: Enables streaming for large operations

Development

Project Structure

second-opinion/
โ”œโ”€โ”€ main.go              # MCP server setup and tool registration
โ”œโ”€โ”€ handlers.go          # Tool handler implementations
โ”œโ”€โ”€ validation.go        # Input validation functions
โ”œโ”€โ”€ config/              # Configuration loading and optimization
โ”‚   โ”œโ”€โ”€ config.go        # Main configuration with optimization methods
โ”‚   โ””โ”€โ”€ optimization_test.go # Comprehensive optimization tests
โ”œโ”€โ”€ llm/                 # LLM provider implementations
โ”‚   โ”œโ”€โ”€ provider.go      # Provider interface, prompts, and optimization wrapper
โ”‚   โ”œโ”€โ”€ openai.go        # OpenAI implementation
โ”‚   โ”œโ”€โ”€ google.go        # Google Gemini implementation
โ”‚   โ”œโ”€โ”€ ollama.go        # Ollama implementation with advanced options
โ”‚   โ””โ”€โ”€ mistral.go       # Mistral implementation with additional parameters
โ”œโ”€โ”€ CLAUDE.md           # Claude Code specific instructions
โ””โ”€โ”€ TODO.md             # Development roadmap

Running Tests

# Run all tests
go test ./... -v

# Run optimization tests specifically
go test ./config -v

# Run specific test suites
go test ./llm -v -run TestProviderConnections

# Run with race detection
go test -race ./...

# Run with coverage
go test -cover ./...

Linting

# Install golangci-lint if not already installed
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

# Run linter
golangci-lint run

# Auto-fix issues where possible
golangci-lint run --fix

Building

# Build for current platform
go build -o bin/second-opinion

# Build with race detector (for development)
go build -race -o bin/second-opinion

# Build for different platforms
GOOS=darwin GOARCH=amd64 go build -o bin/second-opinion-darwin-amd64
GOOS=linux GOARCH=amd64 go build -o bin/second-opinion-linux-amd64
GOOS=windows GOARCH=amd64 go build -o bin/second-opinion-windows-amd64.exe