cosmosdb-data-access-toolkit
A comprehensive solution leveraging Azure Cosmos DB for NoSQL data persistence, offering robust product and order data access. It integrates an intelligent AI Agent to streamline user interaction and data retrieval, supported by a dedicated backend service that interfaces with a responsive frontend client for an optimized operational experience.
Author

patrice-truong
Quick Info
Actions
Tags
Azure Cosmos DB MCP Client and Server Infrastructure
This repository encapsulates a complete demonstration project showcasing the implementation of an MCP Server and companion client specifically tailored for interaction with Azure Cosmos DB.
The architecture is segmented into two primary operational units:
- Presentation Layer (Frontend): Built on NextJS 15, this component renders a dynamic product showcase and incorporates an advanced AI Assistant designed to facilitate product discovery and historical order lookups.
- Data Access Layer (MCP Server): This service component establishes a reliable connection to the Azure Cosmos DB NoSQL database instance, managing all read operations concerning product inventory and transactional order records.
Architectural Blueprint (Azure Infrastructure)
- Data Store: An Azure Cosmos DB instance configured for NoSQL API, housing the core product catalog information.
- Application Server: A Node.js-based server acting as the central hub for the MCP communication protocol.
Supplementary Resources
- Provisioning an Azure Cosmos DB for NoSQL Account
- Creating an Azure Storage Account Instance
- Deploying a Windows Virtual Machine in Azure
- Managing Windows Execution Policies
Setup and Deployment Guide
Initial Provisioning
Azure Cosmos DB Setup
Provision an Azure Cosmos DB for NoSQL account via the Azure portal.
-
Assign a globally unique identifier to the Cosmos DB instance (e.g.,
cosmos-eastus2-nosql-2for this guide). -
Proceed to the "Global distribution" tab (accept defaults).
-
Proceed to the "Networking" tab (accept defaults).
-
Navigate to "Backup Policy":
- Set policy to "Periodic".
- Select "Locally-redundant backup storage".
-
Proceed to the "Encryption" tab.
-
Initiate validation by clicking "Review and Create".
-
Finalize creation by clicking "Create".
Crucially, activate vector search capabilities on the newly created Cosmos DB instance: - Within the account settings, locate 'Features' and enable "Vector Search for NoSQL API". - In the corresponding configuration panel, select the 'Enable' option.
Database and Container Creation (eShop Schema):
- Establish the
eshopdatabase within the Cosmos DB instance. - Create the primary
productscontainer.
Ensure the partition key for the products container is explicitly set to /id (case sensitivity matters).
Expand the "Container Vector Policy" section and use the "Add vector embedding" feature to configure vector support.
- Provision the auxiliary
cartscontainer within theeshopdatabase.
Azure Storage Account Setup
- Establish a dedicated storage account to host product visual assets (images).
Refer to the official documentation for detailed storage account creation steps: https://learn.microsoft.com/en-us/azure/storage/common/storage-account-create?tabs=azure-portal
Prerequisite Software Installation
- Provision an Azure VM or utilize a local development environment.
- Install Node.js, specifically version v22.13.1 (LTS), obtainable from https://nodejs.org/en/download
- Install Visual Studio Code x64 v1.97.0 from https://code.visualstudio.com/download
- Install Git v2.47.12 x64 from https://git-scm.com/downloads
- Install .NET SDK x64 v9.0.102 from https://dotnet.microsoft.com/en-us/download/dotnet/thank-you/sdk-9.0.102-windows-x64-installer
- Open a terminal and register the NuGet source:
dotnet nuget add source https://api.nuget.org/v3/index.json -n nuget.org
- For Windows systems, elevate PowerShell execution policies if necessary. Open PowerShell with administrator privileges and execute:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
- Install required package managers and Azure CLI tools:
# Install Azure CLI
winget install -e --id Microsoft.AzureCLI
# Install NuGet package provider and register source
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
# Update to the latest stable PowerShell release (version 7.5 at this writing)
winget install --id Microsoft.PowerShell --source winget
# Install Azure PowerShell modules
Install-Module -Name Az -Repository PSGallery -Force -AllowClobber
- Clone the repository source code:
git clone https://github.com/patrice-truong/cosmosdb-mcp.git
cd cosmosdb-mcp
- Navigate into the frontend directory and resolve dependencies:
cd cosmosdb-mcp/nextjs
npm install --legacy-peer-deps
- Configure environment variables in the
nextjsdirectory by creating an.envfile populated with the following settings:
AZURE_COSMOSDB_NOSQL_ENDPOINT=https://<cosmosdb_account_name>.documents.azure.com:443/
AZURE_COSMOSDB_NOSQL_DATABASE=eshop
AZURE_COSMOSDB_NOSQL_PRODUCTS_CONTAINER=products
AZURE_COSMOSDB_NOSQL_CARTS_CONTAINER=carts
AZURE_COSMOSDB_NOSQL_ORDERS_CONTAINER=orders
AZURE_STORAGE_ACCOUNT_NAME=<storage_account_name>
AZURE_STORAGE_CONTAINER_NAME=<container_name>
- Obtain the Azure Tenant ID by authenticating via CLI:
az login
az account show --query tenantId -o tsv
- Update the
webapi/appsettings.jsonconfiguration file, substituting the placeholder<tenant_id>with the retrieved value:
{
"CosmosDb": {
"Endpoint": "https:/<cosmosdb_account_name>.documents.azure.com:443/",
"TenantId": "<tenant_id>",
"DatabaseName": "eshop",
"ProductsContainerName": "products",
"CartsContainerName": "carts",
"OrdersContainerName": "orders"
},
"AzureBlobStorage": {
"AccountName": "<storage_account_name>"
}
}
- Register an Application within the Azure Portal (Entra ID).
- Generate a client secret for the newly created application registration.
- Configure Role-Based Access Control (RBAC) to grant the application access to Cosmos DB. Retrieve the four required identifiers and update the
populate/set_rbac.ps1script accordingly.
| Identifier | Source Location in Azure Portal |
|---|---|
| Subscription Id | Cosmos DB > Overview > Subscription Id |
| Azure Cosmos DB account name | cosmos-eastus2-nosql-2 (Example) |
| Resource group name | Cosmos DB > Overview > Resource group name |
| Principal Id | App registration Object ID (Entra ID) |
PowerShell script update:
$SubscriptionId = "<subscription-id>" # Azure subscription identifier
$AccountName = "<cosmosdb-account-name>" # Cosmos DB account identifier
$ResourceGroupName = "<resource-group-name>" # Resource group housing the Cosmos DB account
$PrincipalId = "<principal-id>" # Object ID of the Entra ID application registration
-
In a PowerShell session, execute
Connect-AzAccountand then run the RBAC setup script:./set_rbac.ps1 -
Grant the application (or VM) permissions to interact with the storage account:
-
Navigate to the storage account in the Azure portal.
-
Select "Access Control (IAM)" from the navigation pane.
-
Click "Add role assignment".
-
Search for and select the role: "Storage Blob Data Contributor".
-
Navigate to the "Members" tab.
-
Locate and select the registered application's identity.
-
Confirm selection by clicking "Select", then finalize the assignment by clicking "Review and assign".
-
Provision the necessary container within the storage account and transfer the contents of the local
azure-storagedirectory into it. -
Compile the backend Web API project using the .NET CLI:
cd webapi
dotnet build
-
(If deploying to a secondary region VM, e.g., Australia East) Update the local
.envfile with the network socket address of the primary region's server (East US 2). -
Authentication Note: This project currently lacks integrated authentication mechanisms. The demonstration user email is hardcoded in
/nextjs/models/constants.ts. Adjust this value as needed for your testing scenarios. -
In both the
mcp-serverandnextjsdirectories, duplicate.env.templateto.envand populate all required parameters:
AZURE_COSMOSDB_NOSQL_ENDPOINT=https://<cosmosdb_account>.documents.azure.com:443/
AZURE_COSMOSDB_NOSQL_DATABASE=eshop
AZURE_COSMOSDB_NOSQL_PRODUCTS_CONTAINER=products
AZURE_COSMOSDB_NOSQL_CARTS_CONTAINER=carts
AZURE_COSMOSDB_NOSQL_ORDERS_CONTAINER=orders
NEXT_PUBLIC_AZURE_TENANT_ID=<tenant_id>
NEXT_PUBLIC_AZURE_CLIENT_ID=<client_id>
NEXT_PUBLIC_AZURE_CLIENT_SECRET=<client_secret>
NEXT_PUBLIC_AZURE_STORAGE_ACCOUNT_NAME=<storage_account_name>
NEXT_PUBLIC_AZURE_STORAGE_CONTAINER_NAME=img
AZURE_OPENAI_ENDPOINT=https://<azure_openai_account>.openai.azure.com/
AZURE_OPENAI_API_KEY=<azure_openai_key>
AZURE_OPENAI_EMBEDDING_MODEL=text-embedding-3-small
AZURE_OPENAI_API_VERSION=2024-05-01-preview
- Build the NextJS frontend application assets:
cd nextjs
npm run build
Populating the Product Inventory
This section details the process for ingesting product data from the local populate/catalog.json file into the Azure Cosmos DB NoSQL database.
- Update
appsettings.jsonwith your specific Cosmos DB account endpoint information:
{
"CosmosDb": {
"Endpoint": "https://<cosmosdb_account_name>.documents.azure.com:443/",
"TenantId": "<tenant_id>",
"DatabaseName": "eshop",
"ProductsContainerName": "products",
"OrdersContainerName": "orders",
}
}
-
Execute
az loginin a terminal, navigate to thepopulatedirectory, and run the data seeding application viadotnet run. -
Validate successful population by inspecting the contents of the respective Azure Cosmos DB container.
Demonstration Execution Sequence
Initialization Steps:
- Initiate the MCP server process from your workstation:
cd mcp-server
npx ts-node src/server.ts
-
Launch the client-side application (store front):
-
Navigate to the NextJS project directory (
cd nextjs) -
Execute the start command:
npm start -
Optionally, for protocol inspection, open a separate console and execute the MCP inspector utility: npx -y @modelcontextprotocol/inspector
Interactive Scenario Walkthrough:
- Access the application interface via a web browser at http://localhost:3002.
- Engage the AI Assistant (top-right icon) and input: "I'm interested in backpacks" (Observe the product list dynamically filter to show relevant backpacks).
- Engage the AI Assistant again and input: "Get my orders" (Observe the data display update to show past order history).
