在Eino里调用MCP工具

2026年1月12日 · 386 字 · 2 分钟

在这里使用了Eino调用了魔塔社区的12306的mcp工具

在Eino里调用MCP工具

魔塔社区官网

需要用到的包

	enio_mcp "github.com/cloudwego/eino-ext/components/tool/mcp"
	"github.com/cloudwego/eino/components/tool"
	"github.com/mark3labs/mcp-go/client"
	"github.com/mark3labs/mcp-go/mcp"

创建mcp client

func CreateMcpClient(url, name string) *client.Client {
	//创建mcp的client
	cli, err := client.NewStreamableHttpClient(url)
	if err != nil {
		log.Fatal(err)
	}
	//初始化mcp的client
	initRequest := mcp.InitializeRequest{}
	initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
	initRequest.Params.ClientInfo = mcp.Implementation{
		Name:    name,
		Version: "1.0.0",
	}
	_, err = cli.Initialize(context.Background(), initRequest)
	if err != nil {
		log.Fatal(err)
	}
	return cli
}

client进行初始化

var (
	trainCli *client.Client
)

const (
	TRAIN_MCP_URL  = "https://mcp.api-inference.modelscope.net/67a1aa6795ce4e/mcp"
	TRAIN_MCP_NAME = "12306-mcp"
)

func InitMcpClient() {
	trainCli = CreateMcpClient(TRAIN_MCP_URL, TRAIN_MCP_NAME)
}

获取mcp的tool列表

func CreateMcp12306Tool() []tool.BaseTool {
	ctx := context.Background()
	tools, err := enio_mcp.GetTools(ctx, &enio_mcp.Config{
		Cli:          trainCli,
		ToolNameList: []string{}, //填入需要的工具,不填则需要所有工具
	})
	if err != nil {
		log.Fatal(err)
	}
	return tools
}

func List12306Tool() {
	ctx := context.Background()
	tools := CreateMcp12306Tool()
	for _, tool := range tools {
		info, _ := tool.Info(ctx)
		fmt.Println(info.Name, info.Desc)
	}
}

调用tool

type GetTicketsRequest struct {
	FromStation string `json:"fromStation"`
	ToStation   string `json:"toStation"`
	Date        string `json:"date"`
}

func Use12306Tool() {
	toolName := "get-tickets"
	ctx := context.Background()
	tools, err := enio_mcp.GetTools(ctx, &enio_mcp.Config{
		Cli:          trainCli,
		ToolNameList: []string{toolName}, //填入需要的工具,不填则需要所有工具
	})
	if err != nil {
		log.Fatal(err)
	}
	//basetool只能获取信息,不能运,所以要断言成InvokableTool
	tickerTool := tools[0].(tool.InvokableTool)
	request := &GetTicketsRequest{
		Date:        time.Now().Add(24 * time.Hour).Format("2006-01-02"),
		FromStation: "BJP",
		ToStation:   "SHH",
	}
	jsonRequest, err := sonic.Marshal(request)
	if err != nil {
		log.Fatal(err)
	}
	response, err := tickerTool.InvokableRun(ctx, string(jsonRequest))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(response)
}

最终运行

func main() {
	InitMcpClient()
	List12306Tool()
	Use12306Tool()
}
get-current-date 获取当前日期,以上海时区(Asia/Shanghai, UTC+8)为准,返回格式为 "yyyy-MM-dd"。主要用于解析用户提到的相对日期(如“明天”、“下周三”),提供准确的日期输入。
get-stations-code-in-city 通过中文城市名查询该城市 **所有** 火车站的名称及其对应的 `station_code`,结果是一个包含多个车站信息的列表。
get-station-code-of-citys 通过中文城市名查询代表该城市的 `station_code`。此接口主要用于在用户提供**城市名**作为出发地或到达地时,为接口准备 `station_code` 参数。
get-station-code-by-names 通过具体的中文车站名查询其 `station_code` 和车站名。此接口主要用于在用户提供**具体车站名**作为出发地或到达地时,为接口准备 `station_code` 参数。
get-station-by-telecode 通过车站的 `station_telecode` 查询车站的详细信息,包括名称、拼音、所属城市等。此接口主要用于在已知 `telecode` 的情况下获取更完整的车站数据,或用于特殊查询及调试目的。一般用户对话流程中较少直接触发。
get-tickets 查询12306余票信息。
get-interline-tickets 查询12306中转余票信息。尚且只支持查询前十条。
get-train-route-stations 查询特定列车车次在指定区间内的途径车站、到站时间、出发时间及停留时间等详细经停信息。当用户询问某趟具体列车的经停站时使用此接口。
{"content":[{"type":"text","text":"车次|出发站 -\u003e 到达站|出发时间 -\u003e 到达时间|历时\nG103 北京南(telecode:VNP) -\u003e 上海虹桥(telecode:AOH) 06:20 -\u003e 11:58 历时:05:38\n- 商务座: 剩余10张票 1873元\n- 一等座: 有票 930元\n- 二等座: 有票 553元\n- 无座: 有票 553元\nG1 北京南(telecode:VNP) -\u003e 上海(telecode:SHH) 07:00 -\u003e 11:29 历时:04:29\n- 商务座: 剩余6张票 2337元\n- 一等座: 无票 1070元\n- 二等座: 有票 669元\n- 优选一等座: 无票 1471元\n- 无座: 有票 669元\nG105 北京南(telecode:VNP) -\u003e 上海虹桥(telecode:AOH) 07:17 -\u003e 13:03 历时:05:46\n- 商务座: 剩余6张票 1998元\n- 一等座: 有票 1006元\n- 二等座: 有票 598元\n- 无座: 有票 598元\nG107 北京南(telecode:VNP) -\u003e 上海虹桥(telecode:AOH) 07:25 -\u003e 13:12 历时:05:47\n- 商务座: 剩余17张票 1873元\n- 一等座: 有票 930元\n- 二等座: 有票 553元\n- 无座: 有票 553元\nG3 北京(telecode:BJP) -\u003e 上海(telecode:SHH) 07:40 -\u003e 12:32 历时:04:52\n- 商务座: 无票 2350元\n- 一等座: 无票 1076元\n- 二等座: 有票 673元\n- 无座: 有票 673元\nG109 北京南(telecode:VNP) -\u003e 上海虹桥(telecode:AOH) 07:45 -\u003e 13:49 历时:06:04\n- 商务座: 剩余8张票 1998元\n- 一等座: 有票 969元\n- 二等座: 有票 576元\n- 无座: 有票 576元\nG3 北京南(telecode:VNP) -\u003e 上海(telecode:SHH) 08:00 -\u003e 12:32]}