본문 바로가기

소프트웨어-이야기/프로그래밍 언어와 프레임워크

FastMcp으로 Local MCP 개발하기 🤖

이 글은 YouTube 링크를 로컬 MP3 파일로 변환·다운로드하는 Local Mcp 연동 예제를 다룹니다.

(1) Python으로 Local Mcp 구현하기

준비물

  • Python 3.10 이상 버전 🐍
    • fastmcp를 포함한 최신 버전의 FastAPI 생태계 라이브러리들은 Python 3.10 이상을 필요로 한다. 

Local MCP 예제 코드

requirements.txt 추가

yt-dlp
fastmcp
fastapi
uvicorn[standard]
pydantic
httpx
setuptools
music_server.py 파일 추가
from fastmcp import FastMCP
from pathlib import Path
import yt_dlp

mcp = FastMCP("Music Server")


@mcp.tool
def download_youtube_as_mp3(url, output_path=None, skip_ads_ss=0):
    """
    유튜브 URL을 MP3로 다운로드합니다.

    Args:
        url (str): 유튜브 URL (단일 동영상 또는 플레이리스트)
        output_path (str): 다운로드할 경로 (기본값: 데스크탑)
        skip_ads_ss (int): 광고를 건너뛰고 N초부터 저장할지 여부

    Returns:
        bool: 성공 여부
    """

    if output_path is None:
        output_path = get_desktop_path()
    else:
        output_path = Path(output_path)

    # 출력 경로가 존재하지 않으면 생성
    output_path.mkdir(parents=True, exist_ok=True)

    # yt-dlp 옵션 설정
    ydl_opts = {
        'format': 'bestaudio/best',  # 최고 품질의 오디오 선택
        'outtmpl': str(output_path / '%(title)s.%(ext)s'),  # 출력 파일명 템플릿
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',  # 오디오 추출
            'preferredcodec': 'mp3',      # MP3 형식으로 변환
            'preferredquality': '192',    # 192kbps 품질
        }],
        'postprocessor_args': [
            '-ar', '44100',  # 샘플링 레이트 44.1kHz
        ],
        'prefer_ffmpeg': True,
        'keepvideo': False,  # 원본 비디오 파일 삭제
    }

    # --skip-ads 옵션이 활성화된 경우, FFmpeg에 시작 시간(-ss) 인자 추가
    if skip_ads_ss > 0:
        ydl_opts['postprocessor_args'].insert(0, '-ss')
        ydl_opts['postprocessor_args'].insert(1, '00:00:0'+skip_ads_ss)
        print("💡 "+str(skip_ads_ss)+"초부터 오디오를 저장합니다.")

    try:
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            print(f"다운로드 시작: {url}")
            ydl.download([url])
            print(f"다운로드 완료! 저장 위치: {output_path}")
            return True

    except Exception as e:
        print(f"다운로드 중 오류 발생: {str(e)}")
        return False

def get_desktop_path():
    """데스크탑 경로를 반환합니다."""
    home = Path.home()
    desktop = home / "Desktop"

    # Windows의 경우 바탕화면이 다른 언어로 되어있을 수 있음
    if not desktop.exists():
        desktop = home / "바탕화면"

    return desktop

if __name__ == "__main__":
    mcp.run()

(2) CladeDesktop에 MCP 등록하기

cluade_desktop_config.json 파일 수정

claude desktop > 개발자 > 로컬 MCP 서버 > 구성 편집 클릭.

{
  "mcpServers": {
     "music": {
      "command": "python3",
      "args": ["/Users/americanopeople/workspace/music-mcp/music_server.py"]
    }
  }
}

(3) 결과

다운로드 결과

(4) 참고

  • yt-dlp: 유튜브 영상을 다운받아서 mp3 으로 변환하는 python 라이브러리
  • fastmcp > claudedesktop: claude-desktop에 mcp 서버를 쉽게 등록하는 명령어
  • mcp-architecture: mcp 아키텍처를 이해할 수 있음
  • mcp-tools: mcp-server에 tool을 등록해야하는 배경을 알 수 있음