Get startedGet started for free

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.

This exercise is part of the course

Introduction to Model Context Protocol (MCP)

View Course

Exercise 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.

Hands-on interactive exercise

Have a go at this exercise by completing this sample 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"))
Edit and Run Code