π The fast, Pythonic way to build MCP servers and clients.
Allow direct instantiation of Prompt and Resource classes (#2031)
Jeremiah Lowin20c6749de973
diff --git a/src/fastmcp/prompts/prompt.py b/src/fastmcp/prompts/prompt.pyindex 91c5cf31f..2785d04df 100644--- a/src/fastmcp/prompts/prompt.py+++ b/src/fastmcp/prompts/prompt.py@@ -4,7 +4,6 @@ from __future__ import annotations as _annotationsimport inspectimport json-from abc import ABC, abstractmethodfrom collections.abc import Awaitable, Callable, Sequencefrom typing import Any@@ -62,7 +61,7 @@ class PromptArgument(FastMCPBaseModel):)-class Prompt(FastMCPComponent, ABC):+class Prompt(FastMCPComponent):"""A prompt template that can be rendered with parameters."""arguments: list[PromptArgument] | None = Field(@@ -139,13 +138,16 @@ class Prompt(FastMCPComponent, ABC):meta=meta,)- @abstractmethodasync def render(self,arguments: dict[str, Any] | None = None,) -> list[PromptMessage]:- """Render the prompt with arguments."""- raise NotImplementedError("Prompt.render() must be implemented by subclasses")+ """Render the prompt with arguments.++ This method is not implemented in the base Prompt class and must be+ implemented by subclasses.+ """+ raise NotImplementedError("Subclasses must implement render()")class FunctionPrompt(Prompt):diff --git a/src/fastmcp/resources/resource.py b/src/fastmcp/resources/resource.pyindex 3067fb2c7..d7f9d7177 100644--- a/src/fastmcp/resources/resource.py+++ b/src/fastmcp/resources/resource.py@@ -2,7 +2,6 @@from __future__ import annotations-import abcimport inspectfrom collections.abc import Callablefrom typing import TYPE_CHECKING, Annotated, Any@@ -31,7 +30,7 @@ if TYPE_CHECKING:pass-class Resource(FastMCPComponent, abc.ABC):+class Resource(FastMCPComponent):"""Base class for all resources."""model_config = ConfigDict(validate_default=True)@@ -111,10 +110,13 @@ class Resource(FastMCPComponent, abc.ABC):raise ValueError("Either name or uri must be provided")return self- @abc.abstractmethodasync def read(self) -> str | bytes:- """Read the resource content."""- pass+ """Read the resource content.++ This method is not implemented in the base Resource class and must be+ implemented by subclasses.+ """+ raise NotImplementedError("Subclasses must implement read()")def to_mcp_resource(self,diff --git a/tests/resources/test_resources.py b/tests/resources/test_resources.pyindex 1e165eea9..3e30be1d8 100644--- a/tests/resources/test_resources.py+++ b/tests/resources/test_resources.py@@ -85,14 +85,15 @@ class TestResourceValidation:)assert resource.mime_type == "application/json"- async def test_resource_read_abstract(self):- """Test that Resource.read() is abstract."""+ async def test_resource_read_not_implemented(self):+ """Test that Resource.read() raises NotImplementedError."""class ConcreteResource(Resource):pass- with pytest.raises(TypeError, match="abstract method"):- ConcreteResource(uri=AnyUrl("test://test"), name="test") # type: ignore+ resource = ConcreteResource(uri=AnyUrl("test://test"), name="test") # type: ignore+ with pytest.raises(NotImplementedError, match="Subclasses must implement read"):+ await resource.read()def test_resource_meta_parameter(self):"""Test that meta parameter is properly handled."""