stores/cloudflare/README.md
Cloudflare KV store for Mastra, providing scalable and serverless storage for threads, messages, workflow snapshots, and evaluations. Supports both Cloudflare Workers KV Bindings and the REST API for flexible deployment in serverless and Node.js environments.
npm install @mastra/cloudflare
import { CloudflareStore } from '@mastra/cloudflare';
// Using Workers Binding API
const store = new CloudflareStore({
bindings: {
threads: THREADS_KV_NAMESPACE,
messages: MESSAGES_KV_NAMESPACE,
workflow_snapshot: WORKFLOW_KV_NAMESPACE,
traces: TRACES_KV_NAMESPACE,
},
keyPrefix: 'myapp_', // Optional
});
// Or using REST API
const store = new CloudflareStore({
accountId: process.env.CLOUDFLARE_ACCOUNT_ID!,
apiToken: process.env.CLOUDFLARE_API_TOKEN!,
namespacePrefix: 'myapp_', // Optional
});
// Save a thread
await store.saveThread({
thread: {
id: 'thread-123',
resourceId: 'resource-456',
title: 'My Thread',
metadata: { key: 'value' },
createdAt: new Date(),
},
});
// Add messages
await store.saveMessages({
messages: [
{
id: 'msg-1',
threadId: 'thread-123',
content: 'Hello Cloudflare!',
role: 'user',
createdAt: new Date(),
},
],
});
// Query messages
const messages = await store.listMessages({ threadId: 'thread-123' });
bindings option to pass KV namespaces directly (for Cloudflare Workers).accountId, apiToken, and (optionally) namespacePrefix for server-side usage.keyPrefix/namespacePrefix: Useful for isolating environments (e.g., dev/test/prod).saveThread({ thread }): Create or update a threadgetThreadById({ threadId }): Get a thread by IDlistThreadsByResourceId({ resourceId, offset, limit, orderBy? }): List paginated threads for a resourceupdateThread({ id, title, metadata }): Update thread title and metadatadeleteThread({ threadId }): Delete a thread and its messagessaveMessages({ messages }): Save multiple messageslistMessages({ threadId, perPage?, page? }): Get messages for a thread with paginationlistMessagesById({ messageIds }): Get specific messages by their IDsupdateMessages({ messages }): Update existing messagesgetResourceById({ resourceId }): Get a resource by IDsaveResource({ resource }): Create or save a resourceupdateResource({ resourceId, workingMemory }): Update resource working memorypersistWorkflowSnapshot({ workflowName, runId, snapshot }): Save workflow stateloadWorkflowSnapshot({ workflowName, runId }): Load workflow statelistWorkflowRuns({ workflowName, pagination }): List workflow runs with paginationgetWorkflowRunById({ workflowName, runId }): Get a specific workflow runupdateWorkflowState({ workflowName, runId, state }): Update workflow stateupdateWorkflowResults({ workflowName, runId, results }): Update workflow resultsgetScoreById({ id }): Get a score by IDsaveScore(score): Save an evaluation scorelistScoresByScorerId({ scorerId, pagination }): List scores by scorer with paginationlistScoresByRunId({ runId, pagination }): List scores by run with paginationlistScoresByEntityId({ entityId, entityType, pagination }): List scores by entity with paginationlistScoresBySpan({ traceId, spanId, pagination }): List scores by span with paginationdeleteMessages(messageIds): Message deletion is not currently supportedclearTable({ tableName }): Remove all records from a logical tablebatchInsert({ tableName, records }): Batch insert multiple records.insert({ tableName, record }): Insert a single record into a table.text: Stringtimestamp: ISO8601 string (converted to/from Date)uuid: Stringjsonb: JSON-encoded objectAll records are stored as JSON in KV, with automatic serialization/deserialization for metadata, arrays, and custom fields.
bindings option to pass KV namespaces directly (for Cloudflare Workers).accountId, apiToken, and (optionally) namespacePrefix for server-side usage.keyPrefix/namespacePrefix: Useful for isolating environments (e.g., dev/test/prod).Example:
const store = new CloudflareStore({
bindings: { ... }, // for Workers
keyPrefix: 'dev_',
});
// or
const store = new CloudflareStore({
accountId: '...',
apiToken: '...',
namespacePrefix: 'prod_',
});
Each logical Mastra table (threads, messages, workflow_snapshot, evals, traces) maps to a separate KV namespace. Keys are structured as ${prefix}${tableName}:${primaryKey} or ${prefix}${tableName}:${threadId}:${messageId} for messages. The prefix is set by keyPrefix/namespacePrefix.
No explicit cleanup or disconnect is required; Cloudflare KV is fully managed.