I work at Solo.io, which makes agentgateway.
To force MCP traffic through a gateway on Kubernetes, apply a NetworkPolicy that denies all egress from your agent and MCP server namespaces, then add back only DNS and the agentgateway Service. The gateway is then the one route out, and its own allowlist decides which MCP servers and hosts are reachable. If you found an agent pod calling a remote MCP server directly, this is the fix, and why shadow MCP servers are hard to track is the longer story of how it got there.
A gateway nobody has to use is a suggestion. On Kubernetes, the thing that makes it mandatory is a boring set of NetworkPolicies. Most of them fail quietly: the CNI does not enforce them, DNS breaks, or an allow rule somewhere else opens the door, because policies only add. This page is the exact set, in the order to apply it, and the tests that show it blocks. The stop page covers the order to put controls in. This page builds out the egress step.
Every result on this page comes from one test run on Nov 22, 2026: kind v0.30.0, Kubernetes v1.34.0, a CNI that enforces NetworkPolicy, and the agentgateway v1.7.0 release binary as a CONNECT egress proxy. Hostnames like example.com stand in for remote MCP servers. The network layer does not know what MCP is, and it does not need to.
What does default-deny egress mean in Kubernetes?
A pod is open for egress until some NetworkPolicy selects it and lists Egress in policyTypes. From then on, it may only send traffic that one of the selecting policies allows. A policy that selects pods, lists Egress, and has no egress rules allows nothing. That is default deny.
Pods that no policy selects stay wide open. So the deny has to cover every namespace where agents or MCP servers run, not just the one you were looking at. The Kubernetes NetworkPolicy docs spell this out, and they add the catch that matters most: a NetworkPolicy object does nothing unless your network plugin implements it.
Show me the deny-all egress policy for an agent namespace
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-egress
namespace: agents
spec:
podSelector: {}
policyTypes:
- Egress
Apply one per namespace: one in agents, one in mcp-servers. The empty podSelector selects every pod in the namespace.
This is the only Kubernetes object most people apply that has no rules in it. The missing list is the whole point.
Right after this policy went in, the agent pod could not do anything:
$ kubectl -n agents exec agent -- curl -sS -o /dev/null -w '%{http_code}\n' --connect-timeout 5 --max-time 8 https://example.com/
000
curl: (28) Resolving timed out after 5001 milliseconds
Note the error. It is not a blocked connection. The pod could not even resolve the name, because DNS is egress too.
How do you keep DNS working?
Add a second policy that allows UDP and TCP port 53 to the cluster DNS pods, and nothing else:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns
namespace: agents
spec:
podSelector: {}
policyTypes:
- Egress
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: kube-system
podSelector:
matchLabels:
k8s-app: kube-dns
ports:
- protocol: UDP
port: 53
- protocol: TCP
port: 53
After this, nslookup example.com from the agent pod answered again, and the direct HTTPS call changed from a resolve timeout to Connection timed out after 5002 milliseconds. That is the deny working on the connection itself.
DNS is still a way out. A pod that can query the cluster resolver can push data into names the resolver forwards upstream. Your agent egress proxy never saw the DNS query is that channel and its fix. The core NetworkPolicy API has no hostname rules, so this policy can pin the resolver but cannot judge the names.
How do you allow only agentgateway?
Allow egress to the agentgateway pods in their namespace, on the gateway’s listener port only:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-agentgateway
namespace: agents
spec:
podSelector: {}
policyTypes:
- Egress
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: agentgateway-system
podSelector:
matchLabels:
app.kubernetes.io/name: agentgateway
ports:
- protocol: TCP
port: 3000
Port 3000 is the gateway’s CONNECT listener in this setup. It is the gateway’s port, not 443 on the internet. If your agents talk to agentgateway over HTTP routes instead, use that listener’s port.
The namespaceSelector and podSelector sit in one list item. That makes them AND: pods with that label, in that namespace. Split them into two list items and they become OR:
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: agentgateway-system
- podSelector:
matchLabels:
app.kubernetes.io/name: agentgateway
The wrong version allows every pod in agentgateway-system, on every port, plus any pod with the gateway label in the agent’s own namespace. The test cluster had a second pod in the gateway namespace that was not the gateway. With the OR version, the agent reached it:
$ kubectl -n agents exec agent -- curl -sS -o /dev/null -w '%{http_code}\n' --connect-timeout 5 --max-time 8 http://$IP:8080/
200
With the AND version, the same call got curl: (28) Connection timed out after 5002 milliseconds. One hyphen made the difference. When in doubt, kubectl describe networkpolicy shows how Kubernetes read it.
How does the gateway reach the MCP servers it is allowed to?
The gateway namespace needs its own policy, or it becomes the open door. Deny all egress there too, then give only the gateway pods DNS and the outbound path they need:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: gateway-outbound
namespace: agentgateway-system
spec:
podSelector:
matchLabels:
app.kubernetes.io/name: agentgateway
policyTypes:
- Egress
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: kube-system
podSelector:
matchLabels:
k8s-app: kube-dns
ports:
- {protocol: UDP, port: 53}
- {protocol: TCP, port: 53}
- to:
- ipBlock:
cidr: 0.0.0.0/0
except:
- 10.0.0.0/8
- 172.16.0.0/12
- 192.168.0.0/16
ports:
- {protocol: TCP, port: 443}
This lets the gateway open TCP 443 to public addresses and nothing private. The policy cannot tell one public host from another. That is agentgateway’s job: in the test, its CONNECT listener had an allowlist of two hostnames, matched on TLS SNI, and every other name had no route. agentgateway as an egress proxy has that config and its own proof run. For MCP servers inside the cluster, add a rule to their Service pods by label and port instead of the ipBlock.
Which servers belong on the allowlist should not be a judgment call made in YAML. Generate the gateway’s targets from a registry of record, so the list has an owner per row. Even with the gateway as the only path, egress decided at CONNECT time is the part that keeps a workload from steering the decision.
In what order do NetworkPolicies apply?
They have no order. Every policy that selects a pod adds allowed traffic, and nothing in the core API denies a flow that another policy allows. So one broad “allow egress” policy anywhere in the namespace wins over all the work above.
The test showed it. With the full set in place, one extra policy went into agents:
spec:
podSelector: {}
policyTypes:
- Egress
egress:
- {}
$ kubectl -n agents get networkpolicy
NAME POD-SELECTOR AGE
allow-agentgateway <none> 72s
allow-all-egress <none> 5s
allow-dns <none> 103s
default-deny-egress <none> 2m19s
$ kubectl -n agents exec agent -- curl -sS -o /dev/null -w '%{http_code}\n' --connect-timeout 5 --max-time 8 https://example.com/
200
The deny was still there. It just stopped mattering. After kubectl delete on that one policy, the same call timed out again. Before you trust a deny, run kubectl get networkpolicy -A and read every policy that selects your agent pods, including ones a Helm chart installed.
Why did my egress policy not block anything?
Check these, in this order:
- Your CNI does not enforce NetworkPolicy. The object is accepted and ignored. Check your CNI’s docs for policy support. Enforcement can also depend on the node kernel. The first cluster for this test used kind’s default CNI, which logged that it could not open the netfilter queue it uses for policy, because the test box’s kernel did not have that module. The run moved to a CNI that could enforce on that kernel. Test with a control namespace, below, before you believe anything.
- The pod is not selected. A
podSelectorwith labels the pod does not have selects nothing. Runkubectl get pods --show-labelsand compare. - Another policy allows it. Policies add up. See the previous section.
- The pod uses the host network. How NetworkPolicy treats
hostNetwork: truepods depends on your CNI. Check your CNI. - The test ran from the wrong namespace. A pod in a namespace with no policy gets out, which is exactly what it should do.
How do you prove MCP traffic can only leave through the gateway?
Run these from inside the cluster after the full set is applied. Each row is from the Nov 22 run, with the agent pod in agents unless noted.
| # | Test | Command (short form) | Expected | Result in the run |
|---|---|---|---|---|
| 1 | Direct call to a remote host times out | curl https://example.com/ | timeout | curl: (28) Connection timed out after 5002 milliseconds |
| 2 | Same call through agentgateway, approved host | curl --proxy http://agentgateway.agentgateway-system.svc.cluster.local:3000 https://example.com/ | 200 | 200 |
| 3 | Unapproved host through agentgateway | same proxy, https://pypi.org/ | denied | curl: (35) TLS connect error; gateway log error="route not found" tls.sni=pypi.org |
| 4 | DNS still resolves | nslookup example.com | answer | Name: example.com |
| 5 | A pod in a namespace with no policy still gets out | curl https://example.com/ from control | 200 | 200 |
Test 5 is the one people skip. It proves the policy blocked the traffic, not a broken network. The same direct call from control returned 200 at every stage where it was tested, while the agent pod’s result changed with each policy.
Test 3 fails in a way that can look like a network problem. In CONNECT tunnel mode the gateway accepts the tunnel and then has no route for the name, so the client sees a TLS error, not a 403. Read the gateway log to see the deny. If the gateway is in place and shadow traffic still shows up, check the traffic goes through the gateway at all.
The mcp-servers namespace got the same three policies. Its direct call timed out the same way as the agent’s.
What does default-deny egress not cover?
- Laptops. A cluster policy does nothing for a developer machine with MCP servers on employee machines.
- Vendor-hosted connectors. If the connector runs in a vendor’s cloud, the call never touches your network.
- stdio MCP servers inside the agent pod. They share the pod’s network, so the pod’s deny already covers them. Nothing extra to do, and nothing extra they can do.
- Hostname rules. The core API matches pods, namespaces and IP blocks. If you need names at the network layer, that is your CNI’s own policy type, or a gateway that checks the name, which is what agentgateway does here.
The policy set makes the gateway the only door. What goes through the door is still the gateway’s decision, and the one-paragraph version of all this lives in how to stop shadow MCP servers.