Začněte nyníZačněte zdarma

Tvůj první MCP server

Čas si to vyzkoušet na vlastním MCP serveru! Veškerý kód jsme ti připravili, podrobněji si ho vysvětlíme v dalším videu. Nejdřív se ale podívej, jak kód funguje:

  1. Instance MCP serveru se definuje pomocí FastMCP().
  2. Napíše se funkce nástroje (convert_currency()), která provede určitou akci – v tomto případě načte informace o měnách z Frankfurter API.
  3. Tato funkce se pomocí dekorátoru @mcp.tool() převede na nástroj MCP.

Toto cvičení je součástí kurzu

Introduction to Model Context Protocol (MCP)

Zobrazit kurz

Pokyny k cvičení

  • Prohlédni si připravený kód a zjisti, jak se funkce převede na nástroj MCP serveru.
  • Na řádku 43 otestuj nástroj MCP – zadej libovolnou částku a vyber měny, mezi kterými chceš převádět.

Poznámka: Je potřeba použít oficiální kódy měn, například GBP pro britskou libru.

Interaktivní cvičení na vyzkoušení si v praxi

Vyzkoušejte si toto cvičení dokončením tohoto ukázkového kódu.

# Create an MCP server instance
mcp = FastMCP("Currency Converter")

@mcp.tool()
def convert_currency(amount: float, from_currency: str, to_currency: str) -> str:
    """
    Convert an amount from one currency to another using current exchange rates.

    Args:
        amount: The amount to convert
        from_currency: Source currency code (e.g., 'USD', 'EUR', 'GBP')
        to_currency: Target currency code (e.g., 'USD', 'EUR', 'GBP')

    Returns:
        A string with the conversion result and exchange rate
    """
    # API endpoint for Frankfurter
    url = f"https://api.frankfurter.dev/v1/latest?base={from_currency}&symbols={to_currency}"

    try:
        # Make the API request
        response = requests.get(url)
        response.raise_for_status()

        # Parse the response
        data = response.json()

        # Get the exchange rate
        rate = data['rates'].get(to_currency)

        if rate is None:
            return f"Could not find exchange rate for {from_currency} to {to_currency}"

        # Calculate the converted amount
        converted_amount = amount * rate

        return f"{amount} {from_currency} = {converted_amount:.2f} {to_currency} (Rate: {rate})"

    except requests.exceptions.RequestException as e:
        return f"Error converting currency: {str(e)}"

print("Testing Currency Converter:")
result = convert_currency(amount=100, from_currency="USD", to_currency="EUR")
print(result)
Upravit a spustit kód