The infinispan-mastra package provides an Infinispan vector store for the Mastra TypeScript AI framework.
It enables kNN vector search, hybrid queries, and RAG (Retrieval-Augmented Generation) workflows using Infinispan as the backend.
Getting started with infinispan-mastra
Install and configure the Infinispan vector store for Mastra.
1. Installing infinispan-mastra
Install the Infinispan vector store for Mastra from npm.
-
Node.js 22 or later.
-
An Infinispan Server instance. You can start one with Docker:
docker run -it --rm -p 11222:11222 -e USER=admin -e PASS=password quay.io/infinispan/server:latest
-
Install the package with npm:
npm install infinispan-mastra -
Install the Mastra core package as a peer dependency if you have not already:
npm install @mastra/core
2. Configuring the vector store
Create an InfinispanVector instance to connect to your Infinispan Server.
-
Import the
InfinispanVectorclass:import { InfinispanVector } from 'infinispan-mastra'; const store = new InfinispanVector({ id: 'my-store', host: 'localhost', port: 11222, username: 'admin', password: 'password', }); -
The following configuration options are available:
Option Default Description id(required)
Unique identifier for this vector store instance.
hostlocalhostInfinispan Server hostname.
port11222Infinispan Server Hot Rod port.
username(optional)
Authentication username.
password(optional)
Authentication password.
uri(optional)
Hot Rod URI. When provided,
hostandportare ignored.
3. Registering with Mastra
Register the Infinispan vector store in your Mastra configuration so agents and tools can use it.
-
Add the vector store to the Mastra
vectorsconfiguration:import { Mastra } from '@mastra/core'; import { InfinispanVector } from 'infinispan-mastra'; const mastra = new Mastra({ vectors: { infinispan: new InfinispanVector({ id: 'infinispan', host: 'localhost', port: 11222, username: 'admin', password: 'password', }), }, }); -
Access the vector store from agents or tools:
const vectorStore = mastra.getVector('infinispan');
Vector operations
Use the infinispan-mastra vector store to create indexes, store embeddings, and perform kNN vector search.
4. Creating indexes
Create a vector index with a specified dimension and similarity metric. Infinispan automatically manages the protobuf schema and indexed cache.
-
Create an index:
await store.createIndex({ indexName: 'documents', dimension: 1536, metric: 'cosine', }); -
The
metricparameter supports the following values:Metric Description cosineCosine similarity (default). Best for normalized embeddings.
euclideanL2 distance. Best for spatial data.
dotproductInner product. Best for embeddings that are not normalized.
-
List existing indexes:
const indexes = await store.listIndexes(); -
Get index statistics:
const stats = await store.describeIndex({ indexName: 'documents' }); // { dimension: 1536, count: 42, metric: 'cosine' } -
Delete an index:
await store.deleteIndex({ indexName: 'documents' });
5. Storing vectors
Store embedding vectors with optional metadata and custom IDs.
-
Upsert vectors with metadata:
const ids = await store.upsert({ indexName: 'documents', vectors: [embedding1, embedding2], metadata: [ { title: 'Introduction', source: 'doc.pdf' }, { title: 'Chapter 1', source: 'doc.pdf' }, ], ids: ['doc-1', 'doc-2'], });Metadata is stored as serialized JSON inside Infinispan and is returned in query results.
-
Update an existing vector by ID:
await store.updateVector({ indexName: 'documents', id: 'doc-1', update: { metadata: { status: 'reviewed' }, }, });
6. Querying vectors
Perform kNN (k-nearest neighbor) vector search to find the most similar vectors.
-
Run a basic kNN query:
const results = await store.query({ indexName: 'documents', queryVector: queryEmbedding, topK: 5, }); for (const result of results) { console.log(`${result.id} (score: ${result.score}) - ${result.metadata?.title}`); }Results include the vector ID, similarity score, and metadata.
-
Include the original vectors in results:
const results = await store.query({ indexName: 'documents', queryVector: embedding, topK: 5, includeVector: true, }); // results[0].vector contains the stored embedding
Internally, infinispan-mastra uses the Ickle query language with the <→ operator to perform kNN search over Infinispan indexed caches.
7. Deleting vectors
Remove vectors by ID or in batch.
-
Delete a single vector:
await store.deleteVector({ indexName: 'documents', id: 'doc-1' }); -
Delete multiple vectors by ID:
await store.deleteVectors({ indexName: 'documents', ids: ['doc-1', 'doc-2', 'doc-3'], });
Building a RAG agent
Create an agentic RAG application that uses Infinispan for vector search and Mastra for the AI framework.
8. RAG architecture with Infinispan
Retrieval-Augmented Generation (RAG) combines vector search with LLM generation to answer questions using your own data.
The architecture works as follows:
-
Ingestion: Documents are chunked, embedded using an embedding model (for example OpenAI
text-embedding-3-small), and stored in Infinispan as vectors. -
Query: The user’s question is converted into an embedding vector.
-
Retrieval: Infinispan performs kNN vector search to find the most relevant document chunks.
-
Generation: The retrieved chunks and the original question are sent to an LLM, which generates a grounded answer.
This approach allows using smaller, cheaper LLMs for the final answer because Infinispan handles the retrieval of relevant context. The LLM only needs to synthesize the retrieved chunks into a coherent response.
Infinispan provides several advantages as a RAG backend:
-
kNN vector search with cosine, L2, and inner product similarity.
-
Hybrid queries combining vector search with metadata filtering using the Ickle query language.
-
Distributed indexing that scales across cluster nodes.
-
In-memory performance with optional persistence.
9. Building a RAG agent
Create a Mastra agent that uses createVectorQueryTool to perform semantic search over Infinispan.
-
An Infinispan Server running with indexed data (see Vector operations).
-
An OpenAI API key for embedding generation and LLM responses.
-
Create the agent with a vector query tool:
import { Agent } from '@mastra/core/agent'; import { InfinispanVector } from 'infinispan-mastra'; import { createVectorQueryTool } from '@mastra/rag'; import { ModelRouterEmbeddingModel } from '@mastra/core/llm'; import { Memory } from '@mastra/memory'; const infinispanVector = new InfinispanVector({ id: 'infinispan-vector', host: 'localhost', port: 11222, username: 'admin', password: 'password', }); const vectorQueryTool = createVectorQueryTool({ vectorStore: infinispanVector, indexName: 'scifi-movies', model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'), }); export const infinispanAgent = new Agent({ id: 'infinispan-agent', name: 'Infinispan Agent', instructions: `You are a helpful assistant that answers questions based on the provided context. Base your answer only on the context provided by the tool.`, model: 'openai/gpt-5-nano', tools: { vectorQueryTool }, memory: new Memory(), }); -
Register the agent in your Mastra configuration:
import { Mastra } from '@mastra/core/mastra'; import { LibSQLStore } from '@mastra/libsql'; import { infinispanAgent } from './agents/infinispan-agent'; export const mastra = new Mastra({ agents: { infinispanAgent }, storage: new LibSQLStore({ id: 'mastra-storage', url: 'file:./mastra.db', }), }); -
Run with Mastra Studio:
npx mastra devOpen
localhost:4111, select the agent, and ask questions about your indexed data.
For a complete example with dataset ingestion, see the Infinispan Mastra tutorial.