diff --git a/src/mcp/server/fastmcp/server.py b/src/mcp/server/fastmcp/server.py index bf0ce880a..abbfe9f64 100644 --- a/src/mcp/server/fastmcp/server.py +++ b/src/mcp/server/fastmcp/server.py @@ -388,7 +388,7 @@ def decorator(fn: AnyFunction) -> AnyFunction: resource = FunctionResource( uri=AnyUrl(uri), name=name, - description=description, + description=description or fn.__doc__ or None, mime_type=mime_type or "text/plain", fn=fn, ) diff --git a/tests/server/fastmcp/resources/test_function_resources.py b/tests/server/fastmcp/resources/test_function_resources.py index 5bfc72bf6..5aaeea602 100644 --- a/tests/server/fastmcp/resources/test_function_resources.py +++ b/tests/server/fastmcp/resources/test_function_resources.py @@ -136,3 +136,18 @@ async def get_data() -> str: content = await resource.read() assert content == "Hello, world!" assert resource.mime_type == "text/plain" + + def test_docstring_as_description(self): + """Test that function docstring is used as description when not provided.""" + + def my_func() -> str: + """This is a docstring description.""" + return "test content" + + resource = FunctionResource( + uri=AnyUrl("fn://test"), + name="test", + description=None, # No explicit description + fn=my_func, + ) + assert resource.description == "This is a docstring description." diff --git a/tests/server/fastmcp/test_server.py b/tests/server/fastmcp/test_server.py index e76e59c52..962641d82 100644 --- a/tests/server/fastmcp/test_server.py +++ b/tests/server/fastmcp/test_server.py @@ -347,6 +347,20 @@ async def test_file_resource_binary(self, tmp_path: Path): result.contents[0].blob == base64.b64encode(b"Binary file data").decode() ) + + @pytest.mark.anyio + async def test_resource_docstring_description(self): + """Test that resource decorator uses function docstring as description.""" + mcp = FastMCP() + + @mcp.resource("resource://docstring-test") + def get_data() -> str: + """This is a docstring description for a resource.""" + return "Data" + + resources = await mcp.list_resources() + assert len(resources) == 1 + assert resources[0].description == "This is a docstring description for a resource." class TestServerResourceTemplates: