Skip to content

Python: Bug: Cannot register agent with MCP plugin due to async context manager in HandoffOrchestration #12819

Closed

Description

@MahrRah

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:

  1. Install dependencies pip install -r requirements.txt
  2. Run MCP server python server.py
  3. 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' object

Platform

  • Language: Python (3.12.11)
  • Semantic-Kernel version: 1.35.0
  • IDE: VS Code
  • OS: Mac

Activity

  1. added theissue type on Jul 29, 2025
  2. changed the title [-]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`[/+] on Jul 29, 2025
  3. dantelmomsft commented on Sep 11, 2025

    @dantelmomsft
    Contributor

    issue is still there in sk version = 1.36.0. any updates/plan on the fix Mark Wallace (@markwallace-microsoft) Evan Mattson (@moonbox3) ?

  4. moonbox3 commented on Sep 11, 2025

    @moonbox3
    Collaborator

    I’ll have a PR out for this soon.

  5. moonbox3 commented on Sep 11, 2025

    @moonbox3
    Collaborator

    I tested the PR fix (in #13109) with the provided sample code - no error encountered now.

  6. added a commit that references this issue on Sep 12, 2025
    d8af1ed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't workingpythonPull requests for the Python Semantic Kernel

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions