목표
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 |
