アシスタントのクライアントを作る
OAuthでログインする
アシスタントがAPIキーなしでログインする方法を説明します。ディスカバリー、登録、同意、トークンについて、自分でクライアントを作る人向けです。
AIアシスタントは、APIキーではなくOAuthでSetherionにログインします。ユーザーがSetherionのページでAllowを押すと、アシスタントがトークンを受け取ります。このページは、自分でクライアントを作る人のための仕様です。Claude、ChatGPT、Cursor、VS Codeを接続したいだけの場合は、MCP:AIアシスタントを接続するに従ってください。
このフローは、MCPの認可仕様(2025-06-18)が求めるOAuth 2.1です。
- 認可コードフローで、PKCE(S256)が常に必須です。
- パブリッククライアントのみで、クライアントシークレットはありません。
- 動的クライアント登録(RFC 7591)なので、クライアントIDをどこかに貼り付ける必要はありません。
- トークンは1つのリソース、つまりMCPエンドポイントに紐づきます(RFC 8707)。
トークンはMCPサーバーでのみ使え、それ以外では使えません。このリファレンスのRESTエンドポイントはAPIキーを使います。
| 項目 | 値 |
|---|---|
| Issuer | https://www.setherion.com |
| Resource | https://www.setherion.com/api/mcp |
| Scope | mcp |
| Access token | sto_で始まり、有効期間は1時間 |
| Refresh token | str_で始まり、有効期間は30日で、使うたびに置き換わる |
| Authorization code | stc_で始まり、有効期間は10分で、1回だけ使える |
1. ディスカバリー
認証情報なしでMCPエンドポイントにリクエストを送ります。401のWWW-Authenticateヘッダーが、リソースメタデータ(RFC 9728)を指します。
curl https://www.setherion.com/.well-known/oauth-protected-resource/api/mcp{
"resource": "https://www.setherion.com/api/mcp",
"authorization_servers": ["https://www.setherion.com"],
"bearer_methods_supported": ["header"],
"scopes_supported": ["mcp"],
"resource_name": "Setherion"
}同じドキュメントが/.well-known/oauth-protected-resourceにもあります。次に、認可サーバーのメタデータ(RFC 8414)を読みます。
curl https://www.setherion.com/.well-known/oauth-authorization-server{
"issuer": "https://www.setherion.com",
"authorization_endpoint": "https://www.setherion.com/oauth/authorize",
"token_endpoint": "https://www.setherion.com/api/oauth/token",
"registration_endpoint": "https://www.setherion.com/api/oauth/register",
"response_types_supported": ["code"],
"grant_types_supported": ["authorization_code", "refresh_token"],
"code_challenge_methods_supported": ["S256"],
"token_endpoint_auth_methods_supported": ["none"],
"scopes_supported": ["mcp"]
}この2つのドキュメント、登録エンドポイント、トークンエンドポイントは、どのオリジンからのリクエストにも応答する(Access-Control-Allow-Origin: *)ので、ブラウザー上のページのクライアントから呼び出せます。
2. 登録
https://www.setherion.com/api/oauth/registerプラン: すべてのプラン
インストールごとに1回登録します。登録しただけでは何も許可されません。ユーザーがAllowを押す必要があり、コードはここで登録した正確なアドレスにだけ送り返されます。
curl -X POST https://www.setherion.com/api/oauth/register \
-H "Content-Type: application/json" \
-d '{ "client_name": "My assistant", "redirect_uris": ["http://localhost:8787/callback"] }'| フィールド | 意味 |
|---|---|
redirect_uris | 必須です。1件から10件のアドレスで、各アドレスは2,000文字までです。重複は取り除かれます。 |
client_name | 任意です。同意ページでユーザーに表示されます。100文字で切り詰められ、省略するとAn AI assistantになります。 |
grant_types | 任意です。authorization_codeとrefresh_tokenだけが受け付けられます。 |
response_types | 任意です。codeだけです。 |
リダイレクトアドレスはhttpsか、このマシンに戻るhttp(localhost、127.0.0.1、[::1])でなければなりません。フラグメントやユーザー名を含めることはできません。httpsでもhttpでもない唯一のアドレスは、古いCursorアプリ向けのcursor://anysphere.cursor-mcp/oauth/callbackです。
クライアントがどの認証方式を求めても、シークレットなしのパブリッククライアントとして登録され、レスポンスにもそれが示されます。
{
"client_id": "5f0c2a9d-4e30-4d7f-b8a5-6f1c2a9d4e30",
"client_id_issued_at": 1790000000,
"client_name": "My assistant",
"redirect_uris": ["http://localhost:8787/callback"],
"grant_types": ["authorization_code", "refresh_token"],
"response_types": ["code"],
"token_endpoint_auth_method": "none"
}ステータスは201です。登録は、ネットワークアドレスごとに1時間あたり20回までに制限されています。
登録のエラー
エラーはすべて{ "error": "...", "error_description": "..." }の形です。
| ステータス | error | error_description |
|---|---|---|
400 | invalid_redirect_uri | Send between one and ten redirect_uris. |
400 | invalid_redirect_uri | Redirect URIs must be HTTPS, or HTTP back to localhost. |
400 | invalid_client_metadata | Only authorization_code and refresh_token are supported. |
400 | invalid_client_metadata | Only the code response type is supported. |
429 | slow_down | Too many registrations. Try again later. |
503 | temporarily_unavailable | Not configured.またはConnecting assistants is not switched on yet. |
3. ユーザーに確認する
認可エンドポイントを、ユーザーのブラウザーで開きます。これはAPIではなくページです。ユーザーはSetherionにまだログインしていなければログインし、登録した名前と戻り先を確認して、AllowまたはCancelを押します。
https://www.setherion.com/oauth/authorize
?response_type=code
&client_id=5f0c2a9d-4e30-4d7f-b8a5-6f1c2a9d4e30
&redirect_uri=http%3A%2F%2Flocalhost%3A8787%2Fcallback
&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
&code_challenge_method=S256
&state=af0ifjsldkj
&resource=https%3A%2F%2Fwww.setherion.com%2Fapi%2Fmcp| パラメーター | 意味 |
|---|---|
response_type | 必須です。codeです。 |
client_id | 必須です。登録で得たものです。 |
redirect_uri | 登録したアドレスのどれかで、完全に一致する必要があります。登録したアドレスが1つだけの場合に限り、省略できます。 |
code_challenge | 必須です。ベリファイアのS256ハッシュをbase64urlにした、43文字から128文字の値です。 |
code_challenge_method | 必須です。S256です。plainは受け付けられません。 |
state | 推奨です。そのまま送り返されます。1,000文字までです。 |
resource | 任意です。送る場合はhttps://www.setherion.com/api/mcpでなければなりません(末尾のスラッシュや、スキームとホストの大文字小文字は問いません)。 |
ベリファイアはA-Z a-z 0-9 - . _ ~の文字を使って43文字から128文字にし、ステップ4まで保持しておきます。
ユーザーは次の内容とともにredirect_uriに戻されます。
- Allowの場合:
code、state、iss(Issuer)。 - Cancelの場合:
error=access_denied、error_description=The request was declined.、state、iss。 - リクエストに問題があり、クライアントとアドレスが本物だと分かっている場合:
error、error_description、state、iss。errorはunsupported_response_type、invalid_request(PKCEがない、またはメソッドがS256ではない)、invalid_target(リソースがMCPエンドポイントではない)のどれかです。
クライアントが不明な場合や、アドレスが登録したものではない場合は、ページがユーザーにそう伝え、どこにも移動させません。そのため、このページを使って、登録していないアドレスにユーザーをリダイレクトすることはできません。
4. コードを交換する
https://www.setherion.com/api/oauth/tokenプラン: すべてのプラン
OAuthの仕様どおり、フォームエンコードのフィールドを送ります。同じ名前のJSONも受け付けられます。
curl -X POST https://www.setherion.com/api/oauth/token \
-d grant_type=authorization_code \
-d code=stc_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \
-d client_id=5f0c2a9d-4e30-4d7f-b8a5-6f1c2a9d4e30 \
-d redirect_uri=http://localhost:8787/callback \
-d code_verifier=YOUR_VERIFIER| フィールド | 意味 |
|---|---|
grant_type | authorization_codeです。 |
code | リダイレクトで受け取ったコードです。 |
client_id | コードが発行されたクライアントです。 |
redirect_uri | ステップ3と同じアドレスです。 |
code_verifier | code_challengeとして送ったS256ハッシュの元のベリファイアです。 |
resource | 任意です。送る場合は、コードのリソースと一致しなければなりません。 |
{
"access_token": "sto_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"token_type": "Bearer",
"expires_in": 3600,
"refresh_token": "str_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"scope": "mcp"
}アクセストークンは、Authorization: Bearer sto_...としてMCPサーバーに送ります。トークンはキャッシュされません。レスポンスにはCache-Control: no-storeが付いています。
5. 更新
アクセストークンの有効期間は1時間です。期限が切れる前、またはMCPサーバーが401を返したときに、リフレッシュトークンを交換します。
curl -X POST https://www.setherion.com/api/oauth/token \
-d grant_type=refresh_token \
-d refresh_token=str_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \
-d client_id=5f0c2a9d-4e30-4d7f-b8a5-6f1c2a9d4e30レスポンスはステップ4と同じですが、リフレッシュトークンは新しいものになります。古いものは使えなくなるので、毎回新しいものを保存してください。
再利用されたリフレッシュトークンは接続を終了させる
リフレッシュトークンを2回使うと、盗まれたコピーとみなされ、Setherionは接続全体を終了します。その同意から発行されたすべてのトークンが使えなくなり、ユーザーはもう一度Allowを押す必要があります。例外は、接続がまだ有効な間に、同じクライアントが同じトークンを60秒以内に提示した場合です。これは、1つのアシスタントの2つのプロセス、たとえば2つのウィンドウが、同時に更新できるようにするためです。コードを2回使った場合も、同じように接続が終了します。
エラー
トークンエンドポイントのエラーのうち、ステータスが400なのはserver_errorを除くすべてで、本文は{ "error": "...", "error_description": "..." }です。
トークンのエラー
error | error_description |
|---|---|
unsupported_grant_type | Use authorization_code or refresh_token. |
invalid_grant | Unknown code. |
invalid_grant | This code was already used. |
invalid_grant | This code has expired. |
invalid_grant | This code was issued to another application. |
invalid_grant | redirect_uri does not match the one the code was issued for. |
invalid_grant | PKCE verification failed. |
invalid_grant | Unknown refresh token. |
invalid_grant | This refresh token has expired. |
invalid_grant | This token was issued to another application. |
invalid_grant | This refresh token was already used. The connection has been ended for safety. |
invalid_target | This code was issued for another resource.またはThis token was issued for another resource. |
server_error(ステータス500) | Not configured.またはCould not issue tokens. |
接続を終了する
ユーザーは、Settings、AI assistants and MCPの順に開いて、アシスタントの接続を解除できます。その接続のすべてのトークン(アクセストークンもリフレッシュトークンも)は、1時間の期限を待たず、次のリクエストからすぐに使えなくなります。接続の一覧表示と終了は、ログイン済みのSetherionのセッションからしかできないので、アシスタントが持っているものでは実行できません。
代わりにAPIキーを使う
固定のヘッダーしか送れないクライアントは、これらをすべて省略して、MCPサーバーにAPIキーを送ることができます。認証を見てください。
このページは役に立ちましたか?
