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

Listing Prompts from the Client

Your currency server has been written to currency_server.py with a tool, a resource, and a prompt. Create a client that lists the prompts available on this server so you can discover what prompt templates the LLM can use.

Bu egzersiz

Introduction to Model Context Protocol (MCP)

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

Egzersiz talimatları

  • Connect to the MCP server, initialize the session, and call the method that lists available prompts, assigning the result to a variable.
  • Print each prompt's name (the function name from the server, not the title).

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_prompts():
    """List all available prompts 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()

            # List available prompts
            prompts = await session.____()
            print(f"Available prompts: {[p.____ for p in prompts.prompts]}")

            return prompts.prompts

asyncio.run(list_prompts())
Kodu Düzenle ve Çalıştır