|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/github/github-mcp-server/pkg/translations" |
| 10 | + "github.com/mark3labs/mcp-go/mcp" |
| 11 | + "github.com/mark3labs/mcp-go/server" |
| 12 | +) |
| 13 | + |
| 14 | +// ExecuteGraphQLQuery creates a tool to execute a GraphQL query and return results |
| 15 | +func ExecuteGraphQLQuery(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 16 | + return mcp.NewTool("execute_graphql_query", |
| 17 | + mcp.WithDescription(t("TOOL_EXECUTE_GRAPHQL_QUERY_DESCRIPTION", "Execute a GraphQL query against GitHub's API and return the results.")), |
| 18 | + mcp.WithToolAnnotation(mcp.ToolAnnotation{ |
| 19 | + Title: t("TOOL_EXECUTE_GRAPHQL_QUERY_USER_TITLE", "Execute GraphQL query"), |
| 20 | + ReadOnlyHint: ToBoolPtr(false), |
| 21 | + }), |
| 22 | + mcp.WithString("query", |
| 23 | + mcp.Required(), |
| 24 | + mcp.Description("The GraphQL query string to execute"), |
| 25 | + ), |
| 26 | + mcp.WithObject("variables", |
| 27 | + mcp.Description("Variables for the GraphQL query (optional)"), |
| 28 | + ), |
| 29 | + ), |
| 30 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 31 | + queryStr, err := RequiredParam[string](request, "query") |
| 32 | + if err != nil { |
| 33 | + return mcp.NewToolResultError(err.Error()), nil |
| 34 | + } |
| 35 | + |
| 36 | + variables, _ := OptionalParam[map[string]interface{}](request, "variables") |
| 37 | + if variables == nil { |
| 38 | + variables = make(map[string]interface{}) |
| 39 | + } |
| 40 | + |
| 41 | + client, err := getClient(ctx) |
| 42 | + if err != nil { |
| 43 | + return nil, fmt.Errorf("failed to get GitHub client: %w", err) |
| 44 | + } |
| 45 | + |
| 46 | + // Create a GraphQL request payload |
| 47 | + graphqlPayload := map[string]interface{}{ |
| 48 | + "query": queryStr, |
| 49 | + "variables": variables, |
| 50 | + } |
| 51 | + |
| 52 | + // Use the underlying HTTP client to make a raw GraphQL request |
| 53 | + req, err := client.NewRequest("POST", "graphql", graphqlPayload) |
| 54 | + if err != nil { |
| 55 | + return mcp.NewToolResultError(fmt.Sprintf("failed to create request: %v", err)), nil |
| 56 | + } |
| 57 | + |
| 58 | + // Execute the request |
| 59 | + var response map[string]interface{} |
| 60 | + _, err = client.Do(ctx, req, &response) |
| 61 | + |
| 62 | + result := map[string]interface{}{ |
| 63 | + "query": queryStr, |
| 64 | + "variables": variables, |
| 65 | + } |
| 66 | + |
| 67 | + if err != nil { |
| 68 | + // Query execution failed |
| 69 | + result["success"] = false |
| 70 | + result["error"] = err.Error() |
| 71 | + |
| 72 | + // Try to categorize the error |
| 73 | + errorStr := err.Error() |
| 74 | + if strings.Contains(errorStr, "rate limit") { |
| 75 | + result["error_type"] = "rate_limit" |
| 76 | + } else if strings.Contains(errorStr, "unauthorized") || strings.Contains(errorStr, "authentication") { |
| 77 | + result["error_type"] = "authentication" |
| 78 | + } else if strings.Contains(errorStr, "permission") || strings.Contains(errorStr, "forbidden") { |
| 79 | + result["error_type"] = "permission" |
| 80 | + } else if strings.Contains(errorStr, "not found") || strings.Contains(errorStr, "Could not resolve") || strings.Contains(errorStr, "not exist") { |
| 81 | + result["error_type"] = "not_found" |
| 82 | + } else { |
| 83 | + result["error_type"] = "execution_error" |
| 84 | + } |
| 85 | + } else { |
| 86 | + // Query executed successfully |
| 87 | + result["success"] = true |
| 88 | + result["data"] = response["data"] |
| 89 | + |
| 90 | + // Include any errors from the GraphQL response |
| 91 | + if errors, ok := response["errors"]; ok { |
| 92 | + result["graphql_errors"] = errors |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + r, err := json.Marshal(result) |
| 97 | + if err != nil { |
| 98 | + return nil, fmt.Errorf("failed to marshal response: %w", err) |
| 99 | + } |
| 100 | + |
| 101 | + return mcp.NewToolResultText(string(r)), nil |
| 102 | + } |
| 103 | +} |
0 commit comments