OKdevTV
MCP 주요 구성 요소:
AI 모델 <-> MCP 클라이언트 <-> MCP 서버 <-> 외부 도구/서비스
const express = require('express');
const app = express();
app.use(express.json());
// 도구 정의
const tools = {
calculator: {
add: (a, b) => a + b,
subtract: (a, b) => a - b,
multiply: (a, b) => a * b,
divide: (a, b) => a / b
},
weather: {
getTemperature: (city) => {
// 실제 구현에서는 날씨 API 호출
return `${city}의 현재 온도는 22°C입니다.`;
}
}
};
// MCP 엔드포인트
app.post('/mcp', (req, res) => {
const { tool, method, params } = req.body;
if (!tools[tool] || !tools[tool][method]) {
return res.status(400).json({ error: '유효하지 않은 도구 또는 메서드' });
}
try {
const result = tools[tool][method](...params);
res.json({ result });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.listen(3000, () => {
console.log('MCP 서버가 포트 3000에서 실행 중입니다.');
});
import requests
import json
class MCPClient:
def __init__(self, server_url):
self.server_url = server_url
def call_tool(self, tool, method, params):
payload = {
"tool": tool,
"method": method,
"params": params
}
response = requests.post(
f"{self.server_url}/mcp",
json=payload
)
if response.status_code == 200:
return response.json()["result"]
else:
error = response.json().get("error", "알 수 없는 오류")
raise Exception(f"MCP 호출 실패: {error}")
# 사용 예시
client = MCPClient("http://localhost:3000")
result = client.call_tool("calculator", "add", [5, 3])
print(f"5 + 3 = {result}") # 출력: 5 + 3 = 8
weather = client.call_tool("weather", "getTemperature", ["서울"])
print(weather) # 출력: 서울의 현재 온도는 22°C입니다.
graph TD
A[Claude Desktop] <--> |MCP Protocol: Query and Result| B[SQLite MCP Server]
B <--> |Local Access: SQL Operation| C[SQLite Database ~/test.db]
style A fill:#e6e6ff,stroke:#9999ff
style B fill:#e6e6ff,stroke:#9999ff
style C fill:#e6f0ff,stroke:#99c2ff,stroke-width:2px
subgraph Your Computer
A
B
C
end
최신 AI 모델들은 MCP를 통해 외부 도구와 통합:
MCP를 구현할 때 고려해야 할 주요 보안 사항: