Labsco
tuannvm logo

Kafka MCP Server

β˜… 52

from tuannvm

An MCP server for Apache Kafka, enabling LLMs to perform Kafka operations like producing and consuming messages.

πŸ”₯πŸ”₯πŸ”₯πŸ”₯βœ“ VerifiedFreeAdvanced setup

Kafka MCP Server

A Model Context Protocol (MCP) server for Apache Kafka implemented in Go, leveraging franz-go and mcp-go.

This server provides an implementation for interacting with Kafka via the MCP protocol, enabling LLM models to perform common Kafka operations through a standardized interface.

Go Report Card GitHub Workflow Status Go Version Trivy Scan SLSA 3 Go Reference Docker Image GitHub Release License: MIT

Overview

The Kafka MCP Server bridges the gap between LLM models and Apache Kafka, allowing them to:

  • Produce and consume messages from topics
  • List, describe, and manage topics
  • Monitor and manage consumer groups
  • Assess cluster health and configuration
  • Execute standard Kafka operations

All through the standardized Model Context Protocol (MCP).

Architecture

Copy & paste β€” that's it
graph TB
    subgraph "MCP Client (AI Applications)"
        A[Claude Desktop]
        B[Cursor]
        C[Windsurf]
        D[ChatWise]
    end
    
    subgraph "Kafka MCP Server"
        E[MCP Protocol Handler]
        F[Tools Registry]
        G[Resources Registry]
        H[Prompts Registry]
        I[Kafka Client Wrapper]
    end
    
    subgraph "Apache Kafka Cluster"
        J[Broker 1]
        K[Broker 2]
        L[Broker 3]
        M[Topics & Partitions]
        N[Consumer Groups]
    end
    
    A --> E
    B --> E
    C --> E
    D --> E
    
    E --> F
    E --> G
    E --> H
    
    F --> I
    G --> I
    H --> I
    
    I --> J
    I --> K
    I --> L
    
    J --> M
    K --> M
    L --> M
    
    J --> N
    K --> N
    L --> N
    
    classDef client fill:#e1f5fe
    classDef mcp fill:#f3e5f5
    classDef kafka fill:#fff3e0
    
    class A,B,C,D client
    class E,F,G,H,I mcp
    class J,K,L,M,N kafka

How it works:

  1. MCP Clients (AI applications) connect to the Kafka MCP Server via stdio or HTTP transport
  2. MCP Server exposes three types of capabilities:
    • Tools - Direct Kafka operations (produce/consume messages, describe topics, etc.)
    • Resources - Cluster health reports and diagnostics
    • Prompts - Pre-configured workflows for common operations
  3. Kafka Client Wrapper handles all Kafka communication using the franz-go library
  4. Apache Kafka Cluster processes the actual message streaming and storage

Transport Modes:

  • STDIO: Default mode, ideal for local MCP clients (Claude Desktop, Cursor, etc.)
  • HTTP: Enables remote access with optional OAuth 2.1 authentication

Tools

Prompts & Resources

Key Features

  • Kafka Integration: Implementation of common Kafka operations via MCP
  • Security:
    • Support for SASL (PLAIN, SCRAM-SHA-256, SCRAM-SHA-512) and TLS authentication
    • OAuth 2.1 authentication for HTTP transport (Native and Proxy modes)
    • Support for Okta, Google, Azure AD, and HMAC providers
  • Flexible Transport: STDIO for local clients, HTTP for remote access
  • Error Handling: Error handling with meaningful feedback
  • Configuration Options: Customizable for different environments
  • Pre-Configured Prompts: Set of prompts for common Kafka operations
  • Compatibility: Works with MCP-compatible LLM models

MCP Tools

The server exposes the following tools for Kafka interaction. For detailed documentation including examples and sample responses, see docs/tools.md.

  • produce_message: Produces messages to Kafka topics
  • consume_messages: Consumes messages from Kafka topics in batch operations
  • list_brokers: Lists all configured Kafka broker addresses
  • describe_topic: Provides comprehensive metadata for specific topics
  • list_consumer_groups: Enumerates all consumer groups in the cluster
  • describe_consumer_group: Provides detailed consumer group information including lag metrics
  • describe_configs: Retrieves configuration settings for Kafka resources
  • cluster_overview: Provides comprehensive cluster health summaries
  • list_topics: Lists all topics with metadata including partition and replication information

