MCP सर्वरों में प्रॉम्प्ट्स

Model Context Protocol (MCP) परिचय

James Chapman

AI Curriculum Manager, DataCamp

MCP प्रॉम्प्ट्स क्या हैं?

 

प्रॉम्प्ट्स: पुन: प्रयोज्य टेम्पलेट जो विशिष्ट कार्यों के लिए LLMs को अनुकूलित करते हैं

  • वर्कफ़्लो और व्यवहार
  • उपयोगकर्ताओं पर प्रॉम्प्ट-भार घटाते हैं

प्रॉम्प्ट्स.png

Model Context Protocol (MCP) परिचय

प्रॉम्प्टिंग की आवश्यकता

अस्पष्ट_इनपुट्स.png

Model Context Protocol (MCP) परिचय

टाइमज़ोन कन्वर्टर प्रॉम्प्ट

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.

→ इस प्रॉम्प्ट को टाइमज़ोन रूपांतरण कार्य हेतु उपलब्ध कराएँ

Model Context Protocol (MCP) परिचय

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}"""
Model Context Protocol (MCP) परिचय

लोकल MCP सर्वर: 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")
Model Context Protocol (MCP) परिचय

क्लाइंट: प्रॉम्प्ट्स सूचीबद्ध करना

async def list_prompts():
    """List all available prompts from the MCP server."""
    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()

# List available prompts prompts = await session.list_prompts() print(f"Available prompts: {[p.name for p in prompts.prompts]}")
Model Context Protocol (MCP) परिचय

क्लाइंट: प्रॉम्प्ट्स सूचीबद्ध करना

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

प्रॉम्प्ट का name, title के समान नहीं होता

@mcp.prompt(title="Timezone Conversion")
def convert_timezone_prompt(timezone_request: str) -> str:
    # ...
Model Context Protocol (MCP) परिचय

क्लाइंट: प्रॉम्प्ट्स सूचीबद्ध करना

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

प्रॉम्प्ट का name, title के समान नहीं होता

# List available prompts
prompts = await session.list_prompts()
print(f"Available prompts: {[p.name for p in prompts.prompts]}")
  • क्लाइंट में .name उपयोग किया (.title नहीं)
Model Context Protocol (MCP) परिचय

क्लाइंट: प्रॉम्प्ट प्राप्त करना

async def read_prompt(user_input: str, prompt_name: str = "convert_timezone_prompt") -> str:
    """Retrieve a prompt from the MCP server with user input."""
    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()

# Retrieve the 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
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?
Model Context Protocol (MCP) परिचय

आइए अभ्यास करें!

Model Context Protocol (MCP) परिचय

Preparing Video For Download...