Skip to content

fix(shared): make UrlElicitationRequiredError pickle-safe (refs #2431)#2555

Open
MukundaKatta wants to merge 1 commit intomodelcontextprotocol:mainfrom
MukundaKatta:fix/url-elicitation-pickle
Open

fix(shared): make UrlElicitationRequiredError pickle-safe (refs #2431)#2555
MukundaKatta wants to merge 1 commit intomodelcontextprotocol:mainfrom
MukundaKatta:fix/url-elicitation-pickle

Conversation

@MukundaKatta
Copy link
Copy Markdown

References #2431.

Bug

The default Exception unpickle path reconstructs via cls(*self.args). MCPError.__init__ sets args to (code, message, data) — three positionals. That doesn't match UrlElicitationRequiredError's signature (elicitations, message=None), so unpickling raises:

TypeError: UrlElicitationRequiredError.__init__() takes from 2 to 3 positional arguments but 4 were given

Reproduces on stdlib pickle and cloudpickle alike. Repro on current main:

import pickle
from mcp.shared.exceptions import UrlElicitationRequiredError
from mcp.types import ElicitRequestURLParams

err = UrlElicitationRequiredError([
    ElicitRequestURLParams(mode="url", message="auth", url="https://e.com", elicitation_id="x"),
])
pickle.loads(pickle.dumps(err))  # ↑ TypeError

Fix

Add __reduce__ that reconstructs from the typed (elicitations, message) pair, so the round-trip preserves the high-level field rather than the wire-format data dict.

Tests

  • New test_url_elicitation_required_error_pickle_round_trip — exercises a 2-elicitation error, asserts class identity, message, elicitations list, and reconstructed error.code.
  • New test_mcp_error_pickle_round_trip — pins the parent class's already-working round trip so a future __init__ refactor can't silently break it.
  • 11/11 passing in tests/shared/test_exceptions.py.

Note on issue scope

#2431 describes a related but slightly different constructor shape (McpError(error: ErrorData)) that no longer exists on main — the parent class was refactored to (code, message, data=None) since the report. That refactor accidentally fixed the parent's pickle round-trip but left the URL-elicitation subclass broken on a different code path. This PR closes that remaining gap.

…contextprotocol#2431)

The default Exception unpickle path reconstructs by calling
cls(*self.args), where args was set by the MCPError parent __init__ to
(code, message, data) — three positionals. That doesn't match
UrlElicitationRequiredError's signature (elicitations, message=None=2)
so every unpickle raises:

    TypeError: UrlElicitationRequiredError.__init__() takes from 2 to 3
    positional arguments but 4 were given

Reproduces with stdlib pickle and cloudpickle alike.

Add __reduce__ that reconstructs from the typed (elicitations, message)
pair so the round-trip preserves the high-level fields rather than the
wire-format data dict.

  - regression test for the round-trip on a 2-elicitation error
  - companion test that pins MCPError's (already-working) round-trip so
    a future __init__ refactor can't silently break it

Issue modelcontextprotocol#2431 originally describes a related but slightly different
constructor shape (`McpError(error: ErrorData)`) that no longer exists
on main. The pickle-safety problem in the title still holds — just on
the URL-elicitation subclass — and that's what this fix addresses.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant