Перелік ресурсів з клієнта
Ваш сервер записано у файл currency_server.py, і тепер він готовий до підключення! Спершу створіть клієнт, який зможе перелічити доступні на цьому сервері ресурси.
Ця вправа є частиною курсу
Вступ до Model Context Protocol (MCP)
Інструкції до вправи
- Усередині контексту сеансу сервера отримайте доступні ресурси.
- Проітеруйте
response.resourcesі виведіть URI, назву та опис кожного ресурсу, використовуючи їхні атрибути.
Інтерактивна практична вправа
Спробуйте виконати цю вправу, доповнивши цей зразок коду.
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())