क्लाइंट से Resources सूचीबद्ध करना
आपका सर्वर फ़ाइल currency_server.py में लिखा जा चुका है और अब कनेक्ट होने के लिए तैयार है! सबसे पहले, ऐसा क्लाइंट बनाइए जो इस सर्वर पर उपलब्ध resources को सूचीबद्ध कर सके.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Model Context Protocol (MCP) परिचय
अभ्यास निर्देश
- सर्वर सेशन कॉन्टेक्स्ट के अंदर, उपलब्ध resources प्राप्त करें.
response.resourcesपर loop चलाएँ और हर resource की URI, name, और description उनके attributes का उपयोग करके प्रिंट करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
async def list_resources():
"""List all available resources from the MCP server."""
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()
# Get the list of resources
response = ____ ____()
print("Available resources:")
# Print each resource's URI, name, and description
for resource in response.____:
print(f" - {resource.____}")
print(f" Name: {resource.____}")
print(f" Description: {resource.____}")
return response.resources
asyncio.run(list_resources())