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
26 changes: 26 additions & 0 deletions examples/servers/system_prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from mcp.server.fastmcp import FastMCP
from mcp.server.fastmcp.prompts import base as mcp_messages

mcp = FastMCP("weather")


@mcp.prompt()
def weather_system_prompt() -> mcp_messages.SystemMessage:
"""
Creates a prompt asking an AI to weather
Args:
None: None
"""

return mcp_messages.SystemMessage(
"You are a helpful weather agent. You answer questions clearly and simply. "
"If you don’t know something, say you don’t have that information."
)


mcp_app = mcp.streamable_http_app()

if __name__ == "__main__":
import uvicorn

uvicorn.run("system_prompt:mcp_app", host="0.0.0.0", port=8002, reload=True)
15 changes: 11 additions & 4 deletions src/mcp/server/fastmcp/prompts/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class Message(BaseModel):
"""Base class for all prompt messages."""

role: Literal["user", "assistant"]
role: Literal["user", "assistant","system"]
content: ContentBlock

def __init__(self, content: str | ContentBlock, **kwargs: Any):
Expand All @@ -25,7 +25,7 @@ def __init__(self, content: str | ContentBlock, **kwargs: Any):
class UserMessage(Message):
"""A message from the user."""

role: Literal["user", "assistant"] = "user"
role: Literal["user", "assistant","system"] = "user"

def __init__(self, content: str | ContentBlock, **kwargs: Any):
super().__init__(content=content, **kwargs)
Expand All @@ -34,13 +34,20 @@ def __init__(self, content: str | ContentBlock, **kwargs: Any):
class AssistantMessage(Message):
"""A message from the assistant."""

role: Literal["user", "assistant"] = "assistant"
role: Literal["user", "assistant","system"] = "assistant"

def __init__(self, content: str | ContentBlock, **kwargs: Any):
super().__init__(content=content, **kwargs)

class SystemMessage(Message):
"""A message from the assistant."""

role: Literal["user", "assistant","system"] = "system"

def __init__(self, content: str | ContentBlock, **kwargs: Any):
super().__init__(content=content, **kwargs)

message_validator = TypeAdapter[UserMessage | AssistantMessage](UserMessage | AssistantMessage)
message_validator = TypeAdapter[UserMessage | AssistantMessage | SystemMessage](UserMessage | AssistantMessage | SystemMessage)

SyncPromptResult = str | Message | dict[str, Any] | Sequence[str | Message | dict[str, Any]]
PromptResult = SyncPromptResult | Awaitable[SyncPromptResult]
Expand Down
2 changes: 1 addition & 1 deletion src/mcp/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

ProgressToken = str | int
Cursor = str
Role = Literal["user", "assistant"]
Role = Literal["user", "assistant","system"]
RequestId = Annotated[int | str, Field(union_mode="left_to_right")]
AnyFunction: TypeAlias = Callable[..., Any]

Expand Down
Loading