Skip to content

Commit 443acfd

Browse files
Soki FukudaSoki Fukuda
authored andcommitted
Fix resource docstring descriptions (Issue #488)
1 parent 70115b9 commit 443acfd

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/mcp/server/fastmcp/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ def decorator(fn: AnyFunction) -> AnyFunction:
388388
resource = FunctionResource(
389389
uri=AnyUrl(uri),
390390
name=name,
391-
description=description,
391+
description=description or fn.__doc__ or None,
392392
mime_type=mime_type or "text/plain",
393393
fn=fn,
394394
)

tests/server/fastmcp/resources/test_function_resources.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,18 @@ async def get_data() -> str:
136136
content = await resource.read()
137137
assert content == "Hello, world!"
138138
assert resource.mime_type == "text/plain"
139+
140+
def test_docstring_as_description(self):
141+
"""Test that function docstring is used as description when not provided."""
142+
143+
def my_func() -> str:
144+
"""This is a docstring description."""
145+
return "test content"
146+
147+
resource = FunctionResource(
148+
uri=AnyUrl("fn://test"),
149+
name="test",
150+
description=None, # No explicit description
151+
fn=my_func,
152+
)
153+
assert resource.description == "This is a docstring description."

tests/server/fastmcp/test_server.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,20 @@ async def test_file_resource_binary(self, tmp_path: Path):
347347
result.contents[0].blob
348348
== base64.b64encode(b"Binary file data").decode()
349349
)
350+
351+
@pytest.mark.anyio
352+
async def test_resource_docstring_description(self):
353+
"""Test that resource decorator uses function docstring as description."""
354+
mcp = FastMCP()
355+
356+
@mcp.resource("resource://docstring-test")
357+
def get_data() -> str:
358+
"""This is a docstring description for a resource."""
359+
return "Data"
360+
361+
resources = await mcp.list_resources()
362+
assert len(resources) == 1
363+
assert resources[0].description == "This is a docstring description for a resource."
350364

351365

352366
class TestServerResourceTemplates:

0 commit comments

Comments
 (0)