diff --git a/src/mcp/server/fastmcp/server.py b/src/mcp/server/fastmcp/server.py index c6c0cb5a3..b698b0497 100644 --- a/src/mcp/server/fastmcp/server.py +++ b/src/mcp/server/fastmcp/server.py @@ -957,9 +957,16 @@ async def list_prompts(self) -> list[MCPPrompt]: async def get_prompt(self, name: str, arguments: dict[str, Any] | None = None) -> GetPromptResult: """Get a prompt by name with arguments.""" try: - messages = await self._prompt_manager.render_prompt(name, arguments) + prompt = self._prompt_manager.get_prompt(name) + if not prompt: + raise ValueError(f"Unknown prompt: {name}") - return GetPromptResult(messages=pydantic_core.to_jsonable_python(messages)) + messages = await prompt.render(arguments) + + return GetPromptResult( + description=prompt.description, + messages=pydantic_core.to_jsonable_python(messages), + ) except Exception as e: logger.exception(f"Error getting prompt {name}") raise ValueError(str(e)) diff --git a/tests/server/fastmcp/test_server.py b/tests/server/fastmcp/test_server.py index 37351bc6f..96cece9c3 100644 --- a/tests/server/fastmcp/test_server.py +++ b/tests/server/fastmcp/test_server.py @@ -982,6 +982,46 @@ def fn(name: str) -> str: assert isinstance(content, TextContent) assert content.text == "Hello, World!" + @pytest.mark.anyio + async def test_get_prompt_with_description(self): + """Test getting a prompt through MCP protocol.""" + mcp = FastMCP() + + @mcp.prompt(description="Test prompt description") + def fn(name: str) -> str: + return f"Hello, {name}!" + + async with client_session(mcp._mcp_server) as client: + result = await client.get_prompt("fn", {"name": "World"}) + assert result.description == "Test prompt description" + + @pytest.mark.anyio + async def test_get_prompt_without_description(self): + """Test getting a prompt without description returns empty string.""" + mcp = FastMCP() + + @mcp.prompt() + def fn(name: str) -> str: + return f"Hello, {name}!" + + async with client_session(mcp._mcp_server) as client: + result = await client.get_prompt("fn", {"name": "World"}) + assert result.description == "" + + @pytest.mark.anyio + async def test_get_prompt_with_docstring_description(self): + """Test prompt uses docstring as description when not explicitly provided.""" + mcp = FastMCP() + + @mcp.prompt() + def fn(name: str) -> str: + """This is the function docstring.""" + return f"Hello, {name}!" + + async with client_session(mcp._mcp_server) as client: + result = await client.get_prompt("fn", {"name": "World"}) + assert result.description == "This is the function docstring." + @pytest.mark.anyio async def test_get_prompt_with_resource(self): """Test getting a prompt that returns resource content."""