{"record":{"id":"675abab62f518d50","repo":"charmbracelet/crush","slug":"failed-to-read-mcp-resource-status-code-d","errorCode":null,"errorMessage":"failed to read MCP resource: status code %d","messagePattern":"failed to read MCP resource: status code (.+?)","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/client/config.go","lineNumber":315,"sourceCode":"\tdefer rsp.Body.Close()\n\tif rsp.StatusCode != http.StatusOK {\n\t\treturn fmt.Errorf(\"failed to refresh MCP tools: status code %d\", rsp.StatusCode)\n\t}\n\treturn nil\n}\n\n// ReadMCPResource reads a resource from a named MCP server.\nfunc (c *Client) ReadMCPResource(ctx context.Context, id, name, uri string) ([]MCPResourceContents, error) {\n\trsp, err := c.post(ctx, fmt.Sprintf(\"/workspaces/%s/mcp/read-resource\", id), nil, jsonBody(struct {\n\t\tName string `json:\"name\"`\n\t\tURI  string `json:\"uri\"`\n\t}{Name: name, URI: uri}), http.Header{\"Content-Type\": []string{\"application/json\"}})\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to read MCP resource: %w\", err)\n\t}\n\tdefer rsp.Body.Close()\n\tif rsp.StatusCode != http.StatusOK {\n\t\treturn nil, fmt.Errorf(\"failed to read MCP resource: status code %d\", rsp.StatusCode)\n\t}\n\tvar contents []MCPResourceContents\n\tif err := json.NewDecoder(rsp.Body).Decode(&contents); err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to decode MCP resource: %w\", err)\n\t}\n\treturn contents, nil\n}\n\nfunc (c *Client) ListMCPPrompts(ctx context.Context, id string) ([]proto.MCPPrompt, error) {\n\trsp, err := c.get(ctx, fmt.Sprintf(\"/workspaces/%s/mcp/prompts\", id), nil, nil)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to list MCP prompts: %w\", err)\n\t}\n\tdefer rsp.Body.Close()\n\tif rsp.StatusCode != http.StatusOK {\n\t\treturn nil, fmt.Errorf(\"failed to list MCP prompts: status code %d\", rsp.StatusCode)\n\t}\n\tvar prompts []proto.MCPPrompt","sourceCodeStart":297,"sourceCodeEnd":333,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/client/config.go#L297-L333","documentation":"ReadMCPResource requires an HTTP 200 from /workspaces/{id}/mcp/read-resource. Any other status produces 'failed to read MCP resource: status code %d'. The response body (which may hold server-side error details) is discarded.","triggerScenarios":"Server replies 401/403 (auth), 404 (workspace ID or MCP server name not found), 400 (malformed URI), or 500 (MCP server failed to serve the resource) instead of 200.","commonSituations":"Resource URI typo (wrong scheme/path for that MCP server); MCP server name mismatch with config; expired credentials; workspace stopped.","solutions":["Match the status code to action: 404 → verify MCP server name and workspace ID; 401 → re-authenticate.","Verify the resource URI exists on that MCP server (list resources first if possible).","Check server logs for 5xx root causes.","Ensure the workspace is running before reading resources."],"exampleFix":"// before\ncontents, err := client.ReadMCPResource(ctx, id, name, uri)\n// after\ncontents, err := client.ReadMCPResource(ctx, id, name, uri)\nif err != nil && strings.Contains(err.Error(), \"status code 404\") {\n    return nil, fmt.Errorf(\"resource %q not found on MCP server %q (check server name and URI)\", uri, name)\n}","handlingStrategy":"try-catch","validationCode":"// pre-check: ensure the MCP server is configured and workspace is up\nservers, err := client.WorkspaceAgentMCPStatus(ctx, workspaceID)\n// or simply verify the resource URI scheme matches the server's expectations before calling","typeGuard":null,"tryCatchPattern":"contents, err := client.ReadMCPResource(ctx, id, name, uri)\nif err != nil {\n    var code int\n    if n, _ := fmt.Sscanf(err.Error(), \"failed to read MCP resource: status code %d\", &code); n == 1 {\n        if code == 401 || code == 403 {\n            // re-authenticate then retry once\n        } else if code == 404 {\n            return nil, fmt.Errorf(\"resource %q not found on server %q\", uri, name)\n        }\n    }\n    return nil, err\n}","preventionTips":["List resources/verify URIs before reading them.","Re-authenticate proactively when tokens are near expiry.","Ensure the workspace and MCP server are healthy before reads.","Check server logs when seeing 5xx status codes."],"tags":["http","status-code","mcp"],"backgroundTag":"unexpected-http-status","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}