Skip to main content

AuthSec SDK for MCP auth, services, CIBA, and SPIFFE integration

Project description

AuthSec Python SDK (authsec-sdk)

AuthSec Python SDK covers:

  • MCP OAuth + RBAC enforcement
  • Trust delegation for AI agents
  • Hosted service credential access
  • CIBA / passwordless authentication
  • SPIFFE workload identity helpers

Install

python3 -m pip install -U authsec-sdk

From this repo during development:

python3 -m pip install -e packages/python-sdk

Import Paths

Canonical package import:

from authsec_sdk import protected_by_AuthSec, run_mcp_server_with_oauth

Trust delegation, top-level import:

from authsec_sdk import DelegationClient

Trust delegation, direct submodule import:

from authsec_sdk.delegation_sdk import (
    DelegationClient,
    DelegationError,
    DelegationTokenExpired,
    DelegationTokenNotFound,
)

Legacy compatibility shim:

from AuthSec_SDK import protected_by_AuthSec, run_mcp_server_with_oauth

MCP Quick Start

from authsec_sdk import mcp_tool, protected_by_AuthSec, run_mcp_server_with_oauth


@mcp_tool(
    name="ping",
    description="Health check",
    inputSchema={"type": "object", "properties": {}, "required": []},
)
async def ping(arguments: dict) -> list:
    return [{"type": "text", "text": "pong"}]


@protected_by_AuthSec(
    tool_name="delete_invoice",
    permissions=["tool:delete_invoice"],
    require_all=True,
    description="Delete invoice by id",
    inputSchema={
        "type": "object",
        "properties": {
            "invoice_id": {"type": "string"},
            "session_id": {"type": "string"},
        },
        "required": ["invoice_id"],
    },
)
async def delete_invoice(arguments: dict) -> list:
    user = (arguments.get("_user_info") or {}).get("email_id", "unknown")
    return [{"type": "text", "text": f"Deleted {arguments['invoice_id']} by {user}"}]


if __name__ == "__main__":
    import __main__

    run_mcp_server_with_oauth(
        user_module=__main__,
        client_id="YOUR_CLIENT_ID",
        app_name="my-mcp-server",
        host="127.0.0.1",
        port=3005,
    )

Trust Delegation for Agents

Use trust delegation when an AI agent should pull a delegated JWT-SVID and gate its own tools from delegated permissions.

from authsec_sdk import DelegationClient


client = DelegationClient(
    client_id="YOUR_AGENT_CLIENT_ID",
    userflow_url="https://prod.api.authsec.ai/uflow",
)

token_info = await client.pull_token()

if client.has_permission("users:read"):
    result = await client.request_json(
        "GET",
        "https://api.example.com/users",
    )

Available delegation surface:

  • pull_token()
  • ensure_token()
  • has_permission()
  • has_any_permission()
  • has_all_permissions()
  • request()
  • request_json()
  • get_auth_header()
  • decode_token_claims()
  • properties: token, permissions, spiffe_id, is_expired, expires_in_seconds, client_id

request() returns a buffered DelegationHTTPResponse with:

  • status
  • headers
  • body
  • url
  • ok
  • text()
  • json()

Refresh behavior:

  • If the cached token is near expiry, ensure_token() re-pulls it automatically.
  • If a downstream request returns 401, the client refreshes once and retries once.

Error behavior:

  • DelegationTokenNotFound: no active delegation token for this client
  • DelegationTokenExpired: server reports expired delegation
  • DelegationError: network, parsing, or generic API failures

Agent Compatibility Note

The compatibility benchmark for trust delegation is the external example agent at /Users/pc/Downloads/generic_agent.py.

This package is compatible with that style of usage:

  • direct import from authsec_sdk.delegation_sdk
  • permission checks via has_permission()
  • token access via ensure_token()
  • identity inspection via decode_token_claims()

Important:

  • The SDK does not require any repo-local sys.path hack.
  • A normal pip install authsec-sdk is sufficient.
  • If your agent uses OpenAI, openai is an application dependency. It is not bundled with this SDK.

Other Surfaces

Hosted service access:

from authsec_sdk import ServiceAccessSDK

CIBA:

from authsec_sdk import CIBAClient

SPIFFE:

from authsec_sdk import QuickStartSVID, WorkloadAPIClient, WorkloadSVID

Environment Variables

MCP SDK runtime:

  • AUTHSEC_AUTH_SERVICE_URL
  • AUTHSEC_SERVICES_URL
  • AUTHSEC_TIMEOUT_SECONDS
  • AUTHSEC_RETRIES
  • AUTHSEC_TOOLS_LIST_TIMEOUT_SECONDS
  • AUTHSEC_OAUTH_TOOL_TIMEOUT_SECONDS

Typical trust delegation app config:

  • CLIENT_ID
  • USERFLOW_URL
  • BASE_API_URL

Troubleshooting

  • ModuleNotFoundError: No module named 'authsec_sdk'
    • Install the package into the same Python interpreter you use to run the app.
  • ModuleNotFoundError: No module named 'AuthSec_SDK'
    • Upgrade to a current release or switch to the canonical authsec_sdk import path.
  • DelegationTokenNotFound
    • No delegation exists yet for the agent client. An admin must delegate first.
  • DelegationTokenExpired
    • Pull a fresh delegated token or have an admin renew the delegation.
  • Downstream request fails after refresh
    • Inspect the downstream API, audience, and delegated permissions. The SDK retries only once after 401.

Publishing

cd /absolute/path/to/sdk-authsec/packages/python-sdk
python3 -m pip install --upgrade build twine
python3 -m build
python3 -m twine check dist/*
python3 -m twine upload dist/*

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

authsec_sdk-4.1.0.tar.gz (36.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

authsec_sdk-4.1.0-py3-none-any.whl (37.7 kB view details)

Uploaded Python 3

File details

Details for the file authsec_sdk-4.1.0.tar.gz.

File metadata

  • Download URL: authsec_sdk-4.1.0.tar.gz
  • Upload date:
  • Size: 36.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.1

File hashes

Hashes for authsec_sdk-4.1.0.tar.gz
Algorithm Hash digest
SHA256 271095b2a2fd9e2d0507b023776ca62f5ed548c57c5226d9ac9964e54a2eb1b2
MD5 a3984a8d4f62e2c42799c9e15c26eb80
BLAKE2b-256 942df1d5e04e03c6da94ee2fa68d33dd1dc438c3a03fa4e73825d6078950b463

See more details on using hashes here.

File details

Details for the file authsec_sdk-4.1.0-py3-none-any.whl.

File metadata

  • Download URL: authsec_sdk-4.1.0-py3-none-any.whl
  • Upload date:
  • Size: 37.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.1

File hashes

Hashes for authsec_sdk-4.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 66cfc5b17663a99eb07b3177bb9dc1c7f5a891ab31120129c34c4f7d613cdcb5
MD5 bc089a72126273803f95fd1af23e50a4
BLAKE2b-256 40085289f7f8f7095c6250245974e020cf9f67d3f56c70292c4ee79bf04742ff

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page