1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/cloudwego/eino/components/tool"
"github.com/cloudwego/eino/compose"
"github.com/cloudwego/eino/flow/agent/react"
"github.com/cloudwego/eino/schema"
"github.com/mark3labs/mcp-go/client"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
"github.com/cloudwego/eino-ext/components/model/ollama"
mcpp "github.com/cloudwego/eino-ext/components/tool/mcp"
)
func main() {
startMCPServer()
time.Sleep(1 * time.Second)
ctx := context.Background()
mcpTools := getMCPTool(ctx)
chatModel, err := ollama.NewChatModel(ctx, &ollama.ChatModelConfig{
BaseURL: "http://localhost:11434",
Model: "qwen2.5:7b",
})
if err != nil {
log.Printf("NewChatModel failed, err=%v", err)
return
}
agent, err := react.NewAgent(ctx, &react.AgentConfig{
Model: chatModel,
ToolsConfig: compose.ToolsNodeConfig{Tools: mcpTools},
})
fmt.Println(err)
rsp, err := agent.Generate(ctx, []*schema.Message{
{
Role: schema.User,
Content: "我今天买了一斤苹果和一斤梨,苹果是5块一斤,梨是3块一斤,我一共花了多少钱?",
},
})
fmt.Println(rsp, err)
}
func getMCPTool(ctx context.Context) []tool.BaseTool {
cli, err := client.NewSSEMCPClient("http://localhost:12345/sse")
if err != nil {
log.Fatal(err)
}
err = cli.Start(ctx)
if err != nil {
log.Fatal(err)
}
initRequest := mcp.InitializeRequest{}
initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
initRequest.Params.ClientInfo = mcp.Implementation{
Name: "example-client",
Version: "1.0.0",
}
_, err = cli.Initialize(ctx, initRequest)
if err != nil {
log.Fatal(err)
}
tools, err := mcpp.GetTools(ctx, &mcpp.Config{Cli: cli})
if err != nil {
log.Fatal(err)
}
return tools
}
func startMCPServer() {
svr := server.NewMCPServer("demo", mcp.LATEST_PROTOCOL_VERSION)
svr.AddTool(mcp.NewTool("calculate",
mcp.WithDescription("Perform basic arithmetic operations"),
mcp.WithString("operation",
mcp.Required(),
mcp.Description("The operation to perform (add, subtract, multiply, divide)"),
mcp.Enum("add", "subtract", "multiply", "divide"),
),
mcp.WithNumber("x",
mcp.Required(),
mcp.Description("First number"),
),
mcp.WithNumber("y",
mcp.Required(),
mcp.Description("Second number"),
),
), func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
fmt.Println("Received request:", request)
op := request.Params.Arguments["operation"].(string)
x := request.Params.Arguments["x"].(float64)
y := request.Params.Arguments["y"].(float64)
var result float64
switch op {
case "add":
result = x + y
case "subtract":
result = x - y
case "multiply":
result = x * y
case "divide":
if y == 0 {
return mcp.NewToolResultText("Cannot divide by zero"), nil
}
result = x / y
}
log.Printf("Calculated result: %.2f", result)
return mcp.NewToolResultText(fmt.Sprintf("%.2f", result)), nil
})
go func() {
defer func() {
e := recover()
if e != nil {
fmt.Println(e)
}
}()
err := server.NewSSEServer(svr, server.WithBaseURL("http://localhost:12345")).Start("localhost:12345")
if err != nil {
log.Fatal(err)
}
}()
}
|