Bearerトークンとヘッダー
リクエストごとにアクセストークンを要求し、読み取ります。
「Bearerトークンとヘッダー」はCoddyKit上の無料MCP Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMCP Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 MCP Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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-7f3a9c2e8b1d4a6fReading 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. ✅
よくある質問
「Bearerトークンとヘッダー」レッスンは無料ですか?
はい。「Bearerトークンとヘッダー」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、MCP Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 MCP Academyコースには全4レッスンが含まれています。
「Bearerトークンとヘッダー」で何を学びますか?
リクエストごとにアクセストークンを要求し、読み取ります。 ブラウザで直接実行するハンズオンコードでMCP Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
MCP Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMCP Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「Bearerトークンとヘッダー」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMCP Academyレッスンでコードを書いて実行できますか?
はい。すべてのMCP Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- リモートサーバーに認証が必要な理由
- Bearerトークンとヘッダー
- MCPにおけるOAuthフロー
- トークンの権限範囲を制限する