|
| 1 | +# MongoDB |
| 2 | + |
| 3 | +import ToolInfo from "@/components/ToolInfo"; |
| 4 | +import Badges from "@/components/Badges"; |
| 5 | +import TabbedCodeBlock from "@/components/TabbedCodeBlock"; |
| 6 | +import TableOfContents from "@/components/TableOfContents"; |
| 7 | +import ToolFooter from "@/components/ToolFooter"; |
| 8 | + |
| 9 | +<ToolInfo |
| 10 | + description="Enable agents to interact with MongoDB databases (read only)." |
| 11 | + author="Arcade" |
| 12 | + codeLink="https://github.com/ArcadeAI/arcade-ai/tree/main/toolkits/mongodb" |
| 13 | + authType="database connection string" |
| 14 | + versions={["0.1.0"]} |
| 15 | +/> |
| 16 | + |
| 17 | +<Badges repo="arcadeai/arcade-mongodb" /> |
| 18 | + |
| 19 | +The Arcade MongoDB toolkit provides a pre-built set of tools for interacting with MongoDB databases in a read-only manner. This toolkit enables agents to discover databases and collections, explore document structures, and execute queries safely. This toolkit is a companion to the blog post [Designing SQL Tools for AI Agents](https://blog.arcade.dev/sql-tools-ai-agents-security). |
| 20 | + |
| 21 | +<Note> |
| 22 | +This toolkit is meant to be an example of how to build a toolkit for a database, and is not intended to be used in production - you won't find it listed in the Arcade dashboard or APIs. For production use, we recommend forking this repository and building your own toolkit with use-case specific tools. |
| 23 | +</Note> |
| 24 | + |
| 25 | +## Key Features |
| 26 | + |
| 27 | +This toolkit demonstrates several important concepts for LLM-powered database interactions: |
| 28 | + |
| 29 | +* **Database Discovery**: Automatically discover available databases in the MongoDB instance |
| 30 | +* **Collection Exploration**: Find all collections within a specific database |
| 31 | +* **Schema Inference**: Sample documents to infer schema structure and data types |
| 32 | +* **Safe Query Execution**: Execute find queries with built-in safety measures |
| 33 | +* **Aggregation Support**: Run complex aggregation pipelines for data analysis |
| 34 | +* **Document Counting**: Count documents matching specific criteria |
| 35 | +* **Connection Pooling**: Reuse database connections efficiently |
| 36 | +* **Read-Only Access**: Enforce read-only access to prevent data modification |
| 37 | +* **Result Limits**: Automatically limit query results to prevent overwhelming responses |
| 38 | + |
| 39 | +## Available Tools |
| 40 | + |
| 41 | +<TableOfContents |
| 42 | + headers={["Tool Name", "Description"]} |
| 43 | + data={ |
| 44 | + [ |
| 45 | + ['MongoDB.DiscoverDatabases', "Discover all databases in the MongoDB instance."], |
| 46 | + ['MongoDB.DiscoverCollections', "Discover all collections in a specific database."], |
| 47 | + ['MongoDB.GetCollectionSchema', "Get the schema structure of a collection by sampling documents."], |
| 48 | + ['MongoDB.FindDocuments', "Find documents in a collection with filtering, projection, and sorting."], |
| 49 | + ['MongoDB.CountDocuments', "Count documents matching a specific filter."], |
| 50 | + ['MongoDB.AggregateDocuments', "Execute aggregation pipelines for complex data analysis."], |
| 51 | + ] |
| 52 | + } |
| 53 | +/> |
| 54 | + |
| 55 | +Note that all tools require the `MONGODB_CONNECTION_STRING` secret to be set. |
| 56 | + |
| 57 | +## MongoDB.DiscoverDatabases |
| 58 | + |
| 59 | +Discover all databases in the MongoDB instance. This tool returns a list of all available databases, excluding system databases like `admin`, `config`, and `local` for security. |
| 60 | + |
| 61 | +<TabbedCodeBlock |
| 62 | + tabs={[ |
| 63 | + { |
| 64 | + label: "Call the Tool", |
| 65 | + content: { |
| 66 | + Python: [ |
| 67 | + "/examples/integrations/toolkits/mongodb/discover_databases_example_call_tool.py", |
| 68 | + ], |
| 69 | + JavaScript: ["/examples/integrations/toolkits/mongodb/discover_databases_example_call_tool.js"], |
| 70 | + }, |
| 71 | + } |
| 72 | + ]} |
| 73 | +/> |
| 74 | + |
| 75 | +## MongoDB.DiscoverCollections |
| 76 | + |
| 77 | +Discover all collections in a specific database. This tool should be used before any other tool that requires a collection name. |
| 78 | + |
| 79 | +**Parameters:** |
| 80 | +- `database_name` (str): The database name to discover collections in |
| 81 | + |
| 82 | +<TabbedCodeBlock |
| 83 | + tabs={[ |
| 84 | + { |
| 85 | + label: "Call the Tool", |
| 86 | + content: { |
| 87 | + Python: [ |
| 88 | + "/examples/integrations/toolkits/mongodb/discover_collections_example_call_tool.py", |
| 89 | + ], |
| 90 | + JavaScript: ["/examples/integrations/toolkits/mongodb/discover_collections_example_call_tool.js"], |
| 91 | + }, |
| 92 | + } |
| 93 | + ]} |
| 94 | +/> |
| 95 | + |
| 96 | +## MongoDB.GetCollectionSchema |
| 97 | + |
| 98 | +Get the schema structure of a collection by sampling documents. Since MongoDB is schema-less, this tool samples a configurable number of documents to infer the schema structure and data types. Always use this tool before executing any query. |
| 99 | + |
| 100 | +**Parameters:** |
| 101 | +- `database_name` (str): The database name containing the collection |
| 102 | +- `collection_name` (str): The name of the collection to inspect |
| 103 | +- `sample_size` (int): The number of documents to sample for schema discovery (default: 100) |
| 104 | + |
| 105 | +<TabbedCodeBlock |
| 106 | + tabs={[ |
| 107 | + { |
| 108 | + label: "Call the Tool", |
| 109 | + content: { |
| 110 | + Python: [ |
| 111 | + "/examples/integrations/toolkits/mongodb/get_collection_schema_example_call_tool.py", |
| 112 | + ], |
| 113 | + JavaScript: ["/examples/integrations/toolkits/mongodb/get_collection_schema_example_call_tool.js"], |
| 114 | + }, |
| 115 | + } |
| 116 | + ]} |
| 117 | +/> |
| 118 | + |
| 119 | +## MongoDB.FindDocuments |
| 120 | + |
| 121 | +Find documents in a collection with filtering, projection, and sorting. This tool allows you to build complex queries using MongoDB's query operators while maintaining safety and performance. |
| 122 | + |
| 123 | +**Parameters:** |
| 124 | +- `database_name` (str): The database name to query |
| 125 | +- `collection_name` (str): The collection name to query |
| 126 | +- `filter_dict` (str, optional): MongoDB filter/query as JSON string. Leave None for no filter |
| 127 | +- `projection` (str, optional): Fields to include/exclude as JSON string. Use 1 to include, 0 to exclude |
| 128 | +- `sort` (list[str], optional): Sort criteria as list of JSON strings with 'field' and 'direction' keys |
| 129 | +- `limit` (int): Maximum number of documents to return (default: 100) |
| 130 | +- `skip` (int): Number of documents to skip (default: 0) |
| 131 | + |
| 132 | +**Best Practices:** |
| 133 | +- Always use `discover_collections` and `get_collection_schema` before executing queries |
| 134 | +- Always specify projection to limit fields returned if you don't need all data |
| 135 | +- Always sort your results by the most important fields first |
| 136 | +- Use appropriate MongoDB query operators for complex filtering ($gte, $lte, $in, $regex, etc.) |
| 137 | +- Be mindful of case sensitivity when querying string fields |
| 138 | +- Use indexes when possible (typically on _id and commonly queried fields) |
| 139 | + |
| 140 | +<TabbedCodeBlock |
| 141 | + tabs={[ |
| 142 | + { |
| 143 | + label: "Call the Tool", |
| 144 | + content: { |
| 145 | + Python: [ |
| 146 | + "/examples/integrations/toolkits/mongodb/find_documents_example_call_tool.py", |
| 147 | + ], |
| 148 | + JavaScript: ["/examples/integrations/toolkits/mongodb/find_documents_example_call_tool.js"], |
| 149 | + }, |
| 150 | + } |
| 151 | + ]} |
| 152 | +/> |
| 153 | + |
| 154 | +## MongoDB.CountDocuments |
| 155 | + |
| 156 | +Count documents in a collection matching the given filter. This tool is useful for getting quick counts without retrieving the actual documents. |
| 157 | + |
| 158 | +**Parameters:** |
| 159 | +- `database_name` (str): The database name to query |
| 160 | +- `collection_name` (str): The collection name to query |
| 161 | +- `filter_dict` (str, optional): MongoDB filter/query as JSON string. Leave None to count all documents |
| 162 | + |
| 163 | +<TabbedCodeBlock |
| 164 | + tabs={[ |
| 165 | + { |
| 166 | + label: "Call the Tool", |
| 167 | + content: { |
| 168 | + Python: [ |
| 169 | + "/examples/integrations/toolkits/mongodb/count_documents_example_call_tool.py", |
| 170 | + ], |
| 171 | + JavaScript: ["/examples/integrations/toolkits/mongodb/count_documents_example_call_tool.js"], |
| 172 | + }, |
| 173 | + } |
| 174 | + ]} |
| 175 | +/> |
| 176 | + |
| 177 | +## MongoDB.AggregateDocuments |
| 178 | + |
| 179 | +Execute aggregation pipelines for complex data analysis. This tool allows you to run sophisticated data processing operations including grouping, filtering, and transformations. |
| 180 | + |
| 181 | +**Parameters:** |
| 182 | +- `database_name` (str): The database name to query |
| 183 | +- `collection_name` (str): The collection name to query |
| 184 | +- `pipeline` (list[str]): MongoDB aggregation pipeline as a list of JSON strings |
| 185 | +- `limit` (int): Maximum number of results to return (default: 100) |
| 186 | + |
| 187 | +**Common Aggregation Stages:** |
| 188 | +- `$match` - filter documents |
| 189 | +- `$group` - group documents and perform calculations |
| 190 | +- `$project` - reshape documents |
| 191 | +- `$sort` - sort documents |
| 192 | +- `$limit` - limit results |
| 193 | +- `$lookup` - join with other collections |
| 194 | + |
| 195 | +<TabbedCodeBlock |
| 196 | + tabs={[ |
| 197 | + { |
| 198 | + label: "Call the Tool", |
| 199 | + content: { |
| 200 | + Python: [ |
| 201 | + "/examples/integrations/toolkits/mongodb/aggregate_documents_example_call_tool.py", |
| 202 | + ], |
| 203 | + JavaScript: ["/examples/integrations/toolkits/mongodb/aggregate_documents_example_call_tool.js"], |
| 204 | + }, |
| 205 | + } |
| 206 | + ]} |
| 207 | +/> |
| 208 | + |
| 209 | +## Usage Workflow |
| 210 | + |
| 211 | +For optimal results, follow this workflow when using the MongoDB toolkit: |
| 212 | + |
| 213 | +1. **Discover Databases**: Use `discover_databases` to see available databases |
| 214 | +2. **Discover Collections**: Use `discover_collections` with your target database |
| 215 | +3. **Get Collection Schema**: Use `get_collection_schema` for each collection you plan to query |
| 216 | +4. **Execute Queries**: Use `find_documents`, `count_documents`, or `aggregate_documents` with the schema information |
| 217 | + |
| 218 | +This workflow ensures your agent has complete information about the database structure before attempting queries, reducing errors and improving query performance. |
| 219 | + |
| 220 | +<ToolFooter pipPackageName="arcade_mongodb"/> |
0 commit comments