get_swap_quote
Generate executable token swap quotes with transaction data for DeFi trades. Input chain ID, token addresses, and amounts to receive precise swap details.
Instructions
Get executable quote with transaction data for a token swap
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| buyToken | Yes | Contract address of token to buy | |
| chainId | Yes | Blockchain ID (e.g., 1 for Ethereum) | |
| sellAmount | Yes | Amount of sellToken in base units | |
| sellToken | Yes | Contract address of token to sell | |
| slippageBps | No | Maximum acceptable slippage in basis points (optional, default: 100) | |
| taker | No | Address executing the trade (optional, uses USER_ADDRESS from env) |
Input Schema (JSON Schema)
{
"properties": {
"buyToken": {
"description": "Contract address of token to buy",
"type": "string"
},
"chainId": {
"description": "Blockchain ID (e.g., 1 for Ethereum)",
"type": "integer"
},
"sellAmount": {
"description": "Amount of sellToken in base units",
"type": "string"
},
"sellToken": {
"description": "Contract address of token to sell",
"type": "string"
},
"slippageBps": {
"description": "Maximum acceptable slippage in basis points (optional, default: 100)",
"type": "integer"
},
"taker": {
"description": "Address executing the trade (optional, uses USER_ADDRESS from env)",
"type": "string"
}
},
"required": [
"chainId",
"buyToken",
"sellToken",
"sellAmount"
],
"type": "object"
}
Implementation Reference
- src/toolService.js:40-70 (handler)Primary handler function for the get_swap_quote tool. Validates parameters, adds user address as taker if missing, fetches quote from AgService, enhances result, and returns formatted response with next steps.async getSwapQuote(params) { // Validate required parameters const { chainId, buyToken, sellToken, sellAmount } = params; if (!chainId || !buyToken || !sellToken || !sellAmount) { throw new Error( "Missing required parameters: chainId, buyToken, sellToken, sellAmount" ); } // Add taker address if not provided const quoteParams = { ...params, taker: params.taker || this.userAddress, }; const result = await this.agg.getSwapQuote(quoteParams); // Add chainId to the result for executeSwap to use result.chainId = chainId; return { message: "Swap quote retrieved successfully", data: result, nextSteps: [ "1. Review the quote details including fees and gas estimates", "2. Use execute_swap tool to execute this swap", "3. The permit2 signature will be handled automatically", ], }; }
- src/services/agService.js:30-49 (helper)Helper function in AgService class that performs the actual HTTP request to the aggregator API endpoint /api/swap/quote to retrieve the swap quote data.async getSwapQuote(params) { try { const queryParams = new URLSearchParams(params); const response = await fetch(`${this.baseUrl}/api/swap/quote?${queryParams}`); if (!response.ok) { throw new Error(`HTTP ${response.status}: ${response.statusText}`); } const data = await response.json(); if (!data.success) { throw new Error(data.error || 'API request failed'); } return data.data; } catch (error) { throw new Error(`Failed to get swap quote: ${error.message}`); } }
- src/index.js:120-156 (schema)Tool schema definition in MCP server ListTools handler, including name, description, and detailed inputSchema with properties and required fields for get_swap_quote.{ name: TOOL_NAMES.GET_SWAP_QUOTE, description: "Get executable quote with transaction data for a token swap", inputSchema: { type: "object", properties: { chainId: { type: "integer", description: "Blockchain ID (e.g., 1 for Ethereum)", }, buyToken: { type: "string", description: "Contract address of token to buy", }, sellToken: { type: "string", description: "Contract address of token to sell", }, sellAmount: { type: "string", description: "Amount of sellToken in base units", }, taker: { type: "string", description: "Address executing the trade (optional, uses USER_ADDRESS from env)", }, slippageBps: { type: "integer", description: "Maximum acceptable slippage in basis points (optional, default: 100)", }, }, required: ["chainId", "buyToken", "sellToken", "sellAmount"], }, },
- src/index.js:988-990 (registration)Registration in the MCP CallToolRequestHandler switch statement that dispatches get_swap_quote tool calls to the toolService.getSwapQuote method.case TOOL_NAMES.GET_SWAP_QUOTE: result = await toolService.getSwapQuote(args); break;
- src/constants.js:5-5 (helper)Constant definition for the tool name string used across the codebase.GET_SWAP_QUOTE: "get_swap_quote",