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
50 changes: 50 additions & 0 deletions cluster/images/build-mcp.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright 2024 The Karmada Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Stage 1: Builder
# Use the Go version specified in go.mod (1.24)
FROM golang:1.24-alpine AS builder

# Install make, git and bash
# git is used to clone the repository, make is used to execute build commands, and bash is used to execute the build.sh script
RUN apk add --no-cache make git bash

# Set the working directory
WORKDIR /src

# Clone the specified git repository
RUN git clone https://github.com/warjiang/karmada-mcp-server.git .

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

For better security and build reproducibility, it's recommended to avoid cloning from a personal repository and to pin the dependency to a specific version.

  1. Repository: The build clones from warjiang/karmada-mcp-server, which is a personal repository. This can be a security and maintenance risk. It would be safer to use a repository under the official karmada-io organization.
  2. Versioning: The git clone command uses the default branch, which means the build can change unexpectedly. Pinning to a specific git tag or commit hash ensures that builds are reproducible.
RUN git clone https://github.com/warjiang/karmada-mcp-server.git . && git checkout <specific-version-tag>


# Set the target operating system and architecture for multi-platform builds
# These parameters are provided by the `docker buildx build` command at build time
ARG TARGETOS
ARG TARGETARCH

# Use the Makefile to build the binary, passing in the platform parameters
# This will call the project's own hack/build.sh script
RUN make build GOOS=${TARGETOS} GOARCH=${TARGETARCH}

# Move the compiled binary to the /src directory for easy copying in the next stage
RUN cp /src/_output/bin/${TARGETOS}/${TARGETARCH}/karmada-mcp-server /src/karmada-mcp-server

# Stage 2: Final Image
# Use the scratch image as the final base image
FROM scratch

# Copy the compiled binary from the builder stage to the final image
# The binary has been moved to /src/karmada-mcp-server in the previous stage
COPY --from=builder /src/karmada-mcp-server /karmada-mcp-server

