#
tokens: 2516/50000 7/7 files
lines: off (toggle) GitHub
raw markdown copy
# Directory Structure

```
├── .gitignore
├── assets
│   ├── Flow.png
│   └── mcp server.gif
├── Dockerfile
├── LICENSE
├── package.json
├── README.md
├── smithery.yaml
├── src
│   └── index.ts
└── tsconfig.json
```

# Files

--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------

```
# Dependencies
node_modules/
package-lock.json
yarn.lock

# Build outputs
build/
dist/
*.tsbuildinfo

# Environment variables
.env
.env.local

# IDE specific files
.vscode/
.idea/
*.swp
*.swo

# OS specific files
.DS_Store
Thumbs.db

# Logs
logs/
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Coverage directory used by tools like istanbul
coverage/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

```

--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------

```markdown
# Demo
![MCP Server Demo](./assets/mcp%20server.gif)

# Flow
![Request Flow](./assets/Flow.png)


# Azure Resource Graph MCP Server

This is a Model Context Protocol (MCP) server that provides access to Azure Resource Graph queries. It allows you to retrieve information about Azure resources across your subscriptions using Resource Graph queries.
<a href="https://glama.ai/mcp/servers/@hardik-id/azure-resource-graph-mcp-server">
  <img width="380" height="200" src="https://glama.ai/mcp/servers/@hardik-id/azure-resource-graph-mcp-server/badge" />
</a>

[![MseeP.ai Security Assessment Badge](https://mseep.net/pr/hardik-id-azure-resource-graph-mcp-server-badge.png)](https://mseep.ai/app/hardik-id-azure-resource-graph-mcp-server)

## Features

- Query Azure resources using Resource Graph queries
- Default query returns resource ID, name, type, and location
- Supports custom Resource Graph queries
- Uses Azure DefaultAzureCredential for authentication

## Prerequisites

- Node.js installed
- Azure subscription
- Azure CLI installed and logged in, or other Azure credentials configured

## Running the MCP Server

You can run the MCP server using either Cursor IDE or Visual Studio Code.

### Option 1: Cursor IDE Integration

To integrate the MCP server with Cursor IDE:

1. Clone this repository to your local machine (e.g., `C:\YOUR_WORKSPACE\azure-resource-graph-mcp-server`)
2. Build the project:
```bash 
npm install
npm run build
```
3. Open Cursor Settings (JSON) and add the following configuration:
```json
{
  "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"
      },
    }
  }
}
```
> **Note**: Make sure to update the path to match your local repository location.

4. Restart Cursor IDE to apply the changes

### Option 2: VS Code Integration

To integrate the MCP server with Visual Studio Code:

1. Clone this repository to your local machine
2. Build the project:
```bash 
npm install
npm run build
```
3. Open VS Code Settings (JSON) by pressing `Ctrl+Shift+P`, type "Settings (JSON)" and select "Preferences: Open User Settings (JSON)"
4. Add the following configuration:
```json
{
    "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"
                },
            }
        }
    }
}
```
> **Note**: Make sure to update the path to match your local repository location.

5. Save the settings.json file
6. Restart VS Code to apply the changes

The MCP server will now be available to use within VS Code with cursor integration.

## Usage

The server provides the following tool:

### query-resources

Retrieves resources and their details from Azure Resource Graph.

Parameters:
- `subscriptionId` (optional): Azure subscription ID (defaults to configured ID)
- `query` (optional): Custom Resource Graph query (defaults to "Resources | project id, name, type, location")

## Environment Setup

1. First, make sure you're logged in to Azure CLI by running:
   ```bash
   az login
   ```
   This step is crucial for local development as the DefaultAzureCredential will automatically use your Azure CLI credentials.

2. Set up your environment variables:
   - Copy `.env.example` to `.env`
   - Update `AZURE_SUBSCRIPTION_ID` in `.env` with your actual subscription ID
   - Other variables (`AZURE_TENANT_ID`, `AZURE_CLIENT_ID`, `AZURE_CLIENT_SECRET`) are optional when using Azure CLI authentication

3. Make sure you have proper Azure credentials configured. The server uses DefaultAzureCredential which supports:
   - Azure CLI
   - Managed Identity
   - Visual Studio Code credentials
   - Environment variables

4. If using environment variables, set up:
   - AZURE_SUBSCRIPTION_ID
   - AZURE_TENANT_ID
   - AZURE_CLIENT_ID
   - AZURE_CLIENT_SECRET

## Error Handling

The server includes robust error handling for:
- Azure client initialization failures
- Query execution errors
- Invalid queries or parameters

## Development

To work on this project:

1. Make changes in the `src` directory
2. Build using `npm run build`
3. Test your changes by running the server

## License
This project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for details.


```

