{"id":9939,"library":"microsoft-agents-authentication-msal","title":"Microsoft Agents MSAL Authentication","description":"microsoft-agents-authentication-msal is a Python library providing MSAL-based authentication specifically for Microsoft Agents. It primarily implements the device code flow using MSAL to acquire authentication tokens. Currently at version 0.9.0, it's part of the broader Microsoft Agents framework and follows its release cadence, focusing on integrating with Microsoft services.","status":"active","version":"0.9.0","language":"python","source_language":"en","source_url":"https://github.com/microsoft/Agents","tags":["microsoft","azure","msal","authentication","agents","device-code-flow"],"install":[{"cmd":"pip install microsoft-agents-authentication-msal","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Provides the IAuthentication interface that this library implements.","package":"microsoft-agents-authentication"},{"reason":"The core Microsoft Authentication Library (MSAL) that this package wraps.","package":"msal"}],"imports":[{"wrong":"from microsoft_agents.auth.msal import MSALAuthentication","symbol":"MSALAuthentication","correct":"from microsoft_agents.auth.msal import MSALAuthentication"}],"quickstart":{"code":"import os\nfrom agents.auth.msal import MSALAuthentication\nfrom agents.auth.types import IAuthentication\n\n# For demonstration, retrieve client_id from environment variable.\n# In a real application, you would configure this securely.\nCLIENT_ID = os.environ.get('MSAL_CLIENT_ID', 'YOUR_MSAL_CLIENT_ID_HERE')\nif CLIENT_ID == 'YOUR_MSAL_CLIENT_ID_HERE':\n    print(\"WARNING: Please set the MSAL_CLIENT_ID environment variable or replace 'YOUR_MSAL_CLIENT_ID_HERE' with your actual Azure AD application client ID.\")\n\ntry:\n    # Initialize MSAL authentication using Device Code Flow\n    # This will print a URL and a device code that the user needs to enter in a browser.\n    auth: IAuthentication = MSALAuthentication(client_id=CLIENT_ID)\n\n    print(f\"Attempting to get token with client_id: {CLIENT_ID}...\")\n    # Acquire a token for Microsoft Graph default scope\n    # The actual scope might vary depending on the Microsoft Agent's requirements.\n    # Common scopes include \"https://graph.microsoft.com/.default\" for broad Graph access.\n    # Other scopes like \"api://<your-app-id>/.default\" might be used for custom APIs.\n    token_response = auth.get_token(scope=[\"https://graph.microsoft.com/.default\"])\n\n    print(\"\\nAuthentication successful!\")\n    print(f\"Access Token (first 20 chars): {token_response.access_token[:20]}...\")\n    print(f\"Expires On: {token_response.expires_on}\")\n\nexcept ValueError as e:\n    print(f\"Error during authentication setup: {e}\")\n    if \"client_id\" in str(e):\n        print(\"Hint: Ensure MSAL_CLIENT_ID is correctly set and not empty.\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred during token acquisition: {e}\")\n    print(\"Please check your network connection, client_id, and ensure you completed the device code flow in the browser.\")","lang":"python","description":"Demonstrates basic authentication using the MSAL Device Code Flow. It initializes `MSALAuthentication` with a client ID and attempts to acquire an access token for the Microsoft Graph default scope. Note that this flow requires user interaction in a web browser."},"warnings":[{"fix":"Ensure your Python environment is version 3.10 or higher. Use `python --version` to check.","message":"This library requires Python 3.10 or newer. Installing with older Python versions will lead to dependency resolution errors or runtime issues.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always use `from agents.auth.msal import MSALAuthentication` as the correct import path, matching the internal module structure.","message":"The import path `from microsoft_agents_authentication_msal import MSALAuthentication` is incorrect and will result in a `ModuleNotFoundError`.","severity":"breaking","affected_versions":"All versions"},{"fix":"Be prepared for the application to print a URL and a code. The user must navigate to the URL, enter the code, and complete authentication in their browser. This flow is not suitable for fully automated, headless environments without specific MSAL configuration.","message":"The `MSALAuthentication` class primarily implements the Device Code Flow, which requires user interaction in a web browser.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Register an application in Azure Active Directory (AAD), grant it the necessary API permissions (e.g., Microsoft Graph), and use its assigned Client ID. Ensure the application type supports public client flows for device code.","message":"A valid Azure AD application Client ID is essential for successful authentication. Using a placeholder or an invalid ID will cause errors.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"search_vec":"'0.9.0':39 'acquir':33 'agent':2,7,22,47,64 'authent':4,8,18,34,63 'azur':61 'base':17 'broader':45 'cadenc':53 'code':28,67 'current':36 'devic':27,66 'device-code-flow':65 'flow':29,68 'focus':54 'follow':50 'framework':48 'implement':25 'integr':56 'librari':13 'microsoft':1,6,21,46,58,60 'microsoft-agents-authentication-ms':5 'msal':3,9,16,31,62 'msal-bas':15 'part':42 'primarili':24 'provid':14 'python':12 'releas':52 'servic':59 'specif':19 'token':35 'use':30 'version':38","created_at":"2026-04-17T01:21:33.009463+00:00","updated_at":"2026-04-17T01:21:33.009463+00:00","problems":{"verify_error":"Traceback (most recent call last):\n  File \"<string>\", line 1, in <module>\nModuleNotFoundError: No module named 'microsoft_agents.auth'"},"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.0.0","cli_name":"","cli_version":null,"type":"library","homepage":"https://learn.microsoft.com/en-us/azure/ai-services/agents/","github":"https://github.com/microsoft/Agents","docs":null,"changelog":null,"pypi":"https://pypi.org/project/microsoft-agents-authentication-msal/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["azure","auth-security"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"import_fail","verified_at":"2026-07-03","last_verified":"2026-07-03","next_check":"2026-07-10","install_tag":null}}