OAuth 연결

MCP 클라이언트가 글감 계정을 연결할 때 뒤에서 일어나는 일을 설명합니다. 일반 사용자는 설정에 주소를 적고 브라우저에서 연결 승인을 누르는 것으로 끝나며, 나머지 내용은 그 과정이 어떻게 동작하는지 알고 싶거나 직접 클라이언트를 만드는 개발자를 위한 것입니다.

사용자가 하는 일

주소 등록과 승인 클릭 두 가지입니다. 설정 파일에는 연결 주소만 적습니다. Authorization 헤더나 토큰을 적지 마세요.

{
  "mcpServers": {
    "glgam": {
      "type": "http",
      "url": "https://www.glgam.com/mcp"
    }
  }
}

저장하면 클라이언트가 브라우저를 열어 글감 로그인(카카오) 화면을 보여줍니다. 로그인 뒤 요청 권한 glgam:read(구독 중인 블로그 그룹 콘텐츠 읽기)를 확인하고 연결 승인을 누르면 끝입니다. 클라이언트별 화면과 명령은 글감 MCP에서 볼 수 있습니다.

클라이언트가 자동으로 거치는 흐름

1. 401과 resource_metadata

클라이언트가 토큰 없이 https://www.glgam.com/mcp 에 initialize를 보내면 글감 MCP는 401과 함께 WWW-Authenticate 헤더로 보호 자원 메타데이터 주소를 알려줍니다.

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer resource_metadata="https://www.glgam.com/.well-known/oauth-protected-resource/mcp"

2. 디스커버리

클라이언트는 /.well-known/oauth-protected-resource/mcp 를 읽어 인증 서버가 https://www.glgam.com 임을 알아내고, 이어서 /.well-known/oauth-authorization-server 에서 authorize, token, register 엔드포인트와 PKCE 지원 여부를 읽습니다.

{
  "resource": "https://www.glgam.com/mcp",
  "authorization_servers": ["https://www.glgam.com"],
  "scopes_supported": ["glgam:read"],
  "bearer_methods_supported": ["header"]
}
{
  "issuer": "https://www.glgam.com",
  "authorization_endpoint": "https://www.glgam.com/api/oauth/authorize",
  "token_endpoint": "https://www.glgam.com/api/oauth/token",
  "registration_endpoint": "https://www.glgam.com/api/oauth/register",
  "scopes_supported": ["glgam:read"],
  "response_types_supported": ["code"],
  "grant_types_supported": ["authorization_code", "refresh_token"],
  "code_challenge_methods_supported": ["S256"],
  "token_endpoint_auth_methods_supported": ["none"]
}

3. 동적 클라이언트 등록

클라이언트는 /api/oauth/register 에 자신의 이름과 redirect_uri를 보내 client_id를 받습니다. 미리 발급받은 client_id나 시크릿이 필요 없습니다. 등록한 redirect_uri와 다른 주소로는 code가 전달되지 않습니다. 등록하지 않은 client_id는 이 컴퓨터의 루프백 주소로만 code를 받을 수 있고, https 주소를 쓰려면 반드시 먼저 등록해야 합니다. 루프백 주소는 localhost, 127.0.0.1, ::1 을 같은 주소로 취급하므로 어느 표기로 등록했든 나머지 표기로 요청해도 승인되며, 브라우저가 실제로 이동하는 주소는 127.0.0.1 로 통일됩니다.

POST https://www.glgam.com/api/oauth/register
Content-Type: application/json

{
  "client_name": "My MCP Client",
  "redirect_uris": ["http://127.0.0.1:53210/callback"],
  "grant_types": ["authorization_code", "refresh_token"],
  "response_types": ["code"],
  "token_endpoint_auth_method": "none"
}
{
  "client_id": "YOUR_CLIENT_ID",
  "client_name": "My MCP Client",
  "redirect_uris": ["http://127.0.0.1:53210/callback"],
  "grant_types": ["authorization_code", "refresh_token"],
  "response_types": ["code"],
  "token_endpoint_auth_method": "none",
  "scope": "glgam:read"
}

4. 브라우저 승인

클라이언트는 PKCE code_verifier를 만들고 그 S256 해시를 code_challenge로 넣어 /api/oauth/authorize 를 브라우저에서 엽니다. PKCE는 필수이며 code_challenge_method는 S256만 받습니다. 로그인하지 않은 사용자는 글감 로그인을 거치고, /consent 화면에서 승인 후 이동할 곳(redirect_uri의 주소)을 확인한 뒤 연결 승인을 누르면 redirect_uri로 code와 state가 돌아갑니다. code는 10분 동안 한 번만 쓸 수 있습니다.

https://www.glgam.com/api/oauth/authorize
  ?response_type=code
  &client_id=YOUR_CLIENT_ID
  &redirect_uri=http://127.0.0.1:53210/callback
  &scope=glgam:read
  &state=YOUR_STATE
  &code_challenge=YOUR_CODE_CHALLENGE
  &code_challenge_method=S256

5. 토큰 발급

클라이언트는 받은 code와 code_verifier를 /api/oauth/token 에 보내 액세스 토큰과 refresh_token을 받고, 이후 모든 글감 MCP 요청의 Authorization 헤더에 액세스 토큰을 넣습니다. 만료되면 refresh_token으로 갱신합니다.

POST https://www.glgam.com/api/oauth/token
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code
&code=YOUR_CODE
&redirect_uri=http://127.0.0.1:53210/callback
&client_id=YOUR_CLIENT_ID
&code_verifier=YOUR_CODE_VERIFIER
{
  "access_token": "YOUR_ACCESS_TOKEN",
  "token_type": "Bearer",
  "expires_in": 3600,
  "refresh_token": "YOUR_REFRESH_TOKEN",
  "scope": "glgam:read"
}

액세스 토큰은 1시간(expires_in 3600) 동안 유효하고, 만료되면 클라이언트가 refresh_token으로 새 토큰을 받습니다. refresh_token은 한 번 쓰면 새 refresh_token으로 바뀌며, 이미 쓴 refresh_token을 다시 보내면 탈취로 보고 그 연결 전체를 끊습니다. 연결 자체는 승인 화면에서 고른 기간(30일, 90일, 1년, 10년, 기본 1년) 동안 유지되고, 그 기간이 지나면 같은 프로그램에서 다시 승인해야 합니다. 연결은 글감 설정의 계정 관리 탭에 있는 연결된 앱에서 언제든 해제할 수 있으며, 해제하면 그 연결의 토큰은 즉시 쓸 수 없습니다.

엔드포인트 목록

경로방법설명
/.well-known/oauth-authorization-serverGET인증 서버 메타데이터. authorize, token, register 주소와 지원 grant, PKCE 방식(S256)
/.well-known/oauth-protected-resource/mcpGET글감 MCP 보호 자원 메타데이터. 인증 서버 위치와 scope 목록
/api/oauth/registerPOST동적 클라이언트 등록. client_name과 redirect_uris를 보내면 client_id 발급
/api/oauth/authorizeGET브라우저에서 여는 승인 시작 주소. response_type=code, scope=glgam:read, code_challenge_method=S256
/consentGET글감 로그인 뒤 사용자가 연결 승인을 누르는 화면. 승인하면 redirect_uri로 code 전달
/api/oauth/tokenPOST토큰 발급. grant_type은 authorization_code(code_verifier 필수) 또는 refresh_token

scope는 glgam:read 하나이며 구독 중인 블로그 그룹 콘텐츠를 읽는 권한입니다. 발급된 토큰으로 조회되는 범위는 멤버십과 구독 그룹에 따라 정해지며, 자세한 조건은 인증, 멤버십, 파라미터에 있습니다.

개발자용: 수동 흐름

자체 클라이언트를 만들거나 흐름을 검증할 때 손으로 토큰을 받아 볼 수 있습니다.

  1. /api/oauth/register 에 redirect_uri를 보내 client_id를 받습니다.
  2. 무작위 code_verifier를 만들고 SHA-256 해시를 base64url로 인코딩해 code_challenge로 씁니다.
  3. 위 4단계의 authorize 주소를 브라우저에서 열어 로그인과 승인을 거친 뒤 redirect_uri로 돌아온 code를 복사합니다.
  4. /api/oauth/token 에 code와 code_verifier를 보내 액세스 토큰을 받습니다.

액세스 토큰이 만료되면 refresh_token으로 새 토큰을 받습니다. 응답에 담긴 새 refresh_token을 저장하고, 이전 refresh_token은 버립니다.

POST https://www.glgam.com/api/oauth/token
Content-Type: application/x-www-form-urlencoded

grant_type=refresh_token
&refresh_token=YOUR_REFRESH_TOKEN
&client_id=YOUR_CLIENT_ID

받은 토큰이 동작하는지는 글감 API 그룹 목록 호출로 확인합니다.

curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" https://www.glgam.com/api/agent/groups

다음 단계