|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + |
| 9 | + "github.com/anthropics/anthropic-sdk-go" |
| 10 | + "github.com/anthropics/anthropic-sdk-go/option" |
| 11 | + "github.com/joho/godotenv" |
| 12 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 13 | +) |
| 14 | + |
| 15 | +const serverPath = "../backend/server" |
| 16 | + |
| 17 | +func main() { |
| 18 | + ctx := context.Background() |
| 19 | + godotenv.Load("../.env") |
| 20 | + |
| 21 | + apiKey := os.Getenv("ANTHROPIC_API_KEY") |
| 22 | + if apiKey == "" { |
| 23 | + log.Fatal("ANTHROPIC_API_KEY environment variable not set") |
| 24 | + } |
| 25 | + |
| 26 | + if _, err := os.Stat(serverPath); os.IsNotExist(err) { |
| 27 | + log.Fatalf("Server not found: %s", serverPath) |
| 28 | + } |
| 29 | + |
| 30 | + // Connect to MCP server |
| 31 | + client := mcp.NewClient(&mcp.Implementation{Name: "mcp-client", Version: "v1.0.0"}, nil) |
| 32 | + transport := &mcp.CommandTransport{Command: exec.Command(serverPath)} |
| 33 | + session, err := client.Connect(ctx, transport, nil) |
| 34 | + if err != nil { |
| 35 | + log.Fatal(err) |
| 36 | + } |
| 37 | + defer session.Close() |
| 38 | + |
| 39 | + tools, _ := session.ListTools(ctx, &mcp.ListToolsParams{}) |
| 40 | + |
| 41 | + claudeTools := make([]anthropic.ToolUnionParam, 0) |
| 42 | + for _, tool := range tools.Tools { |
| 43 | + inputSchemaMap, ok := tool.InputSchema.(map[string]interface{}) |
| 44 | + if !ok || inputSchemaMap == nil { |
| 45 | + inputSchemaMap = map[string]interface{}{} |
| 46 | + } |
| 47 | + |
| 48 | + properties := inputSchemaMap["properties"] |
| 49 | + required, _ := inputSchemaMap["required"].([]string) |
| 50 | + |
| 51 | + toolParam := anthropic.ToolUnionParamOfTool( |
| 52 | + anthropic.ToolInputSchemaParam{ |
| 53 | + Type: "object", |
| 54 | + Properties: properties, |
| 55 | + Required: required, |
| 56 | + }, |
| 57 | + tool.Name, |
| 58 | + ) |
| 59 | + |
| 60 | + toolParam.OfTool.Description = anthropic.String(tool.Description) |
| 61 | + |
| 62 | + claudeTools = append(claudeTools, toolParam) |
| 63 | + } |
| 64 | + anthropicClient := anthropic.NewClient( |
| 65 | + option.WithAPIKey(apiKey), |
| 66 | + ) |
| 67 | + message, err := anthropicClient.Messages.New(ctx, anthropic.MessageNewParams{ |
| 68 | + Model: anthropic.ModelClaude_3_Haiku_20240307, |
| 69 | + MaxTokens: 256, |
| 70 | + Messages: []anthropic.MessageParam{ |
| 71 | + anthropic.NewUserMessage(anthropic.NewTextBlock("Give me all failed agents")), |
| 72 | + }, |
| 73 | + Tools: claudeTools, |
| 74 | + }) |
| 75 | + if err != nil { |
| 76 | + log.Fatal(err) |
| 77 | + } |
| 78 | + |
| 79 | + for _, block := range message.Content { |
| 80 | + switch block := block.AsAny().(type) { |
| 81 | + case anthropic.TextBlock: |
| 82 | + println(block.Text) |
| 83 | + println() |
| 84 | + case anthropic.ToolUseBlock: |
| 85 | + params := &mcp.CallToolParams{ |
| 86 | + Name: block.Name, |
| 87 | + Arguments: block.Input, |
| 88 | + } |
| 89 | + res, err := session.CallTool(ctx, params) |
| 90 | + if err != nil { |
| 91 | + log.Fatalf("CallTool failed: %v", err) |
| 92 | + } |
| 93 | + if res.IsError { |
| 94 | + log.Fatal("tool failed") |
| 95 | + } |
| 96 | + for _, c := range res.Content { |
| 97 | + log.Print(c.(*mcp.TextContent).Text) |
| 98 | + println() |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments