MongoDB kubectl MCP服务器
一个MCP(模型上下文协议)服务器,通过以下方式连接到在Kubernetes中运行的MongoDB kubectl exec.处理gcloud身份验证、权限升级和客户端/命名空间选择。
特性
- 🔐 gcloud身份验证:自动gcloud身份验证设置
- ⬆️ 权限升级:升级到生产许可
- 🎯 多上下文支持:适用于多个Kubernetes上下文
- 👥 客户端命名空间:按命名空间选择客户端
- 🍃 MongoDB操作:通过kubectl exec查询MongoDB
先决条件
- Node.js 18+
kubectl配置了访问集群的权限gcloudCLI已安装并配置- MongoDB部署已命名
lim-mongo在您的命名空间中
安装
按照以下步骤安装所有必备组件并设置MCP服务器:
1.安装Node.js
macOS(使用Homebrew):
brew install node@18
# or for latest LTS
brew install nodemacOS(建议使用nvm):
# Install nvm if not already installed
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.bashrc # or restart terminal
# Install Node.js 18+
nvm install 18
nvm use 18Linux:
# Using NodeSource repository
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs验证安装:
node --version # Should be 18 or higher
npm --version2.安装kubectl
macOS(使用Homebrew):
brew install kubectlmacOS(使用curl):
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/arm64/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin/Linux:
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin/验证安装:
kubectl version --client3.安装gcloud命令行界面
macOS(建议使用Homebrew):
brew install --cask google-cloud-sdkmacOS(使用安装程序脚本):
# Download and install
curl https://sdk.cloud.google.com | bash
# Restart your shell or run:
exec -l $SHELL
# Initialize gcloud
gcloud initLinux:
# Add Cloud SDK distribution URI as a package source
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
# Import the Google Cloud Platform public key
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -
# Update and install
sudo apt-get update && sudo apt-get install google-cloud-cli验证安装:
gcloud --version4.配置gcloud身份验证
# Login to your Google account
gcloud auth login
# Set your default project
gcloud config set project apiiro
# Verify authentication
gcloud auth list5.为GKE集群配置kubectl
从GKE获取集群凭据:
选项A:获取特定集群的凭据
# Authenticate with the cluster
gcloud container clusters get-credentials CLUSTER_NAME \
--region REGION \
--project PROJECT_ID
# Example:
gcloud container clusters get-credentials my-cluster \
--region us-central1 \
--project my-project-id选项B:列出并连接到可用集群
# List all clusters in your project
gcloud container clusters list
# Get credentials for a cluster (will add it to your kubeconfig)
gcloud container clusters get-credentials CLUSTER_NAME --region REGION验证kubectl访问权限:
# List contexts (you should see your GKE cluster)
kubectl config get-contexts
# Test connection
kubectl get nodes对于多个集群:
# Get credentials for each cluster
gcloud container clusters get-credentials cluster-1 --region us-central1
gcloud container clusters get-credentials cluster-2 --region us-east1
# List all contexts
kubectl config get-contexts添加研发背景(Apiiro需要):
# Set the R&D project
gcloud config set project apiiro-rnd
# Get credentials for the R&D self-service cluster
gcloud container clusters get-credentials apiiro-rnd-self-service-us \
--region us-central1 \
--project apiiro-rnd
# Verify the R&D context was added
kubectl config get-contexts | grep apiiro-rnd
# Expected context name: gke_apiiro-rnd_us-central1_apiiro-rnd-self-service-us6.安装项目依赖项
克隆或导航到项目目录,然后:
# Install Node.js dependencies
npm install
# Build the project
npm run build7.验证完成设置
运行以下命令以验证所有配置是否正确:
# Check Node.js
node --version
# Check gcloud authentication
gcloud auth list
# Check kubectl can access clusters
kubectl config get-contexts
# Verify R&D context is available
kubectl config get-contexts | grep apiiro-rnd
# Test cluster access (switch to R&D context first)
kubectl config use-context gke_apiiro-rnd_us-central1_apiiro-rnd-self-service-us
kubectl get namespaces
# Verify project build
ls -la dist/index.js您现在可以使用MCP服务器了!继续 用法 部分开始。
用法
1.安装程序(先运行!)
使用 setup 用于验证和选择客户端的工具:
{
"escalate": true,
"context": "gke-project-region-cluster",
"client": "client-name-namespace"
}或者以交互方式运行-如果您不指定,该工具将列出可用选项。
2.查询MongoDB
设置完成后,使用MongoDB工具:
mongo_query:执行MongoDB查询
{
"query": "db.users.find().limit(5)"
}mongo_eval:计算JavaScript表达式
{
"expression": "db.stats()"
}3.列表选项
list_contexts:列出可用的Kubernetes上下文list_namespaces:在上下文中列出命名空间get_status:检查当前配置
MCP配置
克劳德桌面
服务器已在Claude Desktop配置中配置,位置为: ~/Library/Application Support/Claude/claude_desktop_config.json
如果需要手动添加:
{
"mcpServers": {
"mongo-kubectl": {
"command": "node",
"args": ["/Users/arielolin/mongo-kubectl-mcp/dist/index.js"]
}
}
}添加后,重新启动Claude Desktop 以使更改生效。
光标
添加到您的MCP设置中(例如。, ~/.cursor/mcp.json 或类似):
{
"mcpServers": {
"mongo-kubectl": {
"command": "node",
"args": ["/path/to/mongo-kubectl-mcp/dist/index.js"]
}
}
}建筑
- 设置工具:处理gcloud身份验证→ 升级→ 上下文选择→ 命名空间选择
- 状态管理:将配置保存到
config.json为了坚持 - kubectl 执行:用途
kubectl exec -it deploy/lim-mongo具有选定的上下文/命名空间 - 蒙古语:通过mongosh shell执行MongoDB命令
故障排除
- 未找到上下文:确保
kubectl已配置且上下文可用 - 身份验证错误:运行
gcloud auth login如果需要,手动 - 未找到命名空间:验证所选上下文中是否存在命名空间
- MongoDB连接失败:检查一下
lim-mongo命名空间中存在部署
