Prompt di Server MCP

Pengantar Model Context Protocol (MCP)

James Chapman

AI Curriculum Manager, DataCamp

Apa itu Prompt MCP?

 

Prompt: templat ulang pakai yang mengoptimalkan LLM untuk tugas tertentu

  • Alur kerja dan perilaku
  • Mengurangi beban prompt pada pengguna

prompts.png

Pengantar Model Context Protocol (MCP)

Kebutuhan akan Prompting

ambiguous_inputs.png

Pengantar Model Context Protocol (MCP)

Prompt Konverter Zona Waktu

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.

→ Ekspos prompt ini untuk tugas konversi zona waktu

Pengantar Model Context Protocol (MCP)

Mendefinisikan Prompt Server MCP

@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}"""
Pengantar Model Context Protocol (MCP)

Server MCP Lokal: 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")
Pengantar Model Context Protocol (MCP)

Klien: Mendaftar Prompt

async def list_prompts():
    """Daftar semua prompt yang tersedia dari server MCP."""
    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()

# Daftar prompt yang tersedia prompts = await session.list_prompts() print(f"Available prompts: {[p.name for p in prompts.prompts]}")
Pengantar Model Context Protocol (MCP)

Klien: Mendaftar Prompt

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

name prompt tidak sama dengan title

@mcp.prompt(title="Timezone Conversion")
def convert_timezone_prompt(timezone_request: str) -> str:
    # ...
Pengantar Model Context Protocol (MCP)

Klien: Mendaftar Prompt

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

name prompt tidak sama dengan title

# Daftar prompt yang tersedia
prompts = await session.list_prompts()
print(f"Available prompts: {[p.name for p in prompts.prompts]}")
  • Di klien gunakan atribut .name (bukan .title)
Pengantar Model Context Protocol (MCP)

Klien: Mengambil Prompt

async def read_prompt(user_input: str, prompt_name: str = "convert_timezone_prompt") -> str:
    """Ambil prompt dari server MCP dengan masukan pengguna."""
    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()

# Ambil prompt 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
Pengantar 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?
Pengantar Model Context Protocol (MCP)

Ayo berlatih!

Pengantar Model Context Protocol (MCP)

Preparing Video For Download...