github PrefectHQ/fastmcp v3.1.0
v3.1.0: Code to Joy

11 hours ago

FastMCP 3.1 is the Code Mode release. The 3.0 architecture introduced providers and transforms as the extensibility layer — 3.1 puts that architecture to work, shipping the most requested capability since launch: servers that can find and execute code on behalf of agents, without requiring clients to know what tools exist.

Code Mode

Standard MCP has two scaling problems. The entire tool catalog loads into context upfront — with a large server, that's tens of thousands of tokens before the LLM reads a single word of the user's request. And every tool call is a round-trip: the LLM calls a tool, the result flows back through the context window, the LLM reasons about it, calls another tool, and so on. Intermediate results that only exist to feed the next step still burn tokens every time.

CodeMode is an experimental transform that solves both. Instead of seeing your tool catalog directly, the LLM gets meta-tools: it searches for relevant tools on demand (using BM25), inspects their schemas, then writes Python that chains call_tool() calls in a sandbox and returns a final answer. Discovery is staged and targeted; intermediate results never touch the model's context window.

from fastmcp import FastMCP
from fastmcp.experimental.transforms.code_mode import CodeMode

mcp = FastMCP("Server", transforms=[CodeMode()])

Your existing tools don't change — CodeMode wraps them. The default three-stage flow (search → get schemas → execute) is configurable: collapse it to two stages for smaller catalogs, skip discovery entirely for tiny ones. The sandbox supports resource limits on time, memory, and recursion depth.

Read the docs here.

Search Transforms

Code Mode's discovery layer is also available as a standalone transform. SearchTools adds BM25 text search to any server — clients can query against tool names and descriptions and receive ranked results, without needing to know tool names upfront. This is useful anywhere the tool catalog is large, dynamic, or not known in advance.

Prefab Apps

3.1 adds early integration with Prefab, a frontend framework with a Python DSL that compiles to React. The vision: Python developers building MCP servers shouldn't have to leave Python to ship a proper UI. Prefab is still under very active development (their words: "probably shouldn't use it yet"), but the integration is here, the pieces are aligning, and 3.2 is where this gets interesting.

Auth Additions

MultiAuth lets you compose multiple token verification sources into a single auth layer — useful when you need to accept tokens from more than one provider (e.g., internal JWTs alongside a third-party OAuth provider). This release also adds out-of-the-box support for PropelAuth and a Google GenAI sampling handler.

Under the Hood

Heavy imports are now lazy-loaded, meaningfully reducing startup time for servers that don't use every feature. fastmcp run and dev inspector gain a -m/--module flag for module-style invocation, MCPConfigTransport now correctly persists sessions across tool calls, and search_result_serializer gives you a hook to customize how search results are serialized for markdown output. Eight new contributors, and the usual round of fixes.

What's Changed

New Features 🎉

  • feat: Search transforms for tool discovery by @jlowin in #3154
  • Add experimental CodeMode transform by @aaazzam in #3297
  • Add Prefab Apps integration for MCP tool UIs by @jlowin in #3316

Enhancements 🔧

  • Lazy-load heavy imports to reduce import time by @jlowin in #3295
  • Add http_client parameter to all token verifiers for connection pooling by @jlowin in #3300
  • Add in-memory caching for token introspection results by @jlowin in #3298
  • Add SessionStart hook to install gh CLI in cloud sessions by @jlowin in #3308
  • Fix ty 0.0.19 type errors by @jlowin in #3310
  • Code Mode: Add resource limits to MontySandboxProvider by @jlowin in #3326
  • Accept transforms as FastMCP init kwarg by @jlowin in #3324
  • Split large test files to comply with loq line limit by @jlowin in #3328
  • Add -m/--module flag to fastmcp run and dev inspector by @dgenio in #3331
  • Add search_result_serializer hook and serialize_tools_for_output_markdown by @MagnusS0 in #3337
  • Add MultiAuth for composing multiple token verification sources by @jlowin in #3335
  • Adds PropelAuth as an AuthProvider by @andrew-propelauth in #3358
  • Replace vendored DI with uncalled-for by @chrisguidry in #3301
  • Decompose CodeMode into composable discovery tools by @jlowin in #3354
  • feat(contrib): auto-sync MCPMixin decorators with from_function signatures by @AnkeshThakur in #3323
  • Add Google GenAI Sampling Handler by @strawgate in #2977
  • Add ListTools, search limit, and catalog size annotation to CodeMode by @jlowin in #3359
  • Allow configuring FastMCP transport setting in the same way as other configuration by @jvdmr in #1796
  • Add include_unversioned option to VersionFilter by @yangbaechu in #3349

Fixes 🐞

  • Fix docs banner pushing nav down by @jlowin in #3282
  • fix: Replace hardcoded TTL with DEFAULT_TTL_MS - issue #3279 by @cedric57 in #3280
  • fix: stop suppressing server stderr in fastmcp call by @jlowin in #3283
  • fix: skip max_completion_tokens when maxTokens is None by @eon01 in #3284
  • OpenAPI: rewrite $ref under propertyNames and patternProperties in _replace_ref_with_defs; add regression test for dict[StrEnum, Model] by @manojPal23234 in #3306
  • Remove stale add_resource() key parameter from docs by @jlowin in #3309
  • Handle AuthorizationError as exclusion in AuthMiddleware list hooks by @yangbaechu in #3338
  • Fix flaky OpenAPI performance test threshold by @jlowin in #3355
  • Fix flaky SSE timeout test by @jlowin in #3343
  • Remove system role references from docs by @jlowin in #3356
  • Fix session persistence across tool calls in multi-server MCPConfigTransport by @jer805 in #3330

Docs 📚

  • Add v3.0.2 release notes by @jlowin in #3276
  • Fix "FastMCP Constructor Parameters" in documentation server.mdx (Remove old parameters & Add new parameter) by @wangyy04 in #3317
  • Fix stale docs: tag filtering API and missing output_schema param by @jlowin in #3322
  • Narrate search example clients by @jlowin in #3321
  • Code Mode: Document resource limits and fix docs formatting by @jlowin in #3327
  • Add reverse proxy (nginx) section to HTTP deployment docs by @dgenio in #3344
  • Restructure docs navigation: CLI section, Composition, More by @jlowin in #3361

Other Changes 🦾

  • Don't advertise sampling.tools capability by default by @jlowin in #3334

New Contributors

Full Changelog: v3.0.2...v3.1.0

Don't miss a new fastmcp release

NewReleases is sending notifications on new releases.