From 3e45b6763eae634804f4a870abc34055369ba7d9 Mon Sep 17 00:00:00 2001 From: syedmuneeb321 <130203424+syedmuneeb321@users.noreply.github.com> Date: Fri, 27 Jun 2025 14:45:11 +0500 Subject: [PATCH 1/4] update: add the system prompt class in mcp server --- examples/servers/system_prompt.py | 28 ++++++++++++++++++++++++++ src/mcp/server/fastmcp/prompts/base.py | 15 ++++++++++---- src/mcp/types.py | 2 +- 3 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 examples/servers/system_prompt.py diff --git a/examples/servers/system_prompt.py b/examples/servers/system_prompt.py new file mode 100644 index 000000000..ab3b1ad6c --- /dev/null +++ b/examples/servers/system_prompt.py @@ -0,0 +1,28 @@ +from mcp.server import FastMCP + +# from dotenv import load_dotenv +import os +from mcp.server.fastmcp import FastMCP +from mcp.server.fastmcp.prompts import base as mcp_messages + +mcp = FastMCP("weather") # No reason to initialize stateless + + + + +@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. 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.""") + + +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) \ No newline at end of file diff --git a/src/mcp/server/fastmcp/prompts/base.py b/src/mcp/server/fastmcp/prompts/base.py index b45cfc917..ba7093347 100644 --- a/src/mcp/server/fastmcp/prompts/base.py +++ b/src/mcp/server/fastmcp/prompts/base.py @@ -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): @@ -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) @@ -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] diff --git a/src/mcp/types.py b/src/mcp/types.py index 4a9c2bf1a..a0397c32b 100644 --- a/src/mcp/types.py +++ b/src/mcp/types.py @@ -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] From 0ab40afb949d75cccd228a5f949c9a7c5899ba40 Mon Sep 17 00:00:00 2001 From: syedmuneeb321 <130203424+syedmuneeb321@users.noreply.github.com> Date: Fri, 27 Jun 2025 15:00:10 +0500 Subject: [PATCH 2/4] update: Added a system prompt class for returning system messages; now users can directly import and use it and fix ruff format --- examples/servers/system_prompt.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/examples/servers/system_prompt.py b/examples/servers/system_prompt.py index ab3b1ad6c..522e982bb 100644 --- a/examples/servers/system_prompt.py +++ b/examples/servers/system_prompt.py @@ -1,28 +1,26 @@ -from mcp.server import FastMCP - -# from dotenv import load_dotenv -import os from mcp.server.fastmcp import FastMCP from mcp.server.fastmcp.prompts import base as mcp_messages -mcp = FastMCP("weather") # No reason to initialize stateless - - +mcp = FastMCP("weather") @mcp.prompt() def weather_system_prompt() -> mcp_messages.SystemMessage: """ - Creates a prompt asking an AI to weather + Creates a prompt asking an AI to weather Args: None: None """ - - 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.""") + + return mcp_messages.SystemMessage( + "You are a helpful 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) \ No newline at end of file + + uvicorn.run("system_prompt:mcp_app", host="0.0.0.0", port=8002, reload=True) From 35a97b9d222acbd9b56cb0e1ded6cabd834208e1 Mon Sep 17 00:00:00 2001 From: syedmuneeb321 <130203424+syedmuneeb321@users.noreply.github.com> Date: Fri, 27 Jun 2025 15:00:33 +0500 Subject: [PATCH 3/4] Update system_prompt.py --- examples/servers/system_prompt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/servers/system_prompt.py b/examples/servers/system_prompt.py index 522e982bb..5d9667e63 100644 --- a/examples/servers/system_prompt.py +++ b/examples/servers/system_prompt.py @@ -13,7 +13,7 @@ def weather_system_prompt() -> mcp_messages.SystemMessage: """ return mcp_messages.SystemMessage( - "You are a helpful agent. You answer questions clearly and simply. " + "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." ) From 9be8fae4f5ee28170e0e6f5725cf66fc36edeb43 Mon Sep 17 00:00:00 2001 From: syedmuneeb321 <130203424+syedmuneeb321@users.noreply.github.com> Date: Fri, 27 Jun 2025 15:09:24 +0500 Subject: [PATCH 4/4] update: fix format and update prompt --- examples/servers/system_prompt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/servers/system_prompt.py b/examples/servers/system_prompt.py index 5d9667e63..95da5235c 100644 --- a/examples/servers/system_prompt.py +++ b/examples/servers/system_prompt.py @@ -13,7 +13,7 @@ def weather_system_prompt() -> mcp_messages.SystemMessage: """ return mcp_messages.SystemMessage( - "You are a helpful weather agent. You answer questions clearly and simply. " + "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." )