M
MastraAIโ€ข7d ago
Crafty

Adding / Removing agents in live environment

Hi All, Very new to mastra so apologies if this is a dumb question. Im investigating whether it would be possible to add or remove agents from an existing mastra instance? Im looking through the provided type interface of the Mastra class but havent found anything. Looking at the constructor, i found the following
/*
Agents
*/
const agents: Record<string, Agent> = {};
if (config?.agents) {
Object.entries(config.agents).forEach(([key, agent]) => {
if (agents[key]) {
const error = new MastraError({
id: 'MASTRA_AGENT_REGISTRATION_DUPLICATE_ID',
domain: ErrorDomain.MASTRA,
category: ErrorCategory.USER,
text: `Agent with name ID:${key} already exists`,
details: {
agentId: key,
},
});
this.#logger?.trackException(error);
throw error;
}
agent.__registerMastra(this);

agent.__registerPrimitives({
logger: this.getLogger(),
telemetry: this.#telemetry,
storage: this.storage,
memory: this.memory,
agents: agents,
tts: this.#tts,
vectors: this.#vectors,
});

agents[key] = agent;
});
}

this.#agents = agents as TAgents;
/*
Agents
*/
const agents: Record<string, Agent> = {};
if (config?.agents) {
Object.entries(config.agents).forEach(([key, agent]) => {
if (agents[key]) {
const error = new MastraError({
id: 'MASTRA_AGENT_REGISTRATION_DUPLICATE_ID',
domain: ErrorDomain.MASTRA,
category: ErrorCategory.USER,
text: `Agent with name ID:${key} already exists`,
details: {
agentId: key,
},
});
this.#logger?.trackException(error);
throw error;
}
agent.__registerMastra(this);

agent.__registerPrimitives({
logger: this.getLogger(),
telemetry: this.#telemetry,
storage: this.storage,
memory: this.memory,
agents: agents,
tts: this.#tts,
vectors: this.#vectors,
});

agents[key] = agent;
});
}

this.#agents = agents as TAgents;
I could replicate this in my code and just overwrite the private access definitions but id rather avoid that if possible the last option would be to rebuild the mastra instance when changes happen but thats probably quite expensive, Thanks for your help!
6 Replies
_roamin_
_roamin_โ€ข6d ago
Hi @Crafty ! That's not something that's possible at the moment unfortunately
Crafty
CraftyOPโ€ข6d ago
ah ok, thats a pain. is there anything that would bar it in the future? id be willing to put some time into this if theres nothing massive blocking it ๐Ÿ™‚
_roamin_
_roamin_โ€ข6d ago
Could you open a feature request on our github? I could pass that onto the team to see if that's something they'd consider? https://github.com/mastra-ai/mastra/issues
GitHub
mastra-ai/mastra
The TypeScript AI agent framework. โšก Assistants, RAG, observability. Supports any LLM: GPT-4, Claude, Gemini, Llama. - mastra-ai/mastra
Crafty
CraftyOPโ€ข6d ago
GitHub
[FEATURE] Ability to add or remove agents after Mastra Initialisati...
Problem Statement Id like to be able to dynamicly add or remove agents and workflows after the instantiation of the central Mastra class. This would enable several new usecases for mastra. In my ca...
_roamin_
_roamin_โ€ข4d ago
It got closed, my bad, I didn't we were already tracking this request https://github.com/mastra-ai/mastra/issues/7824 Thanks for bringing that up!
GitHub
[FEATURE] Dynamically add agents ยท Issue #7824 ยท mastra-ai/mastra
Problem Statement Currently (or AFAIK), agents are being hardcoded into the system and there is no way to add a dynamically generated agents into the Mastra instance. Proposed Solution Would be goo...
Crafty
CraftyOPโ€ข3d ago
All good, im working on a pr for that issue now ๐Ÿ™‚

Did you find this page helpful?