Azure Cosmos DB Integration
STDIOMCP Server for Azure Cosmos DB with NextJS frontend and AI Assistant integration.
MCP Server for Azure Cosmos DB with NextJS frontend and AI Assistant integration.
This repository contains a project that shows how to create an MCP Server and client for Azure Cosmos DB. The project is divided into 2 parts:
Azure Cosmos DB
In the Azure portal, create an Azure Cosmos DB for NoSQL account.
For this project, you will need to enable vector support on the Azure Cosmos DB account.
In the settings section, select Features, then "Vector Search for NoSQL API"
In the panel that opens, click on the Enable button
Create the Azure Cosmos DB eShop database and the Products container
Click on "..." next to eShop to display the contextual menu and select "New container" to create the "carts" container in the eShop database.
Make sure that the partition key is "/id" (the partition key is case-sensitive)
Expand "Container Vector Policy" and click on the "Add vector embedding" button
Storage account
For more details, refer to the documentation: https://learn.microsoft.com/en-us/azure/storage/common/storage-account-create?tabs=azure-portal
**Install software pre-requisites **
dotnet nuget add source https://api.nuget.org/v3/index.json -n nuget.org
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# install az cli winget install -e --id Microsoft.AzureCLI # install nuget and reference nuget source Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force # update to latest Powershell release (7.5 as of writing) winget install --id Microsoft.PowerShell --source winget # install az modules Install-Module -Name Az -Repository PSGallery -Force -AllowClobber
git clone https://github.com/patrice-truong/cosmosdb-mcp.git cd cosmosdb-mcp
cd cosmosdb-mcp/nextjs npm install --legacy-peer-deps
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>
az login az account show --query tenantId -o tsv
{ "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>" } }
Variable | Reference |
---|---|
Subscription Id | Cosmos DB > Overview > Subscription Id |
Azure Cosmos DB account name | cosmos-eastus2-nosql-2 |
Resource group name | Cosmos DB > Overview > Resource group name |
Principal Id | App registration Object Id |
$SubscriptionId = "<subscription-id>" # Azure subscription id $AccountName = "<cosmosdb-account-name>" # cosmos db account name $ResourceGroupName = "<resource-group-name>" # resource group name of the Cosmos DB account $PrincipalId = "<principal-id>" # object id of the app registered in Entra ID
cd webapi dotnet build
18. On your secondary region VM (Australia East), modify the .env file with the IP address of the socket server in your primary region (East US 2)
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
cd nextjs npm run build
In this section, we'll read the products catalog from the populate/catalog.json file and populate the Azure Cosmos DB for NoSQL database
{ "CosmosDb": { "Endpoint": "https://<cosmosdb_account_name>.documents.azure.com:443/", "TenantId": "<tenant_id>", "DatabaseName": "eshop", "ProductsContainerName": "products", "OrdersContainerName": "orders", } }
Demo initialization:
cd mcp-server npx ts-node src/server.ts
Demo steps: