Skip to content

Commit f55831e

Browse files
megandsouza03ihrpr
andauthored
Adding description field to the FastMCP get_prompt method (#614)
Co-authored-by: ihrpr <inna@anthropic.com>
1 parent 7b1078b commit f55831e

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/mcp/server/fastmcp/server.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -957,9 +957,16 @@ async def list_prompts(self) -> list[MCPPrompt]:
957957
async def get_prompt(self, name: str, arguments: dict[str, Any] | None = None) -> GetPromptResult:
958958
"""Get a prompt by name with arguments."""
959959
try:
960-
messages = await self._prompt_manager.render_prompt(name, arguments)
960+
prompt = self._prompt_manager.get_prompt(name)
961+
if not prompt:
962+
raise ValueError(f"Unknown prompt: {name}")
961963

962-
return GetPromptResult(messages=pydantic_core.to_jsonable_python(messages))
964+
messages = await prompt.render(arguments)
965+
966+
return GetPromptResult(
967+
description=prompt.description,
968+
messages=pydantic_core.to_jsonable_python(messages),
969+
)
963970
except Exception as e:
964971
logger.exception(f"Error getting prompt {name}")
965972
raise ValueError(str(e))

tests/server/fastmcp/test_server.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,46 @@ def fn(name: str) -> str:
982982
assert isinstance(content, TextContent)
983983
assert content.text == "Hello, World!"
984984

985+
@pytest.mark.anyio
986+
async def test_get_prompt_with_description(self):
987+
"""Test getting a prompt through MCP protocol."""
988+
mcp = FastMCP()
989+
990+
@mcp.prompt(description="Test prompt description")
991+
def fn(name: str) -> str:
992+
return f"Hello, {name}!"
993+
994+
async with client_session(mcp._mcp_server) as client:
995+
result = await client.get_prompt("fn", {"name": "World"})
996+
assert result.description == "Test prompt description"
997+
998+
@pytest.mark.anyio
999+
async def test_get_prompt_without_description(self):
1000+
"""Test getting a prompt without description returns empty string."""
1001+
mcp = FastMCP()
1002+
1003+
@mcp.prompt()
1004+
def fn(name: str) -> str:
1005+
return f"Hello, {name}!"
1006+
1007+
async with client_session(mcp._mcp_server) as client:
1008+
result = await client.get_prompt("fn", {"name": "World"})
1009+
assert result.description == ""
1010+
1011+
@pytest.mark.anyio
1012+
async def test_get_prompt_with_docstring_description(self):
1013+
"""Test prompt uses docstring as description when not explicitly provided."""
1014+
mcp = FastMCP()
1015+
1016+
@mcp.prompt()
1017+
def fn(name: str) -> str:
1018+
"""This is the function docstring."""
1019+
return f"Hello, {name}!"
1020+
1021+
async with client_session(mcp._mcp_server) as client:
1022+
result = await client.get_prompt("fn", {"name": "World"})
1023+
assert result.description == "This is the function docstring."
1024+
9851025
@pytest.mark.anyio
9861026
async def test_get_prompt_with_resource(self):
9871027
"""Test getting a prompt that returns resource content."""

0 commit comments

Comments
 (0)