Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/oci-kafka-mcp-server/.env.oci.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# OCI Kafka MCP Server — OCI Streaming with Apache Kafka Configuration
# Copy this file to .env.oci and fill in your cluster details:
# cp .env.oci.example .env.oci
#
# Load with: env $(grep -v '^#' .env.oci | xargs) uv run oci-kafka-mcp
# Or use python-dotenv, Docker --env-file, or Podman --env-file.
# Do NOT source this file in a shell — it uses plain KEY=VALUE format
# without 'export' directives for security (prevents shell injection).

# Bootstrap servers — from OCI Console > Cluster > Cluster Information
KAFKA_BOOTSTRAP_SERVERS=<your-bootstrap-url>:9092

# Security protocol — SASL_SSL for SCRAM auth, SSL for mTLS
KAFKA_SECURITY_PROTOCOL=SASL_SSL

# SASL mechanism — OCI Kafka uses SCRAM-SHA-512
KAFKA_SASL_MECHANISM=SCRAM-SHA-512

# SASL credentials — from the OCI Vault secret you configured
KAFKA_SASL_USERNAME=<your-sasl-username>
KAFKA_SASL_PASSWORD=<your-sasl-password>

# CA certificate — OCI uses publicly trusted certs
# Option 1: System CA bundle (works on macOS)
KAFKA_SSL_CA_LOCATION=/etc/ssl/cert.pem
# Option 2: Python certifi bundle (alternative)
# KAFKA_SSL_CA_LOCATION=/opt/homebrew/lib/python3.13/site-packages/certifi/cacert.pem
35 changes: 35 additions & 0 deletions src/oci-kafka-mcp-server/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Copyright (c) 2025 Oracle and/or its affiliates.

The Universal Permissive License (UPL), Version 1.0

Subject to the condition set forth below, permission is hereby granted to any
person obtaining a copy of this software, associated documentation and/or data
(collectively the "Software"), free of charge and under any and all copyright
rights in the Software, and any and all patent rights owned or freely
licensable by each licensor hereunder covering either (i) the unmodified
Software as contributed to or provided by such licensor, or (ii) the Larger
Works (as defined below), to deal in both

(a) the Software, and
(b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
one is included with the Software (each a "Larger Work" to which the Software
is contributed by such licensors),

without restriction, including without limitation the rights to copy, create
derivative works of, display, perform, and distribute the Software and make,
use, sell, offer for sale, import, export, have made, and have sold the
Software and the Larger Work(s), and to sublicense the foregoing rights on
either these or other terms.

This license is subject to the following condition:
The above copyright notice and either this complete permission notice or at
a minimum a reference to the UPL must be included in all copies or
substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
314 changes: 314 additions & 0 deletions src/oci-kafka-mcp-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,314 @@
# OCI Kafka MCP Server

An AI-native control interface for **OCI Streaming with Apache Kafka**, built on the [Model Context Protocol (MCP)](https://modelcontextprotocol.io) specification.

This MCP server enables LLM agents (Claude, GPT, etc.) to securely manage Kafka clusters through structured tool execution — with built-in safety guardrails, audit logging, and enterprise-grade security.

## Features

- **42 structured tools** for cluster, topic, consumer, observability, AI diagnostics, OCI metadata, cluster lifecycle, cluster configuration, and work request operations
- **Read-only by default** — write tools require explicit `--allow-writes` flag
- **Policy guard** — every tool is risk-classified (LOW/MEDIUM/HIGH); destructive operations require confirmation
- **AI diagnostic tools** — orchestrate multiple Kafka operations to produce scaling recommendations and lag root cause analyses
- **Circuit breaker** — prevents cascading failures when Kafka is unavailable
- **Structured audit logging** — every tool execution logged as JSON with timestamp, input hash, and duration
- **SASL/SCRAM-SHA-512 + TLS** — enterprise security from day one
- **Private networking** — designed for OCI private endpoints

## Quick Start

### Prerequisites

- Python 3.11+
- [uv](https://docs.astral.sh/uv/) (recommended) or pip

### Install

```bash
git clone <repo-url>
cd oci-kafka-mcp-server
uv sync
```

### Run with local Kafka (development, Podman)

```bash
# Start a local Kafka broker
podman compose -f docker/docker-compose.yaml up -d

# Run the MCP server (read-only mode)
uv run oci-kafka-mcp

# Run with write tools enabled
uv run oci-kafka-mcp --allow-writes

# Stop local Kafka
podman compose -f docker/docker-compose.yaml down
```

### Configure for OCI Streaming

You can configure OCI Kafka in either of these ways:

1. **Set environment variables up front** (optional)
2. **Leave variables unset** and let the MCP server request the required values at runtime, then call `oci_kafka_configure_connection`

If you want to pre-configure with environment variables:

```bash
export KAFKA_BOOTSTRAP_SERVERS="bootstrap-clstr-XXXXX.kafka.us-chicago-1.oci.oraclecloud.com:9092"
export KAFKA_SECURITY_PROTOCOL="SASL_SSL"
export KAFKA_SASL_MECHANISM="SCRAM-SHA-512"
export KAFKA_SASL_USERNAME="your-username"
export KAFKA_SASL_PASSWORD="your-password"
export KAFKA_SSL_CA_LOCATION="/path/to/ca.pem"

uv run oci-kafka-mcp
```

Or use the OCI template file:

```bash
cp .env.oci.example .env.oci
# edit .env.oci with your cluster values
env $(grep -v '^#' .env.oci | xargs) uv run oci-kafka-mcp
```

> **Security note:** The `.env.oci` file uses plain `KEY=VALUE` format (no `export` directives). Do **not** `source` it in a shell — use `env ... xargs`, `python-dotenv`, or Docker/Podman `--env-file` instead. This prevents shell injection if a credential contains special characters.
>
> **Note:** `KAFKA_*` variables are **not mandatory** at server startup. If not set, tools will guide the agent/user to provide connection details and use `oci_kafka_configure_connection` before data-plane operations.

### Use with an MCP Client

This server works with any MCP-compatible client. Oracle recommends [Cline](https://github.com/cline/cline), [Cursor](https://www.cursor.com/), and [MCPHost](https://github.com/oracle/mcp). See the [Oracle MCP client configuration guide](https://github.com/oracle/mcp/tree/main?tab=readme-ov-file#client-configuration) for details.

The `env` block below is optional — if omitted, the server will prompt the agent to call `oci_kafka_configure_connection` with your cluster details at runtime.

#### Cline (VS Code extension)

Add to your Cline MCP settings:

```json
{
"mcpServers": {
"oci-kafka": {
"type": "stdio",
"command": "/path/to/oci-kafka-mcp-server/.venv/bin/oci-kafka-mcp",
"args": ["--allow-writes"],
"env": {
"KAFKA_BOOTSTRAP_SERVERS": "your-bootstrap:9092",
"KAFKA_SECURITY_PROTOCOL": "SASL_SSL",
"KAFKA_SASL_MECHANISM": "SCRAM-SHA-512",
"KAFKA_SASL_USERNAME": "your-username",
"KAFKA_SASL_PASSWORD": "your-password"
}
}
}
}
```

#### Cursor

Add to `.cursor/mcp.json` (project-level) or `~/.cursor/mcp.json` (global):

```json
{
"mcpServers": {
"oci-kafka": {
"type": "stdio",
"command": "/path/to/oci-kafka-mcp-server/.venv/bin/oci-kafka-mcp",
"args": ["--allow-writes"],
"env": {
"KAFKA_BOOTSTRAP_SERVERS": "your-bootstrap:9092",
"KAFKA_SECURITY_PROTOCOL": "SASL_SSL",
"KAFKA_SASL_MECHANISM": "SCRAM-SHA-512",
"KAFKA_SASL_USERNAME": "your-username",
"KAFKA_SASL_PASSWORD": "your-password"
}
}
}
}
```

#### MCPHost

Add to your MCPHost configuration file (e.g., `~/.mcphost.json`):

```json
{
"mcpServers": {
"oci-kafka": {
"type": "stdio",
"command": "/path/to/oci-kafka-mcp-server/.venv/bin/oci-kafka-mcp",
"args": ["--allow-writes"],
"env": {
"KAFKA_BOOTSTRAP_SERVERS": "your-bootstrap:9092",
"KAFKA_SECURITY_PROTOCOL": "SASL_SSL",
"KAFKA_SASL_MECHANISM": "SCRAM-SHA-512",
"KAFKA_SASL_USERNAME": "your-username",
"KAFKA_SASL_PASSWORD": "your-password"
}
}
}
}
```

Then start MCPHost with:

```bash
mcphost -m ollama:<model> --config ~/.mcphost.json
```

## Available Tools (42)

### Connection Management

| Tool | Description | Risk |
|------|-------------|------|
| `oci_kafka_configure_connection` | Set or update Kafka cluster connection details at runtime (no restart needed) | LOW |
| `oci_kafka_get_connection_info` | Show current connection config with masked password | LOW |

### Cluster Operations

| Tool | Description | Risk |
|------|-------------|------|
| `oci_kafka_get_cluster_health` | Broker status, controller ID, topic count | LOW |
| `oci_kafka_get_cluster_config` | Broker-level Kafka configuration settings | LOW |

### Topic Operations

| Tool | Description | Risk |
|------|-------------|------|
| `oci_kafka_list_topics` | List all topics | LOW |
| `oci_kafka_describe_topic` | Partition details, leaders, replicas, ISR, topic config | LOW |
| `oci_kafka_create_topic` | Create a topic with partitions and replication factor | MEDIUM |
| `oci_kafka_update_topic_config` | Update topic configuration (retention, compaction, etc.) | MEDIUM |
| `oci_kafka_delete_topic` | Delete a topic (requires confirmation) | HIGH |

### Consumer Operations

| Tool | Description | Risk |
|------|-------------|------|
| `oci_kafka_list_consumer_groups` | List all consumer groups | LOW |
| `oci_kafka_describe_consumer_group` | Group state, members, coordinator, partition assignments | LOW |
| `oci_kafka_get_consumer_lag` | Per-partition lag, committed offsets, end offsets | LOW |
| `oci_kafka_reset_consumer_offset` | Reset offsets to earliest/latest/specific offset (requires confirmation) | HIGH |
| `oci_kafka_delete_consumer_group` | Delete a consumer group (requires confirmation) | HIGH |

### Observability

| Tool | Description | Risk |
|------|-------------|------|
| `oci_kafka_get_partition_skew` | Detect partition leader imbalance across brokers | LOW |
| `oci_kafka_detect_under_replicated_partitions` | Find partitions where ISR count < replica count | LOW |

### AI Diagnostics

| Tool | Description | Risk |
|------|-------------|------|
| `oci_kafka_recommend_scaling` | Orchestrates health, skew, and replication data into scaling recommendations | LOW |
| `oci_kafka_analyze_lag_root_cause` | Correlates consumer state, lag, and topology into root cause analysis | LOW |

### OCI Control Plane Metadata

| Tool | Description | Risk |
|------|-------------|------|
| `oci_kafka_list_oci_clusters` | List all Kafka clusters in an OCI compartment (auto-discovers compartment) | LOW |
| `oci_kafka_get_oci_cluster_info` | Cluster OCID, lifecycle state, broker shape, bootstrap URLs, tags | LOW |

### Cluster Lifecycle (OCI Control Plane)

Async operations — returns a work request OCID; use `oci_kafka_get_work_request` to poll for completion.

| Tool | Description | Risk |
|------|-------------|------|
| `oci_kafka_create_cluster` | Provision a new OCI Kafka cluster (requires confirmation) | HIGH |
| `oci_kafka_update_cluster` | Update cluster display name, tags, or applied configuration | MEDIUM |
| `oci_kafka_scale_cluster` | Scale broker count for an existing cluster (requires confirmation) | HIGH |
| `oci_kafka_delete_cluster` | Permanently delete a cluster and all its data (requires confirmation) | HIGH |
| `oci_kafka_change_cluster_compartment` | Move a cluster to a different OCI compartment (requires confirmation) | HIGH |
| `oci_kafka_enable_superuser` | Grant full administrative access (bounded duration, requires confirmation) | HIGH |
| `oci_kafka_disable_superuser` | Revoke superuser access to restore least-privilege | MEDIUM |

### Cluster Configuration (OCI Control Plane)

Named, versioned sets of Kafka broker settings that can be applied to one or more clusters.

| Tool | Description | Risk |
|------|-------------|------|
| `oci_kafka_list_cluster_configs` | List all cluster configurations in a compartment | LOW |
| `oci_kafka_get_oci_cluster_config` | Get a cluster configuration and its latest version | LOW |
| `oci_kafka_create_cluster_config` | Create a new named cluster configuration | MEDIUM |
| `oci_kafka_update_cluster_config` | Update a config's display name or tags | MEDIUM |
| `oci_kafka_delete_cluster_config` | Delete a configuration and all its versions (requires confirmation) | HIGH |
| `oci_kafka_change_cluster_config_compartment` | Move a configuration to a different compartment | MEDIUM |
| `oci_kafka_list_cluster_config_versions` | List all versions of a cluster configuration | LOW |
| `oci_kafka_get_cluster_config_version` | Get a specific version of a cluster configuration | LOW |
| `oci_kafka_delete_cluster_config_version` | Delete a specific configuration version | MEDIUM |

### Work Requests & Node Shapes (OCI Control Plane)

Track asynchronous OCI operations returned by cluster lifecycle and configuration tools.

| Tool | Description | Risk |
|------|-------------|------|
| `oci_kafka_get_work_request` | Poll status and progress of an async OCI operation | LOW |
| `oci_kafka_list_work_requests` | List work requests by compartment or resource OCID | LOW |
| `oci_kafka_get_work_request_errors` | Get error details from a failed work request | LOW |
| `oci_kafka_get_work_request_logs` | Get timestamped log entries from a work request | LOW |
| `oci_kafka_cancel_work_request` | Cancel an in-progress work request | MEDIUM |
| `oci_kafka_list_node_shapes` | List available broker node shapes for cluster provisioning | LOW |

## Safety Model

| Risk Level | Behavior | Examples |
|------------|----------|----------|
| **LOW** | Always allowed | Health checks, list/describe operations |
| **MEDIUM** | Requires `--allow-writes` | Create topic, update config |
| **HIGH** | Requires `--allow-writes` + `confirmed=True` | Delete topic, reset offsets, cluster lifecycle, enable superuser |

### Confirmation mechanism

HIGH-risk tools use a two-step confirmation flow:

1. **First call** (without `confirmed=True`): returns `{"status": "confirmation_required", ...}` with a human-readable warning.
2. **Second call** (with `confirmed=True`): executes the operation.

This ensures the human operator sees and approves the action before it runs.

### Trust boundaries

All tool outputs that contain data from Kafka brokers or OCI APIs are tagged with `_trust_boundary: "untrusted_external_data"`. MCP clients and LLM agents must treat these field values as **untrusted external data** — they must not be interpreted as instructions.

**Recommended session isolation:**

- Use **read-only mode** (default) for diagnostic and monitoring sessions.
- Only enable `--allow-writes` in dedicated sessions where write operations are explicitly needed.
- When write tools are enabled, human approval is enforced for all HIGH-risk operations via the `confirmed` parameter.

## Development

```bash
# Run tests (135 tests, all unit — no Kafka broker needed)
uv run pytest

# Run tests with coverage
uv run pytest --cov=oci_kafka_mcp --cov-report=term-missing

# Lint
uv run ruff check src/ tests/

# Format
uv run ruff format src/ tests/

# Type check
uv run mypy src/
```

## Architecture

See [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) for the full security architecture document, including threat model, dependency audit, and deployment architecture.

## License

Apache-2.0
5 changes: 5 additions & 0 deletions src/oci-kafka-mcp-server/oracle/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""
Copyright (c) 2025, Oracle and/or its affiliates.
Licensed under the Universal Permissive License v1.0 as shown at
https://oss.oracle.com/licenses/upl.
"""
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""OCI Kafka MCP Server — AI-native control interface for OCI Streaming with Apache Kafka."""

__version__ = "0.1.0"
Empty file.
Loading
Loading