Skip to content

Commit 3e45b67

Browse files
committed
update: add the system prompt class in mcp server
1 parent dced223 commit 3e45b67

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

examples/servers/system_prompt.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from mcp.server import FastMCP
2+
3+
# from dotenv import load_dotenv
4+
import os
5+
from mcp.server.fastmcp import FastMCP
6+
from mcp.server.fastmcp.prompts import base as mcp_messages
7+
8+
mcp = FastMCP("weather") # No reason to initialize stateless
9+
10+
11+
12+
13+
@mcp.prompt()
14+
def weather_system_prompt() -> mcp_messages.SystemMessage:
15+
"""
16+
Creates a prompt asking an AI to weather
17+
Args:
18+
None: None
19+
"""
20+
21+
return mcp_messages.SystemMessage("""You are a helpful weather agent. Your job is to answer weather-related questions clearly and simply. If the user asks for the weather in a city, you tell the current weather, temperature, and a short description like "sunny," "cloudy," or "rainy." If you don’t know the answer, say "Sorry, I don’t have that information.""")
22+
23+
24+
mcp_app = mcp.streamable_http_app()
25+
26+
if __name__ == "__main__":
27+
import uvicorn
28+
uvicorn.run("system_prompt:mcp_app", host="0.0.0.0", port=8002, reload=True)

src/mcp/server/fastmcp/prompts/base.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
class Message(BaseModel):
1414
"""Base class for all prompt messages."""
1515

16-
role: Literal["user", "assistant"]
16+
role: Literal["user", "assistant","system"]
1717
content: ContentBlock
1818

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

28-
role: Literal["user", "assistant"] = "user"
28+
role: Literal["user", "assistant","system"] = "user"
2929

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

37-
role: Literal["user", "assistant"] = "assistant"
37+
role: Literal["user", "assistant","system"] = "assistant"
3838

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

42+
class SystemMessage(Message):
43+
"""A message from the assistant."""
44+
45+
role: Literal["user", "assistant","system"] = "system"
46+
47+
def __init__(self, content: str | ContentBlock, **kwargs: Any):
48+
super().__init__(content=content, **kwargs)
4249

43-
message_validator = TypeAdapter[UserMessage | AssistantMessage](UserMessage | AssistantMessage)
50+
message_validator = TypeAdapter[UserMessage | AssistantMessage | SystemMessage](UserMessage | AssistantMessage | SystemMessage)
4451

4552
SyncPromptResult = str | Message | dict[str, Any] | Sequence[str | Message | dict[str, Any]]
4653
PromptResult = SyncPromptResult | Awaitable[SyncPromptResult]

src/mcp/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
ProgressToken = str | int
3737
Cursor = str
38-
Role = Literal["user", "assistant"]
38+
Role = Literal["user", "assistant","system"]
3939
RequestId = Annotated[int | str, Field(union_mode="left_to_right")]
4040
AnyFunction: TypeAlias = Callable[..., Any]
4141

0 commit comments

Comments
 (0)