Skip to content

examples: MongoDB SessionStore reference adapter#341

Open
alexbevi wants to merge 4 commits into
anthropics:mainfrom
alexbevi:mongodb-session-store
Open

examples: MongoDB SessionStore reference adapter#341
alexbevi wants to merge 4 commits into
anthropics:mainfrom
alexbevi:mongodb-session-store

Conversation

@alexbevi

@alexbevi alexbevi commented Jun 1, 2026

Copy link
Copy Markdown

A self-contained reference SessionStore implementation for MongoDB under examples/session-stores/mongodb/, alongside the existing S3/Redis/Postgres adapters. Imports SessionStore, SessionKey, and SessionStoreEntry types from the published @anthropic-ai/claude-agent-sdk package and passes the 13-contract conformance suite at examples/session-stores/shared/conformance.ts.

Adapter Backend client Unit tests Live tests
mongodb/ mongodb constructor only env-gated SESSION_STORE_MONGODB_URL

Storage model

One document per JSONL entry, ordering via the server-generated _id (ObjectId) — same property the Postgres adapter exploits with BIGSERIAL.

{ _id, projectKey, sessionId, subpath: string|null, entry, createdAt }

ensureSchema() is idempotent and creates two compound indexes covering load()/delete()/listSubkeys() and listSessions() respectively. The adapter follows the Postgres pattern of live-only conformance: there is no in-process Mongo mock that faithfully exercises the aggregation pipeline and distinct.

Running

cd examples/session-stores/mongodb
npm install
npm test                                                              # constructor-only, no DB

docker run -d -p 27017:27017 mongo:latest
SESSION_STORE_MONGODB_URL=mongodb://localhost:27017 npm run test:live # live conformance
SESSION_STORE_MONGODB_URL=mongodb://localhost:27017 npm run demo      # query() + resume

CI / packaging impact: none

Nothing under examples/ is packaged, published, or built by repo CI; examples/.gitignore already excludes node_modules and lockfiles. No workflow files added. The umbrella examples/session-stores/README.md is updated to advertise the new adapter (table row, layout glob, MongoDB section, production checklist).

Verified locally

  • bun run typecheck clean.
  • No-DB unit tests: bun test test/MongoDBSessionStore.test.ts — 2/2 pass; live suite skips cleanly when env unset.
  • Live conformance against docker run -d -p 27017:27017 mongo:latest: 15/15 pass (13 contract tests + ensureSchema is idempotent + listSessions mtime is epoch ms).
  • End-to-end demo round-trip: first call replied pineapple, second call resumed from Mongo and replied pineapple again. mongosh confirmed 18 entries persisted under claude_test.claude_session_entries.

Test plan

  • cd examples/session-stores/mongodb && bun install && bun run typecheck
  • bun test test/MongoDBSessionStore.test.ts — 2/2 pass with no infra
  • docker run -d -p 27017:27017 mongo:latest then SESSION_STORE_MONGODB_URL=mongodb://localhost:27017 bun run test:live — 15/15 pass
  • With ANTHROPIC_API_KEY set, SESSION_STORE_MONGODB_URL=mongodb://localhost:27017 bun run demo.ts prints two pineapple results

alexbevi and others added 4 commits June 1, 2026 15:46
Adds a MongoDB-backed `SessionStore` example under
`examples/session-stores/mongodb/`, mirroring the Postgres adapter's
conventions: one document per JSONL entry, ordering via the
server-generated `_id`, and live-only env-gated conformance.

Provides `MongoDBSessionStore`, an `ensureSchema()` helper that creates
the indexes covering `load()`, `delete()`, and `listSessions()`, plus
a `demo.ts` round-trip and constructor unit tests.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Updates the umbrella `examples/session-stores/README.md` to advertise
the new MongoDB adapter alongside S3, Redis, and Postgres: adapter
table row, layout glob, full MongoDB section (storage model + live
end-to-end snippet), and a production-checklist subsection covering
write concern, retention, and pool sizing.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Updates the README, demo header, and conformance-test header to use
`mongo:latest` instead of `mongo:7` for consistency with the rest of
the project's tooling guidance.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The driver defaults to `w: "majority"`, which is the right thing for
durability-sensitive mirror writes. Steering readers toward `w: 1` or
mentioning `w: 0` at all is bad advice in this context — drop the
bullet entirely.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@alexbevi

alexbevi commented Jun 1, 2026

Copy link
Copy Markdown
Author

Can't update https://code.claude.com/docs/en/agent-sdk/session-storage#reference-implementations, but was hoping to get it added there. I can do the Python example as well if this approach is acceptable.

@alexbevi

alexbevi commented Jun 2, 2026

Copy link
Copy Markdown
Author

Ended up opening this as anthropics/claude-agent-sdk-python#1014 for the Python side

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant