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