Skip to main content
Glama

jupiter_build_swap_transaction

Builds a Solana blockchain transaction for token swaps using Jupiter's API, converting quotes into executable swap transactions.

Instructions

Build a swap transaction on Jupiter

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
quoteResponseYes
userPublicKeyYes
prioritizationFeeLamportsNo
computeUnitPriceMicroLamportsNo
asLegacyTransactionNo

Implementation Reference

  • The main handler function that executes the tool: validates user public key, parses quote response, builds request to Jupiter /swap-instructions API, and returns the serialized swap transaction.
    export const buildSwapTransactionHandler = async (input: BuildSwapTransactionInput): Promise<ToolResultSchema> => {
      try {
        // Validate user public key
        const userPublicKeyResult = validatePublicKey(input.userPublicKey);
        if (!(userPublicKeyResult instanceof PublicKey)) {
          return userPublicKeyResult;
        }
        
        // Parse the quote response
        let quoteResponse;
        try {
          quoteResponse = JSON.parse(input.quoteResponse);
        } catch (error) {
          return createErrorResponse(`Invalid quote response: ${error instanceof Error ? error.message : String(error)}`);
        }
        
        // Build the request body
        const requestBody: any = {
          quoteResponse,
          userPublicKey: input.userPublicKey
        };
        
        if (input.prioritizationFeeLamports !== undefined) {
          requestBody.prioritizationFeeLamports = input.prioritizationFeeLamports;
        }
        
        if (input.computeUnitPriceMicroLamports !== undefined) {
          requestBody.computeUnitPriceMicroLamports = input.computeUnitPriceMicroLamports;
        }
        
        if (input.asLegacyTransaction !== undefined) {
          requestBody.asLegacyTransaction = input.asLegacyTransaction;
        }
        
        // Make the API request
        const response = await fetch(`${JUPITER_API_BASE_URL}/swap-instructions`, {
          method: "POST",
          headers: {
            "Content-Type": "application/json"
          },
          body: JSON.stringify(requestBody)
        });
        
        if (!response.ok) {
          const errorText = await response.text();
          return createErrorResponse(`Error building swap transaction: ${response.status} ${response.statusText} - ${errorText}`);
        }
        
        const swapData = await response.json();
        return createSuccessResponse(`Swap transaction: ${JSON.stringify(swapData, null, 2)}`);
      } catch (error) {
        return createErrorResponse(`Error building swap transaction: ${error instanceof Error ? error.message : String(error)}`);
      }
    };
  • Input schema and metadata definition for the tool used in MCP tool list.
    {
      name: "jupiter_build_swap_transaction",
      description: "Build a swap transaction on Jupiter",
      inputSchema: {
        type: "object",
        properties: {
          quoteResponse: { type: "string" },
          userPublicKey: { type: "string" },
          prioritizationFeeLamports: { type: "number" },
          computeUnitPriceMicroLamports: { type: "number" },
          asLegacyTransaction: { type: "boolean" }
        },
        required: ["quoteResponse", "userPublicKey"]
      }
    },
  • src/tools.ts:59-63 (registration)
    Maps the tool name to its handler function for dynamic dispatch.
    export const handlers: handlerDictionary = {
      "jupiter_get_quote": getQuoteHandler,
      "jupiter_build_swap_transaction": buildSwapTransactionHandler,
      "jupiter_send_swap_transaction": sendSwapTransactionHandler
    };
  • TypeScript type definition for the tool's input parameters, matching the inputSchema.
    export interface BuildSwapTransactionInput {
      quoteResponse: string;
      userPublicKey: string;
      prioritizationFeeLamports?: number;
      computeUnitPriceMicroLamports?: number;
      asLegacyTransaction?: boolean;
    }
  • Helper function used in the handler to validate the userPublicKey.
    export const validatePublicKey = (publicKeyString: string): PublicKey | ToolResultSchema => {
      const { publicKey, error } = createPublicKey(publicKeyString);
      if (error) {
        return createErrorResponse(error);
      }
      return publicKey!;
    };

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/dcSpark/mcp-server-jupiter'

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