सर्वर टूल्स कॉल करना
अब अपने क्लाइंट से सर्वर में टूल कॉल ट्रिगर करने का समय है! फिर से, आप asynchronous operations का उपयोग करेंगे ताकि अन्य operations के पूरा होने तक इंतज़ार करते समय कुछ भी टूटे या फ्रीज़ न हो.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Model Context Protocol (MCP) परिचय
अभ्यास निर्देश
- यूज़र द्वारा दिए गए
tool_nameऔरargumentsपैरामीटर्स के साथ टूल कॉल करें; यह सुनिश्चित करें कि कॉल सर्वर के जवाब का इंतज़ार करने के लिए उपयुक्त Python कीवर्ड का उपयोग करके रुके. - सर्वर रिस्पॉन्स की टेक्स्ट सामग्री निकालें और प्रिंट करें.
- वैध पैरामीटर्स के साथ
"convert_currency"टूल चलाएँ (यहाँ आप अपनी पसंद के values और currencies ले सकते हैं).
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
async def call_mcp_tool(tool_name: str, arguments: dict) -> str:
params = StdioServerParameters(
command=sys.executable,
args=["currency_server.py"],
)
async with stdio_client(params) as (reader, writer):
async with ClientSession(reader, writer) as session:
await session.initialize()
# Call the currency conversion tool
result = ____ ____(tool_name, arguments)
# Extract and print the text content of the server response
text_content = result.____[0].____
print(f"Conversion Result: {text_content}")
return text_content
# Run the "convert_currency" tool
asyncio.run(
call_mcp_tool("convert_currency",
{"amount": 250.0, "from_currency": "USD", "to_currency": "EUR"})
)