MCP环境代理
一个动态MCP(模型上下文协议)代理,允许动态切换环境变量。与其为不同的AWS帐户/区域配置多个MCP服务器,不如使用可以动态切换上下文的单个代理。
问题
当使用多个AWS帐户或Kubernetes集群时,每个环境通常需要一个MCP服务器:
{
"mcpServers": {
"eks-production": { "env": { "AWS_PROFILE": "prod" } },
"eks-staging": { "env": { "AWS_PROFILE": "staging" } },
"eks-dev": { "env": { "AWS_PROFILE": "dev" } }
}
}这会消耗上下文,需要重新启动Claude以切换环境。
解决方案
MCP环境代理提供了一个可以按需在上下文之间切换的MCP服务器:
{
"mcpServers": {
"mcp-env-proxy": {
"command": "uvx",
"args": ["mcp-env-proxy@latest", "-c", "~/.config/mcp-env-proxy/contexts.yaml"]
}
}
}然后动态切换上下文:
> switch_context("eks-production")
> list_k8s_resources(...)
> switch_context("eks-staging")
> list_k8s_resources(...)安装
# With uvx (recommended)
uvx mcp-env-proxy@latest
# With pip
pip install mcp-env-proxy
# From source
git clone https://github.com/KamorionLabs/mcp-env-proxy.git
cd mcp-env-proxy
pip install -e .配置
创建一个 contexts.yaml 文件:
# Default environment variables for all contexts
defaults:
FASTMCP_LOG_LEVEL: ERROR
# Define MCP server types
servers:
eks:
command: uvx
args: ["awslabs.eks-mcp-server@latest"]
ecs:
command: uvx
args: ["awslabs.ecs-mcp-server@latest"]
# Named contexts
contexts:
production:
server: eks
env:
AWS_PROFILE: WordPress-Production/AWSAdministratorAccess
AWS_REGION: eu-west-3
staging:
server: eks
env:
AWS_PROFILE: WordPress-Staging/AWSAdministratorAccess
AWS_REGION: eu-west-3
homebox-prod:
server: ecs
env:
AWS_PROFILE: homebox-production/AdministratorAccess
AWS_REGION: eu-west-3
# Default context on startup
current_context: production配置文件位置(按优先级顺序):
MCP_ENV_PROXY_CONFIG环境变量./contexts.yaml(当前目录)~/.config/mcp-env-proxy/contexts.yaml
可用工具
list_contexts
列出所有可用上下文及其配置。
switch_context(context_name)
切换到其他上下文。这将使用指定的环境变量加载MCP服务器。
get_current_context
获取有关当前活动上下文的信息。
list_proxied_tools
列出当前上下文的MCP服务器中可用的所有工具。
proxy_tool(tool_name, arguments)
调用代理MCP服务器上的工具。
进程池
代理维护一个MCP服务器进程池(默认值:5)。切换上下文时:
- 如果上下文之前已加载,它将重用现有进程(快速)
- 如果这是一个新的上下文,它会产生一个新过程
- 如果池已满,它将驱逐最旧的未使用进程
这提供了快速的上下文切换,同时限制了内存使用。
使用Claude代码
添加到您的Claude Code MCP配置中:
claude mcp add mcp-env-proxy -- uvx mcp-env-proxy@latest -c ~/.config/mcp-env-proxy/contexts.yaml然后在对话中:
User: Switch to the production EKS cluster
Claude: [calls switch_context("production")]
User: List the pods in the wordpress namespace
Claude: [calls proxy_tool("list_k8s_resources", {"resource_type": "pods", "namespace": "wordpress"})]发展
# Clone the repository
git clone https://github.com/KamorionLabs/mcp-env-proxy.git
cd mcp-env-proxy
# Create virtual environment
python -m venv .venv
source .venv/bin/activate
# Install in development mode
pip install -e ".[dev]"
# Run tests
pytest
# Run locally
python -m mcp_env_proxy -c config/contexts.example.yaml -v许可证
MIT许可证-请参阅 许可证 了解详情。
贡献
欢迎投稿!请在GitHub上打开问题或PR。
