"""
Retrieve tokens available for trading on specified blockchain.
"""
import json
from typing import Union
from mcp import types
from pydantic import BaseModel
from ...helpers import make_odos_request, resolve_chain_id
from ...mcp import mcp
class GetSupportedTokensArgs(BaseModel):
"""Arguments for getting supported tokens"""
chain_id: Union[str, int]
@mcp.tool(
name="get_supported_tokens",
description=(
"Get all of the supported ERC-20 tokens that Odos uses for swaps."
"These tokens have been manually audited to ensure they work with Odos"
),
)
async def get_supported_tokens(args: GetSupportedTokensArgs) -> list[types.TextContent]:
"""
Gets all supported ERC-20 tokens for a specific chain.
"""
try:
chain_id = resolve_chain_id(args.chain_id)
# Build URL with path parameters
url = f"/info/tokens/{chain_id}"
response_data = await make_odos_request(url, method="GET")
token_map = response_data.get("tokenMap", {})
output = {
"message": f"Successfully fetched supported tokens for chain {chain_id}.",
"chainId": chain_id,
"tokenMap": token_map,
"tokenCount": len(token_map),
"note": (
"Custom tokens can also be used with Odos"
"but there is no guarantee Odos will find a valid path"
),
}
return [types.TextContent(type="text", text=json.dumps(output, indent=2))]
except ConnectionError as e:
return [
types.TextContent(
type="text", text=f"❌ API Error in get_supported_tokens: {str(e)}"
)
]
except (ValueError, KeyError, TypeError) as e:
return [
types.TextContent(
type="text",
text=f"❌ Unexpected Error in get_supported_tokens: {str(e)}",
)
]