$npx -y skills add mongodb/agent-skills --skill mongodb-query-optimizerHelp with MongoDB query optimization and indexing. Use only when the user asks for optimization or performance: "How do I optimize this query?", "How do I index this?", "Why is this query slow?", "Can you fix my slow queries?", "What are the slow queries on my cluster?", etc. Do
| 1 | # MongoDB Query Optimizer |
| 2 | |
| 3 | ## When this skill is invoked |
| 4 | |
| 5 | Invoke **only** when the user wants: |
| 6 | |
| 7 | - Query/index **optimization** or **performance** help |
| 8 | - **Why** a query is slow or **how to speed it up** |
| 9 | - **Slow queries** on their cluster and/or **how to optimize them** |
| 10 | |
| 11 | Do **not** invoke for routine query authoring unless the user has requested help with optimization, slow queries, or indexing. |
| 12 | |
| 13 | ## High Level Workflow |
| 14 | |
| 15 | ### General Performance Help |
| 16 | |
| 17 | If the user wants to examine slow queries, or is looking for general performance suggestions (not regarding any particular query): |
| 18 | |
| 19 | - Use MongoDB MCP server **atlas-get-performance-advisor** tool to fetch slow query logs and performance advisor output |
| 20 | - Make suggestions based on this information |
| 21 | |
| 22 | If Atlas MCP Server for Atlas is not configured or you don’t have enough information to run **atlas-get-performance-advisor** against the correct cluster, tell the user that general performance analysis requires Atlas MCP Server configuration with API credentials, and suggest they configure it or ask about a specific query instead. |
| 23 | |
| 24 | ### Help with a Specific Query |
| 25 | |
| 26 | If the user is asking about a particular query: |
| 27 | |
| 28 | - Use **collection-indexes**, **explain**, and **find** MCP tools to get existing indexes on the collection, explain() output for the query, and a sample document from the collection |
| 29 | - Use **atlas-get-performance-advisor MCP** tool to fetch slow query logs and performance advisor output |
| 30 | |
| 31 | Then make an optimization suggestion based on collected information and MongoDB best practices and examples from reference files. Prefer creating an index that fully covers the query if possible. If you cannot use MongoDB MCP Server then still try to make a suggestion. |
| 32 | |
| 33 | ## MCP: available tools |
| 34 | |
| 35 | **How to invoke.** Call the **MongoDB MCP server** with the **exact tool name** as `toolName` and a single **arguments object** as `arguments`. Do not pass the tool name as an option, query param, or nested key; pass it as the MCP tool name and the parameters as the arguments object. Full MCP Server tool reference: [MongoDB MCP Server Tools](https://www.mongodb.com/docs/mcp-server/tools/). |
| 36 | |
| 37 | **Database tools** (when the MCP cluster connection works): |
| 38 | |
| 39 | | Tool name (exact) | Arguments object | |
| 40 | | :---- | :---- | |
| 41 | | `collection-indexes` | `{ "database": "<db>", "collection": "<coll>" }` — both required strings. | |
| 42 | | `explain` | `{ "database": "<db>", "collection": "<coll>", "method": [ { "name": "find", "arguments": { "filter": {...}, "sort": {...}, "limit": N } } ], "verbosity": "executionStats" }`. `method` is an array of one object: `name` is `"find"`, `"aggregate"`, or `"count"`; `arguments` holds that method's params (e.g. find: `filter`, `sort`, `limit`; aggregate: `pipeline`; count: `query`). Optional `verbosity`: `"queryPlanner"` (default), `"executionStats"`, `"queryPlannerExtended"`, `"allPlansExecution"`. | |
| 43 | | `find` | `{ "database": "<db>", "collection": "<coll>", "filter": {...}, "projection": {...}, "sort": {...}, "limit": N }` — `database`, `collection`, and `filter` are required. Optional: `projection`, `sort`, `limit`. | |
| 44 | |
| 45 | **Atlas tools** (when Atlas API credentials are configured): |
| 46 | |
| 47 | | Tool name (exact) | Arguments object | |
| 48 | | :---- | :---- | |
| 49 | | `atlas-list-projects` | `{}` or `{ "orgId": "<24-char hex>" }`. Returns projects with their IDs; use to get `projectId` for Performance Advisor. | |
| 50 | | `atlas-get-performance-advisor` | **Required:** `"projectId"` (24-character hex string), `"clusterName"` (string, 1–64 chars, alphanumeric/underscore/dash). **Optional:** `"operations"` — array of strings from `"suggestedIndexes"`, `"dropIndexSuggestions"`, `"slowQueryLogs"`, `"schemaSuggestions"` (request only what you need); for slowQueryLogs only: `"since"` (ISO 8601 date-time), `"namespaces"` (array of `"db.coll"` strings). | |
| 51 | |
| 52 | For a user question, try to fetch information from both the connection string and Atlas API related to the query you are optimizing. |
| 53 | |
| 54 | ### 1\. DB connection string works for MongoDB MCP |
| 55 | |
| 56 | Typical flow: call `collection-indexes` → `explain` → `find` (sample doc). |
| 57 | |
| 58 | - **`collection-indexes`** — Use the result's `cla |