# Set the executable to be executed when the container starts
ENTRYPOINT ["/karmada-mcp-server"]
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ require (
github.com/gobuffalo/flect v1.0.3
github.com/golang-jwt/jwt/v5 v5.3.0
github.com/karmada-io/karmada v1.15.0
github.com/mark3labs/mcp-go v0.26.0
github.com/prometheus/client_golang v1.22.0
github.com/prometheus/common v0.65.0
github.com/sashabaranov/go-openai v1.40.5
github.com/spf13/cobra v1.9.1
github.com/spf13/pflag v1.0.6
golang.org/x/net v0.40.0
Expand Down Expand Up @@ -104,10 +106,12 @@ require (
github.com/rogpeppe/go-internal v1.13.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/samber/lo v1.47.0 // indirect
github.com/spf13/cast v1.7.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
go.opentelemetry.io/otel v1.35.0 // indirect
go.opentelemetry.io/otel/trace v1.35.0 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ github.com/evanphx/json-patch/v5 v5.9.11 h1:/8HVnzMq13/3x9TPvjG08wUGqBTmZBsCWzjT
github.com/evanphx/json-patch/v5 v5.9.11/go.mod h1:3j+LviiESTElxA4p3EMKAB9HXj3/XEtnUf6OZxqIQTM=
github.com/exponent-io/jsonpath v0.0.0-20210407135951-1de76d718b3f h1:Wl78ApPPB2Wvf/TIe2xdyJxTlb6obmF18d8QdkxNDu4=
github.com/exponent-io/jsonpath v0.0.0-20210407135951-1de76d718b3f/go.mod h1:OSYXu++VVOHnXeitef/D8n/6y4QV8uLHSFXX4NeXMGc=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv5E=
Expand Down Expand Up @@ -156,6 +158,8 @@ github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN
github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mailru/easyjson v0.9.0 h1:PrnmzHw7262yW8sTBwxi1PdJA3Iw/EKBa8psRf7d9a4=
github.com/mailru/easyjson v0.9.0/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU=
github.com/mark3labs/mcp-go v0.26.0 h1:xz/Kv1cHLYovF8txv6btBM39/88q3YOjnxqhi51jB0w=
github.com/mark3labs/mcp-go v0.26.0/go.mod h1:rXqOudj/djTORU/ThxYx8fqEVj/5pvTuuebQ2RC7uk4=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
Expand Down Expand Up @@ -210,8 +214,12 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/samber/lo v1.47.0 h1:z7RynLwP5nbyRscyvcD043DWYoOcYRv3mV8lBeqOCLc=
github.com/samber/lo v1.47.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU=
github.com/sashabaranov/go-openai v1.40.5 h1:SwIlNdWflzR1Rxd1gv3pUg6pwPc6cQ2uMoHs8ai+/NY=
github.com/sashabaranov/go-openai v1.40.5/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
Expand All @@ -238,6 +246,8 @@ github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/xlab/treeprint v1.2.0 h1:HzHnuAF1plUN2zGlAFHbSQP2qJ0ZAD3XF5XD7OesXRQ=
github.com/xlab/treeprint v1.2.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd/WEJu0=
github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=
github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
Expand Down
132 changes: 132 additions & 0 deletions pkg/mcp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Karmada Assistant

AI-powered chat functionality for Karmada dashboard with MCP (Model Context Protocol) tool integration.

## 📋 Overview

The Karmada Assistant provides intelligent chat capabilities that can interact with Karmada clusters through MCP tools. It supports both legacy chat (without tools) and modern chat (with real-time cluster tool integration), enabling users to manage and query their Karmada multi-cluster environments through natural language.

## 🚀 Quick Start

### Prerequisites

1. **Karmada Cluster**: A running Karmada control plane
2. **MCP Server**: Karmada MCP server binary
3. **OpenAI API Key**: For AI model access
4. **Kubeconfig**: Valid Karmada cluster configuration

## 💬 Usage Examples

### Basic Cluster Queries

**List all clusters:**
```
User: "Show me all clusters"
Assistant: [Executes list_clusters tool and displays cluster information]
```

**Get cluster status:**
```
User: "What's the status of my member clusters?"
Assistant: [Retrieves and displays cluster health and status information]
```

**Check resource distribution:**
```
User: "How are my workloads distributed across clusters?"
Assistant: [Shows workload distribution and resource allocation]
```

## ⚙️ Configuration

### Command Line Flags

The MCP and OpenAI integration is now configured via command line flags:

```bash
# Complete configuration example
./karmada-dashboard-api \
--enable-mcp=true \
--mcp-transport-mode=stdio \
--mcp-server-path=/usr/local/bin/karmada-mcp-server \
--karmada-kubeconfig=/path/to/karmada.config \
--karmada-context=karmada-apiserver \
--openai-api-key=sk-xxxx \
--openai-model=gpt-4 \
--openai-endpoint=https://api.openai.com/v1

# Minimal MCP configuration with stdio mode
./karmada-dashboard-api \
--enable-mcp=true \
--mcp-server-path=/usr/local/bin/karmada-mcp-server \
--karmada-kubeconfig=/path/to/karmada.config \
--openai-api-key=sk-xxxx

# MCP with SSE mode
./karmada-dashboard-api \
--enable-mcp=true \
--mcp-transport-mode=sse \
--mcp-sse-endpoint=http://localhost:1234/mcp/sse \
--karmada-kubeconfig=/path/to/karmada.config \
--openai-api-key=sk-xxxx
```

#### Configuration Flags:

**MCP Configuration:**
- `--enable-mcp`: Enable MCP integration (default: false)
- `--mcp-transport-mode`: Transport mode - "stdio" or "sse" (default: "stdio")
- `--mcp-server-path`: Path to MCP server binary (required for stdio mode)
- `--mcp-sse-endpoint`: SSE endpoint URL (required for sse mode)

**OpenAI Configuration:**
- `--openai-api-key`: OpenAI API key (required for AI functionality)
- `--openai-model`: OpenAI model name (default: "gpt-3.5-turbo")
- `--openai-endpoint`: OpenAI API endpoint (default: "https://api.openai.com/v1")

**Karmada Configuration:**
- `--karmada-kubeconfig`: Path to Karmada kubeconfig (used by MCP)
- `--karmada-context`: Karmada context name (used by MCP, default: "karmada-apiserver")

### Migration from Environment Variables

❌ **Removed**: The following environment variables are no longer supported:

**MCP Related:**
- `ENABLE_MCP` → Use `--enable-mcp` flag
- `MCP_TRANSPORT_MODE` → Use `--mcp-transport-mode` flag
- `KARMADA_MCP_SERVER_PATH` → Use `--mcp-server-path` flag
- `MCP_SSE_ENDPOINT` → Use `--mcp-sse-endpoint` flag
- `KUBECONFIG` → Use `--karmada-kubeconfig` flag
- `KARMADA_CONTEXT` → Use `--karmada-context` flag

**OpenAI Related:**
- `OPENAI_API_KEY` → Use `--openai-api-key` flag
- `OPENAI_MODEL` → Use `--openai-model` flag
- `OPENAI_ENDPOINT` → Use `--openai-endpoint` flag

### MCP Server Setup

The assistant requires a Karmada MCP server to provide cluster management tools:
> 📢 Note: The MCP server is currently under active development and not yet published via GitHub Releases.
> You'll need to build it from source.

```bash
# Clone the MCP server repository
git clone https://github.com/warjiang/karmada-mcp-server.git
cd karmada-mcp-server

# Build the server
go build -o karmada-mcp-server ./cmd/karmada-mcp-server

# (Optional) Move it to a directory in your PATH
sudo mv karmada-mcp-server /usr/local/bin/

# Verify installation
karmada-mcp-server --help

# Test MCP server (optional)
./karmada-mcp-server stdio \
--karmada-kubeconfig=/path/to/karmada.config \
--karmada-context=karmada-apiserver
```
Loading