Skip to content

ModuleNotFoundError: No module named 'pydantic_core._pydantic_core' when importing from mcp.server even though pydantic-core is installed #570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ABHIJITH-EA opened this issue Apr 23, 2025 · 7 comments

Comments

@ABHIJITH-EA
Copy link

ABHIJITH-EA commented Apr 23, 2025

Describe the bug
I'm using the mcp package (version 1.6.0) in a Docker container based on node:20-alpine, running Python 3.11.12. When I try to import NotificationOptions and Server from mcp.server, I get the following error:

ModuleNotFoundError: No module named 'pydantic_core._pydantic_core'

Even though pydantic-core==2.33.1 and pydantic==2.11.3 are already installed.

To Reproduce
Steps to reproduce the behavior:

  1. Use a Docker image based on node:20-alpine
  2. Install Python 3.11.12
  3. Install mcp==1.6.0 using uv pip install
  4. Try this import in Python:
    from mcp.server import NotificationOptions, Server
  5. See the ModuleNotFoundError for pydantic_core._pydantic_core

Expected behavior
The import should work as expected since pydantic-core is installed. There should be no missing module error if dependencies are properly installed.

Screenshots

Image

Desktop (please complete the following information):

  • OS: Alpine (via Docker node:20-alpine)
  • Python version: 3.13
  • MCP version: 1.6.0
  • Install method: uv pip install mcp==1.6.0

Smartphone (please complete the following information):
Not applicable.

Additional context
Here is the relevant part of uv pip freeze:

pydantic==2.11.3  
pydantic-core==2.33.1  
mcp==1.6.0  
@SlagYouKnowZM
Copy link

Have same issue

@Viicos
Copy link

Viicos commented Apr 24, 2025

Could you provide your Dockerfile, as a MRE so that I can reproduce? More importantly, what is your host platform?

@marcoimme
Copy link

same issue, using python alpine as baseimage.

sample Dockefile

FROM python:3.12.3  AS py-build

COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/

WORKDIR /app

# Python files and uv.lock
COPY pyproject.toml ./
COPY uv.lock ./
COPY *.py ./
RUN uv sync --locked

# Sources
COPY *.py ./
COPY tools/ ./tools
COPY resources/ ./resources
COPY prompts/ ./prompts



# Create final layer
FROM python:3.12.3-alpine

COPY --from=py-build /app /app

WORKDIR /app

ENV PATH="/app/.venv/bin:$PATH"

CMD ["python" ,"server.py", "--transport", "sse", "--port", "8000"]

on run, I have

Traceback (most recent call last):
  File "/app/server.py", line 5, in <module>
    from pydantic import Field
  File "/app/.venv/lib/python3.12/site-packages/pydantic/__init__.py", line 5, in <module>
    from ._migration import getattr_migration
  File "/app/.venv/lib/python3.12/site-packages/pydantic/_migration.py", line 4, in <module>
    from .version import version_short
  File "/app/.venv/lib/python3.12/site-packages/pydantic/version.py", line 5, in <module>
    from pydantic_core import __version__ as __pydantic_core_version__
  File "/app/.venv/lib/python3.12/site-packages/pydantic_core/__init__.py", line 6, in <module>
    from ._pydantic_core import (
ModuleNotFoundError: No module named 'pydantic_core._pydantic_core'

@Viicos
Copy link

Viicos commented Apr 24, 2025

@marcoimme you are installing your dependencies on a different image than the one used to run the application. Alpine uses musl, which requires musl wheels to be used when installing pydantic-core.

Does it used to work? Or did you just encounter the issue when trying to setup your Dockerfile?

@marcoimme
Copy link

the error occurred during docker run, the build worked properly.
Now I switched the intermediate build layer using alpine as well

I workarounded the issue installing pydantic-core from alpine repo

now my Dockerfile looks like that


FROM ${default_image_registry}python:3.12.3-alpine  AS py-build

COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/

WORKDIR /app

RUN apk add py3-pydantic-core

# Python files and uv.lock
COPY pyproject.toml ./
COPY uv.lock ./
COPY *.py ./
RUN uv sync --locked

# Sources
COPY *.py ./
COPY tools/ ./tools
COPY resources/ ./resources
COPY prompts/ ./prompts



# Create final layer
FROM ${default_image_registry}python:3.12.3-alpine

COPY --from=py-build /app /app

WORKDIR /app

ENV PATH="/app/.venv/bin:$PATH"

CMD ["python" ,"server.py", "--transport", "sse", "--port", "8000"]

and it runs

@ABHIJITH-EA
Copy link
Author

@Viicos Here’s a minimal Dockerfile that can be used to reproduce the issue. It's based on the LibreChat Dockerfile, which I was originally using: https://github.com/danny-avila/LibreChat/blob/main/Dockerfile

FROM node:20-alpine

# Install basic deps
RUN apk add --no-cache curl

# Install uv
RUN curl -LsSf https://astral.sh/uv/install.sh | sh

ENV PATH="/root/.local/bin:${PATH}"

WORKDIR /app

# Create a Python 3.11 virtual environment
RUN uv venv --python=3.11 && \
    uv pip install mcp

# Install the MCP server

# Add test file
COPY test_mcp.py .

# Run the test
CMD ["uv", "run", "test_mcp.py"]

test_mcp.py

from mcp.server import NotificationOptions, Server

Build and run Dockerfile

docker build -t mcp-test .

docker run mcp-test`

Error:

Traceback (most recent call last):
  File "/app/test_mcp.py", line 1, in <module>
    from mcp.server import NotificationOptions, Server
  File "/app/.venv/lib/python3.11/site-packages/mcp/__init__.py", line 1, in <module>
    from .client.session import ClientSession
  File "/app/.venv/lib/python3.11/site-packages/mcp/client/session.py", line 6, in <module>
    from pydantic import AnyUrl, TypeAdapter
  File "/app/.venv/lib/python3.11/site-packages/pydantic/__init__.py", line 5, in <module>
    from ._migration import getattr_migration
  File "/app/.venv/lib/python3.11/site-packages/pydantic/_migration.py", line 4, in <module>
    from .version import version_short
  File "/app/.venv/lib/python3.11/site-packages/pydantic/version.py", line 5, in <module>
    from pydantic_core import __version__ as __pydantic_core_version__
  File "/app/.venv/lib/python3.11/site-packages/pydantic_core/__init__.py", line 6, in <module>
    from ._pydantic_core import (
ModuleNotFoundError: No module named 'pydantic_core._pydantic_core'

In my case, I was trying to run this MCP server inside the Docker container: https://github.com/vivekVells/mcp-pandoc

The .python-version in that repo is set to 3.11, I got this error when starting the server:

ModuleNotFoundError: No module named 'pydantic_core._pydantic_core'

@Viicos
Copy link

Viicos commented Apr 24, 2025

Up until 3.13, Python wasn't properly inferring the extension suffix for shared libraries. This was fixed in python/cpython#95855, so you need to use Python 3.13 or latter.

@Kludex Kludex closed this as completed Apr 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants