첫 번째 MCP 서버 구축

Model Context Protocol (MCP) 입문

James Chapman

AI Curriculum Manager, DataCamp

시간대 변환

 

시간대 서버

1 https://opentimezone.com/
Model Context Protocol (MCP) 입문

MCP 코드 흐름

Model Context Protocol (MCP) 입문

1단계: 서버 인스턴스 생성

from mcp.server.fastmcp import FastMCP
import requests

# Create an MCP server instance
mcp = FastMCP("Timezone Converter")
1 https://github.com/modelcontextprotocol/python-sdk
Model Context Protocol (MCP) 입문

2단계: 도구 추가

from mcp.server.fastmcp import FastMCP
import requests

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

@mcp.tool()
def convert_timezone(date_time, from_timezone, to_timezone):
Model Context Protocol (MCP) 입문

2단계: 도구 추가

@mcp.tool()
def convert_timezone(date_time, from_timezone, to_timezone):

    # Define the API endpoint and format input data

url = "https://api.opentimezone.com/convert" payload = {"dateTime": date_time, "fromTimezone": from_timezone, "toTimezone": to_timezone}
response = requests.post(url, json=payload)
data = response.json() converted_time = data.get('dateTime', 'N/A')
return f"Time in {to_timezone}: {converted_time}"
Model Context Protocol (MCP) 입문

3단계: 독스트링 및 타입 힌트

@mcp.tool()
def convert_timezone(date_time: str, from_timezone: str, to_timezone: str) -> str:
    """
    Convert a datetime from one timezone to another.

    Args:
        date_time: The datetime string in ISO format (e.g., '2025-01-20T14:30:00')
        from_timezone: Source timezone (e.g., 'America/New_York')
        to_timezone: Target timezone (e.g., 'Europe/London')

    Returns:
        A string with the converted datetime and timezone information
    """
Model Context Protocol (MCP) 입문

MCP 코드 흐름

Model Context Protocol (MCP) 입문

연습해 봅시다!

Model Context Protocol (MCP) 입문

Preparing Video For Download...