최근 AI 시스템과 외부 데이터 소스의 통합이 중요한 이슈로 부각되고 있습니다. **Model Context Protocol(MCP)**는 이러한 통합을 표준화하여 AI 모델이 외부 데이터에 효율적으로 접근할 수 있도록 돕는 프로토콜입니다. 이번 글에서는 MCP 사양에 맞게 서버와 클라이언트 애플리케이션을 구축하는 방법을 소개하겠습니다.
MCP란 무엇인가?
**MCP(Model Context Protocol)**는 AI 애플리케이션과 외부 데이터 소스 및 도구를 표준화된 방식으로 연결하는 개방형 프로토콜입니다. 이를 통해 AI 모델은 실시간 데이터에 접근하고, 다양한 도구를 활용하여 보다 정확하고 풍부한 응답을 제공할 수 있습니다. 
프로젝트 개요
이 프로젝트는 두 개의 주요 구성 요소로 이루어져 있습니다:
- MCP 서버 (mcp_server.py): MCP 사양에 따라 구현된 서버로, 다양한 파일 형식의 문서를 읽고 데이터를 제공합니다.
- 클라이언트 애플리케이션 (main.py): MCP 서버와 통신하여 문서 데이터를 가져오고, 이를 AI 모델에 전달하여 요약 및 포맷팅을 수행합니다.
[사용자]
↓
[클라이언트 (main.py)] ←→ [MCP 서버 (mcp_server.py)] ←→ [파일 시스템]
↓
[Google Gemini API] ← 문서 요약 및 형식화
기술 스택
- FastAPI: 비동기 웹 프레임워크로 MCP 서버 구현에 사용됩니다.
- MCP 프로토콜: AI 모델과 외부 데이터 소스 간의 표준화된 통신을 위해 사용됩니다.
- Python Asyncio: 비동기 작업 처리를 위해 사용됩니다.
MCP 서버 코드 (mcp_server.py)
아래는 MCP 사양에 따라 구현된 서버 코드입니다:
from typing import Union
import os
import csv
import json
import xml.etree.ElementTree as ET
from fastapi import FastAPI
from docx import Document
import uvicorn
app = FastAPI()
@app.get("/read-text-from-file")
def read_text_from_file(file_path: str) -> Union[str, None]:
    try:
        if not os.path.exists(file_path):
            return {"error": f"File does not exist: {file_path}"}
        _, file_ext = os.path.splitext(file_path)
        if file_ext in (".txt", ".log", ".md"):
            with open(file_path, "r", encoding="utf-8") as f:
                return {"content": f.read()}
        elif file_ext == ".json":
            with open(file_path, "r", encoding="utf-8") as f:
                return {"content": json.dumps(json.load(f), indent=4, ensure_ascii=False)}
        elif file_ext == ".xml":
            tree = ET.parse(file_path)
            return {"content": ET.tostring(tree.getroot(), encoding="unicode")}
        elif file_ext in (".csv", ".tsv"):
            delimiter = "," if file_ext == ".csv" else "\t"
            with open(file_path, "r", encoding="utf-8") as f:
                reader = csv.reader(f, delimiter=delimiter)
                return {"content": "\n".join([delimiter.join(row) for row in reader])}
        elif file_ext in (".docx", ".doc"):
            return {"content": "\n".join([p.text for p in Document(file_path).paragraphs])}
        else:
            return {"error": f"Unsupported file format: {file_ext}"}
    except Exception as e:
        return {"error": f"File read failed: {e}"}
if __name__ == "__main__":
    uvicorn.run(app, host="127.0.0.1", port=8000)
