始める無料で始める

MCP サーバープロンプトの定義

再利用可能なプロンプトを通貨換算 MCP サーバーに追加することで、LLM が換算リクエストを適切に処理し、必要に応じて確認を求める(例:金額や通貨コードが不足している場合など)ようにします。これにより、ユーザーの負担を軽減し、アシスタントの信頼性を高めることができます。

MCP サーバーのインスタンスはすでに作成されています。@mcp.prompt() デコレータと、ユーザーのリクエストを含むプロンプトテンプレートを返す関数を使って、プロンプトを追加しましょう。

この演習はコースの一部です

Model Context Protocol(MCP)入門

コースを見る

演習の手順

  • @mcp.prompt() で関数をデコレートし、タイトルを "Currency Conversion" に設定します。
  • currency_request: str を引数として受け取り、プロンプトテンプレートとユーザーのリクエストを含む文字列を返す関数を定義します。
  • サンプルのリクエストでプロンプト関数を呼び出し、正しく動作するか結果を出力して確認しましょう。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

# Define a prompt for currency conversion
____
def ____(currency_request: str) -> str:
    return f"""You are a currency conversion assistant.

Your task is to:
1. Extract the amount and source currency from the user's natural language input.
2. Identify the target currency.
3. Use the conversion tool to convert the amount.

Rules:
- If the amount or currencies are ambiguous or missing, ask the user for clarification.
- Use only supported currency codes (e.g., USD, EUR, GBP).

User's currency conversion request: {currency_request}"""

# Test the prompt function
print(____("100 USD to EUR"))
コードを編集して実行