使用说明
MCP Kubernetes操作员
用于管理的Kubernetes操作符 模型上下文协议(MCP) 服务器、工具、提示和资源。
概述
此运算符允许动态注册通过Kubernetes中运行的MCP服务器公开的MCP工具、提示和资源。它使用基于标签的选择来自动发现和配置MCP组件。
特性
- MCP服务器 -部署具有自动扩展功能的高可用MCP服务器Pod
- MCP工具 -注册代理Kubernetes服务的工具
- MCPPrompt -定义带有变量替换的提示模板
- MCP资源 -通过HTTP操作或内联内容公开资源
- 基于标签的发现 -MCPServer通过标签选择器自动查找组件
- Redis支持状态 -会话、缓存和速率限制
快速开始
先决条件
- Kubernetes集群(k3d、kind或远程)
- kubectl已配置
- Python 3.11+
- 诗歌
安装
# Clone the repository
git clone https://github.com/yourorg/mcp-operator.git
cd mcp-operator
# Install dependencies
make dev
# Create a k3d cluster (optional, for local development)
make k3d-create
# Install CRDs
make k3d-crds
# Install Redis
make k3d-redis
# Run the operator locally
make run
部署示例
# Create an MCP server that selects tools with label "mcp-server: main"
apiVersion: kubemcp.io/v1alpha1
kind: MCPServer
metadata:
name: main
namespace: mcp-system
spec:
replicas: 2
redis:
serviceName: mcp-redis
toolSelector:
matchLabels:
mcp-server: main
---
# Register a tool
apiVersion: kubemcp.io/v1alpha1
kind: MCPTool
metadata:
name: github-search
namespace: mcp-system
labels:
mcp-server: main
spec:
name: github-search
description: Search GitHub repositories
service:
name: github-tool-svc
port: 8080
path: /search
inputSchema:
type: object
required: [query]
properties:
query:
type: string
kubectl apply -f example.yaml
发展
命令
make dev # Install dependencies
make lint # Run linter
make format # Format code
make test # Run unit tests (80% coverage required)
make test-fast # Run tests without coverage
make check # Run all checks (lint, format, typecheck)
make run # Run operator locally
k3d集群
make k3d-create # Create local cluster
make k3d-crds # Install CRDs
make k3d-redis # Deploy Redis
make k3d-examples # Deploy example resources
make k3d-delete # Tear down cluster
Docker构建
要构建和推送多架构映像(amd64+arm64):
# Build and push to a registry (requires docker buildx)
make docker-build-multiarch REGISTRY=ghcr.io/yourorg IMAGE=mcp-operator TAG=latest
部署
使用Kustomize覆盖:
# Local k3d
make kustomize-k3d
# Production
make kustomize-prod
跨命名空间访问
网络策略将流量限制为 mcp-system 默认情况下。允许Pod从
另一个到达MCP服务器和Redis的命名空间,标记该命名空间:
kubectl label namespace <namespace> kubemcp.io/allow-cross-namespace=true
k3d覆盖层为所有Pod打开MCP服务器入口,以进行本地开发。
操作员API服务器出口自动从进行协调 Endpoints/kubernetes.
要明确地固定它,请设置 MCP_OPERATOR_API_SERVER_CIDR 关于操作员部署
(例如: 192.168.0.10/32).
适用于无选择器后端(包括 ExternalName 服务),添加出口提示
在 MCPTool 或 MCPResource:
metadata:
annotations:
kubemcp.io/egress-mode: cidr # selector|namespace|cidr
kubemcp.io/egress-cidrs: 1.2.3.4/32,10.0.0.0/24
kubemcp.io/egress-ports: "443,8443" # optional; defaults to service port
建筑
┌─────────────────────────────────────────────────────────────────┐
│ MCP Operator │
├─────────────────────────────────────────────────────────────────┤
│ MCPServer Controller MCPTool Controller │
│ MCPPrompt Controller MCPResource Controller │
└───────────────┬─────────────────────────────────────────────────┘
│ watches/creates
▼
┌─────────────────────────────────────────────────────────────────┐
│ Kubernetes Resources │
├─────────────────────────────────────────────────────────────────┤
│ Deployment (MCP Server Pods) Service Ingress ConfigMap │
└───────────────┬─────────────────────────────────────────────────┘
│ connects to
▼
┌─────────────────────────────────────────────────────────────────┐
│ Runtime │
├─────────────────────────────────────────────────────────────────┤
│ Redis (sessions, cache, rate limits) Backing Services │
└─────────────────────────────────────────────────────────────────┘
CRD
| CRD | 描述 |
|---|---|
MCPServer |
部署MCP服务器吊舱,管理入口,选择工具/提示/资源 |
MCPTool |
定义引用Kubernetes服务的工具 |
MCPPrompt |
使用定义提示模板 {{variable}} 占位符 |
MCPResource |
使用HTTP操作或内联内容定义资源 |
配置
MCP服务器
spec:
replicas: 3 # Number of MCP server pods
redis:
serviceName: mcp-redis # Redis service for state
ingress:
host: kubemcp.io # Ingress hostname
tlsSecretName: mcp-tls # TLS secret
pathPrefix: /mcp # Base path
toolSelector:
matchLabels:
mcp-server: main # Select tools/prompts/resources
config:
requestTimeout: 30s
maxConcurrentRequests: 100
MCP工具
spec:
name: my-tool # Tool name (unique per server)
description: Does something
service:
name: backend-svc # K8s service to proxy to
port: 8080
path: /api/tool
inputSchema: {} # JSON Schema for validation
method: POST # HTTP method
MCPPrompt
spec:
name: my-prompt
template: |
Hello {{name}}, your query is: {{query}}
variables:
- name: name
required: true
- name: query
default: "none"
MCP资源
# Service-backed resource
spec:
name: docs
operations:
- method: GET
ingressPath: /resources/docs/{id}
service:
name: docs-svc
port: 8080
path: /api/docs/{id}
# Inline content
spec:
name: config-template
content:
uri: "config://template"
mimeType: text/yaml
text: |
key: value
许可证
麻省理工学院
