www.jlowin.dev

MCP-Native Middleware with FastMCP 2.9 - Mostly Harmless

6/23/2025Updated 3/16/2026

Excerpt

It’s the go-to pattern for adding cross-cutting concerns like authentication, logging, or caching without rewriting your core application logic. Until today, when developers asked how to add middleware to their MCP server, the obvious answer seemed to be wrapping their server with traditional ASGI middleware. Unfortunately, that approach has two critical flaws: 1. It only works for web-based transports like streamable-HTTP and SSE. Until very recently, most major clients only supported the local STDIO transport, making this a non-starter for many. 2. More importantly, it forces you to parse the MCP’s low-level JSON-RPC messages yourself. All the hard work FastMCP does to give you clean, high-level Tool and Resource objects is lost. You’re left trying to reconstruct meaning from a sea of protocol noise. This is a lot of work for a very limited set of outcomes. So, we went back to the drawing board and embraced a core FastMCP principle: **focus on the developer’s intent, not the protocol’s complexity.** ... FastMCP 2.9 is a huge release, and it also includes one highly-requested feature: **server-side type conversion for prompt arguments.** The MCP spec requires all prompt arguments to be strings. This has been a persistent developer pain point. Why? Because the Python function that generates those prompts often needs structured data to perform business logic, such as a list of IDs to look up, a dictionary of configuration, or some filter criteria. This has forced developers to litter their prompt logic with `json.loads()` and pray that the agent provides a compatible input. … An MCP client would call this with `{"user_ids": "[1, 2, 3]", "analysis_type": "performance"}`, but the MCP server would receive a clean `list` and `str`. It’s a small change that removes a huge amount of friction, especially when prompts are doing more than just string interpolation. FastMCP’s implementation of this feature is fully MCP spec-compliant, but because there is no *formal* way to describe the expected JSON Schema format of a prompt argument, it’s possible that some clients will choose to ignore it. As with all agentic users, performance will depend on clarity of your instructions.

Source URL

https://www.jlowin.dev/blog/fastmcp-2-9-middleware

Related Pain Points