|
| 1 | +--- |
| 2 | +Description: "" |
| 3 | +date: "2025-03-12" |
| 4 | +lastmod: "" |
| 5 | +tags: [] |
| 6 | +title: Tool - MCP |
| 7 | +weight: 0 |
| 8 | +--- |
| 9 | + |
| 10 | +## Introduction |
| 11 | + |
| 12 | +Model Context Protocol(MCP) is a standardized open protocol for model access introduced by Anthropic. Eino provides adapters, which can directly access resources on existing MCP Servers. |
| 13 | + |
| 14 | +This section introduces the adapter of MCPTool, which implements the [Eino Tool](/en/docs/eino/core_modules/components/tools_node_guide). |
| 15 | + |
| 16 | +<a href="/img/eino/HotzbOFL6oZFQWxWP10caT9Wnuc.png" target="_blank"><img src="/img/eino/HotzbOFL6oZFQWxWP10caT9Wnuc.png" width="100%" /></a> |
| 17 | + |
| 18 | +Other adapters: |
| 19 | + |
| 20 | +[ChatTemplate - MCP](/en/docs/eino/ecosystem/chat_template/chat_template_mcp) |
| 21 | + |
| 22 | +## HowToUse |
| 23 | + |
| 24 | +Eino MCP adapters referenced the open-source SDK [mcp-go](https://github.com/mark3labs/mcp-go), first initialize a MCP client: |
| 25 | + |
| 26 | +```go |
| 27 | +import "github.com/mark3labs/mcp-go/client" |
| 28 | + |
| 29 | +// stdio client |
| 30 | +cli, err := client.NewStdioMCPClient(myCommand, myEnvs, myArgs...) |
| 31 | + |
| 32 | +// sse client |
| 33 | +cli, err := client.NewSSEMCPClient(myBaseURL) |
| 34 | +// sse client needs to manually start asynchronous communication |
| 35 | +// while stdio does not require it. |
| 36 | +err = cli.Start(ctx) |
| 37 | +``` |
| 38 | + |
| 39 | +Considering the reusability of multiple adapters for the MCP client, the adapter assumes that the client has completed initialization with the Server's [Initialize](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/lifecycle/), so users need to complete the client initialization themselves, for example: |
| 40 | + |
| 41 | +```go |
| 42 | +import "github.com/mark3labs/mcp-go/mcp" |
| 43 | + |
| 44 | +initRequest := mcp.InitializeRequest{} |
| 45 | +initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION |
| 46 | +initRequest.Params.ClientInfo = mcp.Implementation{ |
| 47 | + Name: "example-client", |
| 48 | + Version: "1.0.0", |
| 49 | +} |
| 50 | +_, err = cli.Initialize(ctx, initRequest) |
| 51 | +``` |
| 52 | + |
| 53 | +Then use the Client to create the adapter , which implements Eino Tool: |
| 54 | + |
| 55 | +```go |
| 56 | +import "github.com/cloudwego/eino-ext/components/tool/mcp" |
| 57 | + |
| 58 | +tools, err := mcp.GetTools(ctx, &mcp.Config{Cli: cli}) |
| 59 | +``` |
| 60 | + |
| 61 | +You can call this adapter directly: |
| 62 | + |
| 63 | +``` |
| 64 | +for i, mcpTool := range mcpTools { |
| 65 | + fmt.Println(i, ":") |
| 66 | + info, err := mcpTool.Info(ctx) |
| 67 | + if err != nil { |
| 68 | + log.Fatal(err) |
| 69 | + } |
| 70 | + fmt.Println("Name:", info.Name) |
| 71 | + fmt.Println("Desc:", info.Desc) |
| 72 | + fmt.Println() |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +It can also be used in any Eino Agent, taking the [ReAct Agent](/en/docs/eino/core_modules/flow_integration_components/react_agent_manual) as an example: |
| 77 | + |
| 78 | +``` |
| 79 | +import ( |
| 80 | + "github.com/cloudwego/eino/flow/agent/react" |
| 81 | + "github.com/cloudwego/eino-ext/components/tool/mcp" |
| 82 | +) |
| 83 | +
|
| 84 | +llm, err := /*create a chat model*/ |
| 85 | +tools, err := mcp.GetTools(ctx, &mcp.Config{Cli: cli}) |
| 86 | +
|
| 87 | +agent, err := react.NewAgent(ctx, &react.AgentConfig{ |
| 88 | + Model: llm, |
| 89 | + ToolsConfig: compose.ToolsNodeConfig{Tools: tools}, |
| 90 | +}) |
| 91 | +``` |
| 92 | + |
| 93 | +### SpecifyToolName |
| 94 | + |
| 95 | +You can use the field ToolNameList in config to specify the tools you want to call, avoiding calling unexpected tools: |
| 96 | + |
| 97 | +```go |
| 98 | +import "github.com/cloudwego/eino-ext/components/tool/mcp" |
| 99 | + |
| 100 | +tools, err := mcp.GetTools(ctx, &mcp.Config{ |
| 101 | + Cli: cli, |
| 102 | + ToolNameList: []string{"your tool name"}, |
| 103 | +}) |
| 104 | +``` |
| 105 | + |
| 106 | +## More Information |
| 107 | + |
| 108 | +Examples can be referred to: [https://github.com/cloudwego/eino-ext/blob/main/components/tool/mcp/examples/mcp.go](https://github.com/cloudwego/eino-ext/blob/main/components/tool/mcp/examples/mcp.go) |
0 commit comments