Kafka MCP Integration

An AI-powered Kafka monitoring and management system using the Model Context Protocol (MCP). This project enables natural language interactions with Apache Kafka clusters through AI assistants like Claude Desktop.

๐ŸŽฏ What This Does

Transform complex Kafka operations into simple conversations:

  • "How is my orders topic doing?" โ†’ Get comprehensive health analysis
  • "Are there any consumer lag issues?" โ†’ Receive intelligent lag analysis with recommendations
  • "Show me recent payment failures" โ†’ Inspect and analyze message patterns
  • "Should I scale my Kafka cluster?" โ†’ Get performance insights and scaling advice

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    MCP Protocol    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    Kafka APIs    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   AI Assistant  โ”‚โ—„โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บโ”‚  Kafka MCP      โ”‚โ—„โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บโ”‚  Kafka Cluster  โ”‚
โ”‚                 โ”‚   (stdio/HTTP)     โ”‚    Server       โ”‚  (Admin/Client)  โ”‚                 โ”‚
โ”‚ - Claude        โ”‚                    โ”‚                 โ”‚                  โ”‚ - Brokers       โ”‚
โ”‚ - Cursor        โ”‚                    โ”‚ - Tool Handlers โ”‚                  โ”‚ - Topics        โ”‚
โ”‚ - Custom Apps   โ”‚                    โ”‚ - Kafka Clients โ”‚                  โ”‚ - Partitions    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                    โ”‚ - Data Analysis โ”‚                  โ”‚ - Consumer Grps โ”‚
                                       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“ Project Structure

kafka-mcp-integration/
โ”œโ”€โ”€ docker/                     # Docker configurations
โ”‚   โ”œโ”€โ”€ docker-compose.yml      # Kafka cluster setup
โ”‚   โ””โ”€โ”€ kafka-setup.sh          # Kafka initialization
โ”œโ”€โ”€ mcp-server/                 # MCP server implementation
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ index.ts            # Server entry point
โ”‚   โ”‚   โ”œโ”€โ”€ server.ts           # MCP server logic
โ”‚   โ”‚   โ”œโ”€โ”€ tools/              # Kafka analysis tools
โ”‚   โ”‚   โ”œโ”€โ”€ kafka/              # Kafka client wrappers
โ”‚   โ”‚   โ””โ”€โ”€ types/              # TypeScript definitions
โ”‚   โ”œโ”€โ”€ package.json
โ”‚   โ””โ”€โ”€ tsconfig.json
โ”œโ”€โ”€ mcp-client/                 # MCP client for testing
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ index.ts            # Client entry point
โ”‚   โ”‚   โ”œโ”€โ”€ client.ts           # MCP client implementation
โ”‚   โ”‚   โ””โ”€โ”€ demo.ts             # Demo scenarios
โ”‚   โ”œโ”€โ”€ package.json
โ”‚   โ””โ”€โ”€ tsconfig.json
โ”œโ”€โ”€ config/                     # Configuration files
โ”‚   โ”œโ”€โ”€ kafka.json              # Kafka settings
โ”‚   โ””โ”€โ”€ mcp-server.json         # MCP server config
โ”œโ”€โ”€ scripts/                    # Automation scripts
โ”‚   โ”œโ”€โ”€ setup.ps1               # Complete setup
โ”‚   โ”œโ”€โ”€ start-kafka.ps1         # Start Kafka cluster
โ”‚   โ”œโ”€โ”€ start-mcp-server.ps1    # Start MCP server
โ”‚   โ””โ”€โ”€ demo.ps1                # Run demo scenarios
โ””โ”€โ”€ README.md

๐Ÿš€ Quick Start

Prerequisites

  • Docker Desktop (running)
  • Node.js (v16 or higher)
  • Claude Desktop (for AI integration)

1. Setup & Build

# Clone and navigate to project
git clone <repository-url>
cd kafka-mcp-integration

# Clean build everything
cd mcp-server
Remove-Item -Recurse -Force dist, node_modules -ErrorAction SilentlyContinue
npm install && npm run build

cd ../mcp-client  
Remove-Item -Recurse -Force dist, node_modules -ErrorAction SilentlyContinue
npm install && npm run build

cd ..

2. Start Infrastructure

# Run setup script
./scripts/setup.ps1

# Start Kafka cluster
./scripts/start-kafka.ps1

# Wait for Kafka to be ready
Start-Sleep -Seconds 45

3. Create Test Data

# Create sample topics
docker exec kafka kafka-topics --create --topic orders --bootstrap-server localhost:29092 --partitions 3 --replication-factor 1
docker exec kafka kafka-topics --create --topic payments --bootstrap-server localhost:29092 --partitions 2 --replication-factor 1

# Produce sample messages
docker exec kafka bash -c 'echo "{\"order_id\":\"order_001\",\"amount\":99.99,\"status\":\"completed\"}" | kafka-console-producer --topic orders --bootstrap-server localhost:29092'

4. Test MCP Tools

# Start MCP server
./scripts/start-mcp-server.ps1

# In new terminal, test client
cd mcp-client
node dist/index.js

# Try commands:
# check_topic_health {"topic": "orders"}
# analyze_consumer_lag {"topic": "orders"}
# inspect_messages {"topic": "orders", "limit": 5}
# analyze_broker_performance {}

5. Claude Desktop Integration

Create %APPDATA%\Claude\claude_desktop_config.json:

{
  "mcpServers": {
    "kafka-mcp": {
      "command": "node",
      "args": [
        "C:\\path\\to\\kafka-mcp-integration\\mcp-server\\dist\\index.js"
      ],
      "env": {
        "KAFKA_BROKERS": "localhost:29092",
        "NODE_ENV": "production"
      }
    }
  }
}

Restart Claude Desktop and start chatting with your Kafka cluster!

๐Ÿ› ๏ธ Available Tools

analyze_consumer_lag

Analyzes consumer group lag across topics and partitions.

Usage: {"topic": "orders"}

Returns: Detailed lag analysis with recommendations

check_topic_health

Comprehensive health check for Kafka topics.

Usage: {"topic": "orders"}

Returns: Partition health, replication status, and issues

inspect_messages

Samples and analyzes recent messages from topics.

Usage: {"topic": "orders", "limit": 10}

Returns: Recent messages with metadata and patterns

analyze_broker_performance

Monitors broker metrics and cluster performance.

Usage: {}

Returns: Broker status, metrics, and performance insights

๐ŸŽฎ Demo Scenarios

Basic Health Check

You: "How is my Kafka cluster doing?"
Claude: "Let me check your cluster health..."
[Runs multiple tools and provides comprehensive analysis]

Consumer Lag Investigation

You: "My order processing seems slow, what's wrong?"
Claude: "I'll analyze your consumer lag..."
[Identifies bottlenecks and suggests optimizations]

Message Pattern Analysis

You: "Are there any suspicious payment patterns?"
Claude: "Let me examine recent payment messages..."
[Analyzes messages and identifies anomalies]

๐Ÿ”ง Configuration

Kafka Connection

Edit config/kafka.json:

{
  "brokers": ["localhost:29092"],
  "clientId": "kafka-mcp-server",
  "connectionTimeout": 3000
}

MCP Server Settings

Edit config/mcp-server.json:

{
  "transport": "stdio",
  "tools": {
    "enabled": ["analyze_consumer_lag", "check_topic_health", "inspect_messages", "analyze_broker_performance"]
  }
}

๐Ÿ› Troubleshooting

MCP Server Times Out

# Check compilation
cd mcp-server
npx tsc --noEmit

# Verify Kafka connection
docker exec kafka kafka-topics --list --bootstrap-server localhost:29092

Port Conflicts

# Check port usage
netstat -an | findstr ":29092"
netstat -an | findstr ":9092"

Claude Desktop Connection Issues

  1. Verify config file location: %APPDATA%\Claude\claude_desktop_config.json
  2. Check file paths are absolute and correct
  3. Restart Claude Desktop after config changes
  4. Look for MCP connection indicator in Claude Desktop

๐Ÿงน Cleanup

# Stop all services
taskkill /F /IM node.exe

# Clean Docker
cd docker
docker-compose down --volumes --remove-orphans
docker system prune -f

# Clean builds
Remove-Item -Recurse -Force mcp-server\dist, mcp-client\dist -ErrorAction SilentlyContinue

๐Ÿ“ License

MIT License - see LICENSE file for details

๐Ÿ”— Links


Transform your Kafka operations with the power of AI! ๐Ÿš€