Skip to content

Commit 976b4f9

Browse files
committed
fix: add token-list fallback and 0x api key support
1 parent 8692d1b commit 976b4f9

9 files changed

Lines changed: 342 additions & 47 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,17 @@ npx -y @abstract-foundation/agw-mcp serve --chain-id 2741
126126

127127
# Custom RPC
128128
npx -y @abstract-foundation/agw-mcp serve --chain-id 2741 --rpc-url https://api.mainnet.abs.xyz
129+
130+
# 0x API key override (for swap_tokens quote requests)
131+
npx -y @abstract-foundation/agw-mcp serve --chain-id 2741 --zeroex-api-key YOUR_0X_API_KEY
129132
```
130133

131134
Environment variables are also supported:
132135

133136
```bash
134137
AGW_MCP_CHAIN_ID=2741 npx -y @abstract-foundation/agw-mcp serve
135138
AGW_MCP_RPC_URL=https://api.mainnet.abs.xyz npx -y @abstract-foundation/agw-mcp serve
139+
AGW_MCP_ZEROEX_API_KEY=YOUR_0X_API_KEY npx -y @abstract-foundation/agw-mcp serve
136140
AGW_MCP_APP_URL=https://mcp.abs.xyz npx -y @abstract-foundation/agw-mcp init --chain-id 2741
137141
```
138142

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@abstract-foundation/agw-mcp",
3-
"version": "0.1.0-beta.6",
3+
"version": "0.1.0-beta.7",
44
"description": "MCP server for Abstract Global Wallet session-key workflows",
55
"license": "MIT",
66
"author": "Abstract Foundation",

src/config/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from "./network.js";
2+
export * from "./zeroex.js";

src/config/zeroex.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const ZEROEX_API_KEY_ENV_KEYS = ["AGW_MCP_ZEROEX_API_KEY", "ZEROEX_API_KEY"] as const;
2+
3+
export interface ResolveZeroExConfigInput {
4+
apiKey?: string;
5+
env?: NodeJS.ProcessEnv;
6+
}
7+
8+
export interface ResolvedZeroExConfig {
9+
apiKey?: string;
10+
}
11+
12+
function normalizeOptionalString(value: unknown): string | undefined {
13+
if (typeof value !== "string") {
14+
return undefined;
15+
}
16+
17+
const normalized = value.trim();
18+
return normalized ? normalized : undefined;
19+
}
20+
21+
function resolveEnvValue(env: NodeJS.ProcessEnv, keys: readonly string[]): string | undefined {
22+
for (const key of keys) {
23+
const value = normalizeOptionalString(env[key]);
24+
if (value) {
25+
return value;
26+
}
27+
}
28+
29+
return undefined;
30+
}
31+
32+
export function resolveZeroExConfig(input: ResolveZeroExConfigInput = {}): ResolvedZeroExConfig {
33+
const env = input.env ?? process.env;
34+
const apiKey = normalizeOptionalString(input.apiKey) ?? resolveEnvValue(env, ZEROEX_API_KEY_ENV_KEYS);
35+
36+
return {
37+
apiKey,
38+
};
39+
}

src/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { SessionManager } from "./session/manager.js";
1010
import { Logger } from "./utils/logger.js";
1111

1212
const logger = new Logger("agw-mcp");
13+
const ZEROEX_API_KEY_ENV = "AGW_MCP_ZEROEX_API_KEY";
1314

1415
function resolveCliVersion(): string {
1516
try {
@@ -25,6 +26,18 @@ function resolveCliVersion(): string {
2526
return "0.1.0";
2627
}
2728

29+
function applyZeroExApiKeyOverride(apiKeyValue: unknown): void {
30+
if (apiKeyValue === undefined) {
31+
return;
32+
}
33+
34+
if (typeof apiKeyValue !== "string" || apiKeyValue.trim() === "") {
35+
throw new Error("--zeroex-api-key must be a non-empty string");
36+
}
37+
38+
process.env[ZEROEX_API_KEY_ENV] = apiKeyValue.trim();
39+
}
40+
2841
const program = new Command();
2942
program.name("agw-mcp").description("Local MCP server for AGW session-key workflows").version(resolveCliVersion());
3043

@@ -77,8 +90,11 @@ program
7790
.description("Run the local stdio MCP server")
7891
.option("--chain-id <chainId>", "EVM chain id (env: AGW_MCP_CHAIN_ID)")
7992
.option("--rpc-url <rpcUrl>", "RPC URL override (env: AGW_MCP_RPC_URL)")
93+
.option("--zeroex-api-key <apiKey>", "0x API key override (env: AGW_MCP_ZEROEX_API_KEY)")
8094
.option("--storage-dir <dir>", "Session storage directory")
8195
.action(async options => {
96+
applyZeroExApiKeyOverride(options.zeroexApiKey);
97+
8298
const networkConfig = resolveNetworkConfig({
8399
chainId: options.chainId,
84100
rpcUrl: options.rpcUrl,

src/integrations/zeroex/quote-adapter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { resolveZeroExConfig } from "../../config/zeroex.js";
2+
13
const DEFAULT_ZEROEX_API_BASE_URL = "https://api.0x.org";
24
const DEFAULT_ZEROEX_TIMEOUT_MS = 15_000;
35
const INTEGER_AMOUNT_PATTERN = /^[1-9]\d*$/;
@@ -532,4 +534,5 @@ export function createZeroExQuoteAdapter(config: ZeroExQuoteAdapterConfig = {}):
532534
};
533535
}
534536

535-
export const zeroExQuoteAdapter = createZeroExQuoteAdapter();
537+
const defaultZeroExConfig = resolveZeroExConfig();
538+
export const zeroExQuoteAdapter = createZeroExQuoteAdapter({ apiKey: defaultZeroExConfig.apiKey });

0 commit comments

Comments
 (0)