Tool - MCP

Introduction

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.

This section introduces the adapter of MCPTool, which implements the Eino Tool.

Other adapters:

ChatTemplate - MCP

HowToUse

Eino MCP adapters referenced the open-source SDK mcp-go, first initialize a MCP client:

import "github.com/mark3labs/mcp-go/client"

// stdio client
cli, err := client.NewStdioMCPClient(myCommand, myEnvs, myArgs...)

// sse client
cli, err := client.NewSSEMCPClient(myBaseURL)
// sse client  needs to manually start asynchronous communication
// while stdio does not require it.
err = cli.Start(ctx)

Considering the reusability of multiple adapters for the MCP client, the adapter assumes that the client has completed initialization with the Server’s Initialize, so users need to complete the client initialization themselves, for example:

import "github.com/mark3labs/mcp-go/mcp"

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)

Then use the Client to create the adapter , which implements Eino Tool:

import "github.com/cloudwego/eino-ext/components/tool/mcp"

tools, err := mcp.GetTools(ctx, &mcp.Config{Cli: cli})

You can call this adapter directly:

for i, mcpTool := range mcpTools {
    fmt.Println(i, ":")
    info, err := mcpTool.Info(ctx)
    if err != nil {
       log.Fatal(err)
    }
    fmt.Println("Name:", info.Name)
    fmt.Println("Desc:", info.Desc)
    fmt.Println()
}

It can also be used in any Eino Agent, taking the ReAct Agent as an example:

import (
    "github.com/cloudwego/eino/flow/agent/react"
    "github.com/cloudwego/eino-ext/components/tool/mcp"    
)

llm, err := /*create a chat model*/
tools, err := mcp.GetTools(ctx, &mcp.Config{Cli: cli})

agent, err := react.NewAgent(ctx, &react.AgentConfig{
    Model:                 llm,
    ToolsConfig:           compose.ToolsNodeConfig{Tools: tools},
})

SpecifyToolName

You can use the field ToolNameList in config to specify the tools you want to call, avoiding calling unexpected tools:

import "github.com/cloudwego/eino-ext/components/tool/mcp"

tools, err := mcp.GetTools(ctx, &mcp.Config{
    Cli: cli,
    ToolNameList: []string{"your tool name"},
})

More Information

Examples can be referred to: https://github.com/cloudwego/eino-ext/blob/main/components/tool/mcp/examples/mcp.go


Last modified March 28, 2025 : docs: eino graph update (#1293) (aea58cb)