atualizar_endereco_pelo_cpf_e_cep
Update a client's address automatically by providing their CPF and CEP. The system fetches the address from the CEP and links it to the identified client.
Instructions
Busca o endereço pelo CEP e o vincula automaticamente ao cliente identificado pelo CPF informado.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| cpf | Yes | ||
| cep | Yes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- server.py:26-53 (handler)The tool's handler: an async function that uses the @mcp.tool() decorator to register as 'atualizar_endereco_pelo_cpf_e_cep'. It looks up the client by CPF in the database, fetches the address from BrasilAPI using the CEP, formats the address string, and updates the client's address in the database.
@mcp.tool() async def atualizar_endereco_pelo_cpf_e_cep(cpf: str, cep: str) -> str: """ Busca o endereço pelo CEP e o vincula automaticamente ao cliente identificado pelo CPF informado. """ clientes = database.buscar_registros("clientes", "cpf", cpf) if not clientes: return f"Erro não encontrei nenhum cliente cadastrado com o CPF {cpf}." id_cliente = clientes[0][0] # Busca o endereço na BrasilAPI cep_limpo = cep.replace("-", "").replace(" ", "") async with httpx.AsyncClient() as client: try: resp = await client.get(f"https://brasilapi.com.br/api/cep/v1/{cep_limpo}") if resp.status_code == 200: d = resp.json() endereco = f"{d['street']}, {d['neighborhood']}, {d['city']}-{d['state']}" return database.atualizar_registro("clientes", id_cliente, endereco=endereco) else: return f"CEP {cep} não encontrado na base de dados nacional." except Exception as e: return f"Erro na conexão com a API: {str(e)}" - server.py:26-26 (registration)The tool is registered via the @mcp.tool() decorator on line 26, which is from FastMCP framework. No separate registration file exists; the decorator on the function definition is how it's registered.
@mcp.tool() - database.py:28-37 (helper)Helper function 'atualizar_registro' used by the tool to UPDATE the client's address in SQLite after fetching it from the API.
def atualizar_registro(tabela, registro_id, **kwargs): conn = sqlite3.connect(DB_NAME) cursor = conn.cursor() set_clause = ', '.join([f"{k} = ?" for k in kwargs.keys()]) sql = f"UPDATE {tabela} SET {set_clause} WHERE id = ?" cursor.execute(sql, list(kwargs.values()) + [registro_id]) conn.commit() conn.close() return f" {tabela.capitalize()} ID {registro_id} atualizado!" - database.py:47-56 (helper)Helper function 'buscar_registros' used by the tool to look up the client by CPF before updating their address.
def buscar_registros(tabela, campo_filtro=None, valor_filtro=None): conn = sqlite3.connect(DB_NAME) cursor = conn.cursor() if campo_filtro: cursor.execute(f"SELECT * FROM {tabela} WHERE {campo_filtro} LIKE ?", (f"%{valor_filtro}%",)) else: cursor.execute(f"SELECT * FROM {tabela}") rows = cursor.fetchall() conn.close() return rows - server.py:27-27 (schema)The tool's schema/type signature: accepts 'cpf: str' and 'cep: str' as input parameters and returns 'str'. The decorator @mcp.tool() uses these Python type annotations to generate the MCP tool schema.
async def atualizar_endereco_pelo_cpf_e_cep(cpf: str, cep: str) -> str: