메인 콘텐츠로 건너뛰기

MCP 서버에서 MCP Auth 구성하기

MCP 서버를 OAuth 2.1 또는 OpenID Connect 공급자에 연결하려면 MCP Auth 인스턴스를 구성해야 합니다. 이는 공급자의 인가 서버 메타데이터로 인스턴스를 초기화하고 필요한 인가 플로우를 설정하는 과정을 포함합니다.

MCP Auth 초기화하기

메타데이터 자동 가져오기

MCP Auth 인스턴스를 초기화하는 가장 쉬운 방법은 well-known URL에서 메타데이터를 가져오는 내장 함수를 사용하는 것입니다. 공급자가 다음 표준 중 하나를 준수하는 경우:

fetchServerConfig를 사용하여 issuer URL을 제공하면 메타데이터를 자동으로 가져올 수 있습니다:

from mcpauth import MCPAuth
from mcpauth.config import AuthServerType, fetch_server_config

mcp_auth = MCPAuth(
    server=fetch_server_config(
        '<auth-server-url>',
        type=AuthServerType.OIDC  # 또는 AuthServerType.OAUTH
    )
)

issuer에 경로가 포함된 경우, OAuth 2.0과 OpenID Connect에서 동작이 약간 다릅니다:

  • OAuth 2.0: well-known URL이 issuer의 도메인에 추가됩니다. 예를 들어, issuer가 https://my-project.logto.app/oauth라면, well-known URL은 https://auth.logto.io/.well-known/oauth-authorization-server/oauth가 됩니다.
  • OpenID Connect: well-known URL이 issuer에 직접 추가됩니다. 예를 들어, issuer가 https://my-project.logto.app/oidc라면, well-known URL은 https://auth.logto.io/oidc/.well-known/openid-configuration이 됩니다.

MCP Auth를 초기화하는 다른 방법

커스텀 데이터 변환

경우에 따라 공급자가 반환하는 메타데이터가 예상 형식과 다를 수 있습니다. 공급자가 표준을 준수한다고 확신한다면, transpile_data 옵션을 사용하여 메타데이터를 사용하기 전에 수정할 수 있습니다:

from mcpauth import MCPAuth
from mcpauth.config import AuthServerType
from mcpauth.utils import fetch_server_config

mcp_auth = MCPAuth(
    server=fetch_server_config(
        '<auth-server-url>',
        type=AuthServerType.OIDC,
        transpile_data=lambda data: {**data, 'response_types_supported': ['code']} 
    )
)

이렇게 하면 MCP Auth에서 메타데이터 객체를 사용하기 전에 수정할 수 있습니다. 예를 들어, 필드를 추가하거나 제거하고, 값을 변경하거나, 다른 형식으로 변환할 수 있습니다.

특정 URL에서 메타데이터 가져오기

공급자가 표준이 아닌 특정 메타데이터 URL을 제공하는 경우에도 유사하게 사용할 수 있습니다:

from mcpauth import MCPAuth
from mcpauth.config import AuthServerType
from mcpauth.utils import fetch_server_config_by_well_known_url

mcp_auth = MCPAuth(
    server=fetch_server_config_by_well_known_url(
        '<metadata-url>',
        type=AuthServerType.OIDC # 또는 AuthServerType.OAUTH
    )
)

특정 URL에서 커스텀 데이터 변환과 함께 메타데이터 가져오기

경우에 따라 공급자의 응답이 잘못되었거나 예상 메타데이터 형식과 다를 수 있습니다. 공급자가 표준을 준수한다고 확신한다면, 설정 옵션을 통해 메타데이터를 변환할 수 있습니다:

from mcpauth import MCPAuth
from mcpauth.config import AuthServerType, fetch_server_config_by_well_known_url

mcp_auth = MCPAuth(
    server=fetch_server_config_by_well_known_url(
        '<metadata-url>',
        type=AuthServerType.OIDC,
        transpile_data=lambda data: {**data, 'response_types_supported': ['code']} 
    )
)

메타데이터를 수동으로 제공하기

공급자가 메타데이터 가져오기를 지원하지 않는 경우, 메타데이터 객체를 수동으로 제공할 수 있습니다:

from mcpauth import MCPAuth
from mcpauth.config import AuthServerConfig, AuthServerType, AuthorizationServerMetadata

mcp_auth = MCPAuth(
    server=AuthServerConfig(
        type=AuthServerType.OIDC,  # 또는 AuthServerType.OAUTH
        metadata=AuthorizationServerMetadata(
            issuer='<issuer-url>',
            authorization_endpoint='<authorization-endpoint-url>',
            # ... 기타 메타데이터 필드
        ),
    )
)