|
| 1 | +# MCP Server Hardening Guide |
| 2 | + |
| 3 | +Deployment guidance for running MCP tool servers securely, aligned with |
| 4 | +[OWASP MCP Security Cheat Sheet §3 — Sandbox & Isolate MCP Servers](https://cheatsheetseries.owasp.org/cheatsheets/MCP_Security_Cheat_Sheet.html). |
| 5 | + |
| 6 | +## Transport: prefer stdio over HTTP |
| 7 | + |
| 8 | +When the MCP server runs on the same host as the agent, use **stdio** transport |
| 9 | +rather than HTTP/SSE. This eliminates the network attack surface entirely — |
| 10 | +no open ports, no TLS configuration, no SSRF vectors. |
| 11 | + |
| 12 | +```yaml |
| 13 | +# docker-compose.yml — stdio transport |
| 14 | +services: |
| 15 | + mcp-server: |
| 16 | + image: myregistry/mcp-tools:1.2.3@sha256:abc... |
| 17 | + stdin_open: true |
| 18 | + read_only: true |
| 19 | + security_opt: ["no-new-privileges"] |
| 20 | +``` |
| 21 | +
|
| 22 | +For HTTP transport, require mTLS between agent and server (see §6). |
| 23 | +
|
| 24 | +## Kubernetes: securityContext |
| 25 | +
|
| 26 | +Every MCP server pod should run as a non-root user with a read-only root |
| 27 | +filesystem and all capabilities dropped: |
| 28 | +
|
| 29 | +```yaml |
| 30 | +apiVersion: v1 |
| 31 | +kind: Pod |
| 32 | +metadata: |
| 33 | + name: mcp-server |
| 34 | +spec: |
| 35 | + securityContext: |
| 36 | + runAsNonRoot: true |
| 37 | + runAsUser: 65534 # nobody |
| 38 | + runAsGroup: 65534 |
| 39 | + fsGroup: 65534 |
| 40 | + seccompProfile: |
| 41 | + type: RuntimeDefault |
| 42 | + containers: |
| 43 | + - name: mcp-tools |
| 44 | + image: myregistry/mcp-tools:1.2.3@sha256:abc... |
| 45 | + securityContext: |
| 46 | + allowPrivilegeEscalation: false |
| 47 | + readOnlyRootFilesystem: true |
| 48 | + capabilities: |
| 49 | + drop: ["ALL"] |
| 50 | + resources: |
| 51 | + limits: |
| 52 | + cpu: "500m" |
| 53 | + memory: "256Mi" |
| 54 | + volumeMounts: |
| 55 | + - name: tmp |
| 56 | + mountPath: /tmp |
| 57 | + volumes: |
| 58 | + - name: tmp |
| 59 | + emptyDir: |
| 60 | + sizeLimit: 50Mi |
| 61 | +``` |
| 62 | +
|
| 63 | +## Network Isolation: NetworkPolicy |
| 64 | +
|
| 65 | +Restrict MCP servers so they can **only** communicate with the agent |
| 66 | +orchestrator and required backends (database, blob storage). Block all |
| 67 | +egress to the public internet and to the cloud metadata service: |
| 68 | +
|
| 69 | +```yaml |
| 70 | +apiVersion: networking.k8s.io/v1 |
| 71 | +kind: NetworkPolicy |
| 72 | +metadata: |
| 73 | + name: mcp-server-policy |
| 74 | +spec: |
| 75 | + podSelector: |
| 76 | + matchLabels: |
| 77 | + app: mcp-server |
| 78 | + policyTypes: [Ingress, Egress] |
| 79 | + ingress: |
| 80 | + - from: |
| 81 | + - podSelector: |
| 82 | + matchLabels: |
| 83 | + app: agent-orchestrator |
| 84 | + ports: |
| 85 | + - port: 8080 |
| 86 | + protocol: TCP |
| 87 | + egress: |
| 88 | + # Allow DNS |
| 89 | + - to: |
| 90 | + - namespaceSelector: {} |
| 91 | + ports: |
| 92 | + - port: 53 |
| 93 | + protocol: UDP |
| 94 | + # Allow specific backends |
| 95 | + - to: |
| 96 | + - podSelector: |
| 97 | + matchLabels: |
| 98 | + app: postgres |
| 99 | + ports: |
| 100 | + - port: 5432 |
| 101 | + protocol: TCP |
| 102 | + # Block cloud metadata (SSRF protection) |
| 103 | + # Azure IMDS: 169.254.169.254 |
| 104 | + # AWS IMDS: 169.254.169.254 |
| 105 | + # GCP metadata: metadata.google.internal (100.100.100.200) |
| 106 | + # These are blocked by default when no egress rule matches. |
| 107 | +``` |
| 108 | + |
| 109 | +## gVisor / Kata Containers for Untrusted Servers |
| 110 | + |
| 111 | +For MCP servers that execute arbitrary code (code interpreters, shell tools), |
| 112 | +use a sandbox runtime like [gVisor](https://gvisor.dev/) or |
| 113 | +[Kata Containers](https://katacontainers.io/): |
| 114 | + |
| 115 | +```yaml |
| 116 | +# AKS with gVisor runtime class |
| 117 | +apiVersion: node.k8s.io/v1 |
| 118 | +kind: RuntimeClass |
| 119 | +metadata: |
| 120 | + name: gvisor |
| 121 | +handler: runsc |
| 122 | +--- |
| 123 | +apiVersion: v1 |
| 124 | +kind: Pod |
| 125 | +metadata: |
| 126 | + name: mcp-code-interpreter |
| 127 | +spec: |
| 128 | + runtimeClassName: gvisor |
| 129 | + containers: |
| 130 | + - name: interpreter |
| 131 | + image: myregistry/code-interpreter:1.0@sha256:def... |
| 132 | + securityContext: |
| 133 | + allowPrivilegeEscalation: false |
| 134 | + readOnlyRootFilesystem: true |
| 135 | + capabilities: |
| 136 | + drop: ["ALL"] |
| 137 | +``` |
| 138 | +
|
| 139 | +On **Azure Kubernetes Service (AKS)**: |
| 140 | +- Enable the [Kata Container node pool](https://learn.microsoft.com/azure/aks/use-katacontainers) for VM-level isolation. |
| 141 | +- Use [Azure Container Instances (ACI)](https://learn.microsoft.com/azure/container-instances/) with Hyper-V isolation for per-tool ephemeral sandboxes. |
| 142 | +
|
| 143 | +## File System Restrictions |
| 144 | +
|
| 145 | +MCP tools should only access explicitly mounted paths: |
| 146 | +
|
| 147 | +```yaml |
| 148 | +volumeMounts: |
| 149 | + - name: workspace |
| 150 | + mountPath: /workspace |
| 151 | + readOnly: false # only if tool needs write |
| 152 | + - name: config |
| 153 | + mountPath: /config |
| 154 | + readOnly: true |
| 155 | +``` |
| 156 | +
|
| 157 | +Combine with the `.NET SDK path traversal sanitization pattern` |
| 158 | +(`SanitizationDefaults.AllPatterns` detects `../` sequences) to prevent |
| 159 | +escape even if mounts are misconfigured. |
| 160 | + |
| 161 | +## Resource Limits |
| 162 | + |
| 163 | +Prevent a compromised tool from consuming cluster resources: |
| 164 | + |
| 165 | +| Resource | Recommendation | |
| 166 | +|----------|---------------| |
| 167 | +| CPU | 500m limit per tool pod | |
| 168 | +| Memory | 256Mi limit (512Mi for code interpreters) | |
| 169 | +| Ephemeral storage | 50Mi via emptyDir sizeLimit | |
| 170 | +| Process count | `pids-limit` cgroup (64 for simple tools) | |
| 171 | +| Network bandwidth | Use Cilium/Calico bandwidth annotations | |
| 172 | + |
| 173 | +## Checklist |
| 174 | + |
| 175 | +- [ ] Non-root user (`runAsNonRoot: true`) |
| 176 | +- [ ] Read-only root filesystem |
| 177 | +- [ ] All capabilities dropped |
| 178 | +- [ ] seccomp profile enabled (`RuntimeDefault`) |
| 179 | +- [ ] NetworkPolicy restricts ingress + egress |
| 180 | +- [ ] Cloud metadata IPs blocked (169.254.169.254) |
| 181 | +- [ ] Resource limits set (CPU, memory, storage) |
| 182 | +- [ ] gVisor/Kata for code execution tools |
| 183 | +- [ ] stdio transport where possible |
| 184 | +- [ ] Container images use SHA digest tags |
| 185 | +- [ ] `.NET SDK McpGateway` sanitization + response scanning enabled |
| 186 | + |
| 187 | +## Related |
| 188 | + |
| 189 | +- [McpGateway](../../packages/agent-governance-dotnet/README.md#mcp-protocol-support) — 5-stage governance pipeline |
| 190 | +- [McpSecurityScanner](../../packages/agent-governance-dotnet/README.md#mcp-protocol-support) — tool definition scanning |
| 191 | +- [OWASP MCP Security Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/MCP_Security_Cheat_Sheet.html) |
0 commit comments