python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

ClientSession receive loop swallows callback exceptions, replying "Invalid request parameters" to the server instead of propagating to the call_tool awaiter

Open danielgshea opened this issue 6 months ago • 4 comments

Initial Checks

  • [x] I confirm that I'm using the latest version of MCP Python SDK
  • [x] I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue

Description

I hit this while wiring an MCP tool that uses ctx.elicit(...) into a LangGraph agent — the natural pattern of calling interrupt() from the client elicitation callback does not work because the callback's exception is swallowed by _receive_loop and replied to the server as Invalid request parameters. The same limitation blocks any framework that relies on exception-based control flow on the awaiter's task (custom async-cancellation strategies, Trio nursery patterns, retry/backoff libraries, any human-in-the-loop integration), so a small SDK-level escape hatch would unblock a broad class of MCP client developers, not just this one use case.

Any exception raised inside a ClientSession callback (elicitation_callback, sampling_callback, list_roots_callback) is caught by the blanket except Exception in BaseSession._receive_loop (src/mcp/shared/session.py) and turned into a JSON-RPC Invalid request parameters reply to the server:

except Exception:
    logging.warning("Failed to validate request", exc_info=True)
    error_response = JSONRPCError(
        jsonrpc="2.0",
        id=message.message.id,
        error=ErrorData(code=INVALID_PARAMS, message="Invalid request parameters", data=""),
    )
    await self._write_stream.send(SessionMessage(message=error_response))

Consequences:

  1. The server is blamed for "invalid params" when the failure was on the client. The original callback traceback is lost from the awaiter of session.call_tool(...).
  2. Callbacks have no way to abort back to the awaiter. Frameworks that use exception-based flow control on the caller's task (LangGraph's interrupt() for human-in-the-loop pause/resume is the motivating case) cannot be wired through MCP without per-framework workarounds.

Expected: an opt-in escape hatch so a callback can raise an exception that propagates out of the in-flight session.call_tool(...) instead of becoming a JSON-RPC error to the server. Defaulting opt-in to off preserves existing behaviour.

Proposed minimal fix (single file, ~30 lines, three coordinated edits):

# src/mcp/shared/session.py
#
# (1) BaseSession.__init__: add a per-session stash for marked exceptions.
self._propagate_errors: dict[RequestId, BaseException] = {}

# (2) Inside _receive_loop's request-handler `except Exception as e:` block,
#     branch on the marker before the existing INVALID_PARAMS path.
if getattr(e, "__mcp_propagate__", False):
    # Notify the peer so their request doesn't hang.
    error_response = JSONRPCError(
        jsonrpc="2.0",
        id=message.message.root.id,
        error=ErrorData(code=INTERNAL_ERROR, message="Handler raised", data=""),
    )
    await self._write_stream.send(SessionMessage(message=JSONRPCMessage(error_response)))
    # Surface to the awaiter of any in-flight outgoing request on this session.
    for in_flight_id, stream in list(self._response_streams.items()):
        self._propagate_errors[in_flight_id] = e
        await stream.aclose()
    continue
# ...existing INVALID_PARAMS path unchanged below this point.

# (3) Inside send_request, alongside the existing `except TimeoutError:`,
#     consume the stash on EndOfStream and re-raise the original exception.
except anyio.EndOfStream:
    propagate = self._propagate_errors.pop(request_id, None)
    if propagate is not None:
        raise propagate from None
    raise

I've already implemented and tested this locally against v1.x (and the same shape on main for the V2 rework): ~33 lines added to src/mcp/shared/session.py plus regression tests in tests/shared/test_session.py. The patch is strictly additive — without the __mcp_propagate__ marker, behaviour is byte-identical to today. End-to-end verified by surfacing a LangGraph interrupt() from an elicitation callback as a normal __interrupt__ on the agent's first invoke. ruff / pyright clean, existing session tests still pass. Happy to open a PR once the issue is accepted and you've confirmed the direction.

Example Code

# Self-contained reproduction. No third-party agent framework needed.
#
# Terminal A:  python repro_server.py
# Terminal B:  python repro_client.py
#
# Observed in A: WARNING:root:Failed to validate request: CallbackBoom(...)
# Observed in B: tool result.isError = True, CallbackBoom was NEVER raised
#                on the awaiter — silently converted to INVALID_PARAMS.

# --- repro_server.py ---
from mcp.server.fastmcp import Context, FastMCP
from pydantic import BaseModel

class Answer(BaseModel):
    value: str

server = FastMCP(name="repro", port=8765)

@server.tool()
async def ask(ctx: Context) -> str:
    result = await ctx.elicit(message="hello?", schema=Answer)
    return f"got: {result}"

