環境変数

CLAUDE_CODE_MESSAGING_TOKEN

ユーザーではなく Claude Code が設定する変数である。インボックスソケットをバインドするセッションで、Claude Code はセッションごとのこのトークンを、CLAUDE_CODE_MESSAGING_SOCKET とともにフックと Bash コマンドへエクスポートする。ソケットに投稿するスクリプトは、最初の行として {'type':'auth','token':'<token>'} を送ることで、自分がそのセッションに属することを証明できる。ネイティブ Windows では、Claude Code はこの行を要求し、有効な行で始まらない接続を閉じる。own-child のルールは、Claude Code がいつトークンを参照するかを定める。各セッションは親セッションから継承したものではなく自身のトークンをエクスポートする。設定の env ブロックでは設定できない。Claude Code v2.1.228 以降が必要である

使い方・用途

  • Socket がプロセスの痕跡で子プロセスを検証できない環境(macOS で投稿元プロセスが終了した後、プロセス ID 1 として動作するコンテナ)で、自身のセッションの Socket に投稿するスクリプトや Hook が、最初の行に auth メッセージとして token を送ることで own-child メッセージとして検証を受ける。
  • ネイティブ Windows ではプロセスの痕跡による検証が行えないため、Socket への接続をこの token による認証で通す。
  • project や local settings では設定できずすべてのファイルから無視されるため、Settings env blocks での誤設定による偽装を防ぎたい場合は設定せず、Claude Code 自身のエクスポートに任せる。
英語原文(公式ドキュメントより)

Set by Claude Code, not by you: in sessions that bind an inbox socket, Claude Code exports this per-session token to hooks and Bash commands alongside CLAUDE_CODE_MESSAGING_SOCKET. A script posting to the socket can send {"type":"auth","token":"<token>"} as its first line to prove it belongs to the session. On native Windows, Claude Code requires this line and closes any connection that doesn't open with a valid one. The own-child rules say when Claude Code consults the token. Each session exports its own token, never one inherited from a parent session. Settings env blocks can't set it. Requires Claude Code v2.1.228 or later