Skip to main content
Glama

ODOS_SWAP

Execute a token swap transaction on a blockchain network using the from and to token addresses and the amount in wei.

Instructions

Execute a swap transaction

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
chainNoThe blockchain network to execute the transaction on. uses fraxtal as defaultfraxtal
fromTokenYesThe token to swap from (address).
toTokenYesThe token to swap to (address).
amountYesThe amount of tokens to swap, in wei.
prettyFormatNoWhether to pretty format the quote.

Implementation Reference

  • Main handler for the ODOS_SWAP tool. Fetches a quote, assembles a transaction, checks/sets token allowance, and executes the swap on-chain via the Odos API.
    export const swapTool = {
    	name: "ODOS_SWAP",
    	description: "Execute a swap transaction",
    	parameters: swapParamsSchema,
    	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}`);
    		}
    	},
    };
  • Zod schema defining the input parameters for ODOS_SWAP: chain (default fraxtal), fromToken, toToken, amount (wei string), and 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:16-18 (registration)
    Registration of the ODOS_SWAP tool with the FastMCP server in the main entry point.
    server.addTool(getQuoteTool);
    server.addTool(swapTool);
    server.addTool(chainIdTool);
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations, the description carries full burden. 'Execute a swap transaction' implies a write operation but does not disclose side effects, authorization needs (e.g., token approval), or error conditions, making it insufficiently transparent.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single sentence, concise and front-loaded. It earns its place but could benefit from additional context without becoming verbose.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity (swap transaction with 5 parameters, no output schema), the description is incomplete. It omits critical details like expected output (transaction hash?), slippage parameters, or prerequisite approvals, leaving gaps for the agent.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Input schema covers 100% of parameters with descriptions (e.g., each token address, amount in wei). The description adds no new meaning, so baseline score of 3 is appropriate per guidelines.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description 'Execute a swap transaction' clearly states the verb and resource, distinguishing it from sibling tools like ODOS_GET_QUOTE which provides quotes. However, it lacks additional context about what a swap entails, slightly reducing clarity.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No usage guidelines are provided. The description does not specify when to use this tool (e.g., after obtaining a quote) or when not to use it, leaving the agent without important context for correct invocation.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/IQAIcom/mcp-odos'

If you have feedback or need assistance with the MCP directory API, please join our Discord server