Pattern-Learning-MCP-Server

Anmol369/Pattern-Learning-MCP-Server

3.2

If you are the rightful owner of Pattern-Learning-MCP-Server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.

The Pattern Learning MCP Server is designed to enhance AI code generation by learning from developer corrections, significantly reducing verification time.

Tools
4
Resources
0
Prompts
0

🎯 Pattern Learning MCP Server - Demo

A demonstration of verification time reduction through pattern learning for AI code generation

📋 Overview

This demo showcases how an MCP (Model Context Protocol) server can help AI agents like Goose learn from developer corrections, reducing verification time from 20 minutes to 2 minutes (90% reduction).

The Problem

  • AI generates code with security vulnerabilities
  • Developers spend significant time reviewing and fixing
  • Same mistakes happen repeatedly

The Solution

  • Store correction patterns when developers fix AI-generated code
  • Retrieve and apply these patterns in future code generation
  • Reduce verification time dramatically

🚀 Quick Start

1. Install Dependencies

cd pattern-learning-demo
pip install -r requirements.txt

2. Run the Demo

Option A: Python Script (Recommended for Windows)

python demo_script.py

Option B: Bash Script (Linux/Mac)

chmod +x demo_script.sh
./demo_script.sh

3. Test the MCP Server

python mcp_server.py

The server will start and initialize the pattern database with security patterns.

📊 Demo Flow

Before Pattern Learning (Attempt 1)

  1. Developer asks: "Create Flask login endpoint"
  2. Goose generates code with timing attack vulnerability (password == stored_password)
  3. Developer spends 20 minutes reviewing and fixing
  4. Pattern stored in database

After Pattern Learning (Attempt 2)

  1. Developer asks: "Create Flask login endpoint" (same prompt)
  2. Goose calls MCP server to get security patterns
  3. Goose generates code with secrets.compare_digest() from the start
  4. Developer verifies in 2 minutes
  5. 90% time reduction achieved

🏗️ Architecture

┌─────────────────────────────────────────┐
│        GOOSE AGENT                      │
│  (calls MCP tools via JSON-RPC)        │
└───────────────┬─────────────────────────┘
                │
                ▼
┌─────────────────────────────────────────┐
│   PATTERN LEARNING MCP SERVER           │
│                                         │
│  Tools:                                 │
│  • get_security_patterns()             │
│  • validate_code_security()            │
│  • store_correction_pattern()          │
│  • get_database_stats()                │
└───────────────┬─────────────────────────┘
                │
                ▼
┌─────────────────────────────────────────┐
│     SQLite Pattern Database             │
│  (patterns.db)                          │
└─────────────────────────────────────────┘

🔧 Components

1. pattern_db.py - Database Layer

  • SQLite-based pattern storage
  • Bayesian confidence scoring
  • Pattern retrieval and statistics

2. security_patterns.py - Pattern Definitions

  • Pre-defined security patterns
  • Vulnerability detection (regex-based)
  • Context-aware recommendations

3. mcp_server.py - MCP Server

  • Exposes tools via MCP protocol
  • Handles pattern storage and retrieval
  • Integrates with Goose

4. demo_script.py - Interactive Demo

  • Shows before/after comparison
  • Visualizes time savings
  • Demonstrates the value proposition

📈 Results

MetricBeforeAfterImprovement
Generation Time30 sec30 sec-
Verification Time20 min2 min90%
Vulnerabilities1 (HIGH)0100%
Developer ConfidenceLowHigh

🎯 Key Features

Automatic Pattern Learning - Learns from every correction ✅ Context-Aware - Recommends patterns based on task type ✅ Confidence Scoring - Bayesian updates strengthen patterns over time ✅ Multiple Categories - Security, performance, style, etc. ✅ MCP Integration - Works seamlessly with Goose ✅ Zero Configuration - SQLite, no external database needed

🔒 Security Patterns Included

  1. Timing Attack Prevention

    • Detects: password == stored_password
    • Fix: secrets.compare_digest(password, stored_password)
  2. SQL Injection Prevention

    • Detects: String formatting in SQL queries
    • Fix: Parameterized queries
  3. Hardcoded Secrets Detection

    • Detects: API_KEY = "secret123"
    • Fix: os.environ.get("API_KEY")
  4. Insecure Random Prevention

    • Detects: random.randint()
    • Fix: secrets.token_hex()

🧪 Testing

# Run tests (when implemented)
pytest tests/

📝 Configuration for Goose

To use this MCP server with Goose, add to your Goose configuration:

{
  "mcp_servers": {
    "pattern-learning": {
      "command": "python",
      "args": ["C:/Users/aadis/goose-pattern-demo/pattern-learning-demo/mcp_server.py"]
    }
  }
}

🎬 Demo Script Output

The demo shows:

  1. Attempt 1 - Vulnerable code generation and 20-minute fix
  2. Pattern Storage - Correction saved to database
  3. Attempt 2 - Secure code generated automatically
  4. Results - Time savings and metrics

💡 Future Enhancements

  • AST-based vulnerability detection (more accurate)
  • Vector embeddings for pattern similarity search
  • Multi-agent coordination
  • Autonomous prompt evolution
  • Community pattern sharing
  • Performance pattern categories
  • Style guide enforcement

📞 Support

For questions or issues:

📄 License

MIT License - Feel free to use and modify


Built for the Goose Grant Interview

Demonstrating verification time reduction through experiential learning.