Class MongoTools

java.lang.Object
com.tnsai.tools.database.MongoTools

public class MongoTools extends Object
Function-shape MongoDB tools — the LangChain-style replacement for the legacy MongoDBQueryTool that took database:collection:filter as a single colon-delimited string (#188 — RFC).

Two typed @Tool methods, both read-only by design — the LLM cannot insert, update, or delete documents through this tool. Connection URI and a default database name are constructor-injected so the LLM does not see (or cannot change) which Mongo cluster the agent points at; a per-call database parameter is available for the occasional cross-database query.

Methods

Filter JSON is parsed with Document.parse(String); standard MongoDB operators (e.g. $gt, $in, $regex) work verbatim. An empty or null filter matches all documents.

Connection lifecycle

Like SqlTools, each call opens its own MongoClient and closes it via try-with-resources. For high-throughput workloads, wire a long-lived MongoClient at the application layer and pass it via the full constructor — the POJO will reuse the supplied client without owning its lifecycle.

Since:
0.5.7 (Phase 2 batch 5b — built-in tool migration to function shape, see #188)
  • Constructor Details

    • MongoTools

      public MongoTools()
      Default constructor — picks MONGODB_URI and MONGODB_DATABASE from env.
    • MongoTools

      public MongoTools(String connectionUri)
      Connection-only constructor — defaultDatabase must be supplied per call.
    • MongoTools

      public MongoTools(String connectionUri, String defaultDatabase)
      Explicit URI + default database.
    • MongoTools

      public MongoTools(String connectionUri, String defaultDatabase, int defaultLimit)
      Full constructor — URI + default database + per-instance default limit.
  • Method Details

    • mongoFind

      @Tool(name="mongo_find", description="Find MongoDB documents matching a JSON filter, capped at {limit}. Returns {\"matched\": N, \"documents\": [doc, \u2026]} where each doc is a key\u2192value map. Filter is standard MongoDB query JSON \u2014 empty {} or null matches all. {database} overrides the constructor-default database for this call only.", keywords={"mongodb","mongo","nosql","find","query","document"}, idempotent=true) public Map<String,Object> mongoFind(@ToolParam(description="Collection name") String collection, @ToolParam(description="MongoDB JSON filter (e.g. {\"status\": \"active\"} or {\"age\": {\"$gt\": 25}}); empty/null matches all",required=false,defaultValue="{}") String filter, @ToolParam(description="Database name; defaults to the constructor-injected default",required=false,defaultValue="") String database, @ToolParam(description="Max documents to return (1..1000); 0 falls back to instance default",required=false,defaultValue="0") int limit)
    • mongoCount

      @Tool(name="mongo_count", description="Count MongoDB documents matching a JSON filter. Returns {\"count\": N}. Same filter syntax as mongo_find. Use this when you only need the count and not the documents.", keywords={"mongodb","mongo","nosql","count"}, idempotent=true) public Map<String,Object> mongoCount(@ToolParam(description="Collection name") String collection, @ToolParam(description="MongoDB JSON filter; empty/null counts all documents in the collection",required=false,defaultValue="{}") String filter, @ToolParam(description="Database name; defaults to the constructor-injected default",required=false,defaultValue="") String database)