WeniVooks

검색

MCP 베이스캠프

웹 데이터 가져오기

1. 파일 생성

이번에는 웹에 있는 데이터를 가지고 가공할 수 있는지를 보도록 하겠습니다. tutorial_5.py파일을 생성하고 아래와 같이 코드를 작성해주세요.

from mcp.server.fastmcp import FastMCP
 
# MCP 서버 생성
mcp = FastMCP(name="tutorial_5", host="127.0.0.1", port=5004, timeout=30)
 
 
# URL 입력 받아 크롤링 진행
# https://paullab.co.kr/bookservice/
@mcp.tool()
def crawl_url_return_book_name(url: str) -> str:
    import requests
    from bs4 import BeautifulSoup
 
    response = requests.get(url)
    soup = BeautifulSoup(response.text, "html.parser")
 
    result = []
 
    for book in soup.select(".book_name"):
        result.append(book.text.strip())
 
    return ', '.join(result)
 
 
# 서버 실행
if __name__ == "__main__":
    mcp.run()
from mcp.server.fastmcp import FastMCP
 
# MCP 서버 생성
mcp = FastMCP(name="tutorial_5", host="127.0.0.1", port=5004, timeout=30)
 
 
# URL 입력 받아 크롤링 진행
# https://paullab.co.kr/bookservice/
@mcp.tool()
def crawl_url_return_book_name(url: str) -> str:
    import requests
    from bs4 import BeautifulSoup
 
    response = requests.get(url)
    soup = BeautifulSoup(response.text, "html.parser")
 
    result = []
 
    for book in soup.select(".book_name"):
        result.append(book.text.strip())
 
    return ', '.join(result)
 
 
# 서버 실행
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"
            ]
        },
        "tutorial_4": {
            "command": "python",
            "args": [
                "C:\\test\\tutorial_4.py"
            ]
        },
        "tutorial_5": {
            "command": "python",
            "args": [
                "C:\\test\\tutorial_5.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"
            ]
        },
        "tutorial_4": {
            "command": "python",
            "args": [
                "C:\\test\\tutorial_4.py"
            ]
        },
        "tutorial_5": {
            "command": "python",
            "args": [
                "C:\\test\\tutorial_5.py"
            ]
        }
    }
}

3. 프롬프트

프롬프트에 아래와 같이 입력해주세요.

`https://paullab.co.kr/bookservice/`를 크롤링 하여 책 제목에 '이력서'가 있는 책 제목을 알려줘. 책 제목은 콤마로 구분되어 나와. tutorial_5에 함수가 있으니 참고해줘.
`https://paullab.co.kr/bookservice/`를 크롤링 하여 책 제목에 '이력서'가 있는 책 제목을 알려줘. 책 제목은 콤마로 구분되어 나와. tutorial_5에 함수가 있으니 참고해줘.
답변: "메모혁신 Notion(노션) 활용 가이드, 이력서 작성 가이드 책이 있습니다."
답변: "메모혁신 Notion(노션) 활용 가이드, 이력서 작성 가이드 책이 있습니다."

docstring을 상세하게 적으면 클로드가 사용할 때 도움이 됩니다. 아래와 같이 함수를 수정하고 클로드를 재시작하여 사용하면 답변이 좀 더 구분되어 나옵니다.

@mcp.tool()
def crawl_url_return_book_name(url: str) -> str:
    """
    URL을 입력 받아 해당 URL의 책 제목을 크롤링하여 반환합니다. 각 데이터는 콤마로 연결됩니다. 따라서 사용자에게 보여줄 때에는 콤마를 개행하여 보여주세요.
    """
    import requests
    from bs4 import BeautifulSoup
 
    response = requests.get(url)
    soup = BeautifulSoup(response.text, "html.parser")
 
    result = []
 
    for book in soup.select(".book_name"):
        result.append(book.text.strip())
 
    return ', '.join(result)
@mcp.tool()
def crawl_url_return_book_name(url: str) -> str:
    """
    URL을 입력 받아 해당 URL의 책 제목을 크롤링하여 반환합니다. 각 데이터는 콤마로 연결됩니다. 따라서 사용자에게 보여줄 때에는 콤마를 개행하여 보여주세요.
    """
    import requests
    from bs4 import BeautifulSoup
 
    response = requests.get(url)
    soup = BeautifulSoup(response.text, "html.parser")
 
    result = []
 
    for book in soup.select(".book_name"):
        result.append(book.text.strip())
 
    return ', '.join(result)
답변: "이력서 작성 가이드 책이 있습니다."
답변: "이력서 작성 가이드 책이 있습니다."
2.3 클로드로 엑셀 파일 생성3장 MCP의 미래