Adicionando Docstrings e Dicas de Tipo
É hora de tornar sua ferramenta convert_currency() mais fácil de usar por LLMs com docstrings e type hints. Sem isso, o LLM pode não conseguir escolher qual ferramenta chamar de forma eficaz, ou pode passar valores para os argumentos de maneira incorreta — ambos resultam em desempenho pouco confiável do aplicativo!
Um servidor MCP já foi instanciado usando FastMCP e atribuído a mcp.
Este exercicio faz parte do curso
Introdução ao Model Context Protocol (MCP)
Instruções do exercicio
- Adicione tipos apropriados aos argumentos da função e ao objeto de retorno.
- Complete a docstring para corresponder os três argumentos da função às suas definições.
exercicio interativo prático
Tente este exercicio completando este código de exemplo.
# Adding typing to the function arguments and return object
@mcp.tool()
def convert_currency(amount: ____, from_currency: ____, to_currency: ____) -> ____:
# Complete the docstring with the function arguments
"""
Convert an amount from one currency to another using current exchange rates.
Args:
____: The amount to convert
____: Source currency code (e.g., 'USD', 'EUR', 'GBP')
____: Target currency code (e.g., 'USD', 'EUR', 'GBP')
Returns:
A string with the conversion result and exchange rate
"""
url = f"https://api.frankfurter.dev/v1/latest?base={from_currency}&symbols={to_currency}"
response = requests.get(url)
data = response.json()
rate = data['rates'].get(to_currency)
if rate is None:
return f"Could not find exchange rate for {from_currency} to {to_currency}"
converted_amount = amount * rate
return f"{amount} {from_currency} = {converted_amount:.2f} {to_currency} (Rate: {rate})"
print(convert_currency(amount=100, from_currency="EUR", to_currency="USD"))