complete_authentication
Validate user authentication using a device code to access Microsoft Graph API for managing Outlook, Calendar, OneDrive, and Contacts.
Instructions
Complete the authentication process after the user has entered the device code
Args:
flow_cache: The flow data returned from authenticate_account (the _flow_cache field)
Returns:
Account information if authentication was successful
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| flow_cache | Yes |
Implementation Reference
- src/microsoft_mcp/tools.py:65-130 (handler)The complete_authentication tool handler: completes Microsoft account authentication using device flow after user input. Decorated with @mcp.tool for automatic registration with the FastMCP server.@mcp.tool def complete_authentication(flow_cache: str) -> dict[str, str]: """Complete the authentication process after the user has entered the device code Args: flow_cache: The flow data returned from authenticate_account (the _flow_cache field) Returns: Account information if authentication was successful """ import ast try: flow = ast.literal_eval(flow_cache) except (ValueError, SyntaxError): raise ValueError("Invalid flow cache data") app = auth.get_app() result = app.acquire_token_by_device_flow(flow) if "error" in result: error_msg = result.get("error_description", result["error"]) if "authorization_pending" in error_msg: return { "status": "pending", "message": "Authentication is still pending. The user needs to complete the authentication process.", "instructions": "Please ensure you've visited the URL and entered the code, then try again.", } raise Exception(f"Authentication failed: {error_msg}") # Save the token cache cache = app.token_cache if isinstance(cache, auth.msal.SerializableTokenCache) and cache.has_state_changed: auth._write_cache(cache.serialize()) # Get the newly added account accounts = app.get_accounts() if accounts: # Find the account that matches the token we just got for account in accounts: if ( account.get("username", "").lower() == result.get("id_token_claims", {}) .get("preferred_username", "") .lower() ): return { "status": "success", "username": account["username"], "account_id": account["home_account_id"], "message": f"Successfully authenticated {account['username']}", } # If exact match not found, return the last account account = accounts[-1] return { "status": "success", "username": account["username"], "account_id": account["home_account_id"], "message": f"Successfully authenticated {account['username']}", } return { "status": "error", "message": "Authentication succeeded but no account was found", }
- src/microsoft_mcp/tools.py:65-65 (registration)The @mcp.tool decorator registers the complete_authentication function as an MCP tool.@mcp.tool