Repository navigation
Python: Bug: Cannot register agent with MCP plugin due to async context manager in HandoffOrchestration #12819
Copy link
Copy link
Labels
bugSomething isn't workingSomething isn't workingpythonPull requests for the Python Semantic KernelPull requests for the Python Semantic Kernel
Description
Describe the bug
When using Agents with MCP plugins in a HandoffOrchestration, an error occurs due to cloning the Kernel inside HandoffAgentActor. Specifically, if the plugin implements an async context manager, it leads to a TypeError: cannot pickle 'async_generator' object because such objects cannot be deep-copied or pickled
To Reproduce
Minimal repro: Gits Sample
Steps:
- Install dependencies
pip install -r requirements.txt - Run MCP server
python server.py - Run SK sample using Agent with MCP plugin
python main.py
Expected behavior
HandoffOrchestration should be able to register and use Agents with MCP plugins that implement async context managers. Ideally, HandoffAgentActor should gracefully handle such unpickleable components when it needs to clone the agents’ kernels.
Screenshots
Stacktrace:
ERROR:in_process_runtime:Error constructing agent LibrarianAgent_456e1d30dae946499abe75228090f89c/default
Traceback (most recent call last):
File "/workspaces/sample/.venv/lib/python3.12/site-packages/semantic_kernel/agents/runtime/in_process/in_process_runtime.py", line 781, in _invoke_agent_factory
return cast(T, await agent)
^^^^^^^^^^^
File "/workspaces/sample/.venv/lib/python3.12/site-packages/semantic_kernel/agents/runtime/in_process/in_process_runtime.py", line 744, in factory_wrapper
maybe_agent_instance = agent_factory()
^^^^^^^^^^^^^^^
File "/workspaces/sample/.venv/lib/python3.12/site-packages/semantic_kernel/agents/orchestration/handoffs.py", line 486, in <lambda>
lambda agent=agent, handoff_connections=handoff_connections: HandoffAgentActor( # type: ignore[misc]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspaces/sample/.venv/lib/python3.12/site-packages/semantic_kernel/agents/orchestration/handoffs.py", line 174, in __init__
self._kernel = agent.kernel.clone()
^^^^^^^^^^^^^^^^^^^^
File "/workspaces/sample/.venv/lib/python3.12/site-packages/semantic_kernel/kernel.py", line 541, in clone
plugins=deepcopy(self.plugins),
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 136, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 221, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 143, in deepcopy
y = copier(memo)
^^^^^^^^^^^^
File "/workspaces/sample/.venv/lib/python3.12/site-packages/pydantic/main.py", line 940, in __deepcopy__
_object_setattr(m, '__dict__', deepcopy(self.__dict__, memo=memo))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 136, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 221, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 136, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 221, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 143, in deepcopy
y = copier(memo)
^^^^^^^^^^^^
File "/workspaces/sample/.venv/lib/python3.12/site-packages/pydantic/main.py", line 940, in __deepcopy__
_object_setattr(m, '__dict__', deepcopy(self.__dict__, memo=memo))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 136, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 221, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 162, in deepcopy
y = _reconstruct(x, memo, *rv)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 253, in _reconstruct
y = func(*args)
^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 252, in <genexpr>
args = (deepcopy(arg, memo) for arg in args)
^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 136, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 226, in _deepcopy_method
return type(x)(x.__func__, deepcopy(x.__self__, memo))
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 162, in deepcopy
y = _reconstruct(x, memo, *rv)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 259, in _reconstruct
state = deepcopy(state, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 136, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 221, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 162, in deepcopy
y = _reconstruct(x, memo, *rv)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 259, in _reconstruct
state = deepcopy(state, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 136, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 221, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 162, in deepcopy
y = _reconstruct(x, memo, *rv)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 276, in _reconstruct
item = deepcopy(item, memo)
^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 136, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 201, in _deepcopy_tuple
y = [deepcopy(a, memo) for a in x]
^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 136, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 226, in _deepcopy_method
return type(x)(x.__func__, deepcopy(x.__self__, memo))
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 162, in deepcopy
y = _reconstruct(x, memo, *rv)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 259, in _reconstruct
state = deepcopy(state, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 136, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 221, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/copy.py", line 151, in deepcopy
rv = reductor(4)
^^^^^^^^^^^
TypeError: cannot pickle 'async_generator' objectPlatform
- Language: Python (3.12.11)
- Semantic-Kernel version: 1.35.0
- IDE: VS Code
- OS: Mac
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingpythonPull requests for the Python Semantic KernelPull requests for the Python Semantic Kernel
Type
Projects
- StatusShow more project fieldsBug
Activity
[-]Bug: Cannot register agent with MCP plugin due to async context manager in `HandoffOrchestration`[/-][+]Python: Bug: Cannot register agent with MCP plugin due to async context manager in `HandoffOrchestration`[/+]dantelmomsft commentedon Sep 11, 2025
issue is still there in sk version = 1.36.0. any updates/plan on the fix Mark Wallace (@markwallace-microsoft) Evan Mattson (@moonbox3) ?
moonbox3 commentedon Sep 11, 2025
I’ll have a PR out for this soon.
moonbox3 commentedon Sep 11, 2025
I tested the PR fix (in #13109) with the provided sample code - no error encountered now.
Python: Safely clone plugins (#13109)