Comprendere meglio l'host MCP
MCP๊ฐ ๋ฌด์์ ๋๊น
MCP๋ Anthropic์์ claude๋ฅผ ์ํด ๊ฐ๋ฐ๋ ํ๋กํ ์ฝ์ ๋๋ค. MCP๋ Model Context Protocol์ ์ค์๋ง๋ก์จ LLM์ด ๋ฅ๋์ ์ผ๋ก ์ธ๋ถ์ ๋์์ด๋ ๋ฆฌ์์ค๋ฅผ ์์ฒญํ ์ ์๋๋ก ํด์ฃผ๋ ํ๋กํ ์ฝ์ ๋๋ค. MCP๋ ์ง์ง ๋ฌธ์ ๊ทธ๋๋ก ์์ฒญ๊ณผ ์๋ต์ ์ฃผ๋ ํ๋กํ ์ฝ์ ๋ถ๊ณผํ๊ธฐ ๋๋ฌธ์ ๊ทธ ๊ณผ์ ๊ณผ ์คํ์ ๊ฐ๋ฐ์๊ฐ ํด์ค์ผ ํฉ๋๋ค.
๋ด๋ถ ๋์์ ๋ํด์
๋ด๋ถ ๋์์ ๋ํด ์ค๋ช ํ๊ธฐ ์์, Gemini Function Calling์ ๋ํด ์ง๊ณ ๋์ด ๊ฐ๊ฒ ์ต๋๋ค. Gemini Function Calling๋ MCP์ ๋์ผํ๊ฒ LLM์ด ์ฃผ๋์ ์ผ๋ก ์ธ๋ถ ๋์์ ํธ์ถํ ์ ์๋๋ก ํฉ๋๋ค. ๊ทธ๋ผ ์ Function Calling์ ๊ตณ์ด ๊ฐ์ ธ์๋๊ฐ ์๋ฌธ์ด ๋ค ๊ฒ์ ๋๋ค. ๊ตณ์ด ๊ฐ์ ธ์จ ์ด์ ๋ Function Calling์ด MCP๋ณด๋ค ๋จผ์ ๋์ค๊ธฐ๋ ํ๊ณ , ๋์ผํ๊ฒ OpenAPI ์คํค๋ง๋ฅผ ์ด์ฉํ๋ค๋ ์ ์์ ํธํ์ด ๋์ด, ์ํธ ๊ฐ์ ๋์์ด ์ ์ฌํ ๊ฒ์ผ๋ก ์ถ์ธกํ์ต๋๋ค. ๊ทธ๋ ๋ค๋ณด๋ ๋น๊ต์ Gemini Function Calling์ ์ค๋ช ์ด ๋์ฑ ์์ธํ๊ธฐ์ ๋์์ด ๋ ๊ฒ์ผ๋ก ๋ณด์ฌ ๊ฐ์ ธ์์ต๋๋ค.

