Skip to content

list_resources/list_resource_templates/list_prompts dispatch middleware with message={} while list_tools sends a typed request model #5710

Closed

Description

@BlueX888

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

  1. added
    bugSomething isn't working. Reports of errors, unexpected behavior, or broken functionality.
    serverRelated to FastMCP server implementation or server-side functionality.
    on Oct 12, 2026
  2. BlueX888 commented on Oct 12, 2026

    @BlueX888
    ContributorAuthor

    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:

    Run the issue's Example Code: FastMCP with a middleware that reads context.message on all four list hooks, driven via Client(mcp, mode="legacy") through list_tools/list_resources/list_resource_templates/list_prompts.
    

    Observed output:

    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>
    

    Happy to open a PR with this fix, or to be assigned if you prefer to take it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working. Reports of errors, unexpected behavior, or broken functionality.serverRelated to FastMCP server implementation or server-side functionality.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions