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

Pydantic deserialize file takes much more memory than the actual json

Open minhrongcon2000 opened this issue 8 months ago • 0 comments

Describe the bug When pydantic deserialize large base64-encoded payload, its memory consumption is much larger than the object itself.

To Reproduce Steps to reproduce the behavior:

import asyncio
from contextlib import AsyncExitStack

from mcp import ClientSession
from mcp.client.sse import sse_client


async def main():
    exit_stack = AsyncExitStack()
    read, write = await exit_stack.enter_async_context(
        sse_client("<sse endpoint>")
    )

    session = await exit_stack.enter_async_context(ClientSession(read, write))

    await session.initialize()

    await session.call_tool("<tool returning embedded resource with base64 around 30MB>")

    await exit_stack.aclose()


if __name__ == "__main__":
    asyncio.run(main())

Expected behavior Using memray to investigate, we can see that reading from sse takes 40MB, decoding to python object file takes another 40MB, but pydantic deserialization takes around 200MB or more.

Screenshots

Image

Desktop (please complete the following information):

  • OS: MacOS
  • Browser N/A
  • Version mcp==1.8.0

minhrongcon2000 avatar May 30 '25 08:05 minhrongcon2000