ODOS_SWAP
Facilitates token swaps on decentralized exchanges via the MCP-ODOS server by specifying blockchain, tokens, and amount, ensuring efficient cross-token transactions.
Instructions
Execute a swap transaction
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| amount | Yes | The amount of tokens to swap, in wei. | |
| chain | No | The blockchain network to execute the transaction on. uses fraxtal as default | fraxtal |
| fromToken | Yes | The token to swap from (address). | |
| prettyFormat | No | Whether to pretty format the quote. | |
| toToken | Yes | The token to swap to (address). |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"amount": {
"description": "The amount of tokens to swap, in wei.",
"pattern": "^\\d+$",
"type": "string"
},
"chain": {
"default": "fraxtal",
"description": "The blockchain network to execute the transaction on. uses fraxtal as default",
"type": "string"
},
"fromToken": {
"description": "The token to swap from (address).",
"type": "string"
},
"prettyFormat": {
"default": true,
"description": "Whether to pretty format the quote.",
"type": "boolean"
},
"toToken": {
"description": "The token to swap to (address).",
"type": "string"
}
},
"required": [
"fromToken",
"toToken",
"amount"
],
"type": "object"
}
Implementation Reference
- src/tools/swap.ts:41-115 (handler)The main execute function that implements the ODOS_SWAP tool logic: initializes wallet, fetches quote via GetQuoteActionService, assembles transaction via AssembleService, checks/sets allowance, and executes the swap via ExecuteSwapService.execute: async (args: z.infer<typeof swapParamsSchema>) => { try { const walletPrivateKey = process.env.WALLET_PRIVATE_KEY; if (!walletPrivateKey) { throw new Error( "WALLET_PRIVATE_KEY is not set in the environment. This is required to execute trades.", ); } console.log("[ODOS_SWAP] Called..."); const inputChain = args.chain.toLowerCase(); const chainObject = getChainFromName(inputChain); const walletService = new WalletService( walletPrivateKey, chainObject ?? fraxtal, ); console.log( `[ODOS_SWAP] Using chain: ${chainObject} (${chainObject.id})`, ); console.log( walletService.getWalletClient()?.account?.address ?? "No wallet address found", ); const getQuoteService = new GetQuoteActionService(walletService); const quote = await getQuoteService.execute( args.fromToken, args.toToken, chainObject.id, args.amount, ); if (quote instanceof Error || !quote.pathId) { return `Error fetching quote: ${quote instanceof Error ? quote.message : String(quote)}`; } const assembleService = new AssembleService(walletService); const txn = await assembleService.execute(quote.pathId); if (!txn) { return `Error assembling transaction: ${txn}`; } const executeSwapService = new ExecuteSwapService(walletService); try { await executeSwapService.checkAndSetAllowance( quote.inTokens[0], BigInt(quote.inAmounts[0]), txn.to, ); const hash = await executeSwapService.execute(txn); return args.prettyFormat ? await executeSwapService.formatWithConfirmation(txn, hash) : JSON.stringify({ hash, txn }, null, 2); } catch (error: unknown) { const message = error instanceof Error ? error.message : "An unknown error occurred during the execution."; throw new Error(`Error executing swap: ${message}`); } } catch (error: unknown) { const message = error instanceof Error ? error.message : "An unknown error occurred during the fetch."; console.error(`[ODOS_SWAP] Error: ${message}`); throw new Error(`Failed in swap process: ${message}`); } },
- src/tools/swap.ts:10-35 (schema)Zod schema defining the input parameters for ODOS_SWAP: chain (default fraxtal), fromToken, toToken addresses, amount in wei, optional prettyFormat.const swapParamsSchema = z.object({ chain: z .string() .optional() .describe( "The blockchain network to execute the transaction on. uses fraxtal as default", ) .default("fraxtal"), fromToken: z .string() .refine(isAddress, { message: "Invalid fromToken address" }) .describe("The token to swap from (address)."), toToken: z .string() .refine(isAddress, { message: "Invalid toToken address" }) .describe("The token to swap to (address)."), amount: z .string() .regex(/^\d+$/, { message: "Amount must be a string in wei (no decimals)" }) .describe("The amount of tokens to swap, in wei."), prettyFormat: z .boolean() .optional() .describe("Whether to pretty format the quote.") .default(true), });
- src/index.ts:15-15 (registration)Registers the swapTool (ODOS_SWAP) with the FastMCP server.server.addTool(swapTool);