Skip to content

Commit 3bd7df8

Browse files
committed
docs: add prompt client/server example to address issue #498
1 parent 3d7b311 commit 3bd7df8

File tree

4 files changed

+94
-2
lines changed

4 files changed

+94
-2
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""Prompt client example: list, inspect, and get prompts from a server.
2+
3+
cd to the `examples/snippets` directory and run:
4+
uv run prompt-client
5+
"""
6+
7+
import asyncio
8+
import os
9+
10+
from mcp import ClientSession, StdioServerParameters
11+
from mcp.client.stdio import stdio_client
12+
from mcp.types import TextContent
13+
14+
server_params = StdioServerParameters(
15+
command="uv",
16+
args=["run", "server", "prompt_server", "stdio"],
17+
env={"UV_INDEX": os.environ.get("UV_INDEX", "")},
18+
)
19+
20+
21+
async def run():
22+
"""Connect to the prompt server and exercise the prompts API."""
23+
async with stdio_client(server_params) as (read, write):
24+
async with ClientSession(read, write) as session:
25+
await session.initialize()
26+
27+
# 1. Discover which prompts the server exposes
28+
result = await session.list_prompts()
29+
print("Available prompts:")
30+
for prompt in result.prompts:
31+
args = ", ".join(f"{a.name}{'?' if not a.required else ''}" for a in (prompt.arguments or []))
32+
print(f" - {prompt.name}({args}): {prompt.description}")
33+
34+
# 2. Fetch the single-string prompt and print its message
35+
review = await session.get_prompt(
36+
"review_code",
37+
arguments={"code": "def add(a, b):\n return a + b"},
38+
)
39+
print("\nreview_code prompt messages:")
40+
for msg in review.messages:
41+
text = msg.content.text if isinstance(msg.content, TextContent) else str(msg.content)
42+
print(f" [{msg.role}] {text}")
43+
44+
# 3. Fetch the multi-turn prompt and print each message in the thread
45+
debug = await session.get_prompt(
46+
"debug_error",
47+
arguments={"error": "NameError: name 'x' is not defined"},
48+
)
49+
print("\ndebug_error prompt messages:")
50+
for msg in debug.messages:
51+
text = msg.content.text if isinstance(msg.content, TextContent) else str(msg.content)
52+
print(f" [{msg.role}] {text}")
53+
54+
55+
def main():
56+
"""Entry point for the prompt client."""
57+
asyncio.run(run())
58+
59+
60+
if __name__ == "__main__":
61+
main()

examples/snippets/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ direct-execution-server = "servers.direct_execution:main"
2222
display-utilities-client = "clients.display_utilities:main"
2323
oauth-client = "clients.oauth_client:run"
2424
elicitation-client = "clients.url_elicitation_client:run"
25+
prompt-client = "clients.prompt_client:main"

examples/snippets/servers/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ def run_server():
2020
"""
2121
if len(sys.argv) < 2:
2222
print("Usage: server <server-name> [transport]")
23-
print("Available servers: basic_tool, basic_resource, basic_prompt, tool_progress,")
24-
print(" sampling, elicitation, completion, notifications,")
23+
print("Available servers: basic_tool, basic_resource, basic_prompt, prompt_server,")
24+
print(" tool_progress, sampling, elicitation, completion, notifications,")
2525
print(" mcpserver_quickstart, structured_output, images")
2626
print("Available transports: stdio (default), sse, streamable-http")
2727
sys.exit(1)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Prompt server example showing both return styles.
2+
3+
Run from the repository root:
4+
uv run examples/snippets/servers/prompt_server.py
5+
"""
6+
7+
from mcp.server.mcpserver import MCPServer
8+
from mcp.server.mcpserver.prompts import base
9+
10+
mcp = MCPServer(name="Prompt Server")
11+
12+
13+
@mcp.prompt(title="Code Review")
14+
def review_code(code: str) -> str:
15+
"""Return a single string prompt asking the model to review code."""
16+
return f"Please review this code and suggest improvements:\n\n{code}"
17+
18+
19+
@mcp.prompt(title="Debug Assistant")
20+
def debug_error(error: str) -> list[base.Message]:
21+
"""Return a multi-turn conversation as a list of messages."""
22+
return [
23+
base.UserMessage("I'm seeing this error:"),
24+
base.UserMessage(error),
25+
base.AssistantMessage("I'll help debug that. What have you tried so far?"),
26+
]
27+
28+
29+
if __name__ == "__main__":
30+
mcp.run(transport="stdio")

0 commit comments

Comments
 (0)