Help improve this page
To contribute to this user guide, choose the Edit this page on GitHub link that is located in the right pane of every page.
Give an agent tools with MCP
Agents discover and call tools through the Model Context Protocol (MCP). A tool server differs from most workloads because its caller is a model. The arguments of every tool call come from model output, which a prompt injection can steer, so a tool server has to authenticate every caller, limit which tools each caller can invoke, and treat arguments as untrusted input. Network policy decides which pods can connect to a tool server, but it can’t tell a legitimate request from one that an injected prompt produced.
Tools can run as MCP servers in your cluster, as functions such as Lambda that a gateway calls, or as remote MCP servers and SaaS APIs outside your account. Each placement changes who authenticates the caller and which component sees the call.
The following diagrams show each path.
Open source and AWS services paths
The following table compares how each path serves tools to agents. On both paths, the MCP server runs unsandboxed in its own namespace, mcp-servers.
| Aspect | Open source | AWS services |
|---|---|---|
|
How an agent reaches an in-cluster server |
Through AgentGateway, which aggregates the server as an MCP target on one |
Directly over the cluster network, because AgentCore Gateway can’t reach a ClusterIP Service |
|
Who authenticates the agent |
The gateway, on the agent’s gateway token |
The server, on a projected token whose audience is the server’s own name |
|
Server’s token check ( |
|
|
|
|
Empty |
The server’s name |
|
Server’s network policy admits |
Only the gateway’s namespace |
The agent namespace |
|
Which tools a caller can invoke |
The gateway’s tool policy |
The server’s token check for in-cluster servers, and Cedar policy for AgentCore Gateway targets |
|
Off-cluster tools |
Through AgentGateway, which attaches the credential |
AgentCore Gateway targets: Lambda functions, OpenAPI, and remote MCP servers |
Tool sources
The following table lists where an agent’s tools come from, how the agent reaches each one, and what authorizes the call.
| Tool | How the agent reaches it | What authorizes the call |
|---|---|---|
|
MCP server on the cluster |
The gateway, as a target, on the open source path. A direct call over the cluster network on the AWS services path. |
A projected token the server verifies, plus network policy and mesh authorization. |
|
Off-cluster MCP server or SaaS API |
The gateway, which attaches the credential |
The gateway’s tool policy, plus whatever the destination enforces. |
|
Your own function |
The gateway, as a target |
The gateway’s tool policy and the function’s own IAM. |
The two paths aggregate in-cluster servers differently:
-
Open source path. AgentGateway runs in the cluster and aggregates a ClusterIP Service, so all tool calls go through one endpoint. Put every in-cluster server behind the gateway.
-
AWS services path. AgentCore Gateway runs outside the cluster and can’t reach a ClusterIP Service. Calls from an agent to an in-cluster server are east-west traffic, governed by network policy and optionally the mesh. For a tool you write yourself hosted outside of the cluster, use a Lambda function target with AgentCore Gateway. To put an in-cluster MCP server behind AgentCore Gateway, give the server an HTTPS address routable in the VPC, such as an internal load balancer. Then use either a private endpoint on the gateway target, which places the gateway’s network interfaces in your subnets, or the ingress Application Load Balancer with a credential for the gateway.
Caller authentication
Without authentication, any pod that network policy admits can call the server’s tools, including an agent acting on an injected prompt. The caller can be authenticated at the gateway or at the server, and each needs a different token.
When a gateway sits in front of the server, the gateway authenticates the agent and applies tool policy, and the server trusts only the gateway. The gateway doesn’t forward the agent’s token, so the server can’t tell agents apart, and keeping other callers away from the server becomes a network problem. The server runs outside the agent namespaces, behind a policy that admits only the gateway.
When agents call the server directly, the server authenticates each agent itself, with a projected service account token minted for it. The token’s audience names the server, so a token the agent holds for the gateway or for another service doesn’t work there, and the server refuses any caller without the right token. Network policy becomes a second layer.
| Property | Through the gateway | Direct from the agent |
|---|---|---|
|
Used on |
The open source path, where AgentGateway aggregates the server as a target |
The AWS services path, and any server you don’t put behind the gateway |
|
Authenticates the agent |
The gateway, on the agent’s gateway token. The server can’t, because the gateway doesn’t forward the caller’s token |
The server, on a projected token whose audience is the server’s own name |
|
Keeps other callers out |
A namespace outside the agent namespaces, whose network policy admits only the gateway’s pods. With the mesh on, an Istio |
The server’s token check, with network policy as a second layer |
When the server checks tokens, configure it to fail to start if no OIDC issuer is set. Otherwise a missing value starts the server without authentication, and the deployment appears to work.
Tool scope
Configure each of the following limits separately:
-
Which agents can call the server. Set by the token audience and the subject allowlist, with network policy and mesh authorization as additional layers.
-
Which tools a caller can invoke, and with which arguments. Set by the gateway’s tool policy. Check arguments, because a model generates them.
-
What the tool’s identity can do. A tool that calls AWS has its own IAM role, which limits the damage a misused tool can do. Scope it as you would an agent’s role. See Give each agent its own identity with EKS Pod Identity.
Don’t let the model supply a tool’s target. If the target namespace, pool, or endpoint is a tool argument, the model can redirect the tool. Set those values in the agent’s configuration.
Sessions and replicas
MCP’s Streamable HTTP transport can be stateful. A server that assigns a session returns its ID in the MCP-Session-Id header, and the client sends that header on every later request. A request routed to a replica that doesn’t hold the session fails. Cookie-based load balancer stickiness doesn’t fix this, because an MCP client isn’t a browser.
Before you deploy more than one replica, choose one of the following:
-
Run a single replica.
-
Route on the session ID in front of the server.
-
Store session state outside the pod.
See Expose an agent through an authenticated Application Load Balancer and Store agent session state.
Server placement and token checks
The reference sample places and configure the server differently on each path. Outside these templates, the same split applies. Either the gateway verifies agents and the server admits only the gateway, or the server verifies a token whose audience is its own name.
On both paths the server runs in its own namespace, mcp-servers, and isn’t sandboxed. It’s code that the platform team deploys, not model-generated code. Don’t run it in an agent namespace, for two reasons:
-
The cluster-scoped policy accepts all traffic within an agent namespace, so a policy on the server that admits only some callers has no effect.
-
Admission sandboxes every pod in an agent namespace, and a sandboxed pod can’t receive mesh traffic. With the mesh on, the gateway can’t reach the server.
The paths differ in who calls the server.
-
Open source path: the gateway. The server’s network policy admits only the gateway’s namespace, so agents reach the tools through the gateway and its tool policy.
-
AWS services path: the agents, directly. AgentCore Gateway runs outside the cluster and can’t reach a ClusterIP Service. An
Agentthat setsmcpAudiencegets a network policy rule tomcp-serverson the server’s port, and the server’s network policy admits the agent namespace. The server’s own token check is the access control. An agent that doesn’t setmcpAudiencecan’t connect.
On the Agent, mcpAudience names the server for direct calls and projects a second token at /var/run/secrets/eks-agents/mcp/token. Leave it empty when the gateway fronts the server, because the agent would present the server’s token to the gateway, which rejects it.
MCP_REQUIRE_TOKEN on the server sets whether it verifies a projected token on each request. The sample’s render script sets it, with the image, the cluster’s OIDC issuer, and the server’s namespace:
-
Open source path:
false. The gateway authenticates the agent and doesn’t forward its token. The gateway and network policy control access. -
AWS services path:
true. With the check off, the server serves its tools to every agent that network policy lets connect.
On the open source path, each entry under mcpServers in the agentgateway-config chart’s values registers servers with the gateway. An entry selects Services by label in one namespace, and the gateway serves the tools of every matched Service on a single /mcp path. A matched Service must also set appProtocol: agentgateway.dev/mcp on its port. Without it, or with the wrong namespace, tool calls return 503 while the gateway’s objects still report Accepted, so check both first.
Prerequisites
-
PAVED_PATHset toopen-sourceoraws-services, such as withexport PAVED_PATH=open-source. -
A shell with
kubectlaccess to the cluster, on the bastion or your workstation, and a clone of the reference sample on your workstation. -
On the open source path, the gateway, turned on with
enable_gateway = true. See Route agent egress through a gateway. -
With
mesh_mode = "ambient", the mesh installed. See Choose a service mesh mode for agents.
Step 1: Deploy the server
This step deploys the reference sample’s reference server, in gitops/examples/mcp-server, behind a ClusterIP Service in the mcp-servers namespace, with the network policy and token setting that Server placement and token checks describes for your path.
From the agents directory, render and apply the namespace and the server. The render keeps one ingress rule on the server’s network policy, for the caller on your path.
The following excerpt shows both rules:
apiVersion: networking.k8s.aws/v1alpha1 kind: ApplicationNetworkPolicy metadata: name: mcp-server-ingress namespace: mcp-servers spec: podSelector: matchLabels: app: mcp-server policyTypes: - Ingress ingress: - from: # open source path: the gateway only - namespaceSelector: matchLabels: kubernetes.io/metadata.name: agentgateway-system ports: - protocol: TCP port: 15008 - protocol: TCP port: 8080 - from: # AWS services path: agents calling directly - namespaceSelector: matchLabels: kubernetes.io/metadata.name: agents ports: - protocol: TCP port: 8080
The server checks a token on each request unless MCP_REQUIRE_TOKEN is false, which the render sets on the open source path because the gateway forwards no caller token. The following excerpt from the server shows the check. It verifies the token against the cluster’s OIDC issuer, with mcp-server as the audience, and returns 401 on any failure:
def _verify(request): if not REQUIRE_TOKEN: return "gateway" # network policy admits only the gateway try: caller = verify_cluster_token( bearer_token(dict(request.headers)), audience=INBOUND_AUDIENCE, # MCP_INBOUND_AUDIENCE, default "mcp-server" issuer=OIDC_ISSUER, # CLUSTER_OIDC_ISSUER allowed_namespaces=ALLOWED_CALLER_NAMESPACES, ) except AuthError as exc: raise HTTPException(status_code=401, detail="unauthenticated") from exc return caller.subject
If callers outside the cluster also need the server, put it behind the ingress Application Load Balancer and do the following:
-
Use the workload gate. MCP clients can’t follow the browser gate’s login redirect.
-
Add the label that the ingress network policy selects on. Without it, the namespace-wide policy drops the load balancer’s health checks and traffic.
See Expose an agent through an authenticated Application Load Balancer.
Step 2: Verify that unauthenticated calls are refused
Step 3: Make the server reachable to agents
Step 4: Verify a tool call from an agent
From the agents directory, run the tools phase of the reference sample’s exercise script. It initializes an MCP session from inside the agent’s sandbox with the agent’s token and lists the tools.
./scripts/exercise.sh --path $PAVED_PATH tools
The phase returns a non-empty tool list. The reference server lists get_time, describe_cluster, and lookup_order. On the AWS services path with mcpAudience set, the phase calls the server directly with the agent’s MCP token.
If the call works from a debug pod but fails from the agent, check the following:
-
The token audience matches the server’s name.
-
The mesh authorization policy admits the caller.
-
Network policy permits the mesh’s tunnel port as well as the application port.
Clean up
These steps remove the reference server and the agent settings that reach it. Run them from the agents directory.
Next steps
-
Route agent egress through a gateway: Apply budgets and tool policy to the tools the gateway serves.
-
Act on behalf of a user with an agent on Amazon EKS: Let a tool answer only for the person a call is for.