There is no way to use RuntimeContext as actual DI with playground

I was trying to use RuntimeContext as DI container to put there Gitlab client instance to simplify mocking and testing tools.
const gitlabClient = runtimeContext.get("gitlabClient") as GitLabClient;
const gitlabClient = runtimeContext.get("gitlabClient") as GitLabClient;
While it works for tests and for programmatic agent calls it seems there is no way to define anything non JSON serialisable to use with playground. Also I believe it wouldn't work for network of agents as calling agent can't create and appropriate RuntimeContext
8 Replies
Guria
GuriaOP3w ago
Ok. I found default*Options that I can leverage to inject preconfigured runtime context for Agent. but it still don't work for playground tools section
Mastra Triager
GitHub
[DISCORD:1424460386777825280] There is no way to use RuntimeContext...
This issue was created from Discord post: https://discord.com/channels/1309558646228779139/1424460386777825280 I was trying to use RuntimeContext as DI container to put there Gitlab client instance...
_roamin_
_roamin_3w ago
Hi @Guria ! I don't think there is way to do that in the playground currently. You could maybe use a middleware on the Mastra server side https://mastra.ai/en/docs/server-db/middleware
Middleware
Apply custom middleware functions to intercept requests.
Guria
GuriaOP3w ago
is there any value to allow constructing it for main mastra instance?
_roamin_
_roamin_3w ago
The thing is the runtimeContext was mostly implemented to share JSON serializable things
Guria
GuriaOP2w ago
Worth to have such expectation properly documented in that case and don't call it DI
highbrrsaga
highbrrsaga6d ago
@Romain What's the recommended way to do dependency injection into tools then. My tool needs to call an external API, and I want to DI the client and was using RuntimeContext but looks like that might not be the recommended way?
_roamin_
_roamin_4h ago
Hey @highbrrsaga & @Guria ! There's a comment on the GH issue https://github.com/mastra-ai/mastra/issues/8531#issuecomment-3421107987 Feel free to chime in, we probably need to improve our DI 😉
GitHub
[DISCORD:1424460386777825280] There is no way to use RuntimeContext...
This issue was created from Discord post: https://discord.com/channels/1309558646228779139/1424460386777825280 I was trying to use RuntimeContext as DI container to put there Gitlab client instance...

Did you find this page helpful?