Skip to main content

MongoDB

agentscope-extensions-mongodb provides full-stack MongoDB distributed storage — ideal for deployments that already run MongoDB or prefer a document-oriented backend for agent runtime data.

Dependency

The module depends on the official MongoDB Java Driver. No additional client library is required.

One-Line Setup

From a connection string (the store owns the client lifecycle):
When the connection string includes a database path (e.g. /mydb), that database is used instead of the default agentscope.

Components Provided

1. MongoAgentStateStore

Agent session state persisted to MongoDB. Each session is a single document with a compound _id of {user, session}. State keys are stored inside a states sub-document (via dot notation), keeping them isolated from reserved top-level fields.
  • Single value: stored at states.<key> as a BSON sub-document; version at versions.<key>.
  • List value: stored at states.<key> as a BSON array; content hash at hashes.<key> for incremental-append optimization.
  • TTL: 30-day expiry on _updated_at (sparse index), aligned with snapshot TTL.

2. MongoBaseStore (BaseStore)

Workspace filesystem KV storage for RemoteFilesystemSpec. Uses prefix-matching via range queries on a compound (namespace, key) index — consistent with InMemoryStore and PostgresBaseStore.
  • Namespace paths use \u001F (ASCII Unit Separator) as the segment delimiter, with a trailing separator enabling prefix matching.
  • put / putIfVersion support optimistic concurrency via a version field.

3. MongoSnapshotSpec

Sandbox snapshots stored as BSON Binary in a collection with a 30-day TTL index on createdAt. Aligned with the session TTL so snapshots outlive their sessions.
BSON document size limit is 16 MB; snapshots are capped at 15 MB. For larger workspaces, consider a mixed store (MongoDB for state/lock, OSS for snapshots).

4. MongoSandboxExecutionGuard

Distributed lock using a dedicated MongoDB collection with a TTL index on expiresAt. Each acquisition generates a random UUID token for safe renewal and release — two guard instances in the same JVM cannot accidentally release each other’s locks.
  • lockTimeout bounds acquisition wait only; leaseTtl controls the lock document lifetime.
  • A background watchdog renews the lease every leaseTtl / 3 using a 2-thread pool.
  • The guard implements AutoCloseable and is shut down by MongoDistributedStore.close().

Collections and TTL

All collection names are centralised in MongoConstants and can be overridden per component.

When to Use