Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

feat(api): forward the client IP on Alexandria requests (#5282)

Developers Digest76b657088c58
diff --git a/apps/api/src/controllers/v2/scrape-alexandria.ts b/apps/api/src/controllers/v2/scrape-alexandria.ts
index e3476ad10..524942bdf 100644
--- a/apps/api/src/controllers/v2/scrape-alexandria.ts
+++ b/apps/api/src/controllers/v2/scrape-alexandria.ts
@@ -136,6 +136,7 @@ export async function providerScrapeController(
scrapeId: uuidv7(),
timeoutMs: body.timeout,
bypassBilling: body.__agentInterop?.shouldBill === false,
+ clientIp: req.ip,
});
} catch (error) {
logger.error("Provider scrape unavailable", {
diff --git a/apps/api/src/routes/exchange.ts b/apps/api/src/routes/exchange.ts
index f7bf3f16d..9f661cb12 100644
--- a/apps/api/src/routes/exchange.ts
+++ b/apps/api/src/routes/exchange.ts
@@ -89,6 +89,7 @@ function exchangeProxy(
),
...(hasBody ? { "content-type": "application/json" } : {}),
"x-exchange-team-id": authedReq.auth.team_id,
+ ...(req.ip ? { "x-exchange-client-ip": req.ip } : {}),
// Built here, never forwarded from the caller: the connectors whitelist is this flag.
...(authedReq.acuc?.flags?.exchangeRetrieve
? { "x-exchange-connectors": "true" }
diff --git a/apps/api/src/services/alexandria/access.ts b/apps/api/src/services/alexandria/access.ts
index 65135f7e3..1b9cc86fa 100644
--- a/apps/api/src/services/alexandria/access.ts
+++ b/apps/api/src/services/alexandria/access.ts
@@ -39,6 +39,7 @@ export async function authorizeProviders(
calls: ProviderCall[],
flags: TeamFlags | null | undefined,
orgId: string | null = null,
+ clientIp?: string,
): Promise<ExchangeResponse | undefined> {
const providers = [...new Set(calls.map(call => call.provider))];
const response = await exchangeRequest({
@@ -46,6 +47,7 @@ export async function authorizeProviders(
path: "/v1/provider-terms/requirements",
body: { providers },
timeoutMs: 10000,
+ clientIp,
}).catch(() => undefined);
if (response?.status === 404)
return refusal(404, "Unknown provider. No provider was executed.", {
diff --git a/apps/api/src/services/alexandria/client.test.ts b/apps/api/src/services/alexandria/client.test.ts
index 97eb14b0b..82ee9eea7 100644
--- a/apps/api/src/services/alexandria/client.test.ts
+++ b/apps/api/src/services/alexandria/client.test.ts
@@ -41,6 +41,26 @@ describe("exchangeRequest", () => {
});
});
+ it("sends the client IP only when known", async () => {
+ await exchangeRequest({
+ teamId: "team",
+ path: "/v1/retrieve",
+ body: {},
+ timeoutMs: 1000,
+ clientIp: "203.0.113.7",
+ });
+ expect(headersOf()["x-exchange-client-ip"]).toBe("203.0.113.7");
+
+ mocks.fetch.mockClear();
+ await exchangeRequest({
+ teamId: "team",
+ path: "/v1/retrieve",
+ body: {},
+ timeoutMs: 1000,
+ });
+ expect(headersOf()).not.toHaveProperty("x-exchange-client-ip");
+ });
+
it("sends no connectors grant by default", async () => {
await exchangeRequest({
teamId: "team",
diff --git a/apps/api/src/services/alexandria/client.ts b/apps/api/src/services/alexandria/client.ts
index ce45d05be..a2db7fc7d 100644
--- a/apps/api/src/services/alexandria/client.ts
+++ b/apps/api/src/services/alexandria/client.ts
@@ -17,6 +17,8 @@ export async function exchangeRequest(input: {
maximumCredits?: number;
resultAuthorization?: string;
termsIdentity?: { organizationId: string; apiKeyId: string };
+ /** Caller's IP as the API sees it; Exchange resolves what it needs from it. */
+ clientIp?: string;
/**
* The team may use its own MCP connectors (Alexandria connectors). Sent for teams with the
* `exchangeRetrieve` flag, which is the connectors whitelist; Exchange reads it as the grant.
@@ -44,6 +46,7 @@ export async function exchangeRequest(input: {
}
: {}),
"x-exchange-extended-catalog-access": "true",
+ ...(input.clientIp ? { "x-exchange-client-ip": input.clientIp } : {}),
...(input.connectors ? { "x-exchange-connectors": "true" } : {}),
...(input.requestId ? { "x-request-id": input.requestId } : {}),
...(input.maximumCredits === undefined
diff --git a/apps/api/src/services/alexandria/retrieve.ts b/apps/api/src/services/alexandria/retrieve.ts
index 483f79322..b2274c010 100644
--- a/apps/api/src/services/alexandria/retrieve.ts
+++ b/apps/api/src/services/alexandria/retrieve.ts
@@ -94,6 +94,7 @@ export async function retrieveProviders(input: {
timeoutMs: number;
bypassBilling?: boolean;
resultAuthorization?: string;
+ clientIp?: string;
}): Promise<ProviderRetrieval> {
const notExecuted = (response: ExchangeResponse): ProviderRetrieval => ({
...response,
@@ -243,6 +244,7 @@ export async function retrieveProviders(input: {
input.calls,
input.flags,
input.orgId,
+ input.clientIp,
);
if (denied) return refuse(denied);
@@ -266,6 +268,7 @@ export async function retrieveProviders(input: {
path: "/v1/retrieve/quote",
body: { requests: input.calls },
timeoutMs: Math.min(10000, remaining()),
+ clientIp: input.clientIp,
connectors: input.flags?.exchangeRetrieve === true,
}).catch(error => {
logger.warn("Provider quote unavailable", { error, chargeId: id });
@@ -392,6 +395,7 @@ export async function retrieveProviders(input: {
body: { requests: input.calls },
timeoutMs: remaining(),
requestId: id,
+ clientIp: input.clientIp,
// The team's own MCP connectors (mcp-...) resolve only when Exchange is told it may use them.
connectors: input.flags?.exchangeRetrieve === true,
// Sent with every execution so Exchange tools that call Firecrawl as the caller