Skip to main content
Glama
guilhermelirio

Brazilian CEP MCP

consultar-cep

Query Brazilian postal codes (CEP) to retrieve detailed address information including street names, neighborhoods, cities, states, and IBGE codes.

Instructions

Query address information from a Brazilian postal code (CEP)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
cepYesPostal code to be queried (only numbers, 8 digits)

Implementation Reference

  • Handler function that executes the CEP lookup using ViaCEP API, handles errors, and formats the address response.
    async ({ cep }) => {
      try {
        console.error(`Consulting postal code: ${cep}`);
        
        const url = `https://viacep.com.br/ws/${cep}/json/`;
        const response = await axios.get(url);
        
        // Verificando se houve erro na resposta da API
        if (response.data.erro) { 
          return {
            content: [{ 
              type: "text", 
              text: "Postal code not found in the database." 
            }],
            isError: true
          };
        }
        
        // Formatando a resposta para exibição
        const endereco = response.data;
        return {
          content: [{ 
            type: "text", 
            text: `
    Endereço encontrado:
    CEP: ${endereco.cep}
    Logradouro: ${endereco.logradouro}
    Complemento: ${endereco.complemento || "N/A"}
    Bairro: ${endereco.bairro}
    Cidade: ${endereco.localidade}
    Estado: ${endereco.uf} (${endereco.estado})
    Região: ${endereco.regiao}
    DDD: ${endereco.ddd}
    IBGE: ${endereco.ibge}
            ` 
          }]
        };
        
      } catch (erro: any) {
        console.error(`Error querying postal code: ${erro.message}`);
        
        // Verificando se o erro é devido a formato inválido (status 400)
        if (erro.response && erro.response.status === 400) {
          return {
            content: [{ 
              type: "text", 
              text: "Invalid postal code format. Use only numbers (8 digits)." 
            }],
            isError: true
          };
        }
        
        return {
          content: [{ 
            type: "text", 
            text: `Error querying postal code: ${erro.message}` 
          }],
          isError: true
        };
      }
    }
  • Zod schema for input validation of the 'cep' parameter, ensuring 8 digits.
    {
      cep: z.string()
        .length(8, "Postal code must have exactly 8 digits")
        .regex(/^\d+$/, "Postal code must contain only numbers")
        .describe("Postal code to be queried (only numbers, 8 digits)")
    },
  • src/index.ts:18-88 (registration)
    Registration of the 'consultar-cep' tool on the MCP server with description, schema, and handler.
    server.tool(
      "consultar-cep",
      "Query address information from a Brazilian postal code (CEP)",
      {
        cep: z.string()
          .length(8, "Postal code must have exactly 8 digits")
          .regex(/^\d+$/, "Postal code must contain only numbers")
          .describe("Postal code to be queried (only numbers, 8 digits)")
      },
      async ({ cep }) => {
        try {
          console.error(`Consulting postal code: ${cep}`);
          
          const url = `https://viacep.com.br/ws/${cep}/json/`;
          const response = await axios.get(url);
          
          // Verificando se houve erro na resposta da API
          if (response.data.erro) { 
            return {
              content: [{ 
                type: "text", 
                text: "Postal code not found in the database." 
              }],
              isError: true
            };
          }
          
          // Formatando a resposta para exibição
          const endereco = response.data;
          return {
            content: [{ 
              type: "text", 
              text: `
      Endereço encontrado:
      CEP: ${endereco.cep}
      Logradouro: ${endereco.logradouro}
      Complemento: ${endereco.complemento || "N/A"}
      Bairro: ${endereco.bairro}
      Cidade: ${endereco.localidade}
      Estado: ${endereco.uf} (${endereco.estado})
      Região: ${endereco.regiao}
      DDD: ${endereco.ddd}
      IBGE: ${endereco.ibge}
              ` 
            }]
          };
          
        } catch (erro: any) {
          console.error(`Error querying postal code: ${erro.message}`);
          
          // Verificando se o erro é devido a formato inválido (status 400)
          if (erro.response && erro.response.status === 400) {
            return {
              content: [{ 
                type: "text", 
                text: "Invalid postal code format. Use only numbers (8 digits)." 
              }],
              isError: true
            };
          }
          
          return {
            content: [{ 
              type: "text", 
              text: `Error querying postal code: ${erro.message}` 
            }],
            isError: true
          };
        }
      }
    );
Behavior2/5

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

No annotations are provided, so the description carries the full burden of behavioral disclosure. It states the tool queries address information, which implies a read-only operation, but it does not disclose other traits such as rate limits, error handling, authentication needs, or data sources. The description adds minimal behavioral context beyond the basic function.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that directly states the tool's function without any unnecessary words. It is front-loaded with the core purpose and appropriately sized for a simple tool with one parameter.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's low complexity (one parameter, no output schema, no annotations), the description is adequate but has gaps. It covers the basic purpose and parameter context but lacks details on behavioral aspects like error cases or return format, which would be helpful for an agent to use it correctly without structured output information.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema has 100% description coverage, clearly documenting the 'cep' parameter with format and length constraints. The description adds value by specifying that it queries 'address information' and mentions 'Brazilian postal code (CEP)', providing context about the parameter's purpose and regional scope, which compensates for the lack of output schema.

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

Purpose5/5

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

The description clearly states the specific action ('Query address information') and resource ('from a Brazilian postal code (CEP)'). It uses a precise verb and distinguishes the tool's purpose without redundancy, as there are no sibling tools to differentiate from.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage for retrieving address data from Brazilian postal codes, but it does not provide explicit guidance on when to use this tool versus alternatives (e.g., other geolocation tools) or any exclusions. Since there are no sibling tools, the lack of comparative guidance is less critical, but it still offers only implied context.

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/guilhermelirio/brazilian-cep-mcp'

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