semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

.Net: Dynamic Loading/Unloading of Plugins

Open Vake93 opened this issue 6 months ago • 3 comments

I have a scenario where, during a chat session, user requests may require different plugins. Based on the documentation, it's recommended to import only the necessary plugins.

Currently, my setup builds a new Kernel instance per chat session, loads all tools from various MCP servers as plugins, and creates a ChatCompletionAgent using that kernel.

This works fine for now since the number of registered MCP servers is small. However, I’m concerned that as the number of MCP servers grows, the LLM may struggle to choose the right tool effectively due to overload.

To address this, I experimented with using a parallel LLM call to determine which tools are relevant based on the conversation context, and then dynamically load/unload plugins accordingly.

I tried a basic approach using kernel.Plugins.AddFromFunctions and kernel.Plugins.Clear, but it didn’t work as expected.

Question: Do you have any recommendations on how to properly implement dynamic plugin loading/unloading during a session? Is this use case aligned with how Semantic Kernel is intended to be used, or am I missing a better pattern?

Vake93 avatar May 13 '25 12:05 Vake93

@Vake93 The following sample shows on approach you can use which uses semantic search to find the functions that best match the current user request: https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/Optimization/PluginSelectionWithFilters.cs

Assigning to @SergeyMenshykh for additional suggestions.

markwallace-microsoft avatar May 15 '25 06:05 markwallace-microsoft

This is what I was looking for thanks!

Vake93 avatar May 15 '25 07:05 Vake93

I tried this and it was working great on 1.52.1 with Microsoft.SemanticKernel.Connectors.OpenAI but today I updated to 1.53.0 and now the agent cant find the tools reliably. Other than updating the NuGet nothing changed in our project.

Vake93 avatar May 21 '25 12:05 Vake93