handleExecuteGraphQL executes a GraphQL query or mutation
(ctx context.Context, req mcp.CallToolRequest)
| 86 | |
| 87 | // handleExecuteGraphQL executes a GraphQL query or mutation |
| 88 | func (ms *mcpServer) handleExecuteGraphQL(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 89 | ctx = ms.effectiveContext(ctx) |
| 90 | // Check if raw queries are allowed |
| 91 | if !ms.service.conf.MCP.AllowRawQueries { |
| 92 | return mcp.NewToolResultError("raw queries are not allowed. Use execute_saved_query instead or enable allow_raw_queries in config."), nil |
| 93 | } |
| 94 | |
| 95 | args := req.GetArguments() |
| 96 | query, _ := args["query"].(string) |
| 97 | namespace, _ := args["namespace"].(string) |
| 98 | |
| 99 | if query == "" { |
| 100 | return mcp.NewToolResultError("query is required"), nil |
| 101 | } |
| 102 | |
| 103 | // Check if this is a mutation and if mutations are allowed |
| 104 | if isMutation(query) && !ms.service.conf.MCP.AllowMutations { |
| 105 | return mcp.NewToolResultError("mutations are not allowed. Enable allow_mutations in config."), nil |
| 106 | } |
| 107 | |
| 108 | // Convert variables map to JSON |
| 109 | var varsJSON json.RawMessage |
| 110 | if vars, ok := args["variables"].(map[string]any); ok && len(vars) > 0 { |
| 111 | // Expand cursor IDs to full encrypted cursors |
| 112 | expandedVars, err := ms.expandCursorIDs(ctx, vars) |
| 113 | if err != nil { |
| 114 | return mcp.NewToolResultError(fmt.Sprintf("cursor lookup failed: %v", err)), nil |
| 115 | } |
| 116 | varsJSON, err = json.Marshal(expandedVars) |
| 117 | if err != nil { |
| 118 | return mcp.NewToolResultError(fmt.Sprintf("invalid variables: %v", err)), nil |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | var rc core.RequestConfig |
| 123 | if namespace != "" { |
| 124 | rc.SetNamespace(namespace) |
| 125 | } else { |
| 126 | rc.SetNamespace(ms.getNamespace()) |
| 127 | } |
| 128 | ctx = ms.service.applyIdentityContext(ctx) |
| 129 | |
| 130 | if err := ms.service.checkGraphJinInitialized(); err != nil { |
| 131 | return mcp.NewToolResultError(err.Error()), nil |
| 132 | } |
| 133 | |
| 134 | res, err := ms.service.gj.GraphQL(ctx, query, varsJSON, &rc) |
| 135 | ms.service.recordGraphQLAccessFailures(ctx, query, res, err) |
| 136 | |
| 137 | result := ExecuteResult{} |
| 138 | if err != nil { |
| 139 | result.Errors = []ErrorInfo{ms.errorInfo(query, err.Error(), "execute_graphql")} |
| 140 | } else { |
| 141 | // Replace encrypted cursors with short numeric IDs for LLM-friendly responses |
| 142 | result.Data = ms.processCursorsForMCP(ctx, res.Data) |
| 143 | for _, e := range res.Errors { |
| 144 | result.Errors = append(result.Errors, ms.errorInfoFromCoreError(query, e, "execute_graphql")) |
| 145 | } |