Method TryGet
TryGet(string, out Agent)
Attempts to retrieve an agent with the specified name.
public bool TryGet(string name, out Agent agent)
Parameters
namestringThe name of the agent to retrieve. Case-sensitive.
agentAgentWhen this method returns
true, contains the agent with the specified name. When this method returnsfalse, containsnull.
Returns
- bool
trueif an agent with the specified name was found; otherwise,false.
Examples
Example: Safe agent retrieval with fallback
using LMKit.Agents;
using LMKit.Model;
var registry = new AgentRegistry();
// ... agents registered ...
string requestedAgent = "specialist";
if (registry.TryGet(requestedAgent, out Agent agent))
{
var result = await agent.RunAsync("Handle this specialized task.");
Console.WriteLine(result.Content);
}
else
{
Console.WriteLine($"No agent named '{requestedAgent}' is available.");
Console.WriteLine($"Available agents: {string.Join(", ", registry.Names)}");
}
Remarks
This method is the preferred way to retrieve agents when you are not certain whether the agent exists. It avoids the exception thrown by the indexer when the agent is not found.
Returns false for null, empty, or whitespace names without throwing
an exception.