Skip to content

Commit 57b3d8c

Browse files
leo237Abhilash Panigrahi
andauthored
fix(box): give no MCP route to a project gateway with only HTTP targets (#2971)
On AgentCore Runtime, build routes each other gateway of a Box project to the boxed agent as `[mcp.<gateway>]`. Box parses each POST to that host as an MCP message, so a POST to an HTTP target of the gateway (`passthrough` or `httpRuntime`) was refused with "MCP frame refused", while a GET passed. `add gateway` writes `protocolType: "None"` by default, so the default gateway with a passthrough target hit this. - **Which gateways get a route.** `unroutedReason` is the one rule: a Box gateway, a `CUSTOM_JWT` gateway, and now a gateway whose targets are all HTTP targets get none. A gateway with no target yet, with MCP targets only, or with both kinds keeps its route, whatever its protocolType. A passthrough target with `protocolType: "MCP"` counts as HTTP: a chain of gateways that ends at an MCP server is out of scope. - **Notices, at the write.** `add gateway-target` gives a notice when it adds an HTTP target, `remove gateway-target` when the gateway is left with only HTTP targets (a new `notices` field on the removal result, printed as a warning and shown in the TUI), and `add runtime` for each gateway with HTTP targets. Each only when a boxed runtime that is not PRIVATE_VPC exists. A gateway with both kinds is told that its HTTP targets are not supported yet. - **CUSTOM_JWT with only HTTP targets** gets one notice, which no longer says that AWS_IAM would give it a route. - **Rebuild.** Build removes the `egress-gateway-<name>` target of such a gateway, and the step now says why. - **Text.** The UNSIGNABLE_HOST reason, the gateway rule's doc table, and docs/box-local-testing.md say which gateways get a route. A boxed agent still cannot POST to the HTTP targets of a gateway that also has MCP targets. Routing HTTP targets through an `[egress]` route is a follow-up. Co-authored-by: Abhilash Panigrahi <pabhila@amazon.com>
1 parent 40b8f33 commit 57b3d8c

14 files changed

Lines changed: 634 additions & 43 deletions

File tree

‎docs/box-local-testing.md‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,21 @@ profile that names a pack `box/<name>` fails: box refuses it and names the `@box
247247
remove that target's name from the file to keep the target as yours. Then build no longer records,
248248
changes, or removes it.
249249
- **The other gateways of the project.** A runtime without a box gets each gateway without
250-
`boundRuntime` in `AGENTCORE_GATEWAY_<NAME>_URL`. For a box runtime, each of these gateways gets
251-
the target `egress-gateway-<name>` on `<runtime>-box-egress`, with `passthrough.gateway` set to
250+
`boundRuntime` in `AGENTCORE_GATEWAY_<NAME>_URL`. For a box runtime, each of these gateways, except those below,
251+
gets the target `egress-gateway-<name>` on `<runtime>-box-egress`, with `passthrough.gateway` set to
252252
the gateway's name. The L3 sets the target's endpoint to that gateway's URL, gives the Box
253253
gateway's role `bedrock-agentcore:InvokeGateway` on that gateway only, and sets
254254
`AGENTCORE_GATEWAY_<NAME>_URL` and `_HOST` on the runtime. A `PRIVATE_VPC` runtime gets none of
255255
them. A `CUSTOM_JWT` gateway gets no target, and `agentcore add gateway`, and `agentcore add runtime` for a
256-
box runtime, give one notice for it. The file
256+
box runtime, give one notice for it. A gateway whose targets are all HTTP targets (`passthrough`, also with
257+
`protocolType` `MCP`, or `httpRuntime`) gets no target either: box would take the gateway's host
258+
as an MCP server and refuse each POST that is not an MCP message. A gateway with MCP targets and
259+
HTTP targets keeps its target, for its MCP tools only. A boxed agent on AgentCore Runtime cannot
260+
call HTTP targets yet. When the project has a box runtime that is not `PRIVATE_VPC`,
261+
`agentcore add gateway-target` gives a notice for the gateway each time it adds an HTTP target
262+
to it, `agentcore remove gateway-target` gives one when the gateway is left with only HTTP
263+
targets, and `agentcore add runtime` for a box runtime gives one for each gateway with an HTTP
264+
target. The file
257265
`agentcore/box-gateway-targets.json` lists these targets too. You write nothing for them. A profile
258266
entry that signs for a gateway host with `aws://` stops build. Build writes these targets only when
259267
the `@aws/agentcore-cdk` in `agentcore/cdk` accepts `passthrough.gateway`: it asks that library
@@ -317,8 +325,9 @@ profile that names a pack `box/<name>` fails: box refuses it and names the `@box
317325
`OTEL_METRICS_EXPORTER` (`none`), because the receiver of box takes only spans and logs. When
318326
your `[agent.env]` sets one of these variables, box uses your value. The pack sets no
319327
`OTEL_EXPORTER_OTLP_*` variable, because box sets each one;
320-
- each other gateway of the project gets `[mcp.<gateway>]`, to the host that `@box/agentcore`
321-
reads from `AGENTCORE_GATEWAY_<NAME>_HOST`, through its `egress-gateway-<name>` target. The agent
328+
- each other gateway of the project that gets an `egress-gateway-<name>` target gets
329+
`[mcp.<gateway>]`, through that target, to the host that `@box/agentcore` reads from
330+
`AGENTCORE_GATEWAY_<NAME>_HOST`. The agent
322331
gets `AGENTCORE_GATEWAY_<NAME>_URL`, so the code that calls the gateway without a box works in
323332
the box. Box permits each tool of the server, and a forbid rule of your policy still applies.
324333
A server of your profile with the name of a gateway stops build.

‎src/core/project/boxNetwork/egress.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,10 @@ export function outboundAuthFor(
244244
code: "UNSIGNABLE_HOST",
245245
reason:
246246
`signs requests to ${host}, the host of an AgentCore gateway, with ${secret.ref}. ` +
247-
"Build signs only for the gateways of this project, and it adds the route to each of " +
248-
"them on AgentCore Runtime itself, as [mcp.<gateway name>], with the host that the " +
249-
"deploy gives the gateway. Remove this entry. To call a gateway, add it to this project.",
247+
"Build signs only for the gateways of this project, and on AgentCore Runtime it adds " +
248+
"the route itself, as [mcp.<gateway name>], to each of them that a boxed agent can " +
249+
"call, with the host that the deploy gives the gateway. Remove this entry. To call a " +
250+
"gateway, add it to this project.",
250251
};
251252
}
252253
const signing = signingService(host) ?? boxSigningOf(host);

‎src/core/project/boxNetwork/network.test.ts‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,43 @@ describe("the gateways of the project", () => {
15911591
expect(network.failures).toEqual([]);
15921592
});
15931593

1594+
// Box parses each POST to the gateway's host as an MCP message. A gateway with an MCP target
1595+
// serves MCP at /mcp, so it keeps its route, whatever its protocolType, and so does one with no
1596+
// target yet; one with only HTTP targets has nothing to call through it.
1597+
describe("by the kinds of its targets", () => {
1598+
const MCP_TARGET = {
1599+
name: "search",
1600+
targetType: "mcpServer",
1601+
endpoint: "https://search.example.com/mcp",
1602+
};
1603+
const HTTP_TARGET = {
1604+
name: "api",
1605+
targetType: "passthrough",
1606+
passthrough: { endpoint: "https://api.example.com", protocolType: "CUSTOM" },
1607+
};
1608+
const routed = (...gateways: Record<string, unknown>[]) =>
1609+
model("", {}, { gateways }).network.projectGateways?.map(({ name }) => name);
1610+
1611+
test.each([
1612+
["no target", []],
1613+
["only an MCP target", [MCP_TARGET]],
1614+
["an MCP target and an HTTP target", [MCP_TARGET, HTTP_TARGET]],
1615+
])("gives the agent a None gateway with %s", (_, targets) => {
1616+
expect(routed({ ...TOOLS, protocolType: "None", targets })).toEqual(["tools"]);
1617+
});
1618+
1619+
test.each([
1620+
["passthrough", HTTP_TARGET],
1621+
["httpRuntime", { name: "run", targetType: "httpRuntime", httpRuntime: { runtime: "a" } }],
1622+
])(
1623+
"gives the agent no gateway whose targets are all HTTP targets (%s), and the next gateway still",
1624+
(_, target) => {
1625+
const front = { ...TOOLS, name: "front", protocolType: "None", targets: [target] };
1626+
expect(routed(front, TOOLS)).toEqual(["tools"]);
1627+
},
1628+
);
1629+
});
1630+
15941631
test.each([
15951632
[
15961633
{ accepts: false, reason: "refuses" },
@@ -1751,8 +1788,8 @@ describe("the gateways of the project", () => {
17511788
expect(r.failures().map(([code]) => code)).toEqual(["UNSIGNABLE_HOST"]);
17521789
expect(r.reasons()[0]).toContain(
17531790
`signs requests to ${GATEWAY_HOST}, the host of an AgentCore gateway, with aws://. Build ` +
1754-
"signs only for the gateways of this project, and it adds the route to each of them on " +
1755-
"AgentCore Runtime itself, as [mcp.<gateway name>]",
1791+
"signs only for the gateways of this project, and on AgentCore Runtime it adds the route " +
1792+
"itself, as [mcp.<gateway name>], to each of them that a boxed agent can call",
17561793
);
17571794
},
17581795
);

