시작하기무료로 시작하기

Docstring과 타입 힌트 추가하기

이제 docstring타입 힌트를 사용해 convert_currency() 도구를 LLM이 더 쉽게 활용할 수 있도록 만들어 봅시다. 이 정보가 없으면 LLM이 어떤 도구를 호출해야 할지 제대로 판단하지 못하거나, 인수에 잘못된 값을 전달할 수 있습니다. 두 경우 모두 애플리케이션의 신뢰성을 떨어뜨리는 원인이 됩니다!

FastMCP를 사용해 MCP 서버가 이미 생성되었으며, mcp에 할당되어 있습니다.

이 연습은 강의의 일부입니다

Model Context Protocol (MCP) 입문

강의 보기

연습 안내

  • 함수 인수와 반환 객체에 적절한 타입을 추가하세요.
  • 세 가지 함수 인수와 그 설명이 일치하도록 docstring을 완성하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# 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"))
코드 편집 및 실행