Skip to main content
Glama
guilhermelirio

Brasil API MCP

cnpj-search

Retrieve Brazilian company registration details by entering a valid 14-digit CNPJ number to access official business information.

Instructions

Query information about a Brazilian company by its CNPJ (National Registry of Legal Entities)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
cnpjYesCNPJ to be queried (only numbers, 14 digits)

Implementation Reference

  • The handler function that executes the CNPJ search: logs the CNPJ, fetches data from Brasil API, handles errors, and formats a detailed text response with company information.
        async ({ cnpj }) => {
          console.error(`Consulting CNPJ: ${cnpj}`);
          
          const result = await getBrasilApiData(`/cnpj/v1/${cnpj}`);
          
          if (!result.success) {
            return formatErrorResponse(`Error querying CNPJ: ${result.message}`);
          }
          
          // Format the response data
          const company = result.data;
          return {
            content: [{ 
              type: "text" as const, 
              text: `
    Informações da Empresa:
    CNPJ: ${company.cnpj}
    Razão Social: ${company.razao_social}
    Nome Fantasia: ${company.nome_fantasia || "N/A"}
    Situação Cadastral: ${company.descricao_situacao_cadastral || "N/A"}
    Data de Abertura: ${company.data_inicio_atividade || "N/A"}
    CNAE Principal: ${company.cnae_fiscal} - ${company.cnae_fiscal_descricao || "N/A"}
    Natureza Jurídica: ${company.codigo_natureza_juridica} - ${company.natureza_juridica || "N/A"}
    
    Endereço:
    ${company.logradouro || ""} ${company.numero || ""} ${company.complemento || ""}
    ${company.bairro || ""}, ${company.municipio || ""} - ${company.uf || ""}
    CEP: ${company.cep || "N/A"}
    
    Contato:
    Telefone: ${company.ddd_telefone_1 || "N/A"}
    Email: ${company.email || "N/A"}
    ` 
            }]
          };
        }
  • Input schema using Zod: validates CNPJ as exactly 14 digits string.
    {
      cnpj: z.string()
        .regex(/^\d{14}$/, "CNPJ must contain exactly 14 numbers without any special characters")
        .describe("CNPJ to be queried (only numbers, 14 digits)")
    },
  • Registers the 'cnpj-search' tool on the MCP server, specifying name, description, input schema, and handler function.
      server.tool(
        "cnpj-search",
        "Query information about a Brazilian company by its CNPJ (National Registry of Legal Entities)",
        {
          cnpj: z.string()
            .regex(/^\d{14}$/, "CNPJ must contain exactly 14 numbers without any special characters")
            .describe("CNPJ to be queried (only numbers, 14 digits)")
        },
        async ({ cnpj }) => {
          console.error(`Consulting CNPJ: ${cnpj}`);
          
          const result = await getBrasilApiData(`/cnpj/v1/${cnpj}`);
          
          if (!result.success) {
            return formatErrorResponse(`Error querying CNPJ: ${result.message}`);
          }
          
          // Format the response data
          const company = result.data;
          return {
            content: [{ 
              type: "text" as const, 
              text: `
    Informações da Empresa:
    CNPJ: ${company.cnpj}
    Razão Social: ${company.razao_social}
    Nome Fantasia: ${company.nome_fantasia || "N/A"}
    Situação Cadastral: ${company.descricao_situacao_cadastral || "N/A"}
    Data de Abertura: ${company.data_inicio_atividade || "N/A"}
    CNAE Principal: ${company.cnae_fiscal} - ${company.cnae_fiscal_descricao || "N/A"}
    Natureza Jurídica: ${company.codigo_natureza_juridica} - ${company.natureza_juridica || "N/A"}
    
    Endereço:
    ${company.logradouro || ""} ${company.numero || ""} ${company.complemento || ""}
    ${company.bairro || ""}, ${company.municipio || ""} - ${company.uf || ""}
    CEP: ${company.cep || "N/A"}
    
    Contato:
    Telefone: ${company.ddd_telefone_1 || "N/A"}
    Email: ${company.email || "N/A"}
    ` 
            }]
          };
        }
      );
  • src/index.ts:26-26 (registration)
    Top-level call to register CNPJ tools (including 'cnpj-search') on the main MCP server instance.
    registerCnpjTools(server);
  • Helper function to fetch data from Brasil API, handling requests and errors, used by the CNPJ handler.
    export async function getBrasilApiData(endpoint: string, params: Record<string, any> = {}) {
      try {
        const url = `${BASE_URL}${endpoint}`;
        console.error(`Making request to: ${url}`);
        
        const response = await axios.get(url, { params });
        return { 
          data: response.data,
          success: true
        };
      } catch (error: any) {
        console.error(`Error in API request: ${error.message}`);
        
        // Handle API errors in a structured format
        if (error.response) {
          return {
            success: false,
            statusCode: error.response.status,
            message: error.response.data?.message || error.message,
            error: error.response.data
          };
        }
        
        return {
          success: false,
          message: error.message,
          error
        };
      }
    }

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/guilhermelirio/brasil-api-mcp'

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