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

Lazy import in src/mcp/__init__.py

Open wuliang229 opened this issue 1 month ago • 1 comments

Description

Has the MCP team considered lazy importing in the mcp package?

Right now, if you import anything from mcp, it will run the mcp/init.py file, and it will import from all the submodules at the beginning. However, most people probably only need to develop for the server or client, but not both. Importing all the sub-modules will create a huge upfront cold start latency as more people are integrating MCP with their services in a auto-scaling format. Whenever a new server instance is cold-started, importing from MCP will take a non-trivial amount of time (~1s in my own laptop, it's usually slower on Cloud).

Would love to draft a PR if this is worth the value.

References

No response

wuliang229 avatar Nov 13 '25 00:11 wuliang229

This is something that's been mentioned before in http://github.com/modelcontextprotocol/python-sdk/issues/1508

Currently it won't be prioritised, but potentially looked at for something in v2 of the SDK.

maxisbey avatar Nov 17 '25 16:11 maxisbey

We do need to refactor the module names, and define what is private and public properly, but I don't think we need anything smart regarding imports. The imports being slow was mainly because of jsonschema.

Kludex avatar Dec 04 '25 10:12 Kludex