시작하기무료로 시작하기

MCP 서버 리소스 정의하기

여러분은 환율 변환 MCP 서버를 확장하여, 사용 중인 API가 지원하는 통화 목록에 접근할 수 있도록 하려 합니다. 유럽중앙은행(European Central Bank) 은 통화 코드 목록을 currencies.txt 파일로 제공하며, 이 파일은 서버 디렉터리에 있습니다. 클라이언트는 이 파일을 활용해 LLM이 도구 함수에 올바른 인수 값을 전달하는지 확인할 수 있습니다.

이번 과제에서는 currencies.txt의 내용을 읽는 MCP 리소스 get_currencies()를 정의해 보세요.

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

Model Context Protocol (MCP) 입문

강의 보기

연습 안내

  • 올바른 데코레이터와 "file://currencies.txt" URI를 사용하여 get_currencies() 함수를 리소스로 변환하세요.
  • currencies.txt 파일을 열고 내용을 읽도록 get_currencies() 함수를 완성하세요.
  • get_currencies()를 호출한 결과를 출력하여 리소스가 올바르게 작동하는지 확인하세요.

실습형 인터랙티브 연습

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

from mcp.server.fastmcp import FastMCP

mcp = FastMCP("Currency Converter")

# Define a resource for the currencies file
____
def get_currencies() -> str:
    """
    Get the list of currency names published by the European Central Bank for currency conversion.

    Returns:
        Contents of the currencies.txt file with currency names
    """
    # Open currencies.txt and read the data
    try:
        with open('____', 'r') as f:
            content = f.____
        return content
    except FileNotFoundError:
        return "currencies.txt file not found"

# Test the resource function
print(____)
코드 편집 및 실행