--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------

```dockerfile
FROM node:18-alpine

# Copy package files
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy application code
COPY . .

# Build the application
RUN npm run build

# Command will be provided by smithery.yaml
CMD ["node", "build/index.js"]

```

--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------

```json
{
    "compilerOptions": {
      "target": "ES2022",
      "module": "Node16",
      "moduleResolution": "Node16",
      "outDir": "./build",
      "rootDir": "./src",
      "strict": true,
      "esModuleInterop": true,
      "skipLibCheck": true,
      "forceConsistentCasingInFileNames": true
    },
    "include": ["src/**/*"],
    "exclude": ["node_modules"]
  }

```

--------------------------------------------------------------------------------
/smithery.yaml:
--------------------------------------------------------------------------------

```yaml
# Smithery.ai configuration
startCommand:
  type: stdio
  configSchema:
    # JSON Schema defining the configuration options for the MCP.
    {}
  commandFunction:
    # A function that produces the CLI command to start the MCP on stdio.
    |-
    (config) => ({
      "command": "node",
      "args": [
        "build/index.js"
      ],
      "env": {
        "SUBSCRIPTION_ID": "xxxxxx-xx-xx-xx-xxxxxx"
      }
    })

```

--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------

```json
{
  "type": "module",
  "bin": {
    "azure-resource-graph-mcp-server": "./build/index.js"
  },
  "files": [
    "build",
    ".env"
  ],
  "name": "azure-resource-graph-mcp-server",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "build": "tsc && chmod 755 build/index.js",
    "start": "node build/index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "@azure/arm-resourcegraph": "^4.2.1",
    "@azure/arm-resources": "^6.0.0",
    "@azure/identity": "^4.8.0",
    "@modelcontextprotocol/sdk": "^1.8.0",
    "dotenv": "^16.4.7",
    "zod": "^3.24.2"
  },
  "devDependencies": {
    "@types/node": "^22.14.0",
    "typescript": "^5.8.2"
  }
}

```

--------------------------------------------------------------------------------
/src/index.ts:
--------------------------------------------------------------------------------

```typescript
import {
  McpServer,
} from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
import { ResourceGraphClient } from "@azure/arm-resourcegraph";
import { DefaultAzureCredential } from "@azure/identity";
import { config } from "dotenv";

// Load environment variables
config();

const subscriptionId = process.env.SUBSCRIPTION_ID || "";
console.error("Using subscription ID:", subscriptionId);

// Create an MCP server
const server = new McpServer({
  name: "AzureResourceGraph",
  version: "1.0.0",
});

// Initialize Azure Resource Graph client
let rgClient: ResourceGraphClient;

try {
  // Create Azure clients using DefaultAzureCredential
  const credential = new DefaultAzureCredential();
  rgClient = new ResourceGraphClient(credential);
  console.error("Azure Resource Graph client initialized successfully");
} catch (err) {
  console.error("Failed to initialize Azure Resource Graph client:", err);
  process.exit(1);
}


// Add a tool to query resources
server.tool(
  "query-resources",
  "Retrieves resources and their details from Azure Resource Graph. Use this tool to search, filter, and analyze Azure resources across subscriptions. It supports Kusto Query Language (KQL) for complex queries to find resources by type, location, tags, or properties. Useful for infrastructure auditing, resource inventory, compliance checking, and understanding your Azure environment's current state.",
  {
    subscriptionId: z
      .string()
      .describe("Azure subscription ID")
      .default(process.env.SUBSCRIPTION_ID || ""),
    query: z
      .string()
      .optional()
      .describe("Resource Graph query, defaults to listing all resources"),
  },
  async ({ subscriptionId, query }) => {
    try {
      const defaultQuery = "Resources | project id, name, type, location";
      const queryToUse = query || defaultQuery;

      const resources = await rgClient.resources({
        subscriptions: [subscriptionId],
        query: queryToUse,
      });

      return {
        content: [
          {
            type: "text",
            text: JSON.stringify(resources, null, 2),
          },
        ],
      };
    } catch (err) {
      return {
        content: [
          {
            type: "text",
            text: `Error querying resources: ${
              err instanceof Error ? err.message : String(err)
            }`,
          },
        ],
        isError: true,
      };
    }
  }
);

async function main() {
  const transport = new StdioServerTransport();
  await server.connect(transport);
  console.error("Azure Resource Graph MCP Server running on stdio");
}

main().catch((error) => {
  console.error("Fatal error in main():", error);
  process.exit(1);
});

```