티스토리 뷰

Computer Science/Backends

MCP Remote 서버 포트 지정하기

신쫄이후리스 2025. 12. 22. 16:46
반응형

MCP 리모트 서버를 생성 후 실행시키면 기본적으로 8000번 포트를 사용합니다.

상황에 따라서 포트번호를 변경하거나 host 주소를 변경해야 할 일이 있는데요. 막상 Document와는 달라서 삽질을 좀 했습니다.

 

 

 

기본적으로 MCP서버는 파이썬 패키지의 FastMCP를 이용해서 간단하게 생성이 가능합니다.

페이지의 문서를 찾아보면 다음과 같이 host와 port를 지정해서 실행을 시키던데요.

https://gofastmcp.com/deployment/running-server

 

Running Your Server - FastMCP

Learn how to run your FastMCP server locally for development and testing

gofastmcp.com

 

mcp.run() 의 인자로 Transport Protocol, host, port 를 지정할 수 있다고 되어 있는데, 막상 적용하면 에러가 납니다. 

from fastmcp import FastMCP

mcp = FastMCP("MyServer")

@mcp.tool
def hello(name: str) -> str:
    return f"Hello, {name}!"

if __name__ == "__main__":
    # Start an HTTP server on port 8000
    mcp.run(transport="http", host="127.0.0.1", port=8000)

 

 

host는 localhost가 아닌 0.0.0.0으로 변경하고 포트번호도 8000이 아닌 2424번 포트를 써보려고 넣어봤는데 잘 안되더라구요.

이렇게 실행하면 unexpected keyword argument 'host' 라며 에러가 발생합니다.

mcp.run(transport="http", host="0.0.0.0", port=2424)
TypeError: FastMCP.run() got an unexpected keyword argument 'host'

 

 

그럼 어떻게 하느냐...?

삽질하다가 찾은 방법 입니다.

 

from mcp.server.fastmcp import FastMCP
import aiohttp

mcp = FastMCP("MCP Server", host="0.0.0.0", port=2424)

... (중략)

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

 

FastMCP 생성할때 넣어주면 되는거였습니다.

 

 

 

제가 만들려던 MCP서버는 clickhouse db 쿼리를 지원하는 서버 였는데요.

이런식으로 만들어서 연결해 봤습니다. 참고로 clickhouse server는 docker로 같은 호스트에서 실행중이고 localhost:8123 포트로 접근이 가능하도록 해놓은 상태입니다.

 

from mcp.server.fastmcp import FastMCP
import clickhouse_connect
import uvicorn

# 1. FastMCP 초기화
mcp = FastMCP("ClickHouse-Server", host="0.0.0.0", port=2424)

# 2. ClickHouse 연결 툴들
def get_client():
    return clickhouse_connect.get_client(
        host='127.0.0.1', 
        port=8123, 
        username='default', 
        password=''
    )

@mcp.tool()
def query(sql: str) -> str:
    """ClickHouse SQL을 실행합니다."""
    try:
        client = get_client()
        result = client.query(sql)
        return str(result.result_rows)
    except Exception as e:
        return f"Error: {e}"

if __name__ == "__main__":
    mcp.run(transport='http')

 

반응형