WeniVooks

검색

MCP 베이스캠프

파일 입출력

1. 파일 생성

이번에는 파일을 생성하고 읽는 실습을 해보도록 하겠습니다. 같은 폴더에 tutorial_3.py라는 파일을 만들고 아래와 같이 작성해주세요.

from mcp.server.fastmcp import FastMCP
 
# MCP 서버 생성
mcp = FastMCP(name="tutorial_3", host="127.0.0.1", port=5002, timeout=30)
 
 
# test 폴더에서 파일 읽기
@mcp.tool()
def read_file(file_name: str) -> str:
    """
    c:/test/ 아래 파일을 읽습니다.
    """
    import os
 
    file_path = os.path.join("c:/test", file_name)
    if os.path.exists(file_path):
        with open(file_path, "r") as f:
            content = f.read()
        return f"파일 '{file_name}' 의 내용은:\n{content}"
    else:
        return f"파일 '{file_name}' 는 존재하지 않습니다."
 
 
# 파일 쓰기
@mcp.tool()
def write_file(file_name: str, content: str) -> str:
    """
    c:/test/ 아래 파일을 씁니다.
    """
    import os
 
    file_path = os.path.join("c:/test", file_name)
    with open(file_path, "w") as f:
        f.write(content)
    return f"파일 '{file_name}' 가 생성되었습니다."
 
 
# 서버 실행
if __name__ == "__main__":
    mcp.run()
from mcp.server.fastmcp import FastMCP
 
# MCP 서버 생성
mcp = FastMCP(name="tutorial_3", host="127.0.0.1", port=5002, timeout=30)
 
 
# test 폴더에서 파일 읽기
@mcp.tool()
def read_file(file_name: str) -> str:
    """
    c:/test/ 아래 파일을 읽습니다.
    """
    import os
 
    file_path = os.path.join("c:/test", file_name)
    if os.path.exists(file_path):
        with open(file_path, "r") as f:
            content = f.read()
        return f"파일 '{file_name}' 의 내용은:\n{content}"
    else:
        return f"파일 '{file_name}' 는 존재하지 않습니다."
 
 
# 파일 쓰기
@mcp.tool()
def write_file(file_name: str, content: str) -> str:
    """
    c:/test/ 아래 파일을 씁니다.
    """
    import os
 
    file_path = os.path.join("c:/test", file_name)
    with open(file_path, "w") as f:
        f.write(content)
    return f"파일 '{file_name}' 가 생성되었습니다."
 
 
# 서버 실행
if __name__ == "__main__":
    mcp.run()

2. 설정파일 작성

claude_desktop_config.json 파일을 아래와 같이 수정해주세요.

{
    "mcpServers": {
        "tutorial_1": {
            "command": "python",
            "args": [
                "C:\\test\\tutorial_1.py"
            ]
        },
        "tutorial_2": {
            "command": "python",
            "args": [
                "C:\\test\\tutorial_2.py"
            ]
        },
        "tutorial_3": {
            "command": "python",
            "args": [
                "C:\\test\\tutorial_3.py"
            ]
        }
    }
}
{
    "mcpServers": {
        "tutorial_1": {
            "command": "python",
            "args": [
                "C:\\test\\tutorial_1.py"
            ]
        },
        "tutorial_2": {
            "command": "python",
            "args": [
                "C:\\test\\tutorial_2.py"
            ]
        },
        "tutorial_3": {
            "command": "python",
            "args": [
                "C:\\test\\tutorial_3.py"
            ]
        }
    }
}

3. 프롬프트

hello.txt라는 파일을 생성하고, hello world! 라는 내용을 입력해주세요.
hello.txt라는 파일을 생성하고, hello world! 라는 내용을 입력해주세요.
hello.txt라는 파일을 읽어오고, 안에 내용을 읽어주세요.
hello.txt라는 파일을 읽어오고, 안에 내용을 읽어주세요.
2.1 폴더 생성2.3 클로드로 엑셀 파일 생성