|
| 1 | +import logging |
| 2 | +import os |
| 3 | +from typing import List |
| 4 | + |
| 5 | +from langchain_core.tools import BaseTool |
| 6 | +from langchain_mcp_adapters.client import MultiServerMCPClient |
| 7 | + |
| 8 | +logger = logging.getLogger(__name__) |
| 9 | + |
| 10 | +OKX_MCP_URL = "https://web3.okx.com/api/v1/onchainos-mcp" |
| 11 | + |
| 12 | +# Allowlist of OKX MCP tools to expose (read-only market data only) |
| 13 | +ALLOWED_TOOLS = { |
| 14 | + # Market prices & charts |
| 15 | + "dex-okx-market-price", |
| 16 | + "dex-okx-market-candlesticks", |
| 17 | + "dex-okx-market-candlesticks-history", |
| 18 | + "dex-okx-market-price-chains", |
| 19 | + # Token discovery & analytics |
| 20 | + "dex-okx-market-token-search", |
| 21 | + "dex-okx-market-token-basic-info", |
| 22 | + "dex-okx-market-token-ranking", |
| 23 | + "dex-okx-market-token-holder", |
| 24 | + # Smart money signals |
| 25 | + "dex-okx-market-signal-list", |
| 26 | + "dex-okx-market-signal-supported-chains", |
| 27 | + # Balance / portfolio (read-only) |
| 28 | + "dex-okx-balance-chains", |
| 29 | + "dex-okx-balance-total-token-balances", |
| 30 | + "dex-okx-balance-total-value", |
| 31 | + "dex-okx-balance-specific-token-balance", |
| 32 | + # DEX |
| 33 | + "dex-okx-dex-liquidity", |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +class OKXMCPClient: |
| 38 | + """Manages the OKX MCP connection and exposes market data tools for the agent.""" |
| 39 | + |
| 40 | + def __init__(self, api_key: str | None = None): |
| 41 | + self._api_key = api_key or os.getenv("OKX_API_KEY", "") |
| 42 | + self._client = None |
| 43 | + self._tools: List[BaseTool] = [] |
| 44 | + |
| 45 | + async def connect(self) -> None: |
| 46 | + """Connect to OKX MCP server and load market data tools.""" |
| 47 | + self._client = MultiServerMCPClient( |
| 48 | + { |
| 49 | + "okx": { |
| 50 | + "transport": "streamable_http", |
| 51 | + "url": OKX_MCP_URL, |
| 52 | + "headers": { |
| 53 | + "OK-ACCESS-KEY": self._api_key, |
| 54 | + }, |
| 55 | + } |
| 56 | + } |
| 57 | + ) |
| 58 | + |
| 59 | + all_tools = await self._client.get_tools() |
| 60 | + self._tools = [t for t in all_tools if t.name in ALLOWED_TOOLS] |
| 61 | + |
| 62 | + blocked = [t.name for t in all_tools if t.name not in ALLOWED_TOOLS] |
| 63 | + logger.info( |
| 64 | + f"OKX MCP: loaded {len(self._tools)} market data tools, " |
| 65 | + f"blocked {len(blocked)} execution tools: {blocked}" |
| 66 | + ) |
| 67 | + |
| 68 | + async def disconnect(self) -> None: |
| 69 | + """Disconnect from the MCP server.""" |
| 70 | + self._client = None |
| 71 | + self._tools = [] |
| 72 | + |
| 73 | + def get_tools(self) -> List[BaseTool]: |
| 74 | + """Return the loaded market data tools for use in an agent toolkit.""" |
| 75 | + return self._tools |
0 commit comments