0Pricing
MCP Academy · Aula

Tokens de portador e cabeçalhos

Exija e leia um token de acesso em cada solicitação.

Tokens de portador e cabeçalhos é uma aula grátis de MCP Academy no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de MCP Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de MCP Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

What a Bearer Token Is

A bearer token is a secret string that means "whoever holds this is allowed in." The server trusts the holder, no password needed per call. 🎟️

The Authorization Header

Clients send the token in the HTTP Authorization header so it rides along with every request automatically.

The Bearer Prefix

The header value starts with the word Bearer, a space, then the token. That prefix tells the server which auth scheme is in use.

GET /mcp HTTP/1.1
Authorization: Bearer sk-7f3a9c2e8b1d4a6f

Reading the Header on the Server

Your MCP server pulls the Authorization value off the incoming request before handling the JSON-RPC body.

auth = request.headers.get("Authorization", "")
token = auth.removeprefix("Bearer ").strip()

Compare, Then Decide

The server checks the token against what it expects. A match lets the request through; anything else is rejected.

if token != EXPECTED_TOKEN:
    raise HTTPException(status_code=401)

Use Constant-Time Comparison

Plain equality can leak timing hints. Use a constant-time compare so attackers cannot guess the token character by character.

import hmac
ok = hmac.compare_digest(token, EXPECTED_TOKEN)

401 Means Not Authenticated

Return 401 Unauthorized when the token is missing or invalid. It signals the caller to supply valid credentials.

403 Means Not Allowed

Use 403 Forbidden when the caller is known but lacks permission for that action. It is a different answer than 401.

Keep Tokens Out of URLs

Never put the token in the query string. URLs land in logs and browser history, so the header is the safer home for secrets.

Store Secrets in the Environment

Read the expected token from an environment variable, never hardcoded in source. That keeps it out of your version control.

import os
EXPECTED_TOKEN = os.environ["MCP_TOKEN"]

Rotate When in Doubt

If a token might be exposed, issue a new one and retire the old. Rotation limits how long a leaked secret stays useful.

Quick Check

Spot the correct header format.

Recap: One Header, One Check

Send the secret as Bearer in the Authorization header, compare it safely, and answer 401 when it fails. Simple and solid. ✅

Perguntas Frequentes

A aula “Tokens de portador e cabeçalhos” é grátis?

Sim — o texto completo de “Tokens de portador e cabeçalhos” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de MCP Academy, atualize para CoddyKit PRO. O curso de MCP Academy inclui 4 aulas no total.

O que vou aprender em “Tokens de portador e cabeçalhos”?

Exija e leia um token de acesso em cada solicitação. Você pratica MCP Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar MCP Academy?

Nenhuma experiência prévia é necessária. MCP Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.

Quanto tempo leva a aula “Tokens de portador e cabeçalhos”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de MCP Academy?

Sim. Cada aula de MCP Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Por que servidores remotos precisam de autenticação
  2. Tokens de portador e cabeçalhos
  3. O fluxo OAuth no MCP
  4. Definir o escopo do que um token pode fazer
← Voltar para MCP Academy