MCP Resources

The server provides the following resources that can be accessed through the MCP protocol. For detailed documentation including example responses, see docs/resources.md.

  • kafka-mcp://overview: Comprehensive cluster health summary
  • kafka-mcp://health-check: Detailed health assessment with actionable insights
  • kafka-mcp://under-replicated-partitions: Analysis of partitions with replication issues
  • kafka-mcp://consumer-lag-report: Consumer performance analysis with customizable thresholds

MCP Prompts

The server includes the following pre-configured prompts for Kafka operations and diagnostics. For detailed documentation including arguments and example responses, see docs/prompts.md.

  • kafka_cluster_overview: Generates comprehensive cluster health summaries
  • kafka_health_check: Performs detailed health assessments with actionable recommendations
  • kafka_under_replicated_partitions: Analyzes replication issues with troubleshooting guidance
  • kafka_consumer_lag_report: Provides consumer lag analysis with performance optimization recommendations

End-to-End Example

Here's an interaction example showing how an AI assistant might use these tools to help a user:

User Query: "We need to investigate why our order processing pipeline is falling behind. Can you help me check the consumer lag?"

AI Assistant's workflow:

  1. First, check the consumer groups

    Copy & paste β€” that's it
    > Using list_consumer_groups tool
    > Discovers "order-processor" group
  2. Then, examine the consumer group details

    Copy & paste β€” that's it
    > Using describe_consumer_group tool with group_id "order-processor" and include_offsets=true
    > Discovers lag on several partitions
  3. Get cluster overview to check general health

    Copy & paste β€” that's it
    > Using cluster_overview tool
    > Confirms no under-replicated partitions or offline brokers
  4. Generate a consumer lag report

    Copy & paste β€” that's it
    > Using consumer_lag_report prompt with threshold=1000
    > Provides detailed analysis of lag across consumer groups
  5. Provide actionable recommendations:

    Copy & paste β€” that's it
    Based on my analysis, your order-processor consumer group has significant lag on topic orders, partitions 2 and 3, with lag values of 15,420 and 12,305 messages respectively.
    
    The cluster itself is healthy with no under-replicated partitions, suggesting this is an issue with the consumer application rather than Kafka infrastructure.
    
    I recommend:
    1. Check the consumer instances processing those partitions for slowdowns or errors
    2. Consider scaling up your consumer group by adding more instances
    3. Review the consumer configuration, particularly max.poll.records and max.poll.interval.ms
    4. Examine any processing bottlenecks in the consumer logic

This seamless workflow demonstrates how the Kafka MCP tools enable LLM models to perform sophisticated diagnostics and provide actionable insights.

Security Considerations

The server is designed with enterprise-grade security in mind:

  • Authentication:
    • Kafka: Full support for SASL PLAIN, SCRAM-SHA-256, and SCRAM-SHA-512
    • MCP Server: OAuth 2.1 authentication for HTTP transport (Okta, Google, Azure AD, HMAC)
  • Encryption: TLS support for secure communication with Kafka brokers
  • Input Validation: Thorough validation of all user inputs to prevent injection attacks
  • Error Handling: Secure error handling that doesn't expose sensitive information
  • Token Security: Bearer token validation with 5-minute caching for OAuth-protected endpoints

For OAuth security best practices, see docs/oauth.md.

Development

Testing

Comprehensive test coverage ensures reliability:

Copy & paste β€” that's it
# Run all tests (requires Docker for integration tests)
go test ./...

# Run tests excluding integration tests
go test -short ./...

# Run integration tests with specific Kafka brokers
export KAFKA_BROKERS="your-broker:9092"
export SKIP_KAFKA_TESTS="false"
go test ./kafka -v -run Test

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.