python-sdk
python-sdk copied to clipboard
Pydantic deserialize file takes much more memory than the actual json
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
Desktop (please complete the following information):
- OS: MacOS
- Browser N/A
- Version
mcp==1.8.0