Python MCP SDK
The Descope Python MCP SDK (descope-mcp) provides a simple way to integrate Descope authentication and authorization with MCP (Model Context Protocol) servers built with Python.
Overview
The SDK provides:
- Token validation with scope and audience enforcement
- Connection token retrieval using MCP server access tokens (default) or management keys
- Scope validation following the MCP spec for insufficient scope errors
- Integration with FastMCP and the official MCP SDK
Compatibility
- Python: 3.8+
- MCP SDK: 1.0.0+
- Descope SDK: 1.0.0+
- FastMCP: 2.0+
The SDK can also be imported alongside the official Descope Python SDK without conflicts.
Prerequisites
Before using the SDK, you need to:
- Set up your Descope MCP Server in the Descope Console
- Get your
.well-knownURL from the MCP server settings
Setting Up Your MCP Server
In Descope, create or configure your MCP server in Agentic Identity Hub → MCP Servers → Settings. The settings include:
- MCP Server URL: If set, this becomes the
audclaim in access tokens and should match themcp_server_urlyou pass to this SDK - Discovery endpoints: Use the server's discovery settings to copy the MCP Server's
.well-knownOpenID configuration URL and pass it aswell_known_url
For detailed instructions, see MCP Server Settings.
Installation
To install the SDK, run the following command:
Then to initialize the SDK, use the following code:
Key Functions
The SDK provides the following main functions for working with MCP servers:
validate_token()- Validates MCP server access tokens and returns token claims including user ID and scopesvalidate_token_and_get_user_id()- Convenience function that validates a token and returns just the user IDrequire_scopes()- Validates that a token contains required scopes, raisingInsufficientScopeErrorif scopes are missing (MCP spec-compliant)get_connection_token()- Retrieves OAuth tokens for third-party services stored in Descope Connections, with support for specific scopes or latest token retrieval
Each function is detailed in the sections below.
Token Validation
Validate MCP server access tokens with signature verification, expiration checking, and audience validation:
The validation checks:
- Token signature against JWKs from the
.well-knownendpoint - Token expiration
- Audience (
aud) claim matchesmcp_server_url(if provided) - Token issuer matches the configured Descope project
Scope Validation
The SDK provides scope validation that follows the MCP spec's Runtime Insufficient Scope Errors.
Using require_scopes()
InsufficientScopeError
When require_scopes() detects missing scopes, it raises an InsufficientScopeError that follows the MCP spec:
The error includes:
error: "insufficient_scope"scope: Space-separated list of all scopes (existing + required) - uses recommended approacherror_description: Human-readable descriptionmissing_scopes: List of missing scopestoken_scopes: List of scopes in the tokenrequired_scopes: List of required scopes
Connection Tokens
Retrieve OAuth tokens for third-party services stored in Descope Connections:
The SDK uses MCP server access tokens by default for policy enforcement. Management keys can be used as a fallback for tenant-level tokens or when access tokens aren't available.
When using access_token, Descope enforces your access control policies before returning connection tokens. This ensures only authorized clients can retrieve tokens for specific connections.
Examples
You can find examples of MCP servers using our SDK in our AI repository in GitHub.
Here's a complete example of the SDK in action, showing token validation, scope checking, and connection token retrieval: