BaşlayınÜcretsiz Başlayın

Listing Resources from the Client

Your server has been written to the file currency_server.py, and is now ready to connect to! To start with, create a client that can list the resources available on this server.

Bu egzersiz

Introduction to Model Context Protocol (MCP)

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • Inside the server session context, retrieve the available resources.
  • Loop through response.resources and print each resource's URI, name, and description using their attributes.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

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())
Kodu Düzenle ve Çalıştır