if __name__ == "__main__":
    server.run(transport="streamable-http")


# --- repro_client.py ---
import asyncio
from mcp import ClientSession
from mcp.client.streamable_http import streamablehttp_client

class CallbackBoom(Exception):
    """Stand-in for any framework's flow-control exception (e.g. GraphInterrupt)."""

async def on_elicit(ctx, params):
    raise CallbackBoom("callback wants to abort back to the awaiter")

async def main():
    async with streamablehttp_client("http://127.0.0.1:8765/mcp") as (r, w, _):
        async with ClientSession(r, w, elicitation_callback=on_elicit) as session:
            await session.initialize()
            try:
                result = await session.call_tool("ask", {})
            except CallbackBoom:
                print("OK: CallbackBoom propagated to the awaiter")
                return
            print("BUG: awaiter did not raise; result.isError =", result.isError)
            print("     content =", result.content)

asyncio.run(main())

Python & MCP Python SDK

Python: 3.13
mcp:    1.27.1

danielgshea avatar May 24 '26 03:05 danielgshea

The tricky part here is correlating the callback failure with the caller that is awaiting the outgoing request. A session-level "propagate every in-flight stream" escape hatch works for the simple one-tool-call case, but it could become ambiguous if the client has multiple concurrent requests and only one of them triggered the elicitation/sampling callback.

A narrower shape might be to attach the callback exception to the request context that produced the incoming server request, then close or error only the response stream associated with that interaction. If that correlation is not available today, a regression test with two concurrent call_tool() calls would be useful before adding the escape hatch:

  • tool A triggers ctx.elicit() and the client callback raises a marked exception
  • tool B is still waiting on a normal result
  • expected: tool A raises the original exception to its awaiter, tool B is not closed or failed

That would keep the default JSON-RPC error behavior intact for ordinary callback bugs while making framework-level control-flow exceptions usable without surprising unrelated in-flight requests.

scosemicolon avatar May 27 '26 17:05 scosemicolon

This looks worth pinning down as a protocol-level regression rather than only an implementation bug.

For receive loop swallows callback exceptions, the acceptance test I would want is a minimal client/server fixture that records the exact JSON-RPC or HTTP exchange, then asserts both the wire result and the local lifecycle state after the failure path. In MCP deployments, the dangerous failures are often not just wrong return values; they are pending requests, dropped notifications, mismatched session state, or errors that get translated into a generic response and become impossible to diagnose.

A useful fix boundary would preserve three signals: the protocol error sent to the peer, the local exception or cancellation reason available to application code, and a trace/log field that ties the two together. That makes the behavior debuggable for agent hosts without requiring every host to special-case this SDK detail.

norika1207-lab avatar Jun 02 '26 03:06 norika1207-lab

Following up on @scosemicolon's correlation point, since it's the crux of whether this is actionable:

Correlation scope. The motivating case, a synchronous session.call_tool(...) awaiter whose server triggered the elicitation/sampling callback, has exactly one in-flight outgoing request on the session when the callback runs, so attribution is unambiguous. For sessions with multiple concurrent in-flight requests, the protocol carries no callback→originating-request link, so I'd propose defining the behavior as session-scoped: the marked exception is stashed against every in-flight outgoing request, and each awaiter on that session re-raises it. Coarse but safe (all in-flight callers abort rather than hang); finer per-request attribution would need a protocol change and is explicitly out of scope here.

Rewrite-proof acceptance. Since #2710 deletes BaseSession, the behavior is best pinnssion rather than tied to _receive_loop:

# opt-in marker → exception reaches the awaiter, NOT result.isError
class Boom(Exception):
    __mcp_propagate__ = True
async def on_elicit(ctx, params):
    raise Boom()
# in-memory client/server pair, elicitation_callback=on_elicit
with pytest.raises(Boom):
    await session.call_tool("ask", {})

# no marker → behaviour byte-identical to today (server gets INVALID_PARAMS,
# awaiter sees result.isError, no exception)

That keeps the contract intact whether it lands on the current loop or the new JSONRPCDispatcher/ServerRunner path.

Ar-maan05 avatar Jun 07 '26 08:06 Ar-maan05

I'd like to pick this up and implement the fix — thanks for the detailed write-up and repro.

Root cause (confirmed). In BaseSession._receive_loop (src/mcp/shared/session.py), the request branch wraps the dispatch to _received_request(...) in a blanket except Exception, which turns any callback failure (elicitation_callback / sampling_callback / list_roots_callback) into a JSON-RPC INVALID_PARAMS reply to the server. The original traceback is lost and the awaiter of the in-flight call_tool(...) never sees it, so exception-based control flow (LangGraph interrupt(), Trio nursery cancellation, human-in-the-loop) can't be wired through.

