MastraM
Mastra3mo ago
Adnan A.

Accessing messages inside a tool callback

Hey everyone,

I’m new to Mastra and have a quick question.

From what I understand, Mastra automatically passes messages to the agent (like in the config below). What I’m trying to figure out is: can I access those messages from within a tool callback?

Here’s a simplified example:

const agent = new Agent({
  ...

  tools: async ({ runtimeContext, mastra }) => {
    // How can I read the messages here that were passed to createCompletion?
    return {};
  },

  memory: new Memory({
    storage: new PostgresStore({
      connectionString: process.env.DATABASE_URL,
    }),
    vector: new PgVector({
      connectionString: process.env.DATABASE_URL,
    }),
    embedder: openai.embedding("text-embedding-3-small"),
    options: {
      lastMessages: 2,
      semanticRecall: {
        topK: 3,
        messageRange: 2,
        scope: "thread",
        indexConfig: {
          type: "hnsw",
          metric: "dotproduct",
          m: 16,
          efConstruction: 64,
        },
      },
      threads: {
        generateTitle: true,
      },
    },
  }),
});


Any hints on how to tap into the messages inside the tool context would be appreciated!
Was this page helpful?