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:
Any hints on how to tap into the messages inside the tool context would be appreciated!
6 Replies
Hi @Adnan A. ! You can actually access the messages in the second param of the execute handler, like this:
Hi @Romain! thanks for getting back to me.
I tried what you suggested like this:
But in the console, options?.messages is undefined - actually, options itself is completely undefined.
Then I tried:
And I got this output:
Any idea how I can access the messages from here?
I think there’s a bit of a misunderstanding - you’re referring to executing from
tool/createTool
, but I’m talking about the tools
property inside an Agent
instance definition.
place you are asking is evaluated at agent creation time, there is no messages on that step yet
am I understand correctly that you want to dynamically override tools available based on messages passed to the agent?
Hi @Guria!
That’s correct - I need access to both the messages coming from the request body and the ones retrieved by the memory instance (semantic matches).
This would help me implement dynamic tool selection, since I have a large set of tools and passing them all normally costs a lot of input tokens.
Do you have any suggestions or possible solutions for this?
try to check model middleware. it allows you manipulate context on low level before sending params to actual model instance. but be careful
Language Model Middleware
Learn how to use middleware to enhance the behavior of language models