Skip to content

Fix resource docstring descriptions (Issue #488) #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/mcp/server/fastmcp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
15 changes: 15 additions & 0 deletions tests/server/fastmcp/resources/test_function_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
14 changes: 14 additions & 0 deletions tests/server/fastmcp/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading