Azure Cosmos DB MCP Client
May 6, 2026 ยท View on GitHub
This client demonstrates how to use Microsoft Foundry agents with the Cosmos DB MCP Toolkit.
Setup
-
Create a
.envfile from the example:cp .env.example .env -
Update the
.envfile with your values fromdeployment-info.json:PROJECT_ENDPOINT=<YOUR-AI-FOUNDRY-PROJECT-ENDPOINT> MODEL_DEPLOYMENT_NAME=<YOUR-MODEL-DEPLOYMENT-NAME> CONNECTION_NAME=<YOUR-MCP-CONNECTION-NAME> MCP_SERVER_URL=https://<YOUR-CONTAINER-APP-URL>/mcp MCP_SERVER_LABEL=cosmosdbGet these values from your
deployment-info.jsonfile or Azure portal. -
Install dependencies:
pip install -r requirements.txt -
Make sure you have the MCP connection configured in Microsoft Foundry:
- Connection Name:
<YOUR-MCP-CONNECTION-NAME> - Target URL:
https://<YOUR-CONTAINER-APP-URL>/mcp - Audience:
<YOUR-ENTRA-APP-CLIENT-ID> - Authentication: Project Managed Identity
- Connection Name:
Run
python agents_cosmosdb_mcp.py
What it does
The script creates an AI agent that can:
- List databases in your Cosmos DB account
- List containers in a database
- Get recent documents
- Search for documents
- Perform vector search
- Get container schemas
Input Validation
The MCP server now enforces strict server-side validation for tool inputs.
- Tool calls must match the declared
inputSchemaexactly. - Unknown fields are rejected.
- Required fields, types, and numeric bounds are enforced.
- Free-form string inputs are length-limited and normalized by the server.
For this sample client, tool invocation is still delegated to the Foundry agent runtime rather than manually constructing tools/call payloads. That means no client-side protocol changes are required, but invalid tool arguments may now fail fast instead of being loosely accepted.
Sample Questions
Edit the input_text array in the script to test different questions:
input_text = [
"Can you list all the databases in my Cosmos DB account?",
"Show me the containers in the first database",
"What does the schema look like for the first container?",
"Get me the 5 most recent documents from the first container",
"Search for documents containing 'test' in the name property",
]
Change content=input_text[0] to test different questions (e.g., input_text[1], input_text[2], etc.).
Troubleshooting
If you see "network error":
- Check container app logs:
az containerapp logs show --name <YOUR-CONTAINER-APP-NAME> --resource-group <YOUR-RESOURCE-GROUP> --tail 50 - Verify the MCP connection in Microsoft Foundry has the correct audience
- Make sure the agent has access to the connection
If authentication fails:
- Verify the Entra App Client ID matches your
deployment-info.json - Check role assignments are in place (run
Setup-AIFoundry-RoleAssignment.ps1if needed) - Ensure the container app has the correct environment variables
If a tool call fails with Invalid params:
- Check that the tool arguments match the schema returned by
tools/list. - Remove any unexpected properties from the request.
- Make sure string values are not empty or overly long.
- Make sure numeric values stay within the documented ranges.