feat: add GetEinoTool

This commit is contained in:
lilong.129
2025-05-16 14:06:01 +08:00
parent f8b7a42560
commit 9b77bd1fd2
3 changed files with 52 additions and 1 deletions

View File

@@ -1 +1 @@
v5.0.0-beta-2505161337
v5.0.0-beta-2505161406

View File

@@ -9,6 +9,8 @@ import (
"time"
"github.com/bytedance/sonic"
mcpp "github.com/cloudwego/eino-ext/components/tool/mcp"
"github.com/cloudwego/eino/components/tool"
"github.com/rs/zerolog/log"
)
@@ -183,3 +185,33 @@ func (h *MCPHost) ExportToolsToJSON(ctx context.Context, dumpPath string) error
log.Info().Str("path", dumpPath).Msg("Tools records exported successfully")
return nil
}
// GetEinoTool returns an eino tool from the MCP server
func (h *MCPHost) GetEinoTool(ctx context.Context, serverName, toolName string) (tool.BaseTool, error) {
h.mu.RLock()
defer h.mu.RUnlock()
// filter MCP server by serverName
conn, exists := h.connections[serverName]
if !exists {
return nil, fmt.Errorf("no connection found for server %s", serverName)
}
if conn.Config.IsDisabled() {
return nil, fmt.Errorf("server %s is disabled", serverName)
}
// get tools from MCP server and convert to eino tools
tools, err := mcpp.GetTools(ctx, &mcpp.Config{
Cli: conn.Client,
ToolNameList: []string{toolName},
})
if err != nil || len(tools) == 0 {
log.Error().Err(err).
Str("server", serverName).Str("tool", toolName).
Msg("get MCP tool failed")
return nil, err
}
return tools[0], nil
}

View File

@@ -7,6 +7,7 @@ import (
"testing"
"time"
"github.com/cloudwego/eino/components/tool"
"github.com/mark3labs/mcp-go/mcp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -239,3 +240,21 @@ func TestConvertToolsToRecords(t *testing.T) {
})
}
}
func TestCallEinoTool(t *testing.T) {
hub, err := NewMCPHost("./testdata/test.mcp.json")
require.NoError(t, err)
ctx := context.Background()
err = hub.InitServers(ctx)
require.NoError(t, err)
einoTool, err := hub.GetEinoTool(ctx, "weather", "get_alerts")
require.NoError(t, err)
t.Logf("Tool: %v", einoTool)
tool := einoTool.(tool.InvokableTool)
result, err := tool.InvokableRun(ctx, `{"state": "CA"}`)
require.NoError(t, err)
t.Logf("Result: %v", result)
}