Repository navigation
Description
What happened?
The four typed list hooks on Middleware declare context.message as a request model: on_list_tools / on_list_resources / on_list_resource_templates / on_list_prompts are typed MiddlewareContext[mt.ListToolsRequest] / [mt.ListResourcesRequest] / [mt.ListResourceTemplatesRequest] / [mt.ListPromptsRequest] (fastmcp_slim/fastmcp/server/middleware/middleware.py:262-285, same signatures in the middleware docs). At runtime on_list_tools receives ListToolsRequest(method="tools/list"), but the other three receive a bare {}, so a middleware written against the declared types can read context.message.method on tools/list yet hits AttributeError on the other three list methods.
Output of the example below:
on_list_tools message type=ListToolsRequest .method=tools/list
on_list_resources message type=dict .method=<AttributeError: no .method on dict>
on_list_resource_templates message type=dict .method=<AttributeError: no .method on dict>
on_list_prompts message type=dict .method=<AttributeError: no .method on dict>
The same drift shows up in the bundled StructuredLoggingMiddleware(include_payloads=True): tools/list logs payload {"method":"tools/list","params":null} with payload_type ListToolsRequest, while resources/list and prompts/list log payload {} with payload_type dict.
Expected: all four hooks receive their declared request models, as the tools path already does.
Where it comes from (checked on current main, e2fcc41): list_tools builds MiddlewareContext(message=mcp_types.ListToolsRequest(method="tools/list")) (fastmcp_slim/fastmcp/server/server.py:835), but list_resources, list_resource_templates, and list_prompts pass message={} (fastmcp_slim/fastmcp/server/server.py:976, :1111, :1245).
I searched open issues/PRs and didn't find a prior report of this. Happy to open a PR that constructs the three declared request models at those sites, mirroring the tools path.
Example Code
import anyio
from fastmcp import Client, FastMCP
from fastmcp.server.middleware.middleware import Middleware
class InspectListMessageMiddleware(Middleware):
async def _report(self, hook: str, context) -> None:
message = context.message
try:
method = message.method # attribute on every typed mcp request model
except AttributeError:
method = f"<AttributeError: no .method on {type(message).__name__}>"
print(f"{hook:<28} message type={type(message).__name__:<22} "
f".method={method}")
async def on_list_tools(self, context, call_next):
await self._report("on_list_tools", context)
return await call_next(context)
async def on_list_resources(self, context, call_next):
await self._report("on_list_resources", context)
return await call_next(context)
async def on_list_resource_templates(self, context, call_next):
await self._report("on_list_resource_templates", context)
return await call_next(context)
async def on_list_prompts(self, context, call_next):
await self._report("on_list_prompts", context)
return await call_next(context)
async def main() -> None:
mcp = FastMCP("t")
mcp.add_middleware(InspectListMessageMiddleware())
@mcp.tool
def t1() -> str:
return "ok"
async with Client(mcp, mode="legacy") as client:
await client.list_tools()
await client.list_resources()
await client.list_resource_templates()
await client.list_prompts()
if __name__ == "__main__":
anyio.run(main)Version Information
FastMCP version: 0.0.1.dev53+e2fcc412 (source checkout of main @ e2fcc412001ff7414095c5dc282f94c70299a612)
MCP version: 2.0.0
Python version: 3.12.14
Platform: macOS-26.6.2-arm64
Activity
BlueX888 commentedon Oct 12, 2026
Reproduced this. Root cause: fastmcp_slim/fastmcp/server/server.py:976, :1111, :1245 build MiddlewareContext(message={}) for list_resources/list_resource_templates/list_prompts while list_tools (server.py:835) passes mcp_types.ListToolsRequest(method="tools/list").
Minimal reproduction:
Observed output:
Happy to open a PR with this fix, or to be assigned if you prefer to take it.