Martian-Engineering/microsoft-mcp-server
If you are the rightful owner of microsoft-mcp-server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
An MCP server that integrates Microsoft Graph API with OAuth authentication, acting as a bridge between MCP hosts/clients and Microsoft APIs using a MITM OAuth pattern.
Microsoft MCP Server
An MCP (Model Context Protocol) server that provides Microsoft Graph API integration with OAuth authentication. This server acts as a bridge between MCP hosts/clients and Microsoft APIs using a MITM (Man-in-the-Middle) OAuth pattern.
Features
- Microsoft Calendar Integration: List calendars, view events, create/update/delete events with full Microsoft 365 calendar support
- OAuth Authentication: Secure authentication flow bridging MCP hosts/clients with Azure AD OAuth
- Dynamic Client Registration: Supports MCP OAuth client registration
- MSAL Integration: Uses Microsoft Authentication Library for robust Azure AD authentication
- Automatic Token Refresh: Maintains access using Microsoft refresh tokens
- Comprehensive Error Handling: Maps Microsoft Graph errors to meaningful responses
Prerequisites
- Python 3.11+
- Azure AD account with app registration capabilities
- ngrok account (free tier is sufficient)
Quick Start
Step 1: Azure AD App Setup
- Go to Azure Portal
- Navigate to Azure Active Directory ā App registrations
- Click "New registration"
- Configure your app:
- Name:
MCP Microsoft Server
(or your preference) - Supported account types: Choose based on your needs
- Redirect URI: Will be configured after ngrok setup
- Name:
- After creation, note down:
- Application (client) ID
- Directory (tenant) ID
- Create a client secret:
- Go to Certificates & secrets
- New client secret
- Save the secret value immediately (shown only once)
- Grant API permissions:
- Go to API permissions
- Add permission ā Microsoft Graph ā Delegated permissions
- Select:
User.Read
,Calendars.ReadWrite
- Grant admin consent (if required)
Step 2: Start ngrok tunnel
Set up and start ngrok to expose your local server:
# Install ngrok if needed
# macOS: brew install ngrok
# Linux: See https://ngrok.com/download
# Configure ngrok (first time only)
ngrok config add-authtoken YOUR-AUTH-TOKEN
# Start tunnel
ngrok http 8000
Note the HTTPS URL (e.g., https://abc123.ngrok-free.app
)
Step 3: Configure Azure AD Redirect URI
- Return to your Azure AD app registration
- Go to Authentication ā Add a platform ā Web
- Add redirect URI:
https://YOUR-NGROK-URL.ngrok-free.app/microsoft/callback
- Save changes
Step 4: Launch the server
./launch-server.py
The script will:
- Prompt for your Azure AD credentials if not configured
- Set up Python virtual environment
- Install dependencies
- Start the MCP server
Claude Desktop Configuration
Add the server to Claude Desktop:
- Open Claude Desktop ā Settings ā Developer
- Add new MCP server with your ngrok URL:
- For SSE transport:
https://YOUR-NGROK-URL.ngrok-free.app/sse
- For streamable-http:
https://YOUR-NGROK-URL.ngrok-free.app
- For SSE transport:
- Complete the OAuth flow when prompted
- Calendar tools will now be available!
Available Tools
Microsoft Calendar
list_calendars
- List all accessible Microsoft 365 calendarslist_events
- List events from a calendar with optional time range and filtersget_event
- Get details of a specific eventcreate_event
- Create a new calendar event with attendees, location, remindersdelete_event
- Delete a calendar event
Coming Soon
- Mail/Outlook: Send emails, manage inbox, folders
- Teams: Send messages, manage teams and channels
- OneDrive/SharePoint: File operations, document management
Environment Variables
Required:
MCP_MICROSOFT_CLIENT_ID
- Azure AD application (client) IDMCP_MICROSOFT_CLIENT_SECRET
- Client secret valueMCP_MICROSOFT_TENANT_ID
- Directory (tenant) ID (use "common" for multi-tenant)
Optional:
MCP_MICROSOFT_SERVER_URL_OVERRIDE
- Override server URL (automatically set with ngrok)MCP_SERVER_TRANSPORT
- Transport type:sse
orstreamable-http
(default)MCP_SERVER_PORT
- Server port (default: 8000)MCP_SERVER_HOST
- Server host (default: localhost)
How It Works
OAuth Flow (MITM Pattern)
This server implements a Man-in-the-Middle OAuth pattern to bridge MCP hosts/clients with Microsoft's OAuth:
MCP Host/Client ā Our Server ā Azure AD ā User
- Discovery: MCP host discovers OAuth endpoints via
/.well-known/oauth-authorization-server
- Registration: MCP host registers as a client via Dynamic Client Registration
- Authorization:
- MCP host requests authorization
- Our server redirects to Microsoft login with our app credentials
- User authenticates with Microsoft
- Azure AD redirects back to our server
- Our server redirects to MCP host with an MCP authorization code
- Token Exchange: MCP host exchanges the code for MCP tokens
- API Calls: MCP tokens are internally mapped to Microsoft access tokens for Graph API access
Architecture
- Authentication: MSAL (Microsoft Authentication Library) for Azure AD OAuth
- API Client: msgraph-sdk for Microsoft Graph API calls
- Framework: FastMCP with MCP 1.0+ protocol support
- Transport: Streamable-HTTP (default) or SSE
Troubleshooting
Common Issues
-
"Id is malformed" errors:
- This can occur intermittently with get_event/delete_event
- Usually related to session state or caching
- Try creating a new event and immediately operating on it
-
403 Forbidden Errors:
- Verify all required permissions are granted in Azure AD
- Check that admin consent is granted if required
- Ensure the user has access to the requested resources
-
OAuth Redirect Mismatch:
- Ensure the redirect URI in Azure AD matches exactly
- Include
/microsoft/callback
at the end - Use HTTPS (via ngrok) not HTTP
-
Connection Issues:
- Verify ngrok is running and accessible
- Check firewall/proxy settings
- Ensure environment variables are set correctly
Logging
The server uses structured logging to stdout. To see detailed logs:
# Run with debug logging
MCP_LOG_LEVEL=DEBUG ./launch-server.py
Development
Project Structure
microsoft_mcp/
āāā __init__.py
āāā server.py # FastMCP server setup and entry point
āāā settings.py # Configuration via environment variables
āāā auth/
ā āāā __init__.py
ā āāā provider.py # MITM OAuth provider implementation
āāā tools/
ā āāā calendar/ # Calendar tool implementations
ā āāā create_event.py
ā āāā delete_event.py
ā āāā get_event.py
ā āāā list_calendars.py
ā āāā list_events.py
ā āāā utils.py # Graph API wrapper utilities
āāā utils/
āāā errors.py # Error definitions
āāā error_handlers.py
āāā rate_limiter.py
Adding New Tools
To add new Microsoft Graph services:
- Create a new directory under
tools/
(e.g.,mail/
) - Implement individual tool files with
register_<tool_name>_tool()
functions - Import and register tools in
server.py
- Add required Graph API permissions to the scope list in
settings.py
- Update Azure AD app permissions accordingly
Manual Setup (Advanced)
If you need to run the server manually:
# Create virtual environment
python3 -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies
pip install -e .
# Set environment variables
export MCP_MICROSOFT_CLIENT_ID="your-client-id"
export MCP_MICROSOFT_CLIENT_SECRET="your-client-secret"
export MCP_MICROSOFT_TENANT_ID="your-tenant-id"
export MCP_MICROSOFT_SERVER_URL_OVERRIDE="https://YOUR-NGROK-URL.ngrok-free.app"
# Run server
python -m microsoft_mcp.server
Security Considerations
- OAuth tokens are stored in memory only (cleared on restart)
- Microsoft refresh tokens are used to maintain access
- All API calls use secure HTTPS connections
- Client secrets should never be committed to version control
- Consider using Azure Key Vault for production deployments
Known Limitations
- Only calendar tools are currently implemented
- Tokens are stored in memory (lost on server restart)
- Auto-registration currently hardcoded for Claude AI clients
- Some event operations may have intermittent encoding issues
Contributing
This project is in active development. Contributions are welcome for:
- Implementing mail, teams, and file tools
- Improving error handling and retry logic
- Adding comprehensive test coverage
- Enhancing the OAuth flow security
License
[License information to be added]