Back to feed
Dev.to
Dev.to
7/16/2026
I fixed a smolagents bug that confused everyone who hit it (with Sentry watching the whole time)

I fixed a smolagents bug that confused everyone who hit it (with Sentry watching the whole time)

Short summary

A detailed account of fixing a long-standing bug in HuggingFace's smolagents framework (issue #1108) where calling to_dict() on a CodeAgent with MCP tools crashed with a confusing ValueError. The root cause: smolagents serializes tools by reconstructing standalone Python source, but MCP tools are runtime-generated closures wrapping live server sessions that cannot be serialized. The fix extends an existing guard clause to detect MCP tools and raise a clear, actionable error message instead of the cryptic internal failure.

  • smolagents crashed when serializing agents with MCP tools due to runtime-generated closures that can't be reconstructed from source
  • Root cause: MCPAdaptTool is generated at runtime by mcpadapt with required __init__ params and closure-captured session state
  • Fix adds a guard clause that detects MCP tools and raises a clear error telling users to remove MCP tools before serialization

Generated with AI, which can make mistakes.

Is this a good recommendation for you?

Comments

Failed to load comments. Please try again.

Explore more