설명:
- read_text_from_file엔드포인트는 파일 경로를 입력받아 해당 파일의 내용을 반환합니다.
- 지원하는 파일 형식에는 .txt,.json,.xml,.csv,.docx등이 있습니다.
- MCP 사양에 따라 JSON 형태의 응답을 반환하며, 오류 발생 시 에러 메시지를 포함합니다.
클라이언트 애플리케이션 코드 (main.py)
아래는 MCP 서버와 통신하여 문서 데이터를 가져오고, 이를 AI 모델에 전달하는 클라이언트 애플리케이션 코드입니다:
import json
import httpx
import os
from dotenv import load_dotenv
load_dotenv()
MCP_SERVER_URL = os.getenv("MCP_SERVER_URL", "http://127.0.0.1:8000")
async def fetch_text_from_mcp(file_path: str):
    async with httpx.AsyncClient() as client:
        response = await client.get(
            f"{MCP_SERVER_URL}/read-text-from-file",
            params={"file_path": file_path},
        )
        if response.status_code == 200:
            data = response.json()
            if "content" in data:
                return data["content"]
            else:
                return f"Error: {data.get('error', 'Unknown error')}"
        else:
            return f"Error: {response.status_code}, {response.text}"
async def main():
    file_path = "data.txt"
    text_content = await fetch_text_from_mcp(file_path)
    print("text_content--------", text_content)
    if text_content and not text_content.startswith("Error:"):
        # AI 모델에 텍스트를 전달하여 요약 및 포맷팅 수행
        # 예: formatted_response = generate_gemini_response(text_content)
        # print("=== Processed Response ===")
        # print(formatted_response)
        pass
    else:
        print("Failed to read file.")
if __name__ == "__main__":
    import asyncio
    asyncio.run(main())
설명:
- .env파일을 통해 MCP 서버의 URL을 설정합니다.
- fetch_text_from_mcp함수는 MCP 서버의- /read-text-from-file엔드포인트에 비동기 GET 요청을 보내어 파일 내용을 가져옵니다. 응답이 성공적이면 JSON 데이터를 파싱하여 파일 내용을 반환하고, 실패 시 에러 메시지를 반환합니다.
- main함수는 지정된 파일 경로(- file_path = "data.txt")에 대해- fetch_text_from_mcp를 호출하여 텍스트 내용을 가져옵니다. 가져온 텍스트가 유효하면 AI 모델에 전달하여 요약 및 포맷팅을 수행할 수 있습니다. 현재 해당 부분은 주석 처리되어 있으며, 실제 구현 시 AI 모델과의 연동 코드를 추가해야 합니다.
실행 방법
- 필요한 패키지를 설치합니다: 
- pip install fastapi uvicorn httpx python-dotenv python-docx
 
- .env파일을 생성하고, MCP 서버의 URL을 설정합니다:- MCP_SERVER_URL=http://127.0.0.1:8000
 
- MCP 서버를 실행합니다: 
- python mcp_server.py
 
- 클라이언트 애플리케이션을 실행합니다: 
- python main.py
 
결론
이 프로젝트는 MCP 사양에 따라 서버와 클라이언트 애플리케이션을 구축하여 AI 모델이 외부 문서 데이터를 효율적으로 활용할 수 있는 방법을 제시합니다. MCP를 준수함으로써 AI 시스템과 외부 데이터 소스 간의 통합을 표준화하고, 확장성과 유지보수성을 향상시킬 수 있습니다.
- MCP 검색 서버
- MCP 클라우드 플랫폼 서버
- MCP 버전 관리 서버
- MCP 개발자 도구 서버
- MCP 데이터베이스 연결 서버
- MCP 브라우저 자동화 서버
- MCP 프롬프트(Prompts) 이해하기
- MCP 데이터 과학 도구 서버
- MCP 파일 시스템 접근 서버
- 이제는 MCP의 시대, 오픈AI가 선택한 이유는?
- MCP 리소스(Resources) 개념 쉽게 이해하기
- MCP 핵심 아키텍처 개념 쉽게 이해하기
- MCP(Model Context Protocol) 기본 개념
- Model Context Protocol(MCP) 기반의 문서 처리 시스템 구축
 
                         
                         
                         
                         
                         
                         
                         
                         
			 
			 
			 
			