|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "io/ioutil" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/mark3labs/mcp-go/mcp" |
| 14 | + "github.com/mark3labs/mcp-go/server" |
| 15 | +) |
| 16 | + |
| 17 | +func main() { |
| 18 | + apiKeys := os.Getenv("DIFY_API_KEYS") |
| 19 | + if apiKeys == "" { |
| 20 | + fmt.Println("DIFY_API_KEYS environment variable is required") |
| 21 | + return |
| 22 | + } |
| 23 | + |
| 24 | + workflowNames := os.Getenv("DIFY_WORKFLOW_NAME") |
| 25 | + if workflowNames == "" { |
| 26 | + fmt.Println("DIFY_WORKFLOW_NAME environment variable is required") |
| 27 | + return |
| 28 | + } |
| 29 | + workflows := parseAPIKeys(apiKeys, workflowNames) |
| 30 | + baseURL := flag.String("base-url", "http://localhostr/v1", "Base URL for Dify API") |
| 31 | + flag.Parse() |
| 32 | + |
| 33 | + s := server.NewMCPServer( |
| 34 | + "Dify Workflow Server", |
| 35 | + "1.0.0", |
| 36 | + server.WithResourceCapabilities(true, true), |
| 37 | + server.WithLogging(), |
| 38 | + ) |
| 39 | + |
| 40 | + listWorkflowsTool := mcp.NewTool("list_workflows", |
| 41 | + mcp.WithDescription("List authorized workflows"), |
| 42 | + ) |
| 43 | + s.AddTool(listWorkflowsTool, listWorkflowsHandler(workflows)) |
| 44 | + |
| 45 | + executeWorkflowTool := mcp.NewTool("execute_workflow", |
| 46 | + mcp.WithDescription("Execute a specified workflow"), |
| 47 | + mcp.WithString("workflow_name", |
| 48 | + mcp.Required(), |
| 49 | + mcp.Description("Name of the workflow to execute"), |
| 50 | + ), |
| 51 | + mcp.WithString("input", |
| 52 | + mcp.Description("Input data for the workflow"), |
| 53 | + ), |
| 54 | + ) |
| 55 | + s.AddTool(executeWorkflowTool, executeWorkflowHandler(workflows, *baseURL)) |
| 56 | + |
| 57 | + if err := server.ServeStdio(s); err != nil { |
| 58 | + fmt.Printf("Server error: %v\n", err) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func parseAPIKeys(apiKeys, workflowNames string) map[string]string { |
| 63 | + workflows := make(map[string]string) |
| 64 | + |
| 65 | + apiKeyList := strings.Split(apiKeys, ",") |
| 66 | + workflowNameList := strings.Split(workflowNames, ",") |
| 67 | + |
| 68 | + if len(apiKeyList) != len(workflowNameList) { |
| 69 | + fmt.Printf("The number of API Keys does not match the number of workflow names\n") |
| 70 | + os.Exit(1) |
| 71 | + } |
| 72 | + |
| 73 | + for i, name := range workflowNameList { |
| 74 | + workflows[name] = apiKeyList[i] |
| 75 | + } |
| 76 | + |
| 77 | + return workflows |
| 78 | +} |
| 79 | + |
| 80 | +func listWorkflowsHandler(workflows map[string]string) func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 81 | + return func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 82 | + workflowNames := make([]string, 0, len(workflows)) |
| 83 | + for name := range workflows { |
| 84 | + workflowNames = append(workflowNames, name) |
| 85 | + } |
| 86 | + return mcp.NewToolResultText(fmt.Sprintf("Authorized workflows: %v", workflowNames)), nil |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func executeWorkflowHandler(workflows map[string]string, baseURL string) func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 91 | + return func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 92 | + workflowName := request.Params.Arguments["workflow_name"].(string) |
| 93 | + apiKey, ok := workflows[workflowName] |
| 94 | + if !ok { |
| 95 | + return mcp.NewToolResultError(fmt.Sprintf("Workflow %s not found", workflowName)), nil |
| 96 | + } |
| 97 | + |
| 98 | + input := "" |
| 99 | + if in, ok := request.Params.Arguments["input"].(string); ok { |
| 100 | + input = in |
| 101 | + } |
| 102 | + |
| 103 | + response, err := callDifyWorkflowAPI(baseURL, apiKey, input) |
| 104 | + if err != nil { |
| 105 | + return mcp.NewToolResultError(fmt.Sprintf("Failed to execute workflow: %v", err)), nil |
| 106 | + } |
| 107 | + |
| 108 | + return mcp.NewToolResultText(response), nil |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func callDifyWorkflowAPI(baseURL, apiKey, input string) (string, error) { |
| 113 | + client := &http.Client{} |
| 114 | + |
| 115 | + body := fmt.Sprintf(`{ |
| 116 | + "inputs": {"message":"%s"}, |
| 117 | + "response_mode": "blocking", |
| 118 | + "user": "mcp-server" |
| 119 | + }`, input) |
| 120 | + |
| 121 | + req, err := http.NewRequest("POST", fmt.Sprintf("%s/workflows/run", baseURL), strings.NewReader(body)) |
| 122 | + if err != nil { |
| 123 | + return "", err |
| 124 | + } |
| 125 | + |
| 126 | + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey)) |
| 127 | + req.Header.Set("Content-Type", "application/json") |
| 128 | + |
| 129 | + resp, err := client.Do(req) |
| 130 | + if err != nil { |
| 131 | + return "", err |
| 132 | + } |
| 133 | + defer resp.Body.Close() |
| 134 | + |
| 135 | + respBody, err := ioutil.ReadAll(resp.Body) |
| 136 | + if err != nil { |
| 137 | + return "", err |
| 138 | + } |
| 139 | + |
| 140 | + var completionResponse DifyResponse |
| 141 | + if err := json.Unmarshal(respBody, &completionResponse); err != nil { |
| 142 | + return "", err |
| 143 | + } |
| 144 | + |
| 145 | + if completionResponse.Data.Status == "succeeded" { |
| 146 | + return fmt.Sprintf("%s", completionResponse.Data.Outputs["text"]), nil |
| 147 | + } |
| 148 | + |
| 149 | + return "", fmt.Errorf("Workflow execution failed: %s", respBody) |
| 150 | +} |
0 commit comments