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

AttributeError: 'JSONRPCMessage' object has no attribute 'message' in MCP 1.4.0

Open cgoncalves94 opened this issue 1 year ago • 8 comments

Describe the bug
I encountered an AttributeError when running the example code from MCP Quickstart - Client with MCP version 1.4.0. The error suggests that the JSONRPCMessage object is missing the message attribute. This issue does not occur with MCP 1.3.0.

To Reproduce
Steps to reproduce the behavior:

  1. Install MCP version 1.4.0:

    uv add mcp==1.4.0
    
  2. Run the example client code from the MCP Quickstart.

  3. Observe the following error:

    Exception Group Traceback (most recent call last):
    |   File "/Users/cesaraugustogoncalves/mcp-client/.venv/lib/python3.11/site-packages/mcp/client/stdio.py", line 153, in stdio_client
    |     yield read_stream, write_stream
    |   File "/Users/cesaraugustogoncalves/.local/share/uv/python/cpython-3.11.11-macos-aarch64-none/lib/python3.11/contextlib.py", line 728, in __aexit__
    |     cb_suppress = await cb(*exc_details)
    |                   ^^^^^^^^^^^^^^^^^^^^^^
    |   File "/Users/cesaraugustogoncalves/mcp-client/.venv/lib/python3.11/site-packages/mcp/shared/session.py", line 217, in __aexit__
    |     return await self._task_group.__aexit__(exc_type, exc_val, exc_tb)
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "/Users/cesaraugustogoncalves/mcp-client/.venv/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 767, in __aexit__
    |     raise BaseExceptionGroup(
    | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
    +-+---------------- 1 ----------------
      | Traceback (most recent call last):
      |   File "/Users/cesaraugustogoncalves/mcp-client/.venv/lib/python3.11/site-packages/mcp/shared/session.py", line 324, in _receive_loop
      |     message = raw_message.message
      |               ^^^^^^^^^^^^^^^^^^^
      |   File "/Users/cesaraugustogoncalves/mcp-client/.venv/lib/python3.11/site-packages/pydantic/main.py", line 891, in __getattr__
      |     raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}')
      | AttributeError: 'JSONRPCMessage' object has no attribute 'message'
    

Expected behavior
The client should run without raising an AttributeError, as it does with MCP 1.3.0.

Desktop (please complete the following information):

  • OS: macOS Sequoia 15.3.2 (M1 ARM64)
  • Python Version: 3.11.11
  • MCP Client Version: 1.4.0

Additional context

  • The issue does not occur when using MCP 1.3.0.
  • It seems like the JSONRPCMessage class no longer has a message attribute in version 1.4.0, but this is just based on the traceback.

Would appreciate any insights on whether this is an intended change or a regression. Thanks!

cgoncalves94 avatar Mar 14 '25 00:03 cgoncalves94

I encountered the same issue.

Upon further investigation of the error, I discovered that the type of raw_message is consistently mcp.types.JSONRPCMessage at this location:

https://github.com/modelcontextprotocol/python-sdk/blob/ebb81d3b2b675cf48225047c0f85e6d6296dc970/src/mcp/shared/session.py#L324

This appears to be happening because the server extracts the inner JSONRPCRequest or JSONRPCResponse from MessageFrame and sends it at this point:

https://github.com/modelcontextprotocol/python-sdk/blob/ebb81d3b2b675cf48225047c0f85e6d6296dc970/src/mcp/server/stdio.py#L85-L86

jkawamoto avatar Mar 14 '25 03:03 jkawamoto

Same here

Soulter avatar Mar 14 '25 03:03 Soulter

Same here

Henry-Jessie avatar Mar 14 '25 08:03 Henry-Jessie

Yeah, it seems this line: https://github.com/modelcontextprotocol/python-sdk/blob/ebb81d3b2b675cf48225047c0f85e6d6296dc970/src/mcp/server/stdio.py#L85-L86

Should have been message.message instead.


I would prefer to revert https://github.com/modelcontextprotocol/python-sdk/pull/239 - not really because of this, but because of the type changes.

Kludex avatar Mar 14 '25 08:03 Kludex

+1 having this issue

xinyang-zhu avatar Mar 14 '25 09:03 xinyang-zhu

Same here

ryaningli avatar Mar 14 '25 10:03 ryaningli

The fix was released in mcp 1.4.1. FYI

Kludex avatar Mar 14 '25 10:03 Kludex

Fixed in v1.4.1.

@Kludex we need to revisit this, but we need to have a way for servers to get access to the raw http request, in order to get access to headers ,etc.

dsp-ant avatar Mar 14 '25 10:03 dsp-ant