Adding description field to the FastMCP get_prompt method (#614)

Co-authored-by: ihrpr <inna@anthropic.com>
This commit is contained in:
Megan Dsouza 2025-07-09 15:50:38 -04:00 • committed by GitHub
commit f55831ee79
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 49 additions and 2 deletions

View file

@ -957,9 +957,16 @@ class FastMCP:
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))

View file

@ -982,6 +982,46 @@ class TestServerPrompts:
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."""