Skip to main content
Glama

Microsoft MCP

new_client.py•7.19 kB
import asyncio import json from fastmcp import Client async def main(): print("šŸš€ Starting MCP client...") try: async with Client("http://0.0.0.0:8001/mcp/") as client: # List available tools print("šŸ“” Connecting to server...") tools = await client.list_tools() tool_count = len(tools) if isinstance(tools, list) else len(tools.tools) print(f"āœ… Connected! Found {tool_count} tools\n") # Check if user already has accounts print("šŸ” Checking for existing accounts...") try: accounts_result = await client.call_tool("list_accounts", {}) # Extract the content from CallToolResult if accounts_result.content and len(accounts_result.content) > 0: account_list_text = accounts_result.content[0].text account_list = json.loads(account_list_text) print(f"[DEBUG] Account list type: {type(account_list)}") print(f"[DEBUG] Account list: {account_list}") # Check if account_list is empty (could be a list or dict) if isinstance(account_list, list): has_accounts = len(account_list) > 0 else: has_accounts = bool(account_list) print(f"[DEBUG] has_accounts: {has_accounts}") else: has_accounts = False print("[DEBUG] No content in accounts_result") except Exception as e: print(f"āš ļø Error checking accounts: {e}") import traceback traceback.print_exc() has_accounts = False if not has_accounts: print("āŒ No accounts found. Starting authentication...\n") # Start authentication print("šŸ”„ Requesting device code from server...") auth_result = await client.call_tool("authenticate_account", {}) print(f"šŸ“¦ Raw auth result: {auth_result}") auth_data = json.loads(auth_result.content[0].text) print(f"šŸ“„ Parsed auth data: {auth_data}") print("\n" + "="*60) print("šŸ“± MICROSOFT AUTHENTICATION REQUIRED") print("="*60) print(f"\n🌐 Step 1: Open this URL in your browser:") print(f" {auth_data['verification_url']}") print(f"\nšŸ”‘ Step 2: Enter this code:") print(f" {auth_data['device_code']}") print(f"\nšŸ‘¤ Step 3: Sign in with your Microsoft account") print(f"\nā° This code expires in {auth_data['expires_in']} seconds") print("="*60) # Wait for user to authenticate input("\nšŸ‘‰ Press Enter after you've completed authentication in your browser...") # Complete authentication print("\nšŸ”„ Completing authentication...") complete_result = await client.call_tool( "complete_authentication", {"flow_cache": auth_data["_flow_cache"]} ) complete_data = json.loads(complete_result.content[0].text) if complete_data.get("status") == "success": print(f"āœ… Authentication successful!") print(f" Username: {complete_data['username']}") account_id = complete_data["account_id"] username = complete_data["username"] elif complete_data.get("status") == "pending": print("āš ļø Authentication still pending. Please complete it and try again.") return else: print(f"āŒ Authentication failed: {complete_data.get('message')}") return else: # Use existing account print(f"āœ… Found {len(account_list) if isinstance(account_list, list) else 1} existing account(s)") account_data = account_list[0] if isinstance(account_list, list) else account_list account_id = account_data["account_id"] username = account_data["username"] print(f" Using account: {username}\n") # Get emails print(f"šŸ“§ Retrieving emails for {username}...") emails_result = await client.call_tool("list_emails", { "account_id": account_id, "folder": "inbox", "limit": 5, "include_body": True }) # The result is in content[0].text as a JSON string emails_text = emails_result.content[0].text emails_data = json.loads(emails_text) # Check if it's wrapped in a structure or is the direct list if isinstance(emails_data, dict): # Might be wrapped - look for common keys if 'value' in emails_data: emails = emails_data['value'] elif '@odata.etag' in emails_data: # Single email object, wrap it in a list emails = [emails_data] else: # Unknown structure, try to find the list print(f"[DEBUG] Unexpected dict structure. Keys: {list(emails_data.keys())}") emails = [emails_data] elif isinstance(emails_data, list): emails = emails_data else: raise ValueError(f"Unexpected data type: {type(emails_data)}") print(f"\nšŸ“¬ Latest {len(emails)} emails:") for i, email in enumerate(emails, 1): print(f"\n {i}. Subject: {email.get('subject', 'No subject')}") if 'from' in email and 'emailAddress' in email['from']: print(f" From: {email['from']['emailAddress']['address']}") print(f" Date: {email.get('receivedDateTime', 'Unknown')}") print(f" Read: {'Yes' if email.get('isRead', False) else 'No'}") if email.get('hasAttachments'): print(f" šŸ“Ž Has attachments") # Show email body if available if 'body' in email and 'content' in email['body']: body_content = email['body']['content'] # Truncate long bodies if len(body_content) > 500: body_content = body_content[:500] + "..." print(f"\n šŸ“„ Body:\n {body_content}\n") print("\nāœ… Done!") except Exception as e: print(f"\nāŒ Error: {e}") import traceback traceback.print_exc() asyncio.run(main())

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/purva-kashyap/microsoft-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server