MCP Server의 구조와 동작 원리를 직접 학습하기 위해 실습을 진행했다.
Claude Desktop은 기본적으로 실시간 외부 데이터를 불러오는 기능을 제공하지 않기 때문에, 이를 보완하고자 MCP Server를 적용하여 OpenWeatherMap API와 연동하였다. 이를 통해 Claude에서 실시간 날씨 정보를 가져올 수 있게 실습을 진행했다.
1. Python 패키지 매니저 uv 설치
- uv는 Python 패키지를 설치, 관리, 빌드, 배포까지 가능한 초고속 도구.
- 기존 pip 대비 최대 100배 빠른 속도를 자랑.
- 아래 명령어로 설치 가능:
curl -Ls <https://astral.sh/uv/install.sh> | sh
2. 날씨 API(OpenWeatherMap) 연결 테스트
- API Key 발급 후 날씨 데이터를 가져오는 코드로 테스트.
city:str = "Seoul"
apiKey = os.getenv("OPENWEATHERMAP_APIKEY")
lang:str = "kr"
units:str = 'metric' #화씨 온도를 섭씨 온도로 변경
api:str = f"<http://api.openweathermap.org/data/2.5/weather?q={city}&APPID={apiKey}&lang={lang}&units={units}>"
result = requests.get(api)
result = json.loads(result.text)
print(result)
3. MCP 서버 만들기
FastMCP 설치
uv add "mcp[cli]" httpx
MCP 주요 데코레이터 개념
- @mcp.resource(): 정적 데이터 정의 (예: 지역 목록, 요일 등)
- @mcp.tool: AI가 직접 호출 가능한 동작 정의 (예: 날씨 가져오기)
- @mcp.prompt: 출력 결과를 다듬기 위한 템플릿 정의
Claude에 MCP 서버 연결
- Claude 설정 폴더 이동:
cd ~/Library/Application\\ Support/Claude
- claude_desktop_config.json 수정:
{
"mcpServers": {
"weather": {
"command": "/Users/name/.local/bin/uv", // uv의 절대경로로 설정
"args": [
"--directory",
"/Users/name/desktop/project/MCP_learn",
"run",
"weather.py"
]
}
}
}
에러 발생 :
- spawn uv ENOENT 오류 → uv의 경로를 명확히 지정해야 함 (command -v uv로 경로 확인)
4. 프롬프트 문제 해결
문제 : @mcp.prompt() 를 통해 원하는 형태로 답변을 얻으려 했으나 실패.
원인 : @mcp.tool() 을 통해 얻은 값이 @mcp.prompt() 를 거치지 않는 문제였다.
해결 방법 : 반환값을 weather_forecast_prompt(forecast) 로 연결하여 워하는 값으로 나오게 했다.
@mcp.tool()
async def get_weather(location: str) -> dict:
"""특정 위치의 날씨 정보를 가져옵니다."""
...
return await weather_forecast_prompt(forecast)
@mcp.prompt()
async def weather_forecast_prompt(forecast: dict) -> str:
"""날씨 정보를 보기 좋게 포맷합니다."""
...
5. 결과
Claude Desktop에서 실시간 날씨 정보를 가져오는 MCP Server를 성공적으로 구현했다.
Github
GitHub - Byeong98/MCP_Learn
Contribute to Byeong98/MCP_Learn development by creating an account on GitHub.
github.com
'AI' 카테고리의 다른 글
MCP Clients 실습 - OpenAI (1) | 2025.06.24 |
---|---|
MCP란 무엇인가? (0) | 2025.05.09 |