{"record":{"id":"4bdc128c995e215e","repo":"dgraph-io/dgraph","slug":"error-running-query-w","errorCode":null,"errorMessage":"error running query: %w","messagePattern":"error running query: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"dgraph/cmd/mcp/mcp_server.go","lineNumber":339,"sourceCode":"\t\treturn mcp.NewToolResultText(string(resp.GetJson())), nil\n\t})\n\n\tschemaResource := mcp.NewResource(\n\t\t\"dgraph://schema\",\n\t\t\"dgraph_schema\",\n\t\tmcp.WithResourceDescription(\"The current Dgraph DQL schema\"),\n\t\tmcp.WithMIMEType(\"text/plain\"),\n\t)\n\n\ts.AddResource(schemaResource, func(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error) {\n\t\t// Execute operation\n\t\tconn, err := getConn(connectionString)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"error opening connection with Dgraph Alpha: %w\", err)\n\t\t}\n\t\tresp, err := conn.NewTxn().Query(ctx, \"schema {}\")\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"error running query: %w\", err)\n\t\t}\n\n\t\treturn []mcp.ResourceContents{\n\t\t\tmcp.TextResourceContents{\n\t\t\t\tURI:      \"dgraph://schema\",\n\t\t\t\tMIMEType: \"text/plain\",\n\t\t\t\tText:     string(resp.Json),\n\t\t\t},\n\t\t}, nil\n\t})\n\n\tcommonQueriesTool := mcp.NewTool(\"get_common_queries\",\n\t\tmcp.WithDescription(\"Get common queries that you can run on the db. If you are seeing issues with your queries, you can check this tool once.\"),\n\t\tmcp.WithToolAnnotation(mcp.ToolAnnotation{\n\t\t\tReadOnlyHint:    &True,\n\t\t\tDestructiveHint: &False,\n\t\t\tIdempotentHint:  &True,\n\t\t\tOpenWorldHint:   &False,","sourceCodeStart":321,"sourceCodeEnd":357,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/dgraph/cmd/mcp/mcp_server.go#L321-L357","documentation":"The schema MCP resource opened a connection successfully but the `schema {}` query transaction on the Alpha failed; the underlying query error is wrapped with %w. This indicates the Alpha accepted the connection but the read-only schema query errored (cluster not ready, leadership election in progress, internal error, or canceled context).","triggerScenarios":"Calling the dgraph://schema resource while the cluster is still booting or has no elected leader; the Alpha connection drops mid-query; context deadline exceeded on a slow cluster.","commonSituations":"Querying a brand-new single-node cluster before it is healthy; zero/alpha split-brain during startup; cluster under heavy load causing timeouts.","solutions":["Wait for the cluster to be healthy (`/health` returns all good, leader elected) and retry the resource read","Check Alpha logs for the underlying query error and fix the cluster issue it reports","Increase the MCP client timeout if the cluster is slow","Retry the request — transient election/consensus errors usually resolve"],"exampleFix":"// retry pattern\nfor i := 0; i < 3; i++ {\n    resp, err := conn.NewTxn().Query(ctx, \"schema {}\")\n    if err == nil { break }\n    time.Sleep(2 * time.Second)\n}","handlingStrategy":"retry","validationCode":"// Confirm a leader is elected before querying schema\nconst state = await (await fetch('http://alpha:8080/state')).json();\nif (!state.leader) throw new Error('Cluster has no leader yet; defer schema query');","typeGuard":null,"tryCatchPattern":"try {\n  resp = await conn.NewTxn().Query(ctx, 'schema {}');\n} catch (e) {\n  if (isRetryable(e)) { // e.g. 'no leader', 'unavailable'\n    await sleep(1000 * attempt);\n    resp = await conn.NewTxn().Query(ctx, 'schema {}');\n  } else { throw e; }\n}","preventionTips":["Delay MCP tool/resource registration until /health reports the cluster is all good","Increase client timeouts on slow or loaded clusters","Check Alpha logs for the underlying wrapped query error"],"tags":["query","grpc","cluster-health","mcp"],"backgroundTag":"query-failed","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}