dgraph-io/dgraph · error

error running query: %w

Error message

error running query: %w

What it means

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).

Source

Thrown at dgraph/cmd/mcp/mcp_server.go:339

		return mcp.NewToolResultText(string(resp.GetJson())), nil
	})

	schemaResource := mcp.NewResource(
		"dgraph://schema",
		"dgraph_schema",
		mcp.WithResourceDescription("The current Dgraph DQL schema"),
		mcp.WithMIMEType("text/plain"),
	)

	s.AddResource(schemaResource, func(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error) {
		// Execute operation
		conn, err := getConn(connectionString)
		if err != nil {
			return nil, fmt.Errorf("error opening connection with Dgraph Alpha: %w", err)
		}
		resp, err := conn.NewTxn().Query(ctx, "schema {}")
		if err != nil {
			return nil, fmt.Errorf("error running query: %w", err)
		}

		return []mcp.ResourceContents{
			mcp.TextResourceContents{
				URI:      "dgraph://schema",
				MIMEType: "text/plain",
				Text:     string(resp.Json),
			},
		}, nil
	})

	commonQueriesTool := mcp.NewTool("get_common_queries",
		mcp.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."),
		mcp.WithToolAnnotation(mcp.ToolAnnotation{
			ReadOnlyHint:    &True,
			DestructiveHint: &False,
			IdempotentHint:  &True,
			OpenWorldHint:   &False,

View on GitHub (pinned to 759e242be6)

Solutions

  1. Wait for the cluster to be healthy (`/health` returns all good, leader elected) and retry the resource read
  2. Check Alpha logs for the underlying query error and fix the cluster issue it reports
  3. Increase the MCP client timeout if the cluster is slow
  4. Retry the request — transient election/consensus errors usually resolve

Example fix

// retry pattern
for i := 0; i < 3; i++ {
    resp, err := conn.NewTxn().Query(ctx, "schema {}")
    if err == nil { break }
    time.Sleep(2 * time.Second)
}
Defensive patterns

Strategy: retry

Validate before calling

// Confirm a leader is elected before querying schema
const state = await (await fetch('http://alpha:8080/state')).json();
if (!state.leader) throw new Error('Cluster has no leader yet; defer schema query');

Try / catch

try {
  resp = await conn.NewTxn().Query(ctx, 'schema {}');
} catch (e) {
  if (isRetryable(e)) { // e.g. 'no leader', 'unavailable'
    await sleep(1000 * attempt);
    resp = await conn.NewTxn().Query(ctx, 'schema {}');
  } else { throw e; }
}

Prevention

When it happens

Trigger: 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.

Common situations: Querying a brand-new single-node cluster before it is healthy; zero/alpha split-brain during startup; cluster under heavy load causing timeouts.

Related errors


AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01). Data as JSON: /api/errors/4bdc128c995e215e. Report an issue: GitHub.