()
| 19 | ) |
| 20 | |
| 21 | func main() { |
| 22 | fmt.Println("=== Agent SDK - MCP 集成示例 ===") |
| 23 | |
| 24 | // 1. 创建工具注册表 |
| 25 | toolRegistry := tools.NewRegistry() |
| 26 | |
| 27 | // 2. 注册内置工具 |
| 28 | builtin.RegisterAll(toolRegistry) |
| 29 | |
| 30 | // 3. 创建 MCP Manager 并添加 MCP Server |
| 31 | mcpManager := mcp.NewMCPManager(toolRegistry) |
| 32 | |
| 33 | // 示例: 添加一个 MCP Server (需要替换为实际的 MCP Server 地址) |
| 34 | mcpEndpoint := os.Getenv("MCP_ENDPOINT") |
| 35 | if mcpEndpoint == "" { |
| 36 | mcpEndpoint = "http://localhost:8080/mcp" // 默认地址 |
| 37 | } |
| 38 | |
| 39 | mcpAccessKey := os.Getenv("MCP_ACCESS_KEY") |
| 40 | mcpSecretKey := os.Getenv("MCP_SECRET_KEY") |
| 41 | |
| 42 | fmt.Printf("配置 MCP Server:\n") |
| 43 | fmt.Printf(" Endpoint: %s\n", mcpEndpoint) |
| 44 | fmt.Printf(" Access Key: %s\n\n", maskString(mcpAccessKey)) |
| 45 | |
| 46 | // 添加 MCP Server |
| 47 | server, err := mcpManager.AddServer(&mcp.MCPServerConfig{ |
| 48 | ServerID: "my-mcp-server", |
| 49 | Endpoint: mcpEndpoint, |
| 50 | AccessKeyID: mcpAccessKey, |
| 51 | AccessKeySecret: mcpSecretKey, |
| 52 | }) |
| 53 | |
| 54 | if err != nil { |
| 55 | log.Printf("⚠️ 添加 MCP Server 失败: %v", err) |
| 56 | log.Println(" 提示: 如果没有实际的 MCP Server,这是正常的") |
| 57 | log.Println(" 继续使用内置工具运行示例...") |
| 58 | } else { |
| 59 | fmt.Printf("✓ MCP Server 已添加: %s\n", server.GetServerID()) |
| 60 | |
| 61 | // 4. 连接到 MCP Server 并注册工具 |
| 62 | ctx := context.Background() |
| 63 | if err := mcpManager.ConnectServer(ctx, "my-mcp-server"); err != nil { |
| 64 | log.Printf("⚠️ 连接 MCP Server 失败: %v\n", err) |
| 65 | log.Println(" 提示: 确保 MCP Server 正在运行并且可访问") |
| 66 | log.Println(" 继续使用内置工具运行示例...") |
| 67 | } else { |
| 68 | fmt.Printf("✓ 已连接到 MCP Server\n") |
| 69 | fmt.Printf(" 发现工具数量: %d\n\n", server.GetToolCount()) |
| 70 | |
| 71 | // 列出所有 MCP 工具 |
| 72 | tools := server.ListTools() |
| 73 | if len(tools) > 0 { |
| 74 | fmt.Println("可用的 MCP 工具:") |
| 75 | for i, tool := range tools { |
| 76 | fmt.Printf(" %d. %s - %s\n", i+1, tool.Name, tool.Description) |
| 77 | } |
| 78 | fmt.Println() |
nothing calls this directly
no test coverage detected