ブラウザを開けない CLI / MCP クライアントをどうログインさせるか、OAuth Device Code Flow を実装した話 ブラウザを開けない CLI / MCP クライアントをどうログインさせるか、OAuth Device Code Flow を実装した話
- MCP サーバを自分のサービスに繋ぐとき、地味に困るのが「CLI からのログイン」だ。
- ブラウザがある Web アプリなら、OAuth の authorization code flow が使える。
- ユーザーをログイン画面にリダイレクトして、
Web アプリであれば OAuth の Authorization Code Flow でブラウザリダイレクトを使った認証が一般的だが、CLI や MCP クライアントはブラウザを直接操作できないため、このフローをそのまま適用するのは難しい。自作サービスに MCP サーバを接続しようとすると、この「CLI からのログイン」問題が地味に壁になる。
この記事では、そうした環境向けに設計された OAuth Device Code Flow の仕組みと、実際の MCP サーバへの組み込み方を解説している。Device Code Flow ではクライアントがユーザーコードと検証 URL を表示し、ユーザーが別デバイスのブラウザで認証を完了する間、クライアント側はポーリングでトークン取得を待つ設計になっている。
使用言語・ライブラリ・対応 IdP などの実装詳細は元記事で確認することを推奨する。MCP サーバを自サービスと連携させたいエンジニアにとって実践的な参考になる内容と推察される。
When building web applications, the OAuth Authorization Code Flow with browser redirects is the standard authentication approach. However, CLI tools and MCP (Model Context Protocol) clients typically cannot open or control a browser, making redirect-based flows impractical. Connecting an MCP server to an external service therefore surfaces a subtle but real authentication challenge.
This article addresses that gap by documenting an implementation of OAuth Device Code Flow for browser-constrained environments. In this flow, the client presents a user code and a verification URL; the user authenticates on a separate browser-capable device while the client polls the authorization server until a token is issued. The pattern is purpose-built for headless or input-limited contexts such as CLI tools and MCP clients.
The post appears to cover the integration of Device Code Flow directly into an MCP server, though specific details—programming language, libraries, and identity provider support—should be verified in the original article. Engineers who need to add authentication to MCP servers connecting with their own services will likely find this a concise and actionable reference.
本ページの本文・要約は AI による自動生成です。正確性は元記事 (zenn.dev) をご確認ください。