Adding description field to the FastMCP get_prompt method (#614)
Co-authored-by: ihrpr <inna@anthropic.com>
This commit is contained in:
parent
7b1078b515
commit
f55831ee79
2 changed files with 49 additions and 2 deletions
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -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."""
|
||||
|
|
|
|||
Loading…
Reference in a new issue