Skip to content
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
4 changes: 4 additions & 0 deletions src/mcp/server/mcpserver/prompts/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import functools
import inspect
from collections.abc import Awaitable, Callable, Sequence
from typing import TYPE_CHECKING, Annotated, Any, Literal

Expand Down Expand Up @@ -188,6 +189,9 @@ async def render(
else:
result = await anyio.to_thread.run_sync(functools.partial(self.fn, **call_args))

if inspect.isawaitable(result):
result = await result

if isinstance(result, InputRequiredResult):
return result

Expand Down
18 changes: 18 additions & 0 deletions tests/server/mcpserver/prompts/test_base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import threading
from collections.abc import Awaitable
from pathlib import Path
from typing import Any

Expand Down Expand Up @@ -332,3 +333,20 @@ def fn() -> Image:

with pytest.raises(ValueError):
await Prompt.from_function(fn).render(None, Context())


@pytest.mark.anyio
async def test_prompt_render_awaits_awaitable_returned_by_sync_callback():
async def resolve() -> list[str]:
return []

coroutine = resolve()

def prompt() -> Awaitable[list[str]]:
return coroutine

try:
messages = await Prompt.from_function(prompt).render(None, Context())
finally:
coroutine.close()
assert messages == []
Loading