LLM AI Agent toy project - #3 MCP Server 예제 (python FastMCP)

2026. 5. 28. 00:13·LLM AI Agent

목표

FastMCP를 이용해서 LLM에 MCP server 연결해보기

코드

코드 링크: https://github.com/ybjeon/ai-agent-toyproject

  • $ python mcp_server.py - MCP 서버 실행
  • $ python test_mcp.py- MCP 클라 실행

 

mcp_server.py

from mcp.server.fastmcp import FastMCP
mcp = FastMCP("example-server")

@mcp.tool()
def add(a: int, b: int) -> int:
    """
    Add two numbers.

    Args:
        a: first number
        b: second number
    """
    return a + b

mcp server에 tool 추가

if __name__ == "__main__":
    import sys
    transport = sys.argv[1] if len(sys.argv) > 1 else "streamable-http"  # streamable-http / stdio
    mcp.run(transport=transport)

transport 방식 두 가지 허용: streamable-http / stdio (뒤 test_mcp.py 코드에서 설명)

test_mcp.py

async def main(transport: str) -> None:
    if transport == "stdio":
        client = MultiServerMCPClient(
            {
                "example-server": {
                    "command": "python3",
                    "args": ["mcp_server.py", transport],
                    "transport": transport,
                }
            }
        )
    elif transport == "streamable-http":
        client = MultiServerMCPClient(
            {
                "example-server": {
                    "url": MCP_SERVER_URL,
                    "transport": transport,
                }
            }
        )
    else:
        raise ValueError(f"Unsupported transport: {transport}")

    tools = await client.get_tools()
    logger.info("Loaded %d tool(s) from MCP server: %s", len(tools), [t.name for t in tools])

    agent = create_agent(llm, tools)
  • stdio: 클라이언트(test_mcp.py)가 해당 명령을 자식 프로세스로 직접 실행하고 stdin/stdout을 파이프로 연결. 서버가 미리 실행되어 있을 필요가 없음. 로컬/테스트 개발
  • streamable-http: url을 지정하면 클라이언트가 이미 떠 있는 서버에 HTTP로 연결. 서버가 미리 실행되어야함. 실제 MCP 서버를 네트워크 상에 올릴 때

결과

2026-05-30 11:50:03 [INFO] Loaded 2 tool(s) from MCP server: ['add', 'echo']

>>>>>>> User: What is 7 plus 15?
2026-05-30 11:50:05 [INFO] HTTP Request: POST http://localhost:11434/api/chat "HTTP/1.1 200 OK"
2026-05-30 11:50:05 [INFO] [TOOL START] add | input: {'a': 7, 'b': 15}
Processing request of type CallToolRequest
Processing request of type ListToolsRequest
2026-05-30 11:50:05 [INFO] [TOOL END]   output: content=[{'type': 'text', 'text': '22', 'id': 'lc_4d6b1a12-eeab-44d1-9143-66261caa3037'}] name='add' tool_call_id='7b2e9639-71da-48a5-84d7-e0b35d055a4d' artifact={'structured_content': {'result': 22}}
2026-05-30 11:50:05 [INFO] HTTP Request: POST http://localhost:11434/api/chat "HTTP/1.1 200 OK"

<<<<<<< Assistant: The sum of 7 and 15 is 22.
──────────────────────────────────────────────────

>>>>>>> User: Echo the message 'Hello from MCP!'
2026-05-30 11:50:05 [INFO] HTTP Request: POST http://localhost:11434/api/chat "HTTP/1.1 200 OK"
2026-05-30 11:50:05 [INFO] [TOOL START] echo | input: {'message': 'Hello from MCP!'}
Processing request of type CallToolRequest
Processing request of type ListToolsRequest
2026-05-30 11:50:06 [INFO] [TOOL END]   output: content=[{'type': 'text', 'text': 'echo: Hello from MCP!', 'id': 'lc_cbf08253-a223-4260-b41e-dba50e84b9be'}] name='echo' tool_call_id='d8a16877-320a-403e-9ae6-248affd27c1c' artifact={'structured_content': {'result': 'echo: Hello from MCP!'}}
2026-05-30 11:50:06 [INFO] HTTP Request: POST http://localhost:11434/api/chat "HTTP/1.1 200 OK"

<<<<<<< Assistant: The message has been echoed successfully. Here is what was echoed: 'Hello from MCP!'

'LLM AI Agent' 카테고리의 다른 글

LLM 출력에서 자주 보이는 <think> 태그  (0) 2026.05.31
LLM AI Agent toy project - #2 Tool call  (0) 2026.05.29
LLM AI Agent toy project - #1 기본 LLM 테스트  (0) 2026.05.29
'LLM AI Agent' 카테고리의 다른 글
  • LLM 출력에서 자주 보이는 <think> 태그
  • LLM AI Agent toy project - #2 Tool call
  • LLM AI Agent toy project - #1 기본 LLM 테스트
ybjeon.today
ybjeon.today
#Security #AI #LLM #일상 #공부 #연구, 프로필: https://ybjeon.today
  • ybjeon.today
    ybjeon's today
    ybjeon.today
  • 전체
    오늘
    어제
  • 링크

    • Github
    • Homepage
  • 블로그 메뉴

    • 전체 보기
    • Security
    • LLM AI Agent
    • AI Agent Security
    • Development
    • 방명록
  • 태그

    NEWS
    Agent
    review
    Security
    llm
    coding
    ToDo
    ai
  • 인기 글

    • 분류 전체보기 (16) N
      • Dev (4) N
      • Security (2)
      • AI Agent Security (5) N
      • LLM AI Agent (4)
      • Tech News (1) N
  • 공지사항

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.6
ybjeon.today
LLM AI Agent toy project - #3 MCP Server 예제 (python FastMCP)
상단으로

티스토리툴바