Skip to content

fix(pydantic): import restate.ext.pydantic without the mcp extra - #246

Open
Wali05 wants to merge 1 commit into
restatedev:mainfrom
Wali05:fix/pydantic-ext-without-mcp
Open

Wali05 wants to merge 1 commit into
restatedev:mainfrom
Wali05:fix/pydantic-ext-without-mcp

Conversation

@Wali05

@Wali05 Wali05 commented Oct 26, 2026

Copy link
Copy Markdown

Fixes #221.

_toolset.py imported pydantic_ai.mcp at module level for RestateMCPToolset, and pydantic_ai.mcp raises ImportError when the MCP client packages aren't installed. Because restate/ext/pydantic/__init__.py imports _toolset, the whole extension failed to import unless pydantic-ai-slim[mcp] was installed, even for agents that only use function tools.

RestateAgent already imports RestateMCPToolset lazily inside a try/except ImportError, but the guard never got a chance to work because _toolset was imported unconditionally a few lines above it. So I moved RestateMCPToolset and its two serde wrappers into a new _mcp_toolset.py, and pointed the lazy import there. _toolset.py keeps RestateContextRunToolSet and no longer touches pydantic_ai.mcp. The MCP code itself is moved as-is, no behaviour change.

RestateMCPToolset was never exported from restate.ext.pydantic, so nothing public moves.

One thing I noticed while testing: the repo's own uv sync --all-extras environment has mcp but not fastmcp (pydantic-ai-slim 2.15 needs both for pydantic_ai.mcp), so on main import restate.ext.pydantic fails there as well.

Testing

Added tests/ext_pydantic.py:

  • test_import_without_mcp imports restate.ext.pydantic in a subprocess with mcp/fastmcp blocked
  • test_agent_with_function_tools_without_mcp builds a RestateAgent with a function tool in the same setup and checks the tool is still wrapped in RestateContextRunToolSet
  • test_agent_wraps_mcp_toolsets checks an MCPToolset is still wrapped in RestateMCPToolset (skipped when pydantic_ai.mcp can't be imported)

The first two fail on main with the ImportError from the issue and pass with the change. I ran the third with pydantic-ai-slim[mcp]==2.15.0 installed, and it passes. Because the dev environment doesn't have fastmcp yet, test_agent_wraps_mcp_toolsets will be skipped in CI for now. Happy to add fastmcp to the dev deps in a follow-up so that path gets covered.

I also checked the exact scenario from the issue in a clean venv with only pydantic-ai-slim==2.15.0: import restate.ext.pydantic fails on main and works on this branch.

All the just verify steps pass locally (ruff format/check, pyright and mypy on python/, examples/, tests/, test-services/, plus pytest tests/*: 42 passed, 1 skipped).

_toolset.py imported pydantic_ai.mcp at module level for RestateMCPToolset,
so importing the extension failed unless pydantic-ai-slim[mcp] was
installed, even for agents with no MCP toolsets. Move RestateMCPToolset
into its own module, which RestateAgent already imports lazily inside its
ImportError guard.

Fixes restatedev#221
@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@Wali05

Wali05 commented Oct 26, 2026

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

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

Successfully merging this pull request may close these issues.

restate.ext.pydantic requires pydantic-ai-slim[mcp] because _toolset.py imports pydantic_ai.mcp unguarded

1 participant