Skip to main content

MongoDB session manager (storage backend) for Strands Agents — persists sessions, agent state, and messages

Project description

strands-session-mongodb

MongoDB session manager (storage backend) for Strands Agents. Persists an agent's sessions, agent state, conversation-manager state, and messages to MongoDB so conversations resume across runs.

Part of the strands-agents-session family — it implements the base SessionStorage interface, so all the Strands session-repository logic (message indexing, restore, removed_message_count offsetting, tool-use repair) comes from the core.

Storage only, by design. Message pruning in Strands is a ConversationManager concern, decoupled from storage. This package does not prune.

Installation

pip install strands-session-mongodb
# or, via the family's extra:
pip install "strands-agents-session[mongodb]"

Requires Python 3.10+ and a reachable MongoDB (local, Atlas, or self-hosted).

Quick start

from strands import Agent
from strands_session_mongodb import MongoDBSessionManager

session_manager = MongoDBSessionManager(
    session_id="user-123",
    connection_string="mongodb://127.0.0.1:27017/",
)

agent = Agent(session_manager=session_manager)
agent("Hi, I'm Kamal")
agent("What's my name?")   # remembers within the session

Next run, same session_id → the agent restores its full history and state from MongoDB.

API

MongoDBSessionManager(session_id, *, connection_string="mongodb://127.0.0.1:27017/", database_name="strands_sessions", collection_name="sessions", client=None, ttl_seconds=None)

Parameter Description
session_id Session identifier
connection_string MongoDB URI (local, Atlas mongodb+srv://…, etc.)
database_name Database (default strands_sessions)
collection_name Collection (default sessions)
client Optional pre-built pymongo.MongoClient to reuse
ttl_seconds If set, adds a TTL index so documents expire automatically

Data model

A single collection; each document has a pk + sk (compound-indexed, unique) and a data field with the serialized payload:

Item pk sk
Session SESSION#<session_id> META
Agent SESSION#<session_id> AGENT#<agent_id>
Message SESSION#<session_id>#AGENT#<agent_id> MSG#<zero-padded id>

The (pk, sk) index gives ordered range scans, so list_messages(offset, limit) is a simple indexed find(...).sort(sk).limit(...) — matching the removed_message_count offset semantics Strands relies on.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

strands_session_mongodb-0.1.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

strands_session_mongodb-0.1.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file strands_session_mongodb-0.1.0.tar.gz.

File metadata

  • Download URL: strands_session_mongodb-0.1.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for strands_session_mongodb-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d0760a589c7b91570813cac3bff8bbfe94e556f61cdf158057c1263239b0c202
MD5 9f5f89830b4e0c167b9f4c23faba38be
BLAKE2b-256 a929649c0ffe293b01801bc095f9492d7bec51a855c1b52850076837a70a497a

See more details on using hashes here.

File details

Details for the file strands_session_mongodb-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: strands_session_mongodb-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for strands_session_mongodb-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6637badb27ed59a1c0e369bd97c48c4d1813f745c3144b186835bad93baf6d6c
MD5 2dbe467cf293f681dd5a5ef53f327f2a
BLAKE2b-256 5fb0525a69d85748584b0619dc568784b7eed6a2ad0fc03f13c8af71d97567fc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page