0Pricing
MCP Academy · Lekcja

Tokeny Bearer i nagłówki

Wymagaj tokenu dostępu i odczytuj go przy każdym żądaniu.

Tokeny Bearer i nagłówki to bezpłatna lekcja MCP Academy na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej MCP Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs MCP Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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. ✅

Często zadawane pytania

Czy lekcja „Tokeny Bearer i nagłówki” jest bezpłatna?

Tak — pełny tekst „Tokeny Bearer i nagłówki” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu MCP Academy, przejdź na CoddyKit PRO. Kurs MCP Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „Tokeny Bearer i nagłówki”?

Wymagaj tokenu dostępu i odczytuj go przy każdym żądaniu. Ćwiczysz MCP Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć MCP Academy?

Nie wymagamy żadnego doświadczenia. MCP Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.

Ile czasu zajmuje lekcja „Tokeny Bearer i nagłówki”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji MCP Academy?

Tak. Każda lekcja MCP Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Dlaczego zdalne serwery potrzebują uwierzytelniania
  2. Tokeny Bearer i nagłówki
  3. Przepływ OAuth w MCP
  4. Ograniczanie możliwości tokenu
← Powrót do MCP Academy