### Development Commands Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Run tests, build the project, and start development mode using npm. ```bash npm test # Run tests npm run build # Build project npm start # Development mode ``` -------------------------------- ### Local Development: Clone and Build Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Clones the repository, installs dependencies, and builds the project for local development. ```bash git clone https://github.com/hendrickcastro/MCPCosmosDB.git cd MCPCosmosDB npm install && npm run build ``` -------------------------------- ### Container Analysis: List, Get Definition, and Stats Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Demonstrates listing containers, retrieving container definition, and getting container statistics. Uses connection_id to specify the connection; if not specified, the default connection is used. sample_size controls the number of documents sampled for stats. ```typescript // List all containers (uses default connection if connection_id not specified) const containers = await mcp_list_containers({ connection_id: "production" }); // Get container definition const containerDef = await mcp_get_container_definition({ connection_id: "production", container_id: "users" }); // Get container statistics const stats = await mcp_get_container_stats({ connection_id: "production", container_id: "users", sample_size: 1000 }); ``` -------------------------------- ### Project Structure Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Directory layout showing tool implementations, database connection, server setup, and tool definitions. ```plaintext src/ ├── tools/ # Tool implementations │ ├── containerAnalysis.ts # Container operations │ ├── dataOperations.ts # Data queries & CRUD │ └── types.ts # Type definitions ├── db.ts # CosmosDB connection & multi-connection management ├── server.ts # MCP server setup └── tools.ts # Tool definitions ``` -------------------------------- ### Document Operations: Get by ID and Analyze Schema Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Shows how to retrieve a specific document by ID and partition key, and how to analyze the schema of a container with a sample size. ```typescript // Get specific document by ID const document = await mcp_get_document_by_id({ connection_id: "production", container_id: "users", document_id: "user-123", partition_key: "user-123" }); // Analyze schema const schema = await mcp_analyze_schema({ connection_id: "production", container_id: "products", sample_size: 500 }); ``` -------------------------------- ### Example cosmos-connections.json Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Sample JSON configuration file listing two connections (athlete and events) with their connection strings, database IDs, and modification settings. Both have allowModifications set to false, meaning write operations are disabled by default. ```json [ { "id": "athlete", "connectionString": "AccountEndpoint=https://dbsqlcosmosathlete.documents.azure.com:443/;AccountKey=...;", "databaseId": "data", "allowModifications": false, "description": "Athlete data" }, { "id": "events", "connectionString": "AccountEndpoint=https://dbsqlcosmosevents.documents.azure.com:443/;AccountKey=...;", "databaseId": "events", "allowModifications": false, "description": "Events data" } ] ``` -------------------------------- ### Multi-Connection with External File: MCP client configuration Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Configures the MCP client to use the external connections file via the COSMOS_CONNECTIONS_FILE environment variable. This is the recommended option for multi-connection setups. ```json { "mcpServers": { "cosmosdb": { "command": "npx", "args": ["-y", "mcpcosmosdb@latest"], "env": { "COSMOS_CONNECTIONS_FILE": "/path/to/cosmos-connections.json" } } } } ``` -------------------------------- ### NPX from GitHub Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Runs the MCP server directly from the GitHub repository using npx. Requires a connection string and database ID. ```json { "mcpServers": { "cosmosdb": { "command": "npx", "args": ["-y", "hendrickcastro/MCPCosmosDB"], "env": { "COSMOS_CONNECTION_STRING": "AccountEndpoint=https://...;AccountKey=...;", "COSMOS_DATABASE_ID": "your-database-name" } } } } ``` -------------------------------- ### Local Development: MCP Client Configuration Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Configures the MCP client to run the locally built server using Node.js. Points to the built server file and uses the external connections file. ```json { "mcpServers": { "cosmosdb": { "command": "node", "args": ["path/to/MCPCosmosDB/dist/server.js"], "env": { "COSMOS_CONNECTIONS_FILE": "/path/to/cosmos-connections.json" } } } } ``` -------------------------------- ### Multi-Connection Usage: List and Query Connections Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Shows how to list all available connections and query data from specific connections using connection_id. The list returns connection objects with id, databaseId, and isConnected fields. Queries use TOP and specific fields to avoid timeouts. ```typescript // List all available connections const connections = await mcp_list_connections(); // Returns: { connections: [{id: "prod", databaseId: "ProdDB", isConnected: true}, ...] } // Query specific connection using connection_id const prodData = await mcp_cosmos_query({ connection_id: "production", container_id: "users", query: "SELECT TOP 10 c.id, c.name FROM c ORDER BY c._ts DESC" }); const devData = await mcp_cosmos_query({ connection_id: "development", container_id: "users", query: "SELECT TOP 10 c.id, c.name FROM c ORDER BY c._ts DESC" }); ``` -------------------------------- ### CRUD Operations: Create, Update, Upsert, Delete Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Demonstrates CRUD operations that require allowModifications: true. Shows creating, updating (full replacement), upserting, and deleting documents with partition keys. ```typescript // Create a new document const created = await mcp_create_document({ connection_id: "development", // Use a connection with write access container_id: "users", document: { id: "user-456", email: "user@example.com", name: "John Doe", status: "active" }, partition_key: "user-456" }); // Update a document (full replacement) const updated = await mcp_update_document({ connection_id: "development", container_id: "users", document_id: "user-456", document: { id: "user-456", email: "newemail@example.com", name: "John Doe", status: "inactive" }, partition_key: "user-456" }); // Upsert a document (create or update) const upserted = await mcp_upsert_document({ connection_id: "development", container_id: "users", document: { id: "user-789", email: "another@example.com", name: "Jane Doe" }, partition_key: "user-789" }); // Delete a document const deleted = await mcp_delete_document({ connection_id: "development", container_id: "users", document_id: "user-456", partition_key: "user-456" }); ``` -------------------------------- ### Multi-Connection with External File: connections file (cosmos-connections.json) Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Defines multiple CosmosDB connections with IDs, connection strings, database IDs, modification flags, and descriptions. Used with the COSMOS_CONNECTIONS_FILE environment variable. ```json [ { "id": "production", "connectionString": "AccountEndpoint=https://myapp-prod.documents.azure.com:443/;AccountKey=...;", "databaseId": "ProductionDB", "allowModifications": false, "description": "Production database (read-only)" }, { "id": "development", "connectionString": "AccountEndpoint=https://myapp-dev.documents.azure.com:443/;AccountKey=...;", "databaseId": "DevDB", "allowModifications": true, "description": "Development database" }, { "id": "analytics", "connectionString": "AccountEndpoint=https://analytics.documents.azure.com:443/;AccountKey=...;", "databaseId": "AnalyticsDB", "allowModifications": false, "description": "Analytics database" } ] ``` -------------------------------- ### Querying Data: Correct and Incorrect Patterns Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Shows correct querying with TOP and specific fields, and warns against SELECT * which causes timeouts and high RU consumption. Also demonstrates using filter_conditions, order_by, and limit for document retrieval. ```typescript // ✅ CORRECT: Using TOP and specific fields const result = await mcp_cosmos_query({ connection_id: "production", container_id: "products", query: "SELECT TOP 50 c.id, c.name, c.price FROM c WHERE c.category = @category", parameters: { category: "electronics" } }); // ❌ WRONG: SELECT * without TOP (will timeout on large containers) // query: "SELECT * FROM c WHERE c.category = @category" // Get documents with simple filters const documents = await mcp_get_documents({ connection_id: "production", container_id: "orders", filter_conditions: { status: "completed" }, order_by: "_ts", order_direction: "DESC", limit: 100 }); ``` -------------------------------- ### mcp_list_containers Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Lists all containers in a specified connection. Uses the default connection if connection_id is not provided. ```APIDOC ## mcp_list_containers ### Description Lists all containers in a specified connection. Uses the default connection if connection_id is not provided. ### Method Function call ### Endpoint mcp_list_containers(params) ### Parameters #### Request Body - **connection_id** (string) - Optional - The connection identifier to use. If not provided, uses the default connection. ### Request Example ```json { "connection_id": "production" } ``` ### Response #### Success Response Returns a list of containers. #### Response Example ```json { "containers": [ { "id": "users", "databaseId": "ProdDB" } ] } ``` ``` -------------------------------- ### Multi-Connection with Inline JSON Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Configures the MCP client with an inline JSON array of connections via the COSMOS_CONNECTIONS environment variable. Useful when you cannot use an external file. ```json { "mcpServers": { "cosmosdb": { "command": "npx", "args": ["-y", "mcpcosmosdb@latest"], "env": { "COSMOS_CONNECTIONS": "[{\"id\":\"prod\",\"connectionString\":\"AccountEndpoint=https://...\",\"databaseId\":\"ProdDB\",\"allowModifications\":false},{\"id\":\"dev\",\"connectionString\":\"AccountEndpoint=https://...\",\"databaseId\":\"DevDB\",\"allowModifications\":true}]" } } } } ``` -------------------------------- ### mcp_list_connections Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Lists all available connections to Azure Cosmos DB. Returns an array of connection objects with id, databaseId, and connection status. ```APIDOC ## mcp_list_connections ### Description Lists all available connections to Azure Cosmos DB. Returns an array of connection objects with id, databaseId, and connection status. ### Method Function call ### Endpoint mcp_list_connections() ### Parameters None ### Response #### Success Response - **connections** (array) - List of connection objects - **id** (string) - Connection identifier - **databaseId** (string) - Database identifier - **isConnected** (boolean) - Whether the connection is active #### Response Example ```json { "connections": [ { "id": "prod", "databaseId": "ProdDB", "isConnected": true } ] } ``` ``` -------------------------------- ### mcp_create_document Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Creates a new document in a container. Requires the connection to have write access (allowModifications: true). ```APIDOC ## mcp_create_document ### Description Creates a new document in a container. Requires the connection to have write access (allowModifications: true). ### Method Function call ### Endpoint mcp_create_document(params) ### Parameters #### Request Body - **connection_id** (string) - Required - The connection identifier to use. - **container_id** (string) - Required - The container identifier. - **document** (object) - Required - The document to create. - **partition_key** (string) - Required - The partition key value. ### Request Example ```json { "connection_id": "development", "container_id": "users", "document": { "id": "user-456", "email": "user@example.com", "name": "John Doe", "status": "active" }, "partition_key": "user-456" } ``` ### Response #### Success Response Returns the created document. #### Response Example ```json { "id": "user-456", "email": "user@example.com", "name": "John Doe", "status": "active" } ``` ``` -------------------------------- ### mcp_get_container_definition Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Retrieves the definition of a specific container, including its schema and settings. ```APIDOC ## mcp_get_container_definition ### Description Retrieves the definition of a specific container, including its schema and settings. ### Method Function call ### Endpoint mcp_get_container_definition(params) ### Parameters #### Request Body - **connection_id** (string) - Required - The connection identifier to use. - **container_id** (string) - Required - The container identifier. ### Request Example ```json { "connection_id": "production", "container_id": "users" } ``` ### Response #### Success Response Returns the container definition. #### Response Example ```json { "id": "users", "partitionKey": { "paths": ["/id"], "kind": "Hash" } } ``` ``` -------------------------------- ### Single Connection (Legacy) - With Write Operations Enabled Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Enables write operations by setting DB_ALLOW_MODIFICATIONS to "true". Use only when write operations are required. ```json { "mcpServers": { "cosmosdb": { "command": "npx", "args": ["-y", "mcpcosmosdb@latest"], "env": { "COSMOS_CONNECTION_STRING": "AccountEndpoint=https://your-cosmos-account.documents.azure.com:443/;AccountKey=your-account-key-here;", "COSMOS_DATABASE_ID": "your-database-name", "DB_ALLOW_MODIFICATIONS": "true" } } } } ``` -------------------------------- ### Single Connection (Legacy) - Read-Only Mode Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Configures a single CosmosDB connection using COSMOS_CONNECTION_STRING and COSMOS_DATABASE_ID. Write operations are disabled by default for safety. ```json { "mcpServers": { "cosmosdb": { "command": "npx", "args": ["-y", "mcpcosmosdb@latest"], "env": { "COSMOS_CONNECTION_STRING": "AccountEndpoint=https://your-cosmos-account.documents.azure.com:443/;AccountKey=your-account-key-here;", "COSMOS_DATABASE_ID": "your-database-name" } } } } ``` -------------------------------- ### mcp_get_document_by_id Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Retrieves a single document by its ID and partition key. ```APIDOC ## mcp_get_document_by_id ### Description Retrieves a single document by its ID and partition key. ### Method Function call ### Endpoint mcp_get_document_by_id(params) ### Parameters #### Request Body - **connection_id** (string) - Required - The connection identifier to use. - **container_id** (string) - Required - The container identifier. - **document_id** (string) - Required - The document ID. - **partition_key** (string) - Required - The partition key value. ### Request Example ```json { "connection_id": "production", "container_id": "users", "document_id": "user-123", "partition_key": "user-123" } ``` ### Response #### Success Response Returns the document. #### Response Example ```json { "id": "user-123", "name": "John Doe" } ``` ``` -------------------------------- ### mcp_cosmos_query Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Executes a SQL query against a specified container in Azure Cosmos DB. Supports query parameters and requires the use of TOP N and specific fields to avoid timeouts. ```APIDOC ## mcp_cosmos_query ### Description Executes a SQL query against a specified container in Azure Cosmos DB. Supports query parameters and requires the use of TOP N and specific fields to avoid timeouts. ### Method Function call ### Endpoint mcp_cosmos_query(params) ### Parameters #### Request Body - **connection_id** (string) - Required - The connection identifier to use. - **container_id** (string) - Required - The container identifier to query. - **query** (string) - Required - The SQL query string. Must include TOP N and specific fields. - **parameters** (object) - Optional - Query parameters as key-value pairs. ### Request Example ```json { "connection_id": "production", "container_id": "products", "query": "SELECT TOP 50 c.id, c.name, c.price FROM c WHERE c.category = @category", "parameters": { "category": "electronics" } } ``` ### Response #### Success Response Returns the query result set. #### Response Example ```json { "documents": [ { "id": "product-1", "name": "Laptop", "price": 999.99 } ] } ``` ``` -------------------------------- ### mcp_get_documents Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Retrieves documents from a container with optional filters, sorting, and limit. Supports simple filter conditions. ```APIDOC ## mcp_get_documents ### Description Retrieves documents from a container with optional filters, sorting, and limit. Supports simple filter conditions. ### Method Function call ### Endpoint mcp_get_documents(params) ### Parameters #### Request Body - **connection_id** (string) - Required - The connection identifier to use. - **container_id** (string) - Required - The container identifier. - **filter_conditions** (object) - Optional - Key-value pairs for filtering documents. - **order_by** (string) - Optional - Field to order results by. - **order_direction** (string) - Optional - Sort direction: 'ASC' or 'DESC'. - **limit** (number) - Optional - Maximum number of documents to return. ### Request Example ```json { "connection_id": "production", "container_id": "orders", "filter_conditions": { "status": "completed" }, "order_by": "_ts", "order_direction": "DESC", "limit": 100 } ``` ### Response #### Success Response Returns a list of documents. #### Response Example ```json { "documents": [ { "id": "order-1", "status": "completed" } ] } ``` ``` -------------------------------- ### mcp_get_container_stats Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Retrieves statistics for a specific container, such as document count and size. Allows specifying a sample size for analysis. ```APIDOC ## mcp_get_container_stats ### Description Retrieves statistics for a specific container, such as document count and size. Allows specifying a sample size for analysis. ### Method Function call ### Endpoint mcp_get_container_stats(params) ### Parameters #### Request Body - **connection_id** (string) - Required - The connection identifier to use. - **container_id** (string) - Required - The container identifier. - **sample_size** (number) - Optional - The number of documents to sample for statistics. ### Request Example ```json { "connection_id": "production", "container_id": "users", "sample_size": 1000 } ``` ### Response #### Success Response Returns container statistics. #### Response Example ```json { "documentCount": 10000, "sizeBytes": 1048576 } ``` ``` -------------------------------- ### mcp_analyze_schema Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Analyzes the schema of documents in a container, returning a summary of fields and types. Allows specifying a sample size. ```APIDOC ## mcp_analyze_schema ### Description Analyzes the schema of documents in a container, returning a summary of fields and types. Allows specifying a sample size. ### Method Function call ### Endpoint mcp_analyze_schema(params) ### Parameters #### Request Body - **connection_id** (string) - Required - The connection identifier to use. - **container_id** (string) - Required - The container identifier. - **sample_size** (number) - Optional - The number of documents to sample for schema analysis. ### Request Example ```json { "connection_id": "production", "container_id": "products", "sample_size": 500 } ``` ### Response #### Success Response Returns schema analysis. #### Response Example ```json { "fields": { "id": "string", "name": "string", "price": "number" } } ``` ``` -------------------------------- ### mcp_delete_document Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Deletes a document by ID and partition key. Requires write access. ```APIDOC ## mcp_delete_document ### Description Deletes a document by ID and partition key. Requires write access. ### Method Function call ### Endpoint mcp_delete_document(params) ### Parameters #### Request Body - **connection_id** (string) - Required - The connection identifier to use. - **container_id** (string) - Required - The container identifier. - **document_id** (string) - Required - The ID of the document to delete. - **partition_key** (string) - Required - The partition key value. ### Request Example ```json { "connection_id": "development", "container_id": "users", "document_id": "user-456", "partition_key": "user-456" } ``` ### Response #### Success Response Returns a success message or the deleted document. #### Response Example ```json { "deleted": true } ``` ``` -------------------------------- ### mcp_update_document Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Updates an existing document by replacing it entirely. Requires write access. ```APIDOC ## mcp_update_document ### Description Updates an existing document by replacing it entirely. Requires write access. ### Method Function call ### Endpoint mcp_update_document(params) ### Parameters #### Request Body - **connection_id** (string) - Required - The connection identifier to use. - **container_id** (string) - Required - The container identifier. - **document_id** (string) - Required - The ID of the document to update. - **document** (object) - Required - The new document content (full replacement). - **partition_key** (string) - Required - The partition key value. ### Request Example ```json { "connection_id": "development", "container_id": "users", "document_id": "user-456", "document": { "id": "user-456", "email": "newemail@example.com", "name": "John Doe", "status": "inactive" }, "partition_key": "user-456" } ``` ### Response #### Success Response Returns the updated document. #### Response Example ```json { "id": "user-456", "email": "newemail@example.com", "name": "John Doe", "status": "inactive" } ``` ``` -------------------------------- ### ConnectionConfig TypeScript Interface Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Defines the structure for a CosmosDB connection configuration. The allowModifications property is optional and defaults to false, enabling write operations only when explicitly set to true. ```typescript interface ConnectionConfig { id: string; // Unique identifier for the connection connectionString: string; // CosmosDB connection string databaseId: string; // Database ID to connect to allowModifications?: boolean; // Enable write operations (default: false) description?: string; // Optional description } ``` -------------------------------- ### mcp_upsert_document Source: https://github.com/hendrickcastro/mcpcosmosdb/blob/master/README.md Creates a new document or updates an existing one if the ID already exists. Requires write access. ```APIDOC ## mcp_upsert_document ### Description Creates a new document or updates an existing one if the ID already exists. Requires write access. ### Method Function call ### Endpoint mcp_upsert_document(params) ### Parameters #### Request Body - **connection_id** (string) - Required - The connection identifier to use. - **container_id** (string) - Required - The container identifier. - **document** (object) - Required - The document to upsert. - **partition_key** (string) - Required - The partition key value. ### Request Example ```json { "connection_id": "development", "container_id": "users", "document": { "id": "user-789", "email": "another@example.com", "name": "Jane Doe" }, "partition_key": "user-789" } ``` ### Response #### Success Response Returns the upserted document. #### Response Example ```json { "id": "user-789", "email": "another@example.com", "name": "Jane Doe" } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.