CommencerCommencer gratuitement

Defining an MCP Server Prompt

You're adding a reusable prompt to your currency converter MCP server so that an LLM knows how to handle conversion requests and when to ask for clarification (e.g., missing amount or currency codes). This reduces prompt-load on users and makes the assistant more reliable.

An MCP server instance is already created. Add a prompt using the @mcp.prompt() decorator and a function that returns the prompt template with the user's request appended.

Cet exercice fait partie du cours

Introduction to Model Context Protocol (MCP)

Afficher le cours

Instructions

  • Decorate a function with @mcp.prompt() and set the title to "Currency Conversion".
  • Define a function that takes currency_request: str and returns a string containing the prompt template and the user's request.
  • Call the prompt function with a sample request and print the result to verify it works.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# 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"))
Modifier et exécuter le code