Skip to main content
Glama

fetchOrderBook

Retrieve real-time order book data for cryptocurrency trading pairs from exchanges like Binance or Coinbase to analyze market depth and liquidity.

Instructions

Fetch order book for a symbol on an exchange

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
exchangeIdYesExchange ID (e.g., 'binance', 'coinbase')
symbolYesTrading symbol (e.g., 'BTC/USDT')
limitNoLimit the number of orders returned (optional)

Implementation Reference

  • The core handler function for the fetchOrderBook tool. It retrieves a public CCXT exchange instance, calls fetchOrderBook on the specified symbol with optional limit, and returns the order book as formatted JSON or an error message.
    async ({ exchangeId, symbol, limit }) => {
      try {
        // 공개 인스턴스 사용
        const exchange = ccxtServer.getPublicExchangeInstance(exchangeId);
        const orderbook = await exchange.fetchOrderBook(symbol, limit);
    
        return {
          content: [
            {
              type: "text",
              text: JSON.stringify(orderbook, null, 2)
            }
          ]
        };
      } catch (error) {
        return {
          content: [
            {
              type: "text",
              text: `Error fetching order book: ${(error as Error).message}`
            }
          ],
          isError: true
        };
      }
    }
  • Input schema definition using Zod for validating parameters: exchangeId (string), symbol (string), limit (optional number).
    {
      exchangeId: z.string().describe("Exchange ID (e.g., 'binance', 'coinbase')"),
      symbol: z.string().describe("Trading symbol (e.g., 'BTC/USDT')"),
      limit: z.number().optional().describe("Limit the number of orders returned (optional)")
    },
  • Registration of the fetchOrderBook tool on the MCP server within the registerMarketTools function, including name, description, input schema, and handler.
    server.tool(
      "fetchOrderBook",
      "Fetch order book for a symbol on an exchange",
      {
        exchangeId: z.string().describe("Exchange ID (e.g., 'binance', 'coinbase')"),
        symbol: z.string().describe("Trading symbol (e.g., 'BTC/USDT')"),
        limit: z.number().optional().describe("Limit the number of orders returned (optional)")
      },
      async ({ exchangeId, symbol, limit }) => {
        try {
          // 공개 인스턴스 사용
          const exchange = ccxtServer.getPublicExchangeInstance(exchangeId);
          const orderbook = await exchange.fetchOrderBook(symbol, limit);
    
          return {
            content: [
              {
                type: "text",
                text: JSON.stringify(orderbook, null, 2)
              }
            ]
          };
        } catch (error) {
          return {
            content: [
              {
                type: "text",
                text: `Error fetching order book: ${(error as Error).message}`
              }
            ],
            isError: true
          };
        }
      }
    );
  • src/server.ts:372-372 (registration)
    Invocation of registerMarketTools in the CcxtMcpServer's registerTools method, which registers the fetchOrderBook tool (among others) to the main MCP server instance.
    registerMarketTools(this.server, this);

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/lazy-dinosaur/ccxt-mcp'

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