शुरू करेंमुफ़्त में शुरू करें

MCP सर्वर्स में LLM टूल उपयोग

आपने एक MCP सर्वर बनाया है जिसमें करेंसी कन्वर्ज़न का टूल है, जो अप-टू-डेट एक्सचेंज रेट्स का उपयोग करता है. इसे किसी LLM से इंटीग्रेट करने पर यह करेंसीज़ और एक्सचेंज रेट्स से जुड़े प्रश्नों के सटीक उत्तर दे पाएगा — जो यह डिफ़ॉल्ट रूप से नहीं कर सकता.

यहाँ अधिकतर कोड आपको दिया गया है, क्योंकि फोकस सिंटैक्स पर नहीं बल्कि वर्कफ़्लो को समझने पर होना चाहिए.

यह अभ्यास पाठ्यक्रम का हिस्सा है

Model Context Protocol (MCP) परिचय

पाठ्यक्रम देखें

अभ्यास निर्देश

  • यूज़र क्वेरी (user_query) और टूल्स की फ़ॉर्मैटेड लिस्ट (anthropic_tools) Claude को भेजें.
  • टूल यूज़ ब्लॉक से निकाले गए नाम और आर्ग्युमेंट्स का उपयोग करके, LLM द्वारा चुने गए MCP टूल को कॉल करें.
  • अंतिम प्रतिक्रिया के लिए परिणाम (result) दोबारा Claude को भेजें.

इंटरैक्टिव व्यावहारिक अभ्यास

इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।

from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client

async def call_anthropic_llm(user_query: str):
    """Call Claude with MCP tools."""
    
    print(f"\nUser: {user_query}\n")

    mcp_tools = await get_tools_from_mcp()
    
    anthropic_tools = []
    for tool in mcp_tools:
        anthropic_tool = {
            "name": tool.name,
            "description": tool.description or "",
            "input_schema": tool.inputSchema,
        }
        anthropic_tools.append(anthropic_tool)
    
    # Send the user query and formatted tools to the LLM
    client = AsyncAnthropic(api_key="")

    response = await client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=1024,
        messages=____,
        tools=____,
    )

    if response.stop_reason == "tool_use":
        tool_use = next(block for block in response.content if block.type == "tool_use")
        name = tool_use.name
        args = tool_use.input

        print(f"Model decided to call: {name}")
        print(f"Arguments: {args}\n")

        # Call the MCP tool
        result = await call_mcp_tool(____, ____)

        # Send the result back to Claude for the final response
        followup = await client.messages.create(
            model="claude-sonnet-4-6",
            max_tokens=1024,
            messages=[
                {"role": "user", "content": user_query},
                {"role": "assistant", "content": response.content},
                {
                    "role": "user",
                    "content": [
                        {
                            "type": "tool_result",
                            "tool_use_id": tool_use.id,
                            "content": ____,
                        }
                    ],
                },
            ],
            tools=anthropic_tools,
        )

        final_text = next((block.text for block in followup.content if block.type == "text"), None)
        if final_text:
            print(f"\nAssistant: {final_text}")
            return str(final_text)
        else:
            print("No follow-up message from model.")

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


if __name__ == "__main__":
    asyncio.run(call_anthropic_llm("How much is 250 US dollars in euros?"))
कोड संपादित करें और चलाएँ