π The fast, Pythonic way to build MCP servers and clients.
Remove deprecated PromptToolMiddleware / ResourceToolMiddleware (3.1)
Jeremiah Lowind74f7b5996f0
diff --git a/fastmcp_slim/fastmcp/server/middleware/tool_injection.py b/fastmcp_slim/fastmcp/server/middleware/tool_injection.pyindex 00a9c3a65..ef88dfc0f 100644--- a/fastmcp_slim/fastmcp/server/middleware/tool_injection.py+++ b/fastmcp_slim/fastmcp/server/middleware/tool_injection.py@@ -1,19 +1,11 @@"""A middleware for injecting tools into the MCP server context."""-import warningsfrom collections.abc import Sequencefrom logging import Logger-from typing import Annotated, Anyimport mcp_types-from mcp_types import Prompt-from pydantic import AnyUrlfrom typing_extensions import override-import fastmcp-from fastmcp.exceptions import FastMCPDeprecationWarning-from fastmcp.resources.base import ResourceResult-from fastmcp.server.context import Contextfrom fastmcp.server.middleware.middleware import CallNext, Middleware, MiddlewareContextfrom fastmcp.tools.base import Tool, ToolResultfrom fastmcp.utilities.logging import get_logger@@ -52,94 +44,3 @@ class ToolInjectionMiddleware(Middleware):return await tool.run(arguments=context.message.arguments or {})return await call_next(context)---async def list_prompts(context: Context) -> list[Prompt]:- """List prompts available on the server."""- return await context.list_prompts()---list_prompts_tool = Tool.from_function(- fn=list_prompts,-)---async def get_prompt(- context: Context,- name: Annotated[str, "The name of the prompt to render."],- arguments: Annotated[- dict[str, Any] | None, "The arguments to pass to the prompt."- ] = None,-) -> mcp_types.GetPromptResult:- """Render a prompt available on the server."""- return await context.get_prompt(name=name, arguments=arguments)---get_prompt_tool = Tool.from_function(- fn=get_prompt,-)---class PromptToolMiddleware(ToolInjectionMiddleware):- """A middleware for injecting prompts as tools into the context.-- .. deprecated::- Use ``fastmcp.server.transforms.PromptsAsTools`` instead.- """-- def __init__(self) -> None:- if fastmcp.settings.deprecation_warnings:- warnings.warn(- "PromptToolMiddleware is deprecated. Use the PromptsAsTools transform instead: "- "from fastmcp.server.transforms import PromptsAsTools",- FastMCPDeprecationWarning,- stacklevel=2,- )- tools: list[Tool] = [list_prompts_tool, get_prompt_tool]- super().__init__(tools=tools)---async def list_resources(context: Context) -> list[mcp_types.Resource]:- """List resources available on the server."""- return await context.list_resources()---list_resources_tool = Tool.from_function(- fn=list_resources,-)---async def read_resource(- context: Context,- uri: Annotated[AnyUrl, "The URI of the resource to read."],-) -> ResourceResult:- """Read a resource available on the server."""- # Typed as AnyUrl (not `AnyUrl | str`) so pydantic normalizes the incoming- # URI the same way the MCP protocol boundary does (e.g. `file://config.txt`- # -> `file://config.txt/`). A bare `str` branch would skip normalization and- # miss resources whose stored key carries the canonical trailing slash.- return await context.read_resource(uri=uri)---read_resource_tool = Tool.from_function(- fn=read_resource,-)---class ResourceToolMiddleware(ToolInjectionMiddleware):- """A middleware for injecting resources as tools into the context.-- .. deprecated::- Use ``fastmcp.server.transforms.ResourcesAsTools`` instead.- """-- def __init__(self) -> None:- if fastmcp.settings.deprecation_warnings:- warnings.warn(- "ResourceToolMiddleware is deprecated. Use the ResourcesAsTools transform instead: "- "from fastmcp.server.transforms import ResourcesAsTools",- FastMCPDeprecationWarning,- stacklevel=2,- )- tools: list[Tool] = [list_resources_tool, read_resource_tool]- super().__init__(tools=tools)diff --git a/tests/deprecated/test_tool_injection_middleware.py b/tests/deprecated/test_tool_injection_middleware.pydeleted file mode 100644index bd0209a80..000000000--- a/tests/deprecated/test_tool_injection_middleware.py+++ /dev/null@@ -1,256 +0,0 @@-"""Tests for deprecated PromptToolMiddleware and ResourceToolMiddleware."""--import pytest-from inline_snapshot import snapshot-from mcp_types import TextContent-from mcp_types import Tool as SDKTool--from fastmcp import FastMCP-from fastmcp.client import Client-from fastmcp.client.client import CallToolResult-from fastmcp.client.transports import FastMCPTransport-from fastmcp.server.middleware.tool_injection import (- PromptToolMiddleware,- ResourceToolMiddleware,-)---class TestPromptToolMiddleware:- """Tests for PromptToolMiddleware."""-- @pytest.fixture- def server_with_prompts(self):- """Create a FastMCP server with prompts."""- mcp = FastMCP("PromptServer")-- @mcp.tool- def add(a: int, b: int) -> int:- """Add two numbers."""- return a + b-- @mcp.prompt- def greeting(name: str) -> str:- """Generate a greeting message."""- return f"Hello, {name}!"-- @mcp.prompt- def farewell(name: str) -> str:- """Generate a farewell message."""- return f"Goodbye, {name}!"-- return mcp-- async def test_prompt_tools_added_to_list(self, server_with_prompts: FastMCP):- """Test that prompt tools are added to the tool list."""- middleware = PromptToolMiddleware()- server_with_prompts.add_middleware(middleware)-- async with Client[FastMCPTransport](server_with_prompts) as client:- tools: list[SDKTool] = await client.list_tools()-- tool_names: list[str] = [tool.name for tool in tools]- # Should have: add, list_prompts, get_prompt- assert len(tools) == 3- assert "add" in tool_names- assert "list_prompts" in tool_names- assert "get_prompt" in tool_names-- async def test_list_prompts_tool_works(self, server_with_prompts: FastMCP):- """Test that the list_prompts tool can be called."""- middleware = PromptToolMiddleware()- server_with_prompts.add_middleware(middleware)-- async with Client[FastMCPTransport](server_with_prompts) as client:- result: CallToolResult = await client.call_tool(- name="list_prompts", arguments={}- )-- assert result.content == snapshot(- [- TextContent(- type="text",- text='[{"name":"greeting","title":null,"description":"Generate a greeting message.","arguments":[{"name":"name","title":null,"description":null,"required":true}],"icons":null,"_meta":{"fastmcp":{"tags":[]}}},{"name":"farewell","title":null,"description":"Generate a farewell message.","arguments":[{"name":"name","title":null,"description":null,"required":true}],"icons":null,"_meta":{"fastmcp":{"tags":[]}}}]',- )- ]- )- assert result.structured_content is not None- assert result.structured_content["result"] == snapshot(- [- {- "name": "greeting",- "title": None,- "description": "Generate a greeting message.",- "arguments": [- {- "name": "name",- "title": None,- "description": None,- "required": True,- }- ],- "icons": None,- "_meta": {"fastmcp": {"tags": []}},- },- {- "name": "farewell",- "title": None,- "description": "Generate a farewell message.",- "arguments": [- {- "name": "name",- "title": None,- "description": None,- "required": True,- }- ],- "icons": None,- "_meta": {"fastmcp": {"tags": []}},- },- ]- )-- async def test_get_prompt_tool_works(self, server_with_prompts: FastMCP):- """Test that the get_prompt tool can be called."""- middleware = PromptToolMiddleware()- server_with_prompts.add_middleware(middleware)-- async with Client[FastMCPTransport](server_with_prompts) as client:- result: CallToolResult = await client.call_tool(- name="get_prompt",- arguments={"name": "greeting", "arguments": {"name": "World"}},- )-- # The tool returns the prompt result with structured_content- assert result.content == snapshot(- [- TextContent(- type="text",- text='{"_meta":null,"description":"Generate a greeting message.","messages":[{"role":"user","content":{"type":"text","text":"Hello, World!","annotations":null,"_meta":null}}],"resultType":"complete"}',- )- ]- )- assert result.structured_content is not None- assert result.structured_content == snapshot(- {- "_meta": None,- "description": "Generate a greeting message.",- "messages": [- {- "role": "user",- "content": {- "type": "text",- "text": "Hello, World!",- "annotations": None,- "_meta": None,- },- }- ],- "resultType": "complete",- }- )---class TestResourceToolMiddleware:- """Tests for ResourceToolMiddleware."""-- @pytest.fixture- def server_with_resources(self):- """Create a FastMCP server with resources."""- mcp = FastMCP("ResourceServer")-- @mcp.tool- def add(a: int, b: int) -> int:- """Add two numbers."""- return a + b-- @mcp.resource("file://config.txt")- def config_resource() -> str:- """Get configuration."""- return "debug=true"-- @mcp.resource("file://data.json")- def data_resource() -> str:- """Get data."""- return '{"count": 42}'-- return mcp-- async def test_resource_tools_added_to_list(self, server_with_resources: FastMCP):- """Test that resource tools are added to the tool list."""- middleware = ResourceToolMiddleware()- server_with_resources.add_middleware(middleware)-- async with Client[FastMCPTransport](server_with_resources) as client:- tools: list[SDKTool] = await client.list_tools()-- tool_names: list[str] = [tool.name for tool in tools]- # Should have: add, list_resources, read_resource- assert len(tools) == 3- assert "add" in tool_names- assert "list_resources" in tool_names- assert "read_resource" in tool_names-- async def test_list_resources_tool_works(self, server_with_resources: FastMCP):- """Test that the list_resources tool can be called."""- middleware = ResourceToolMiddleware()- server_with_resources.add_middleware(middleware)-- async with Client[FastMCPTransport](server_with_resources) as client:- result: CallToolResult = await client.call_tool(- name="list_resources", arguments={}- )-- assert result.structured_content is not None- assert result.structured_content["result"] == snapshot(- [- {- "name": "config_resource",- "title": None,- "uri": "file://config.txt/",- "description": "Get configuration.",- "mimeType": "text/plain",- "size": None,- "icons": None,- "annotations": None,- "_meta": {"fastmcp": {"tags": []}},- },- {- "name": "data_resource",- "title": None,- "uri": "file://data.json/",- "description": "Get data.",- "mimeType": "text/plain",- "size": None,- "icons": None,- "annotations": None,- "_meta": {"fastmcp": {"tags": []}},- },- ]- )-- async def test_read_resource_tool_works(self, server_with_resources: FastMCP):- """Test that the read_resource tool can be called."""- middleware = ResourceToolMiddleware()- server_with_resources.add_middleware(middleware)-- async with Client[FastMCPTransport](server_with_resources) as client:- result: CallToolResult = await client.call_tool(- name="read_resource", arguments={"uri": "file://config.txt"}- )-- assert result.content == snapshot(- [- TextContent(- type="text",- text='{"contents":[{"content":"debug=true","mime_type":"text/plain","meta":null}],"meta":null}',- )- ]- )- assert result.structured_content == snapshot(- {- "contents": [- {"content": "debug=true", "mime_type": "text/plain", "meta": None}- ],- "meta": None,- }- )