Skip to main content
Glama
CodaLabs-xyz

X402 MCP Template

by CodaLabs-xyz

example_api_call

Make X402-protected API calls with gasless micropayments using EIP-712 signatures and USDC transfers. Send queries and control result limits for secure API consumption.

Instructions

Example tool for making X402-protected API calls. Replace with your actual API endpoints.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesExample query parameter - customize for your API
limitNoOptional: Maximum number of results to return

Implementation Reference

  • The handler for 'example_api_call' tool. It processes input arguments, handles demo mode with sample data, and in payment mode makes a POST request to the configured API endpoint using the X402 payment client, with proper error handling for payment failures.
    case "example_api_call": {
      const { query, limit = 10 } = args as { query: string; limit?: number };
    
      if (!query?.trim()) {
        throw new McpError(ErrorCode.InvalidParams, "Query parameter is required");
      }
    
      // Demo mode - return sample data with setup instructions
      if (!paymentEnabled) {
        return {
          content: [
            {
              type: "text",
              text: JSON.stringify(
                {
                  demo_mode: true,
                  message: "X402 payment client not configured - returning sample data",
                  sample_data: {
                    query: query,
                    limit: limit,
                    results: [
                      {
                        id: "sample-1",
                        name: "Sample Result 1",
                        description: "This is sample data returned in demo mode",
                      },
                      {
                        id: "sample-2",
                        name: "Sample Result 2",
                        description: "Configure PRIVATE_KEY in .env to enable real payments",
                      },
                    ],
                  },
                  setup_instructions: {
                    step_1: "Get testnet USDC from https://faucet.circle.com/",
                    step_2: "Add your private key to .env file: PRIVATE_KEY=0x...",
                    step_3: "Set RESOURCE_SERVER_URL to your X402 API endpoint",
                    step_4: "Rebuild and restart: npm run build",
                    step_5: "Try the tool again - payments will be automatic!",
                  },
                  payment_info: {
                    protocol: "X402 v1.0",
                    network: network,
                    cost: "Typically $0.001 - $0.01 per request",
                    gasless: true,
                    note: "You only pay for the API call, no gas fees!",
                  },
                },
                null,
                2
              ),
            },
          ],
        };
      }
    
      // Payment mode - make real API call
      // TODO: Customize endpoint path and request structure for your API
      const endpointPath = "/api/your-endpoint"; // ← Replace with your actual endpoint
    
      try {
        const response = await client.post(endpointPath, {
          query: query.trim(),
          limit: limit,
        });
    
        return {
          content: [
            {
              type: "text",
              text: JSON.stringify(
                {
                  success: true,
                  payment_processed: true,
                  data: response.data,
                  metadata: {
                    endpoint: endpointPath,
                    payment_protocol: "X402 v1.0",
                    network: network,
                  },
                },
                null,
                2
              ),
            },
          ],
        };
      } catch (error: any) {
        // Handle X402 payment errors
        if (error.response?.status === 402) {
          throw new McpError(
            ErrorCode.InternalError,
            `Payment Required: ${error.response.data?.error || "Insufficient USDC balance or payment failed"}`
          );
        }
        throw error;
      }
    }
  • The input schema definition for the 'example_api_call' tool, specifying required 'query' string and optional 'limit' number.
    inputSchema: {
      type: "object",
      properties: {
        query: {
          type: "string",
          description: "Example query parameter - customize for your API",
        },
        limit: {
          type: "number",
          description: "Optional: Maximum number of results to return",
          minimum: 1,
          maximum: 100,
          default: 10,
        },
      },
      required: ["query"],
    },
  • index.ts:109-129 (registration)
    The registration of the 'example_api_call' tool in the listTools response, including name, description, and input schema.
    {
      name: "example_api_call",
      description: "Example tool for making X402-protected API calls. Replace with your actual API endpoints.",
      inputSchema: {
        type: "object",
        properties: {
          query: {
            type: "string",
            description: "Example query parameter - customize for your API",
          },
          limit: {
            type: "number",
            description: "Optional: Maximum number of results to return",
            minimum: 1,
            maximum: 100,
            default: 10,
          },
        },
        required: ["query"],
      },
    },
Behavior2/5

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

With no annotations provided, the description carries full burden for behavioral disclosure. It mentions 'X402-protected' which suggests some authentication/authorization requirement, but doesn't explain what this protection entails, whether there are rate limits, error behaviors, or what the tool actually does beyond making calls. The template nature ('Replace with your actual API endpoints') further obscures actual behavior.

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 brief (two sentences) and gets straight to the point about being an example tool for API calls. However, the second sentence 'Replace with your actual API endpoints' adds confusion rather than clarity, slightly reducing efficiency. Overall it's appropriately sized for what it attempts to convey.

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?

For a tool with 2 parameters, no annotations, and no output schema, the description is insufficient. It doesn't explain what the tool returns, what 'X402-protected' means operationally, or provide enough context for an agent to understand when and how to use it effectively. The template disclaimer undermines completeness for actual usage.

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?

Schema description coverage is 100%, so the schema fully documents both parameters (query and limit). The description adds no parameter-specific information beyond what's in the schema. However, since schema coverage is complete, the baseline score of 3 is appropriate as the description doesn't need to compensate for schema gaps.

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

Purpose2/5

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

The description states 'Example tool for making X402-protected API calls' which provides a vague purpose (making API calls with some protection), but it's generic and doesn't specify what resource or action it performs. The phrase 'Replace with your actual API endpoints' indicates this is a template rather than a functional tool, which creates ambiguity about its actual purpose.

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 guidance is provided about when to use this tool versus the sibling tools (health_check, service_info). The description only mentions it's for 'X402-protected API calls' without explaining what that means or when this protection is needed. There's no comparison to alternatives or context for selection.

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/CodaLabs-xyz/Template-x402-Mcp'

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