‎src/core/project/boxNetwork/projectGateways.test.ts‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import { serializeRuntimeProfile } from "../runtimeProfile/serialize";
1010
import type { TomlTable } from "../runtimeProfile/types";
1111
import { AGENTCORE_PACK_PATH, renderAgentCorePack } from "../templates/boxProfile";
1212
import {
13+
customJwtGatewayNotice,
14+
gatewayNotices,
15+
httpTargetsNotice,
16+
isHttpTarget,
1317
projectGatewaysOf,
1418
projectGatewayTargetFor,
1519
type BoxProjectGateway,
@@ -37,6 +41,81 @@ describe("projectGatewayTargetFor", () => {
3741
});
3842
});
3943

44+
describe("httpTargetsNotice", () => {
45+
const api = {
46+
name: "api",
47+
targetType: "passthrough",
48+
passthrough: { endpoint: "https://api.example.com", protocolType: "CUSTOM" },
49+
} as const;
50+
const gateway = { name: "front", authorizerType: "AWS_IAM", targets: [api] } as const;
51+
52+
// Each of these gets no route for another reason, which its own notice or none gives.
53+
test.each([
54+
["a CUSTOM_JWT gateway", { ...gateway, authorizerType: "CUSTOM_JWT" as const }],
55+
["a Box gateway", { ...gateway, boundRuntime: "agent" }],
56+
["a gateway with no HTTP target", { ...gateway, targets: [] }],
57+
])("gives none for %s", (_, candidate) => {
58+
expect(httpTargetsNotice(candidate as never)).toBe(undefined);
59+
});
60+
61+
test("names each HTTP target of a gateway that has only HTTP targets", () => {
62+
const targets = [api, { ...api, name: "api-2" }];
63+
expect(httpTargetsNotice({ ...gateway, targets } as never)).toStartWith(
64+
"the gateway 'front' has only the HTTP targets 'api' and 'api-2', so build adds no route ",
65+
);
66+
});
67+
68+
test("counts an httpRuntime target as an HTTP target", () => {
69+
const runtime = {
70+
name: "agent-http",
71+
targetType: "httpRuntime",
72+
httpRuntime: { runtime: "a" },
73+
};
74+
expect(httpTargetsNotice({ ...gateway, targets: [runtime] } as never)).toStartWith(
75+
"the gateway 'front' has only the HTTP target 'agent-http', ",
76+
);
77+
});
78+
});
79+
80+
describe("isHttpTarget", () => {
81+
// A connector serves MCP tools, so it keeps its gateway's route; a new target type must be added
82+
// to NON_MCP_TARGET_TYPES to count as HTTP.
83+
test.each([
84+
["passthrough", true],
85+
["httpRuntime", true],
86+
["connector", false],
87+
["mcpServer", false],
88+
["lambda", false],
89+
] as const)("is true only for an HTTP target: %s gives %p", (targetType, http) => {
90+
expect(isHttpTarget({ targetType })).toBe(http);
91+
});
92+
});
93+
94+
describe("gatewayNotices", () => {
95+
const api = {
96+
name: "api",
97+
targetType: "passthrough",
98+
passthrough: { endpoint: "https://api.example.com", protocolType: "CUSTOM" },
99+
} as const;
100+
const jwt = { name: "secure", authorizerType: "CUSTOM_JWT", targets: [] } as const;
101+
102+
test("gives a CUSTOM_JWT gateway with only HTTP targets one notice, which does not say AWS_IAM fixes it", () => {
103+
const notices = gatewayNotices({ ...jwt, targets: [api] } as never);
104+
expect(notices).toEqual([customJwtGatewayNotice("secure", true)]);
105+
expect(notices[0]).not.toContain("AWS_IAM");
106+
expect(notices[0]).toEndWith("a boxed agent on AgentCore Runtime cannot call HTTP targets yet");
107+
});
108+
109+
test("gives a CUSTOM_JWT gateway with no target the notice that says AWS_IAM fixes it", () => {
110+
expect(gatewayNotices(jwt as never)).toEqual([customJwtGatewayNotice("secure")]);
111+
expect(customJwtGatewayNotice("secure")).toContain("Set its authorizerType to AWS_IAM");
112+
});
113+
114+
test("gives a Box gateway none", () => {
115+
expect(gatewayNotices({ ...jwt, boundRuntime: "agent", targets: [api] } as never)).toEqual([]);
116+
});
117+
});
118+
40119
describe("the target name of each gateway", () => {
41120
const targetsOf = (...names: string[]) =>
42121
projectGatewaysOf(

‎src/core/project/boxNetwork/projectGateways.ts‎

Lines changed: 111 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
/**
22
* The gateways of the project that a boxed agent calls through its runtime's Box gateway. A runtime
33
* without a box gets each gateway without `boundRuntime` in `AGENTCORE_GATEWAY_<NAME>_URL`. On
4-
* AgentCore Runtime, the boxed agent gets the same variable, and box sends each call to the
5-
* gateway's host to a Box gateway target that names that gateway (`passthrough.gateway`).
4+
* AgentCore Runtime, the boxed agent gets the same variable for each gateway that
5+
* {@link projectGatewaysOf} gives it, and box sends each call to that gateway's host to a Box
6+
* gateway target that names that gateway (`passthrough.gateway`). Box takes that host as an MCP
7+
* server, so the agent can call the MCP tools of the gateway, and not its HTTP targets.
68
*/
79
import { createHash } from "node:crypto";
8-
import type { AgentCoreGatewayTarget } from "../../../projectSchemas/gateway";
10+
import {
11+
NON_MCP_TARGET_TYPES,
12+
type AgentCoreGateway,
13+
type AgentCoreGatewayTarget,
14+
} from "../../../projectSchemas/gateway";
915
import { gatewayDiscoveryPrefix, type ProjectSpec } from "../../../projectSchemas/project";
1016
import type { ProjectRuntime } from "../../../projectSchemas/runtime";
1117
import type { PassthroughGatewayCheck } from "../backends/cdk/compatibility";
@@ -47,12 +53,29 @@ function projectGatewayTargetName(name: string): string {
4753
}
4854

4955
/**
50-
* The gateways of `spec` that `runtime` calls through its Box gateway: each gateway without
51-
* `boundRuntime`, as the CDK library binds it to a runtime without a box. A `PRIVATE_VPC` runtime
52-
* gets none, and no `CUSTOM_JWT` gateway, because the Box gateway cannot get its token;
53-
* `add gateway` or `add runtime` gives {@link customJwtGatewayNotice} for it once. When `check`
54-
* does not accept `passthrough.gateway`, the runtime gets none, and one warning names them and says
55-
* why.
56+
* Why a boxed agent on AgentCore Runtime gets no route to `gateway`, in the order build checks it;
57+
* undefined when it gets one. `bound`: a Box gateway, which belongs to its runtime. `custom-jwt`:
58+
* the Box gateway signs only with its IAM role, so it cannot get the gateway's token. `http-only`:
59+
* each target of the gateway is an HTTP target ({@link isHttpTarget}), and box parses each POST to
60+
* the gateway's host as an MCP message, so it would refuse each POST to them. The one rule for
61+
* {@link projectGatewaysOf}, {@link gatewayNotices}, and the step that removes a gateway's target.
62+
*/
63+
export function unroutedReason(
64+
gateway: Pick<AgentCoreGateway, "targets" | "boundRuntime" | "authorizerType">,
65+
): "bound" | "custom-jwt" | "http-only" | undefined {
66+
if (gateway.boundRuntime !== undefined) return "bound";
67+
if (gateway.authorizerType === "CUSTOM_JWT") return "custom-jwt";
68+
if (isHttpOnlyGateway(gateway)) return "http-only";
69+
return undefined;
70+
}
71+
72+
/**
73+
* The gateways of `spec` that `runtime` calls through its Box gateway: each gateway with no
74+
* {@link unroutedReason}, as the CDK library binds each gateway without `boundRuntime` to a runtime
75+
* without a box. A `PRIVATE_VPC` runtime gets none. `add gateway`, `add gateway-target`,
76+
* `remove gateway-target`, and `add runtime` give the {@link gatewayNotices} of a gateway that gets
77+
* no route, or that has HTTP targets. When `check` does not accept `passthrough.gateway`, the
78+
* runtime gets none, and one warning names them and says why.
5679
*/
5780
export function projectGatewaysOf(
5881
spec: ProjectSpec,
@@ -63,8 +86,7 @@ export function projectGatewaysOf(
6386
const warnings: string[] = [];
6487
if (runtime.containment === "PRIVATE_VPC") return { gateways, warnings };
6588
for (const gateway of spec.agentCoreGateways) {
66-
if (gateway.boundRuntime !== undefined) continue;
67-
if (gateway.authorizerType === "CUSTOM_JWT") continue;
89+
if (unroutedReason(gateway) !== undefined) continue;
6890
gateways.push({
6991
name: gateway.name,
7092
target: projectGatewayTargetName(gateway.name),
@@ -143,15 +165,90 @@ export function gatewayList(names: readonly string[]): string {
143165
return quoted.length > 1 ? `${quoted.slice(0, -1).join(", ")} and ${quoted.at(-1)}` : quoted[0]!;
144166
}
145167

146-
/** What `add gateway` or `add runtime` tells the user once about a `CUSTOM_JWT` gateway in a Box project. */
147-
export function customJwtGatewayNotice(gatewayName: string): string {
168+
/**
169+
* What a command that writes a `CUSTOM_JWT` gateway tells the user about it in a Box project. When
170+
* `httpOnly`, its targets are all HTTP targets, so `AWS_IAM` would not give it a route either.
171+
*/
172+
export function customJwtGatewayNotice(gatewayName: string, httpOnly = false): string {
148173
return (
149174
`the gateway '${gatewayName}' takes CUSTOM_JWT tokens, and a Box gateway signs only with its ` +
150175
`IAM role, so a boxed agent cannot call '${gatewayName}' on AgentCore Runtime, and build adds ` +
151-
"no route to it. Set its authorizerType to AWS_IAM to let a boxed agent call it"
176+
"no route to it. " +
177+
(httpOnly
178+
? `Each target of '${gatewayName}' is an HTTP target too, and ${HTTP_UNSUPPORTED}`
179+
: "Set its authorizerType to AWS_IAM to let a boxed agent call it")
152180
);
153181
}
154182

183+
/**
184+
* Whether `target` is an HTTP target, whose POSTs box refuses on the MCP route to its gateway:
185+
* `httpRuntime`, or `passthrough`. A `passthrough` target with `protocolType` `MCP` counts as HTTP
186+
* too: a boxed agent calls the MCP servers that a gateway's own MCP targets serve, and a chain of
187+
* gateways that ends at an MCP server is out of scope.
188+
*/
189+
export function isHttpTarget(target: Pick<AgentCoreGatewayTarget, "targetType">): boolean {
190+
return NON_MCP_TARGET_TYPES.includes(target.targetType);
191+
}
192+
193+
/** The names of the HTTP targets of `gateway`. */
194+
function httpTargetsOf(gateway: Pick<AgentCoreGateway, "targets">): string[] {
195+
return gateway.targets.filter(isHttpTarget).map(({ name }) => name);
196+
}
197+
198+
/**
199+
* Whether each target of `gateway` is an HTTP target, so a boxed agent has nothing to call through
200+
* an MCP route to it. A gateway with no target yet is not: none of its POSTs would be refused, and
201+
* it keeps the route for the MCP targets it may take.
202+
*/
203+
export function isHttpOnlyGateway(gateway: Pick<AgentCoreGateway, "targets">): boolean {
204+
return gateway.targets.length > 0 && httpTargetsOf(gateway).length === gateway.targets.length;
205+
}
206+
207+
const HTTP_UNSUPPORTED = "a boxed agent on AgentCore Runtime cannot call HTTP targets yet";
208+
209+
/**
210+
* What a command tells the user about a gateway with HTTP targets in a Box project; undefined for a
211+
* gateway with another {@link unroutedReason}, which {@link gatewayNotices} gives, or with no HTTP
212+
* target.
213+
*/
214+
export function httpTargetsNotice(
215+
gateway: Pick<AgentCoreGateway, "name" | "targets" | "boundRuntime" | "authorizerType">,
216+
): string | undefined {
217+
const reason = unroutedReason(gateway);
218+
if (reason === "bound" || reason === "custom-jwt") return undefined;
219+
const http = httpTargetsOf(gateway);
220+
if (http.length === 0) return undefined;
221+
const targets = `${http.length > 1 ? "targets" : "target"} ${gatewayList(http)}`;
222+
const route =
223+
"box routes a gateway of the project as an MCP server, and refuses each POST to it that is " +
224+
"not an MCP message";
225+
return reason === "http-only"
226+
? `the gateway '${gateway.name}' has only the HTTP ${targets}, so build adds no route to it: ` +
227+
`${route}. ${capitalized(HTTP_UNSUPPORTED)}`
228+
: `a boxed agent on AgentCore Runtime can call the MCP tools of the gateway '${gateway.name}', ` +
229+
`but cannot POST to its HTTP ${targets}: ${route}. HTTP targets are not supported yet`;
230+
}
231+
232+
/**
233+
* The notices that a command which writes `gateway` gives in a Box project with a boxed runtime that
234+
* is not `PRIVATE_VPC`: for a `CUSTOM_JWT` gateway, or one with HTTP targets. None for a Box gateway.
235+
*/
236+
export function gatewayNotices(
237+
gateway: Pick<AgentCoreGateway, "name" | "targets" | "boundRuntime" | "authorizerType">,
238+
): string[] {
239+
const reason = unroutedReason(gateway);
240+
if (reason === "bound") return [];
241+
if (reason === "custom-jwt") {
242+
return [customJwtGatewayNotice(gateway.name, isHttpOnlyGateway(gateway))];
243+
}
244+
const notice = httpTargetsNotice(gateway);
245+
return notice ? [notice] : [];
246+
}
247+
248+
function capitalized(text: string): string {
249+
return text.charAt(0).toUpperCase() + text.slice(1);
250+
}
251+
155252
/** The `@box/agentcore` inputs that carry the URL and the host of `gateway` on the Runtime. */
156253
export function projectGatewayInputs(gateway: Pick<BoxProjectGateway, "name">): {
157254
readonly url: string;

0 commit comments

Comments
 (0)