- MCP Graph API Integration
MCP Graph API Integration
MCP Graph API Integration
A Model Context Protocol (MCP) implementation that integrates with Microsoft Graph API to provide AI-assisted access to Azure Active Directory/Entra ID resources.
Overview
This solution demonstrates how to build a Model Context Protocol (MCP) server that connects to Microsoft Graph API, allowing an AI assistant to query and interact with Azure Active Directory/Entra ID resources. The solution includes:
- MCP.Server: A .NET Web API that implements the MCP protocol and connects to Microsoft Graph API
- MCP.Client: A Blazor WebAssembly client for interacting with the MCP server
- MCP.Shared: Shared models and contracts between the server and client
The solution enables users to chat with an AI assistant about their Azure AD resources, particularly application registrations, using natural language.
Architecture
┌─────────────┐ ┌────────────┐ ┌──────────────┐ ┌───────────────┐
│ MCP.Client │─────► MCP.Server │─────► Azure OpenAI │ │ Microsoft │
│ (Blazor) │ │ (Web API) │ │ Service │ │ Graph API │
└─────────────┘ └────────────┘ └──────────────┘ └───────────────┘
│ ▲
└────────────────────────────────────────┘
Features
- AI-powered chat interface for querying Azure AD resources
- MCP implementation that provides function calling capabilities to the AI model
- Integration with Microsoft Graph API to access Azure AD data
- Authentication and permission handling for secure access
- Testing tools to verify Graph API connectivity and permissions
Prerequisites
- .NET 9.0 SDK or newer
- Azure subscription with:
- Azure OpenAI Service instance
- Application registration in Azure AD with appropriate permissions
- Visual Studio 2022 or Visual Studio Code
Configuration
Server Configuration
The MCP Server requires configuration in appsettings.json:
-
Azure OpenAI Configuration:
"AzureOpenAI": { "Endpoint": "https://your-endpoint.openai.azure.com/", "DeploymentName": "your-deployment-name", "ApiKey": "your-api-key" } -
Azure AD Configuration:
"AzureAd": { "Instance": "https://login.microsoftonline.com/", "TenantId": "your-tenant-id", "ClientId": "your-client-id", "ClientSecret": "your-client-secret", "Scopes": ["User.Read", "Application.Read.All"] }
Required Azure AD Permissions
The application requires the following Microsoft Graph permissions:
Application.Read.All
To grant these permissions:
- Go to Azure Portal > App registrations > [Your App]
- Select API permissions
- Add Microsoft Graph > Application permissions > Application.Read.All
- Click "Grant admin consent"
Getting Started
Building and Running the Solution
- Clone the repository
- Configure the
appsettings.jsonfiles with your Azure OpenAI and Azure AD credentials - Open the solution in Visual Studio or VS Code
- Build the solution:
dotnet build - Run the server:
cd MCP.Server dotnet run - Run the client:
cd MCP.Client dotnet run
Testing the Application
- Navigate to the client application (typically https://localhost:5001)
- Click "Test Connection" to verify connectivity to the MCP server
- Click "Test Graph API" to verify Graph API connectivity and permissions
- Begin chatting with the AI assistant, asking about your Azure AD resources
If you encounter permission issues, refer to the GraphApiPermissionFix.md document.
Troubleshooting
Graph API Permissions
If the AI reports it doesn't have permissions to access certain resources, run the included PowerShell script to test your permissions:
.\TestGraphPermissions.ps1
This script will help diagnose permission issues and provide guidance on how to fix them.
Common Issues
- API Key or Secret Expired: Ensure your Azure OpenAI API keys and Azure AD client secrets are valid
- Missing Permissions: Confirm that admin consent has been granted for all required permissions
- CORS Issues: If experiencing client connection problems, check CORS settings in the server's Program.cs
Development
Adding New Graph API Capabilities
To extend the application with additional Graph API capabilities:
- Add new function declarations in
McpGraphService.cs - Implement the corresponding Graph API calls
- Register the functions in the
InitializeFunctionsmethod
MCP Protocol
This project implements the Model Context Protocol, which allows:
- Structured communication between client and AI services
- Function calling capabilities for the AI model
- Context management across conversation turns