az

azure-resource-graph-mcp-server

A Model Context Protocol server for querying and analyzing Azure resources at scale using Azure Resource Graph, enabling AI assistants to explore and monitor Azure infrastructure.

Publisherazure-resource-graph-mcp-server
Submitted date4/13/2025

Unleashing the Power of LLMs with Azure Resource Graph: A Deep Dive into the Model Context Protocol Server

MCP Server Demo

Request Flow

This document details the implementation and usage of an Azure Resource Graph Model Context Protocol (MCP) server. This server acts as a bridge, enabling Large Language Model (LLM) applications to seamlessly access and leverage the wealth of information contained within Azure Resource Graph. By providing a standardized interface, this server empowers developers to build intelligent, context-aware applications that can understand and interact with their Azure infrastructure.

This server is designed to be easily integrated into various development environments, including Cursor IDE and Visual Studio Code, allowing developers to quickly prototype and deploy LLM-powered solutions.

Core Capabilities

  • Azure Resource Querying: Execute powerful queries against Azure Resource Graph to retrieve detailed information about your Azure resources.
  • Customizable Queries: Tailor your queries to extract specific data points, enabling highly focused and relevant context for your LLM applications. The server supports both a default query (returning resource ID, name, type, and location) and custom queries defined by the user.
  • Seamless Authentication: Leverages Azure's DefaultAzureCredential for streamlined authentication, supporting various methods including Azure CLI, Managed Identity, and environment variables. This ensures secure and efficient access to your Azure resources.

Prerequisites: Setting the Stage for Success

Before diving into the implementation, ensure you have the following prerequisites in place:

  • Node.js: A JavaScript runtime environment is essential for running the server.
  • Azure Subscription: An active Azure subscription is required to access Azure resources.
  • Azure CLI: The Azure Command-Line Interface provides a convenient way to manage Azure resources and authenticate with your subscription.

Deployment Strategies: Integrating with Your Development Environment

This MCP server can be seamlessly integrated into your preferred development environment. Below are detailed instructions for both Cursor IDE and Visual Studio Code.

Option 1: Cursor IDE Integration - A Step-by-Step Guide

  1. Clone the Repository: Begin by cloning the repository to your local machine. For example: C:\YOUR_WORKSPACE\azure-resource-graph-mcp-server.

  2. Build the Project: Navigate to the cloned directory in your terminal and execute the following commands:

    npm install npm run build

    These commands will install the necessary dependencies and build the project.

  3. Configure Cursor Settings: Open Cursor's settings (JSON) file and add the following configuration block:

    { "mcpServers": { "azure-resource-graph-mcp-server": { "command": "node", "args": [ "C:\\YOUR_WORKSPACE\\azure-resource-graph-mcp-server\\build\\index.js" ], "env": { "SUBSCRIPTION_ID": "xxxxxx-xx-xx-xx-xxxxxx" }, } } }

    Important: Replace "C:\\YOUR_WORKSPACE\\azure-resource-graph-mcp-server" with the actual path to your local repository. Also, replace "xxxxxx-xx-xx-xx-xxxxxx" with your Azure Subscription ID.

  4. Restart Cursor: Restart Cursor IDE to apply the changes and activate the MCP server integration.

Option 2: VS Code Integration - A Detailed Walkthrough

  1. Clone the Repository: Clone the repository to your local machine.

  2. Build the Project: Navigate to the cloned directory in your terminal and execute the following commands:

    npm install npm run build

    These commands will install the necessary dependencies and build the project.

  3. Open VS Code Settings (JSON): Press Ctrl+Shift+P to open the command palette, type "Settings (JSON)", and select "Preferences: Open User Settings (JSON)".

  4. Add Configuration: Add the following configuration block to your settings.json file:

    { "mcp": { "servers": { "azure-resource-graph": { "type": "stdio", "command": "node", "args": [ "C:\\YOUR_WORKSPACE\\azure-resource-graph-mcp-server\\build\\index.js" ], "env": { "SUBSCRIPTION_ID": "xxxxxx-xx-xx-xx-xxxxxx" }, } } } }

    Important: Replace "C:\\YOUR_WORKSPACE\\azure-resource-graph-mcp-server" with the actual path to your local repository. Also, replace "xxxxxx-xx-xx-xx-xxxxxx" with your Azure Subscription ID.

  5. Save and Restart: Save the settings.json file and restart VS Code to apply the changes.

With these steps completed, the MCP server will be readily available for use within VS Code, enhancing your development workflow with powerful LLM integration.

Usage: Harnessing the Power of query-resources

The server exposes a single, powerful tool: query-resources. This tool allows you to retrieve detailed information about your Azure resources from Azure Resource Graph.

Parameters:

  • subscriptionId (optional): Specifies the Azure subscription ID to query. If not provided, the server will use the configured SUBSCRIPTION_ID environment variable.
  • query (optional): Allows you to define a custom Resource Graph query. If not provided, the server will use the default query: "Resources | project id, name, type, location".

Environment Configuration: Setting the Stage for Authentication

Proper environment configuration is crucial for seamless authentication and access to your Azure resources. Follow these steps to ensure your environment is correctly set up:

  1. Azure CLI Login: Ensure you are logged in to the Azure CLI by running the following command:

    az login

    This step is essential for local development, as DefaultAzureCredential will automatically leverage your Azure CLI credentials.

  2. Environment Variables:

    • Create a copy of the .env.example file and rename it to .env.
    • Update the AZURE_SUBSCRIPTION_ID variable in the .env file with your actual Azure subscription ID.
    • The AZURE_TENANT_ID, AZURE_CLIENT_ID, and AZURE_CLIENT_SECRET variables are optional when using Azure CLI authentication.
  3. Azure Credentials: The server utilizes DefaultAzureCredential, which supports various authentication methods:

    • Azure CLI
    • Managed Identity
    • Visual Studio Code credentials
    • Environment variables
  4. Environment Variable Setup (if applicable): If you choose to use environment variables for authentication, ensure the following variables are properly configured:

    • AZURE_SUBSCRIPTION_ID
    • AZURE_TENANT_ID
    • AZURE_CLIENT_ID
    • AZURE_CLIENT_SECRET

Robust Error Handling: Ensuring Stability and Reliability

The server incorporates comprehensive error handling mechanisms to ensure stability and reliability. These mechanisms cover:

  • Azure Client Initialization Failures: Handles errors that may occur during the initialization of the Azure client.
  • Query Execution Errors: Catches and manages errors that may arise during the execution of Resource Graph queries.
  • Invalid Queries or Parameters: Detects and handles invalid queries or parameters, providing informative error messages to the user.

Development Workflow: Contributing to the Project

If you wish to contribute to the project, follow these steps:

  1. Make Changes: Implement your desired changes within the src directory.
  2. Build the Project: Execute the npm run build command to build the project.
  3. Test Your Changes: Thoroughly test your changes by running the server and verifying the functionality.

By following these guidelines, you can effectively contribute to the ongoing development and improvement of this valuable tool.

Visit More

View All