Prompts in MCP-Servern

Einführung in das Model Context Protocol (MCP)

James Chapman

AI Curriculum Manager, DataCamp

Was sind MCP-Prompts?

 

Prompts: wiederverwendbare Vorlagen, die LLMs für bestimmte Aufgaben optimieren

  • Workflows und Verhaltensweisen
  • Reduziert die Prompt-Last für Nutzer

Prompts

Einführung in das Model Context Protocol (MCP)

Warum braucht es Prompts?

mehrdeutige Eingaben

Einführung in das Model Context Protocol (MCP)

Der Zeitzonen-Prompt

You are a timezone conversion engine.

Your task is to:
1. Extract the source datetime from the user's natural language input.
2. Identify the source timezone (explicit or inferred).
3. Convert the datetime into the target timezone.

Rules:
- If the date is ambiguous (e.g., "next Friday"), resolve it relative to the provided datetime.
- If the input cannot be resolved confidently, seek clarification.

→ Diesen Prompt für die Zeitzonenumrechnung bereitstellen

Einführung in das Model Context Protocol (MCP)

MCP-Server-Prompts definieren

@mcp.prompt(title="Timezone Conversion")

def convert_timezone_prompt(user_input: str) -> str:
return f"""You are a timezone conversion engine. Your task is to: 1. Extract the source datetime... Rules: - If the date is ambiguous (e.g., "next Friday"), resolve it relative to the provided datetime. - If the input cannot be resolved confidently, seek clarification. User's timezone conversion request: {user_input}"""
Einführung in das Model Context Protocol (MCP)

Lokaler MCP-Server: timezone_server.py

from mcp.server.fastmcp import FastMCP

mcp = FastMCP("Timezone Converter")

# Tools and resources from before...

@mcp.prompt(title="Timezone Conversion")
def convert_timezone_prompt(timezone_request: str) -> str:
    # ...

if __name__ == "__main__":
    mcp.run(transport="stdio")
Einführung in das Model Context Protocol (MCP)

Client: Prompts auflisten

async def list_prompts():
    """Alle verfügbaren Prompts vom MCP-Server auflisten."""
    params = StdioServerParameters(command=sys.executable, args=["timezone_server.py"])

    async with stdio_client(params) as (reader, writer):
        async with ClientSession(reader, writer) as session:
            await session.initialize()

# Verfügbare Prompts auflisten prompts = await session.list_prompts() print(f"Available prompts: {[p.name for p in prompts.prompts]}")
Einführung in das Model Context Protocol (MCP)

Client: Prompts auflisten

print(asyncio.run(list_prompts()))
Available prompts: ['convert_timezone_prompt']

Der Prompt-name ist nicht dasselbe wie der title

@mcp.prompt(title="Timezone Conversion")
def convert_timezone_prompt(timezone_request: str) -> str:
    # ...
Einführung in das Model Context Protocol (MCP)

Client: Prompts auflisten

print(asyncio.run(list_prompts()))
Available prompts: ['convert_timezone_prompt']

Der Prompt-name ist nicht dasselbe wie der title

# Verfügbare Prompts auflisten
prompts = await session.list_prompts()
print(f"Available prompts: {[p.name for p in prompts.prompts]}")
  • Im Client das Attribut .name verwendet (nicht .title)
Einführung in das Model Context Protocol (MCP)

Client: Prompts abrufen

async def read_prompt(user_input: str, prompt_name: str = "convert_timezone_prompt") -> str:
    """Einen Prompt mit Nutzereingabe vom MCP-Server abrufen."""
    params = StdioServerParameters(command=sys.executable, args=["timezone_server.py"])

    async with stdio_client(params) as (reader, writer):
        async with ClientSession(reader, writer) as session:
            await session.initialize()

            prompts = await session.list_prompts()

# Prompt abrufen if prompts.prompts: prompt = await session.get_prompt(prompt_name, arguments={"timezone_request": user_input})
print(f"Prompt result: {prompt.messages[0].content.text}") return prompt.messages[0].content.text
Einführung in das Model Context Protocol (MCP)
print(asyncio.run(read_prompt(user_input="It is 9:50 AM in the UK in January. What time is
    it in Lisbon, Portugal?")))
You are a timezone conversion engine.

Your task is to:
1. Extract the source datetime from the user's natural language input.
2. Identify the source timezone (explicit or inferred).
3. Convert the datetime into the target timezone.

Rules:
- If the date is ambiguous (e.g., "next Friday"), resolve it relative to the...
- If the input cannot be resolved confidently, seek clarification.

User's timezone conversion request: It is 9:50 AM in the UK in January. What time is it in
Lisbon, Portugal?
Einführung in das Model Context Protocol (MCP)

Lass uns üben!

Einführung in das Model Context Protocol (MCP)

Preparing Video For Download...