On the session-level escape hatch. I agree with @scosemicolon's concern: a session-wide "propagate to every in-flight stream" approach is racy under concurrency — if two call_tool(...) calls are in flight and only one triggers the failing callback, closing/erroring all response streams would incorrectly tear down the unrelated sibling. The escape hatch should be scoped to the single interaction that produced the failing server request.

Proposed direction — precise correlation. The transport already knows which outgoing request a server-initiated request belongs to: in streamable_http.py, each POST opens its own per-request SSE stream and tracks the original_request_id. The problem is that this correlation isn't surfaced to the session layer today — incoming messages are wrapped as SessionMessage(message) with no metadata, so _receive_loop has no way to map the failing callback back to the originating outgoing request.

Concretely I'd:

  1. Have the streamable-HTTP client attach related_request_id (the SSE stream's original_request_id) to the inbound SessionMessage.metadata for server-initiated requests — mirroring how the server side already uses ServerMessageMetadata.related_request_id for outbound routing.
  2. In _receive_loop, on an opt-in marked callback exception, error only the _response_streams[related_request_id] entry and re-raise the original exception to that specific awaiter — leaving sibling in-flight requests untouched. Default behavior (no marker) stays byte-identical to today.

This keeps the default JSON-RPC INVALID_PARAMS path intact for ordinary callback bugs while making framework-level control-flow exceptions usable without surprising unrelated requests. I'd add a regression test with two concurrent call_tool() calls (one triggers a raising callback, the other must complete normally), as @scosemicolon suggested.

This does span two modules (transport + session), so before I open a PR I'd like to confirm the direction:

  • (a) Is the precise-correlation approach (threading related_request_id from transport into the session layer) acceptable?
  • (b) Or would you prefer a lighter-weight mechanism to achieve the same per-request scoping?

I have a working local prototype and am happy to open the PR once the direction is confirmed.

Oxygen56 avatar Jun 10 '26 07:06 Oxygen56

I have a working implementation of this with tests, but the thread has an unanswered direction question and #2674 was closed unreviewed, so I'd rather ask before opening a PR.

First, the issue text is now half stale. The v2 refactor (#2838, BaseSession → JSONRPCDispatcher) already fixed the misattribution part. On current main the awaiter no longer sees Invalid request parameters — it gets an MCPError carrying the callback's real message:

mcp.shared.exceptions.MCPError: ordinary callback bug

So the server is no longer blamed for the client's failure, and the message survives. What remains is only that the exception's type is erased. That's the part exception-based control flow can't tolerate — interrupt() is useless if the caller can only catch MCPError.

Approach: correlated delivery, never fan-out. @scosemicolon's objection to the session-wide escape hatch is right, and it rules out stashing the exception against every in-flight request: two concurrent call_tools where only one triggers the failing callback would tear down the innocent sibling.

The correlation already exists and was simply being discarded. In streamable HTTP a server-initiated request rides the SSE body of the POST that provoked it, so that stream's original_request_id names the originating outgoing request. Attaching it as ServerMessageMetadata on the inbound SessionMessage (mirroring how the server side already uses that field for outbound routing) lets the dispatcher hand the exception to exactly one waiter. Opt-in via a marker; without it, wire behaviour is byte-identical.

Deliberate limitation: only streamable HTTP carries that link. Over stdio, SSE and in-memory the protocol has nothing to attribute with, so the marker is inert there and the awaiter keeps today's flattened MCPError. Guessing an originator when exactly one request happens to be in flight is racy for the same reason the fan-out was, so I didn't.

The question — which opt-in mechanism do you want?

  1. Marker attribute (__mcp_propagate__ = True) — what I built; zero API surface, works with exceptions defined in another library that you don't control.
  2. A MCPPropagate base class users inherit from — explicit and discoverable, but can't be applied to a third-party framework's existing exception type.
  3. A ClientSession(propagate_callback_exceptions=...) flag — session-scoped rather than per-exception, so it can't distinguish a control-flow signal from an ordinary callback bug.

I went with (1) because the motivating cases are exceptions defined by other libraries, which rules out (2), and because the distinction being drawn is per-exception, not per-session, which rules out (3). Happy to switch if you'd prefer either.

Implementation is ~50 lines across client/streamable_http.py and shared/jsonrpc_dispatcher.py, plus five tests (including the concurrent-sibling case @scosemicolon asked for). Full suite green at 100% coverage. I'll open the PR as soon as someone signals a direction.

UgaTheDev avatar Aug 25 '26 04:08 UgaTheDev