์ ์ฒด์ ์ธ ํ๋ฆ์ ์ด๋ ์ต๋๋ค.
- ํจ์๋ฅผ ์ ์ํฉ๋๋ค.
- ํ๋กฌํํธ์ ํจ๊ป Gemini์ ํจ์ ์ ์๋ฅผ ์ ์กํฉ๋๋ค.
- "Send user prompt along with the function declaration(s) to the model. It analyzes the request and determines if a function call would be helpful. If so, it responds with a structured JSON object."
- Gemini๊ฐ ํ์ํ๋ฉด ํจ์ ํธ์ถ์ ์์ฒญํฉ๋๋ค.
- Gemini๊ฐ ํ์ํ๋ฉด ํจ์ ํธ์ถ์ ์ํ ์ด๋ฆ๊ณผ ํจ๋ฌ๋ฏธํฐ๋ฅผ ํธ์ถ์๊ฐ ์ ๋ฌ๋ฐ์ต๋๋ค.
- ํธ์ถ์๋ ์คํ์ ํ ์ง, ๋ง์ง ์ ํ ์ ์์ต๋๋ค.
- ํธ์ถํด์ ์ ๋นํ ๊ฐ์ ๋๋ ค์ค ๊ฒ์ธ์ง
- ํธ์ถํ์ง ์๊ณ ํธ์ถํ ๊ฒ์ฒ๋ผ ๋ฐ์ดํฐ๋ฅผ ๋ฐํํ ์ง
- ๊ทธ๋ฅ ๋ฌด์ํ ์ง
- Gemini๋ ์ ๊ณผ์ ์์ ํ๋ฒ์ ์ฌ๋ฌ๊ฐ์ ํจ์๋ฅผ ํธ์ถํ๊ฑฐ๋, ํจ์ ํธ์ถ ํ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ณ ๋ ํธ์ถํ๋ ๋ฑ์ ๋์์ ์ํ ๋ฐ ์์ฒญํฉ๋๋ค.
- ๊ฒฐ๊ณผ์ ์ผ๋ก ์ ๋๋ ๋๋ต์ด ๋์ค๋ฉด ์ข ๋ฃ๋ฉ๋๋ค.
์ด ํ๋ฆ์ ์ผ๋ฐ์ ์ผ๋ก MCP์ ์ผ๋งฅ์ํตํฉ๋๋ค. ์ด๋ MCP์ ํํ ๋ฆฌ์ผ์์๋ ๋น์ทํ๊ฒ ์ค๋ช ํ๊ณ ์์ต๋๋ค. ์ด๋ ollama tools๋ ๋น์ทํฉ๋๋ค.
๊ทธ๋ฆฌ๊ณ ์ ๋ง ๋คํ์ด๊ฒ๋ ์ด 3๊ฐ์ง ๋๊ตฌ, ollama tools, MCP, Gemini Function Calling์ ์คํค๋ง ๊ตฌ์กฐ๊ฐ ๊ณต์ ๋๋ค์ํผ ํด์ MCP ํ๋๋ง ๊ตฌํํจ์ผ๋ก 3๊ณณ์ ๋ค ์ธ ์๋ ์๋ค๋ ๊ฒ์ ๋๋ค.
์ ๊ทธ๋ฆฌ๊ณ ๋ชจ๋๊ฐ ๊ณต์ ํ๋ ๋จ์ ์ด ์์ต๋๋ค. ๊ฒฐ๊ตญ ๋ชจ๋ธ์ด ์คํ์์ผ์ฃผ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ์ฌ๋ฌ๋ถ์ด ์ฐ๋ ๋ชจ๋ธ์ด ์ํ๊ฐ ์ ์ข๋ค๋ฉด, ํจ์๋ฅผ ํธ์ถํ์ง ์๊ฑฐ๋, ์ด์ํ๊ฒ ํธ์ถํ๋ค๊ฑฐ๋, MCP ์๋ฒ์ DOS๋ฅผ ๋ ๋ฆฌ๋ ๋ฑ์ ์ค๋์์ ํ ์ ์์ต๋๋ค.
Go๋ก ๋ MCP ํธ์คํธ
mark3lab's mcphost
Go์๋ mark3lab์ด๋ ์กฐ์ง์์ ๊ฐ๋ฐ ์ค์ธ mcphost๊ฐ ์์ต๋๋ค.
์ฌ์ฉ๋ฒ์ ๋งค์ฐ ๊ฐ๋จํฉ๋๋ค.
1go install github.com/mark3labs/mcphost@latest
์ค์น ํ, $HOME/.mcp.json ํ์ผ์ ๋ง๋ค์ด์ ๋ค์๊ณผ ๊ฐ์ด ์์ฑํฉ๋๋ค.
1{
2 "mcpServers": {
3 "sqlite": {
4 "command": "uvx",
5 "args": [
6 "mcp-server-sqlite",
7 "--db-path",
8 "/tmp/foo.db"
9 ]
10 },
11 "filesystem": {
12 "command": "npx",
13 "args": [
14 "-y",
15 "@modelcontextprotocol/server-filesystem",
16 "/tmp"
17 ]
18 }
19 }
20}
๊ทธ๋ฆฌ๊ณ ๋ค์๊ณผ ๊ฐ์ด ollama ๋ชจ๋ธ๋ก ์คํํฉ๋๋ค.
๋ฌผ๋ก ๊ทธ ์ ์ ํ์ํ๋ฉด ollama pull mistral-small๋ก ๋ชจ๋ธ์ ๋ฐ์ต๋๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก claude๋ qwen2.5๋ฅผ ์ถ์ฒํ์ง๋ง, ์ ๋ ํ์ฌ๋ก์ฌ mistral-small์ ์ถ์ฒํฉ๋๋ค.
1mcphost -m ollama:mistral-small
๋ค๋ง ์ด๋ ๊ฒ ์คํํ๋ฉด, CLI ํ๊ฒฝ์์ ์ง์์๋ต ์์ผ๋ก๋ง ์ฌ์ฉํ ์ ์์ต๋๋ค.
๊ทธ๋ ๊ธฐ์ ์ ํฌ๋ ์ด mcphost์ ์ฝ๋๋ฅผ ์์ ํด์ ์ข ๋ ํ๋ก๊ทธ๋๋จธ๋ธ ํ๊ฒ ๋์ํ ์ ์๊ฒ ์์ ํด๋ณด๊ฒ ์ต๋๋ค.
mcphost ํฌํฌ
์ด๋ฏธ ํ์ธํ๋ค์ํผ mcphost์๋ MCP๋ฅผ ํ์ฉํด์ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ์ถ์ถํ๊ณ , ํจ์๋ฅผ ํธ์ถํ๋ ๊ธฐ๋ฅ์ด ํฌํจ๋์ด ์์ต๋๋ค. ๊ทธ๋ฌ๋ฏ๋ก llm์ ํธ์ถํ๋ ๋ถ๋ถ, mcp ์๋ฒ๋ฅผ ๋ค๋ฃจ๋ ๋ถ๋ถ, ๋ฉ์์ง ํ์คํ ๋ฆฌ๋ฅผ ๊ด๋ฆฌํ๋ ๋ถ๋ถ์ด ํ์ํฉ๋๋ค.
ํด๋นํ๋ ๋ถ๋ถ์ ๊ฐ์ ธ์จ ๊ฒ์ด ๋ค์ ํจํค์ง์ Runner์
๋๋ค.
1package runner
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "log"
8 "strings"
9 "time"
10
11 mcpclient "github.com/mark3labs/mcp-go/client"
12 "github.com/mark3labs/mcp-go/mcp"
13
14 "github.com/mark3labs/mcphost/pkg/history"
15 "github.com/mark3labs/mcphost/pkg/llm"
16)
17
18type Runner struct {
19 provider llm.Provider
20 mcpClients map[string]*mcpclient.StdioMCPClient
21 tools []llm.Tool
22
23 messages []history.HistoryMessage
24}
ํด๋นํ๋ ๋ถ๋ถ์ ๋ด๋ถ ์ ์ธ์ ๋ฐ๋ก ๋ณด์ง ์๊ฒ ์ต๋๋ค. ๋ค๋ง ๊ฑฐ์ ์ด๋ฆ ๊ทธ๋๋ก์ ๋๋ค.
1func NewRunner(systemPrompt string, provider llm.Provider, mcpClients map[string]*mcpclient.StdioMCPClient, tools []llm.Tool) *Runner {
2 return &Runner{
3 provider: provider,
4 mcpClients: mcpClients,
5 tools: tools,
6 messages: []history.HistoryMessage{
7 {
8 Role: "system",
9 Content: []history.ContentBlock{{
10 Type: "text",
11 Text: systemPrompt,
12 }},
13 },
14 },
15 }
16}
์ฌ๊ธฐ์ ์ฐ์ผ mcpClients์ tools์ ๋ํด์๋ ํด๋น ํ์ผ์ ํ์ธํด ์ฃผ์ธ์.provider๋ ollama์ ๊ฒ์ ์ธ ํ
๋ ํด๋น ํ์ผ์ ํ์ธํด ์ฃผ์ธ์.
๋ฉ์ธ ์๋ฆฌ๋ Run ๋ฉ์๋์
๋๋ค.
1func (r *Runner) Run(ctx context.Context, prompt string) (string, error) {
2 if len(prompt) != 0 {
3 r.messages = append(r.messages, history.HistoryMessage{
4 Role: "user",
5 Content: []history.ContentBlock{{
6 Type: "text",
7 Text: prompt,
8 }},
9 })
10 }
11
12 llmMessages := make([]llm.Message, len(r.messages))
13 for i := range r.messages {
14 llmMessages[i] = &r.messages[i]
15 }
16
17 const initialBackoff = 1 * time.Second
18 const maxRetries int = 5
19 const maxBackoff = 30 * time.Second
20
21 var message llm.Message
22 var err error
23 backoff := initialBackoff
24 retries := 0
25 for {
26 message, err = r.provider.CreateMessage(
27 context.Background(),
28 prompt,
29 llmMessages,
30 r.tools,
31 )
32 if err != nil {
33 if strings.Contains(err.Error(), "overloaded_error") {
34 if retries >= maxRetries {
35 return "", fmt.Errorf(
36 "claude is currently overloaded. please wait a few minutes and try again",
37 )
38 }
39
40 time.Sleep(backoff)
41 backoff *= 2
42 if backoff > maxBackoff {
43 backoff = maxBackoff
44 }
45 retries++
46 continue
47 }
48
49 return "", err
50 }
51
52 break
53 }
54
55 var messageContent []history.ContentBlock
56
57 var toolResults []history.ContentBlock
58 messageContent = []history.ContentBlock{}
59
60 if message.GetContent() != "" {
61 messageContent = append(messageContent, history.ContentBlock{
62 Type: "text",
63 Text: message.GetContent(),
64 })
65 }
66
67 for _, toolCall := range message.GetToolCalls() {
68 input, _ := json.Marshal(toolCall.GetArguments())
69 messageContent = append(messageContent, history.ContentBlock{
70 Type: "tool_use",
71 ID: toolCall.GetID(),
72 Name: toolCall.GetName(),
73 Input: input,
74 })
75
76 parts := strings.Split(toolCall.GetName(), "__")
77
78 serverName, toolName := parts[0], parts[1]
79 mcpClient, ok := r.mcpClients[serverName]
80 if !ok {
81 continue
82 }
83
84 var toolArgs map[string]interface{}
85 if err := json.Unmarshal(input, &toolArgs); err != nil {
86 continue
87 }
88
89 var toolResultPtr *mcp.CallToolResult
90 req := mcp.CallToolRequest{}
91 req.Params.Name = toolName
92 req.Params.Arguments = toolArgs
93 toolResultPtr, err = mcpClient.CallTool(
94 context.Background(),
95 req,
96 )
97
98 if err != nil {
99 errMsg := fmt.Sprintf(
100 "Error calling tool %s: %v",
101 toolName,
102 err,
103 )
104 log.Printf("Error calling tool %s: %v", toolName, err)
105
106 toolResults = append(toolResults, history.ContentBlock{
107 Type: "tool_result",
108 ToolUseID: toolCall.GetID(),
109 Content: []history.ContentBlock{{
110 Type: "text",
111 Text: errMsg,
112 }},
113 })
114
115 continue
116 }
117
118 toolResult := *toolResultPtr
119
120 if toolResult.Content != nil {
121 resultBlock := history.ContentBlock{
122 Type: "tool_result",
123 ToolUseID: toolCall.GetID(),
124 Content: toolResult.Content,
125 }
126
127 var resultText string
128 for _, item := range toolResult.Content {
129 if contentMap, ok := item.(map[string]interface{}); ok {
130 if text, ok := contentMap["text"]; ok {
131 resultText += fmt.Sprintf("%v ", text)
132 }
133 }
134 }
135
136 resultBlock.Text = strings.TrimSpace(resultText)
137
138 toolResults = append(toolResults, resultBlock)
139 }
140 }
141
142 r.messages = append(r.messages, history.HistoryMessage{
143 Role: message.GetRole(),
144 Content: messageContent,
145 })
146
147 if len(toolResults) > 0 {
148 r.messages = append(r.messages, history.HistoryMessage{
149 Role: "user",
150 Content: toolResults,
151 })
152
153 return r.Run(ctx, "")
154 }
155
156 return message.GetContent(), nil
157}
์ฝ๋ ์์ฒด๋ ํด๋น ํ์ผ์ ์ผ๋ถ ์ฝ๋๋ฅผ ์ง์ง๊ธฐ ํ์์ต๋๋ค.
๋ด์ฉ์ ๋๋ต ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
- ํ๋กฌํํธ์ ํจ๊ป ํด ๋ชฉ๋ก์ ์ ์กํ์ฌ ์คํ ์ฌ๋ถ, ํน์ ์๋ต ์์ฑ์ ์์ฒญํฉ๋๋ค.
- ์๋ต์ด ์์ฑ๋๋ฉด ์ฌ๊ท๋ฅผ ๋ฉ์ถ๊ณ ๋ฐํํฉ๋๋ค.
- LLM์ด ํด ์คํ ์์ฒญ์ ๋จ๊ธด๋ค๋ฉด, ํธ์คํธ์์๋ MCP Server๋ฅผ ํธ์ถํฉ๋๋ค.
- ์๋ต์ ํ์คํ ๋ฆฌ์ ์ถ๊ฐํด์ ๋ค์ 1๋ฒ์ผ๋ก ๋์๊ฐ๋๋ค.
๋์ผ๋ก
๋ฒ์จ ๋?
์ฌ์ค ํ ๋ง์ด ๊ทธ๋ ๊ฒ ๋ง์ง ์์ต๋๋ค. ๋๋ต์ ์ผ๋ก MCP Server๊ฐ ์ด๋ป๊ฒ ๋์๋๋ ์ง์ ๋ํ ์ดํด๋ฅผ ๋์ ๋๋ฆฌ๊ธฐ ์ํด ์์ฑ๋ ๊ธ์ ๋๋ค. ์ด ๊ธ์ด ์ฌ๋ฌ๋ถ๋ค์๊ฒ ์๊ทธ๋งฃ๊ฒ๋๋ง MCP host์ ๋์์ ์ดํดํจ์ ๋์์ด ๋์๊ธธ ๋ฐ๋๋๋ค.