Skip to content

PyInstaller compatibility: Missing diskcache dependency and import check fails in frozen applications #2514

@akhil-vempali

Description

@akhil-vempali

Description

When using pydantic-ai-slim[fastmcp] with PyInstaller to create a frozen executable, the application fails with an ImportError claiming that fastmcp is not installed, even though it is properly installed and works fine when running the script directly with Python.

The root cause appears to be:

  1. Missing hidden dependencies (diskcache, and possibly others) that PyInstaller doesn't automatically detect
  2. The import check in pydantic_ai/toolsets/fastmcp.py fails in frozen executables

Environment

  • Python: 3.11.11
  • PyInstaller: 6.17.0
  • pydantic-ai-slim: 1.25.1
  • fastmcp: 2.13.2
  • OS: macOS 15.7.1 (arm64)

Steps to Reproduce

1. Create a minimal project:

pyproject.toml:

[project]
name = "fastmcp-pyinstaller-test"
version = "0.1.0"
requires-python = ">=3.11"
dependencies = [
    "pydantic-ai-slim[anthropic,fastmcp]>=1.25.1",
    "python-dotenv>=1.2.1",
]

[dependency-groups]
dev = [
    "pyinstaller>=6.17.0",
]

2. Create main script:

main.py:

import json
from dotenv import load_dotenv
from pydantic_ai import Agent
from pydantic_ai.models.anthropic import AnthropicModel
from pydantic_ai.toolsets.fastmcp import FastMCPToolset

load_dotenv()

with open("mcp.json") as f:
    mcp_config = json.load(f)

model = AnthropicModel("claude-3-5-sonnet-20241022")
toolsets = [FastMCPToolset(mcp_config)]
agent = Agent(model=model, toolsets=toolsets)

if __name__ == "__main__":
    agent.run_sync("hi")

3. Build with PyInstaller:

reprod.sh:

rm -rf dist/ build/ main.spec
uv sync --all-groups
uv run pyinstaller \
  --onedir \
  --noconfirm \
  --copy-metadata=pydantic_ai_slim \
  --copy-metadata=genai-prices \
  --copy-metadata=fastmcp \
  main.py
dist/main/main

4. Run the script:

bash reprod.sh

Expected Behavior

The frozen executable should work the same as running the script directly with Python.

Actual Behavior

The build succeeds, but running the executable fails with:

Traceback (most recent call last):
  File "key_value/aio/stores/disk/multi_store.py", line 13, in <module>
ModuleNotFoundError: No module named 'diskcache'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "pydantic_ai/toolsets/fastmcp.py", line 20, in <module>
  ...
  File "key_value/aio/stores/disk/multi_store.py", line 17, in <module>
ImportError: DiskStore requires py-key-value-aio[disk]

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    from pydantic_ai.toolsets.fastmcp import FastMCPToolset
  ...
ImportError: Please install the `fastmcp` package to use the FastMCP server, you can use the `fastmcp` optional group — `pip install "pydantic-ai-slim[fastmcp]"`

Version Information

FastMCP version:                                                                                   2.13.2
MCP version:                                                                                       1.22.0
Python version:                                                                                   3.11.11
Platform:                                                                    macOS-15.7.1-arm64-arm-64bit
FastMCP root path: ~/fastmcp-oauth-issue/.venv/lib/python3.11/site-packages

Metadata

Metadata

Assignees

Labels

bugSomething isn't working. Reports of errors, unexpected behavior, or broken functionality.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions