MCP और LLMs: प्रॉम्प्ट्स और रिसोर्सेस

Model Context Protocol (MCP) परिचय

James Chapman

AI Curriculum Manager, DataCamp

LLM फ्लो में रिसोर्सेस और प्रॉम्प्ट्स

 

  • Resources: केवल-पढ़ने योग्य संदर्भ
  • Prompts: निर्देश टेम्पलेट्स जो व्यवहार निर्धारित करें और कार्य हेतु मॉडल को ट्यून करें

 

Anthropic Messages API के साथ जारी रखें

 

LLM फ्लो प्रिमिटिव्स

Model Context Protocol (MCP) परिचय

प्रॉम्प्ट-रिसोर्स वर्कफ़्लो

 

प्रॉम्प्ट-रिसोर्स वर्कफ़्लो

Model Context Protocol (MCP) परिचय

प्रॉम्प्ट-रिसोर्स वर्कफ़्लो

 

प्रॉम्प्ट-रिसोर्स वर्कफ़्लो

Model Context Protocol (MCP) परिचय

प्रॉम्प्ट-रिसोर्स वर्कफ़्लो

 

प्रॉम्प्ट-रिसोर्स वर्कफ़्लो

Model Context Protocol (MCP) परिचय

प्रॉम्प्ट-रिसोर्स वर्कफ़्लो

 

प्रॉम्प्ट-रिसोर्स वर्कफ़्लो

Model Context Protocol (MCP) परिचय
from mcp.server.fastmcp import FastMCP

mcp = FastMCP("Timezone Converter")

@mcp.tool()
def convert_timezone(date_time: str, from_timezone: str, to_timezone: str) -> str:
    # ...

@mcp.resource("file://locations.txt")
def get_locations() -> str:
    # ...

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

if __name__ == "__main__":
    mcp.run(transport="stdio")
Model Context Protocol (MCP) परिचय

क्लाइंट हेल्पर फ़ंक्शंस

 

  • read_resource(resource_uri): URI से किसी रिसोर्स की सामग्री लाता है
  • read_prompt(prompt_name, user_input): यूज़र रिक्वेस्ट सहित प्रॉम्प्ट टेम्पलेट लाता है
Model Context Protocol (MCP) परिचय

1. रिसोर्स और प्रॉम्प्ट प्राप्त करें

async def get_context_from_mcp(user_query: str) -> tuple[str, str]:
    """Fetch resource content and prompt text 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()

# Get the resource (supported locations) resource_result = await session.read_resource("file://locations.txt") resource_text = resource_result.contents[0].text
# Get the prompt with the user's query prompt_result = await session.get_prompt("convert_timezone_prompt", arguments={"timezone_request": user_query}) prompt_text = prompt_result.messages[0].content.text
return resource_text, prompt_text
Model Context Protocol (MCP) परिचय

2. सिस्टम संदेश बनाएँ और LLM कॉल करें

async def call_llm_with_context(user_query: str):
    """Call the LLM with resource and prompt context from MCP."""

resource_text, prompt_text = await get_context_from_mcp(user_query)
# Combine prompt (task + rules + user request) with resource (supported locations) full_prompt = prompt_text + "\n\nSupported locations:\n" + resource_text
client = AsyncAnthropic(api_key="<ANTHROPIC_API_TOKEN>") response = await client.messages.create( model="claude-sonnet-4-6", max_tokens=1024, messages=[{"role": "user", "content": full_prompt}], tools=anthropic_tools, # from get_tools_from_mcp(), formatted for Anthropic )
Model Context Protocol (MCP) परिचय

3. प्रतिक्रिया संभालें

    if response.stop_reason == "end_turn":
        text = next((b.text for b in response.content if b.type == "text"), "")
        print(f"\nAssistant: {text}")
        return str(text)

if response.stop_reason == "tool_use": tool_use = next(b for b in response.content if b.type == "tool_use") result = await call_mcp_tool( tool_use.name, tool_use.input) # ... send the tool_result back in a follow-up # request, then print the reply, as before
Model Context Protocol (MCP) परिचय

उदाहरण: अस्पष्ट अनुरोध

if __name__ == "__main__":
    asyncio.run(call_llm_with_context("What time is it in Canada?"))
Assistant: Canada has several time zones. Which city or region do you mean?
For example, Toronto, Vancouver, or Halifax?
Model Context Protocol (MCP) परिचय

उदाहरण: स्पष्ट अनुरोध

if __name__ == "__main__":
    asyncio.run(call_llm_with_context("It is 9:50 AM in the UK in January. What
        time is it in Lisbon, Portugal?"))
Assistant: It's 9:50 AM in Lisbon as well.
Model Context Protocol (MCP) परिचय

रिकैप: LLM के साथ रिसोर्सेस और प्रॉम्प्ट्स

 

प्रॉम्प्ट-रिसोर्स वर्कफ़्लो

Model Context Protocol (MCP) परिचय

Ayo berlatih!

Model Context Protocol (MCP) परिचय

Preparing Video For Download...