Skip to content

deepseek 使用agent stream 调用mcp #82

@peach-zhang

Description

@peach-zhang

`package main

import (
"context"
"errors"
"fmt"
"github.com/cloudwego/eino-ext/components/model/openai"
mcpp "github.com/cloudwego/eino-ext/components/tool/mcp"
"github.com/cloudwego/eino/compose"
"github.com/cloudwego/eino/flow/agent"
"github.com/cloudwego/eino/flow/agent/react"
"github.com/cloudwego/eino/schema"
"github.com/mark3labs/mcp-go/client"
"github.com/mark3labs/mcp-go/client/transport"
"github.com/mark3labs/mcp-go/mcp"
"io"
"log"
)
func main() {

ctx := context.Background()
llm, err := openai.NewChatModel(ctx, &openai.ChatModelConfig{
	APIKey:  "sk-130a9e31e7b149788xxxxxx2",
	Model:   "deepseek-reasoner",
	BaseURL: "https://api.deepseek.com/",
})
if err != nil {
	log.Fatal(err)
}

fmt.Println("Initializing HTTP client...")
httpURL := "http://192.168.123.180:8081/mcp"

headers := transport.WithHTTPHeaders(map[string]string{
	"Authorization": "890fce93-6d6f-4fa6-a17f-7c32b6dcac32",
})
httpTransport, err := transport.NewStreamableHTTP(httpURL, headers)
if err != nil {
	log.Fatalf("Failed to create HTTP transport: %v", err)
}
// Create client with the transport
newClient := client.NewClient(httpTransport)
fmt.Println("Initializing client...")
initRequest := mcp.InitializeRequest{}
initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
initRequest.Params.ClientInfo = mcp.Implementation{
	Name:    "MCP-Go Simple Client Example",
	Version: "1.0.0",
}
initRequest.Params.Capabilities = mcp.ClientCapabilities{}

_, err = newClient.Initialize(ctx, initRequest)
if err != nil {
	return
}

mcpTools, err := mcpp.GetTools(ctx, &mcpp.Config{Cli: newClient})
if err != nil {
	return
}
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()
}

ragent, err := react.NewAgent(ctx, &react.AgentConfig{
	ToolCallingModel: llm,
	ToolsConfig:      compose.ToolsNodeConfig{Tools: mcpTools},
})
if err != nil {
	log.Fatal(err)
}

basePrompt := `你是一个智能AI助手,具备使用各种工具的能力。你可以智能地分析用户需求,选择合适的工具来完成任务。

		核心能力:
		1. 理解用户意图和需求
		2. 判断是否需要使用工具
		3. 选择最合适的工具执行任务
		4. 基于工具结果生成准确的回答
		
		工作模式:
		- 当用户的请求可以直接回答时,无需使用工具
		- 当用户需要文件操作、搜索信息、获取实时数据时,主动使用相应工具
		- 工具调用失败时,尝试其他方法或明确告知用户
		
		回答要求:
		- 使用中文回答
		- 回答要准确、简洁、有用
		- 如果使用了工具,可以说明使用了什么工具`

sr, err := ragent.Stream(ctx, []*schema.Message{
	{
		Role:    schema.System,
		Content: basePrompt,
	},
	{
		Role:    schema.User,
		Content: "不使用参数,查询所有土地资源信息",
	},
}, agent.WithComposeOptions(compose.WithCallbacks(&LoggerCallback{})))
if err != nil {
	Errorf("failed to stream: %v", err)
	return
}

defer sr.Close()

Infof("\n\n===== start streaming =====\n\n")

for {
	recv, err := sr.Recv()
	if err != nil {
		if errors.Is(err, io.EOF) {
			// finish
			break
		}
		// error
		Infof("failed to recv: %v", err)
		return
	}
	if len(recv.ToolCalls) > 0 {
		for _, toolCall := range recv.ToolCalls {
			fmt.Println("tool call id:", toolCall.ID)
			fmt.Println("tool call type:", toolCall.Type)
		}
	}

}

Infof("\n\n===== finished =====\n")

}
`
需要怎么调用? 有具体的代码示例吗? 当前官方推荐怎么调用?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions