MCP部署
用于在Kubernetes上部署和管理MCP(模型上下文协议)服务器的Go库。
特性
- 交互式CLI向导 便于MCP服务器部署
- MCP服务器部署的抽象接口
- Kubernetes实现与部署和服务创建
- 自动标签
mcp.opendatahub.io/mcp-server标签 - 支持:
- 自定义图像和端口 - 环境变量(简单值或秘密引用) - 命令行参数 - 秘密坐骑 - 服务账户 - 资源限制和请求(CPU和内存) - 自定义标签和注释
安装
安装库
go get github.com/grs/mcp-deployment构建CLI向导
cd cmd/wizard
go build -o wizard或者从项目根目录:
go build -o wizard ./cmd/wizard项目结构
.
├── cmd/
│ └── wizard/ # Interactive CLI tool
│ └── main.go
├── pkg/
│ └── deployer/ # Main library package
│ ├── deployer.go # Interface and type definitions
│ └── simple_deployer.go # Simple Kubernetes implementation
├── examples/
│ └── basic/ # Basic usage example
│ └── main.go
├── go.mod
├── go.sum
├── README.md
├── CONTRIBUTING.md
└── .gitignore用法
使用CLI向导(建议用于入门)
该向导提供了一个交互式命令行界面,用于部署和管理MCP服务器:
./wizard向导将向您显示一个菜单:
=== MCP Server Deployment Wizard ===
1. List MCP servers
2. Deploy new MCP server
3. Delete MCP server
4. Exit
Select an option:列出MCP服务器:选择选项1并输入命名空间以查看所有已部署的MCP服务器。
部署新的MCP服务器:选择选项2,向导将交互式提示您:
- 服务器名称和命名空间
- 集装箱图像和港口
- 环境变量(可选择简单值或秘密引用)
- 命令行参数
- 秘密挂载(秘密的卷挂载)
- 服务账号
- 标签和注释
- 资源限制和请求(CPU和内存)
对于环境变量,向导会询问每个变量是否应该:
- 价值:一个简单的字符串值
- 秘密:对Kubernetes秘密的引用(您将提供秘密名称和密钥)
删除MCP服务器:选择选项3删除现有的MCP服务器。向导将:
- 列出命名空间中所有可用的MCP服务器
- 提示删除服务器名称
- 在继续之前显示警告并要求确认
程序化使用
对于Go应用程序中的编程使用:
创建部署程序
import (
"github.com/grs/mcp-deployment/pkg/deployer"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
// Create Kubernetes client
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
// handle error
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
// handle error
}
// Create MCP deployer
mcpDeployer := deployer.NewSimpleDeployer(clientset)部署MCP服务器
import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
spec := &deployer.MCPServerSpec{
Name: "my-mcp-server",
Namespace: "default",
Image: "my-mcp-image:latest",
Port: 8080,
EnvVars: []corev1.EnvVar{
{
Name: "API_KEY",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "api-credentials",
},
Key: "key",
},
},
},
},
Args: []string{"--verbose", "--config=/etc/config.yaml"},
SecretMounts: []deployer.SecretMount{
{
SecretName: "mcp-config",
MountPath: "/etc/config",
},
},
ServiceAccount: "mcp-server-sa",
Labels: map[string]string{
"app": "my-mcp-server",
"environment": "production",
},
Annotations: map[string]string{
"description": "Production MCP server",
},
Resources: &corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("100m"),
corev1.ResourceMemory: resource.MustParse("128Mi"),
},
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("500m"),
corev1.ResourceMemory: resource.MustParse("256Mi"),
},
},
}
err := mcpDeployer.DeployMCPServer(context.Background(), spec)
if err != nil {
// handle error
}列出MCP服务器
servers, err := mcpDeployer.ListMCPServers(context.Background(), "default")
if err != nil {
// handle error
}
for _, server := range servers {
fmt.Printf("Name: %s\n", server.Name)
fmt.Printf("Namespace: %s\n", server.Namespace)
fmt.Printf("Image: %s\n", server.Image)
fmt.Printf("Available: %t\n", server.Available)
fmt.Println("Conditions:")
for _, condition := range server.Conditions {
fmt.Printf(" - %s\n", condition)
}
}删除MCP服务器
err := mcpDeployer.DeleteMCPServer(context.Background(), "default", "my-mcp-server")
if err != nil {
// handle error
}接口
这 MCPDeployer 接口提供了三种主要方法:
type MCPDeployer interface {
// DeployMCPServer creates a Deployment and Service for an MCP server
DeployMCPServer(ctx context.Context, spec *MCPServerSpec) error
// ListMCPServers lists all MCP servers in the specified namespace
ListMCPServers(ctx context.Context, namespace string) ([]MCPServerStatus, error)
// DeleteMCPServer deletes an MCP server (Deployment and Service) by name
DeleteMCPServer(ctx context.Context, namespace, name string) error
}自动标注
所有部署的MCP服务器都会自动标记为 mcp.opendatahub.io/mcp-server=true 除了您提供的任何自定义标签。此标签用于标识和列出MCP服务器部署。
