Skip to main content
Glama
jikime

Python MCP Korea Weather Service

get_forecast

Retrieve current weather conditions and 6-hour forecasts for specific Korean locations using Korea Meteorological Administration data. Provides temperature, precipitation, humidity, wind, and sky conditions based on grid coordinates.

Instructions

한국 기상청의 초단기예보 API를 호출하여 특정 지역의 날씨 예보 정보를 제공합니다. 사용자가 입력한 지역 정보와 격자 좌표를 바탕으로 현재 시점에서의 기상 정보를 조회합니다. 이 도구는 온도, 강수량, 하늘상태, 습도, 풍향, 풍속 등 상세한 기상 정보를 포함하며, 6시간 이내의 단기 예보를 제공합니다.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
cityYes
guYes
dongYes
nxYes
nyYes

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • src/server.py:55-58 (registration)
    MCP tool registration decorator for the 'get_forecast' tool, specifying name and detailed description of inputs and functionality.
    @mcp.tool(
        name="get_forecast",
        description="한국 기상청의 초단기예보 API를 호출하여 특정 지역의 날씨 예보 정보를 제공합니다. 사용자가 입력한 지역 정보와 격자 좌표를 바탕으로 현재 시점에서의 기상 정보를 조회합니다. 이 도구는 온도, 강수량, 하늘상태, 습도, 풍향, 풍속 등 상세한 기상 정보를 포함하며, 6시간 이내의 단기 예보를 제공합니다."
    )
  • The tool handler function decorated by @mcp.tool. It receives city, gu, dong, nx, ny parameters and delegates execution to the get_forecast_api helper.
    async def get_forecast(city: str, gu: str, dong: str, nx: int, ny: int) -> str:
        """Get weather forecast for a location.
        
        Args:
            city: City Name (e.g. 서울특별시)
            gu: Gu Name (e.g. 서초구)
            dong: Dong Name (e.g. 양재1동)
            nx: Grid X coordinate
            ny: Grid Y coordinate
        """
        return await get_forecast_api(city, gu, dong, nx, ny)
  • Core handler implementation in get_forecast_api: authenticates with API key, constructs URL for ultra-short forecast API using grid coordinates, fetches data asynchronously, parses items, maps weather codes (SKY, PTY, T1H, REH, VEC, WSD) to Korean descriptions, formats per time slot, handles errors.
    async def get_forecast_api(city: str, gu: str, dong: str, nx: float, ny: float) -> str:
      try:
        serviceKey = os.environ.get("KO_WEATHER_API_KEY")
        if not serviceKey:
          raise ValueError("KO_WEATHER_API_KEY 환경변수가 설정되지 않았습니다.")
        
        
        base_date = datetime.now().strftime("%Y%m%d") # 발표 일자
        base_time = datetime.now().strftime("%H%M") # 발표 시간
        # nx = '62' # 예보 지점 x좌표
        # ny = '123' # 예보 지점 y좌표
        
        # 알고 싶은 시간
        input_d = datetime.strptime(base_date + base_time, "%Y%m%d%H%M" )
        
        # 실제 입력 시간
        input_d = datetime.strptime(base_date + base_time, "%Y%m%d%H%M" ) - timedelta(hours=1)
        
        input_datetime = datetime.strftime(input_d, "%Y%m%d%H%M")
        input_date = input_datetime[:-4]
        input_time = input_datetime[-4:]
        
        # url
        url = f"http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getUltraSrtFcst?serviceKey={serviceKey}&numOfRows=60&pageNo=1&dataType=json&base_date={input_date}&base_time={input_time}&nx={nx}&ny={ny}"
        
        data = await make_api_request(url)
        
        if not data:
          raise ValueError("API 요청 결과가 비어있습니다.")
        
        if 'response' not in data:
          raise KeyError("API 응답에 'response' 항목이 없습니다.")
        
        if 'body' not in data['response']:
          raise KeyError("API 응답에 'body' 항목이 없습니다.")
        
        if 'items' not in data['response']['body']:
          raise KeyError("API 응답에 'items' 항목이 없습니다.")
        
        if 'item' not in data['response']['body']['items']:
          raise KeyError("API 응답에 'item' 항목이 없습니다.")
        
        res = data['response']['body']['items']['item']
        if not res:
          return [f"{city} {gu} {dong} 지역의 날씨 정보를 찾을 수 없습니다."]
    
        informations = dict()
        for items in res:
          try:
            cate = items['category']
            fcstTime = items['fcstTime']
            fcstValue = items['fcstValue']
            
            if fcstTime not in informations.keys():
              informations[fcstTime] = dict()
            
            informations[fcstTime][cate] = fcstValue
          except KeyError as e:
            print(f"날씨 데이터 항목 누락: {e}")
            continue
    
        if not informations:
          return [f"{city} {gu} {dong} 지역의 날씨 정보를 처리할 수 없습니다."]
    
        forecasts = []
        for key, val in zip(informations.keys(), informations.values()):
          features = dict()
          try:
            template = f"""{base_date[:4]}년 {base_date[4:6]}월 {base_date[-2:]}일 {key[:2]}시 {key[2:]}분 {city} {gu} {dong} 지역의 날씨는 """
            
            # 하늘 상태
            if 'SKY' in val and val['SKY']:
              try:
                sky_temp = sky_code[int(val['SKY'])]
                features['sky'] = sky_temp
              except (ValueError, KeyError):
                print(f"하늘 상태 코드 처리 오류: {val['SKY']}")
            
            # 강수 형태
            if 'PTY' in val and val['PTY']:
              try:
                pty_temp = pyt_code[int(val['PTY'])]
                features['rain'] = pty_temp
                template += pty_temp
                # 강수 있는 경우
                if 'RN1' in val and val['RN1'] != '강수없음':
                  rn1_temp = val['RN1']
                  # template += f"시간당 {rn1_temp}mm "
                  features['rain'] = rn1_temp
              except (ValueError, KeyError):
                print(f"강수 형태 코드 처리 오류: {val['PTY']}")
            
            # 기온
            if 'T1H' in val and val['T1H']:
              try:
                t1h_temp = float(val['T1H'])
                # template += f" 기온 {t1h_temp}℃ "
                features['temp'] = t1h_temp
              except ValueError:
                print(f"기온 값 처리 오류: {val['T1H']}")
            
            # 습도
            if 'REH' in val and val['REH']:
              try:
                reh_temp = float(val['REH'])
                # template += f"습도 {reh_temp}% "
                features['humidity'] = reh_temp
              except ValueError:
                print(f"습도 값 처리 오류: {val['REH']}")
            
            # 풍향/ 풍속
            if 'VEC' in val and val['VEC'] and 'WSD' in val and val['WSD']:
              try:
                vec_temp = deg_to_dir(float(val['VEC']))
                wsd_temp = val['WSD']
                # template += f"풍속 {vec_temp} 방향 {wsd_temp}m/s"
                features['wind_direction'] = vec_temp
                features['wind_speed'] = wsd_temp
              except ValueError:
                print(f"풍향/풍속 값 처리 오류: VEC={val.get('VEC')}, WSD={val.get('WSD')}")
            
            forecasts.append(template + format_weather_features(features))
          except Exception as e:
            print(f"날씨 정보 처리 중 오류 발생: {e}")
            continue
    
        if not forecasts:
          return f"{city} {gu} {dong} 지역의 날씨 정보를 생성할 수 없습니다."
        
        print(forecasts)
        return "\n---\n".join(forecasts)
        
      except Exception as e:
        print(f"날씨 API 요청 중 오류 발생: {e}")
        return [f"날씨 정보를 가져오는 중 오류가 발생했습니다: {str(e)}"]
  • format_weather_features: utility to convert raw weather dict into formatted Korean strings for sky, rain, temp, humidity, wind.
    def format_weather_features(features: dict) -> str:
      formatted_features = []
      for key, value in features.items():
        if key == 'sky':
          formatted_features.append(f"하늘 상태: {value}")
        elif key == 'rain':
          formatted_features.append(f"강수 형태: {value}")
        elif key == 'rain_amount':
          formatted_features.append(f"강수량: {value}mm")
        elif key == 'temp':
          formatted_features.append(f"기온: {value}℃")
        elif key == 'humidity':
          formatted_features.append(f"습도: {value}%")
        elif key == 'wind_direction':
          formatted_features.append(f"풍향: {value}")
        elif key == 'wind_speed':
          formatted_features.append(f"풍속: {value}m/s")
      return "\n".join(formatted_features)
  • make_api_request: async HTTP client utility using httpx to GET the weather API URL with user-agent and timeout, returns JSON or None on error.
    async def make_api_request(url: str) -> dict[str, Any] | None:
        """Make a request to the API with proper error handling."""
        headers = {
            "User-Agent": USER_AGENT,
            "Accept": "application/json"
        }
        async with httpx.AsyncClient() as client:
            try:
                response = await client.get(url, headers=headers, timeout=30.0)
                response.raise_for_status()
                return response.json()
            except Exception:
                return None
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It describes what the tool does (calls an API, provides weather data) and the timeframe (within 6 hours), but lacks critical behavioral information such as rate limits, authentication requirements, error handling, response format details, or whether this is a read-only operation. For a tool that calls an external API with 5 required parameters, this represents significant gaps in behavioral transparency.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is reasonably concise with three sentences that each add value. The first sentence establishes the core functionality, the second explains the input basis, and the third details the output content and timeframe. There's no redundant information, and the structure flows logically from purpose to implementation to output details.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's moderate complexity (5 required parameters, external API call) and the presence of an output schema, the description provides basic contextual information about what the tool does and what data it returns. However, with no annotations and poor parameter documentation, it lacks sufficient information about behavioral aspects, parameter usage, and differentiation from sibling tools. The output schema existence reduces the need to describe return values, but other gaps remain significant.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters2/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 0% schema description coverage for all 5 parameters, the description provides minimal parameter semantics. It mentions that the tool uses 'region information and grid coordinates' as input, which vaguely corresponds to the city, gu, dong, nx, and ny parameters, but doesn't explain what each parameter represents, their relationships, valid values, or how they should be formatted. The description fails to compensate for the complete lack of schema documentation.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: it calls the Korea Meteorological Administration's ultra-short-term forecast API to provide weather forecast information for a specific region. It specifies the data source, timeframe (within 6 hours), and types of weather information included (temperature, precipitation, sky conditions, humidity, wind direction, wind speed). However, it doesn't explicitly differentiate from the sibling tool 'get_grid_location' beyond mentioning grid coordinates as input.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. While it mentions using grid coordinates and region information as input, it doesn't explain when this tool is appropriate compared to the sibling 'get_grid_location' or other potential weather tools. There's no mention of prerequisites, limitations, or specific use cases.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jikime/py-mcp-ko-weather'

If you have feedback or need assistance with the MCP directory API, please join our Discord server