{"record":{"id":"ea331b247f89ef37","repo":"microsoft/aspire","slug":"toolbox-mcp-request-failed-error-getrawtext","errorCode":null,"errorMessage":"Toolbox MCP request failed: {error.GetRawText()}","messagePattern":"Toolbox MCP request failed: (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Foundry/Toolbox/FoundryToolboxReadinessProbe.cs","lineNumber":222,"sourceCode":"            using var candidate = JsonDocument.Parse(responseMessage);\n            if (candidate.RootElement.TryGetProperty(\"id\", out var responseId) &&\n                responseId.ValueKind == JsonValueKind.Number &&\n                responseId.GetInt32() == expectedId)\n            {\n                matchingResponse = candidate.RootElement.Clone();\n                break;\n            }\n        }\n\n        if (matchingResponse is null)\n        {\n            throw new InvalidOperationException(\n                $\"The Toolbox MCP response did not contain JSON-RPC response ID {expectedId}.\");\n        }\n\n        if (matchingResponse.Value.TryGetProperty(\"error\", out var error))\n        {\n            throw new InvalidOperationException($\"Toolbox MCP request failed: {error.GetRawText()}\");\n        }\n\n        var result = matchingResponse.Value.TryGetProperty(\"result\", out var resultElement)\n            ? resultElement.Clone()\n            : default;\n        return new(result, responseSessionId);\n    }\n\n    private sealed record McpResponse(\n        JsonElement Result,\n        string? SessionId,\n        bool IsRetryableFailure = false);\n}\n","sourceCodeStart":204,"sourceCodeEnd":236,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Foundry/Toolbox/FoundryToolboxReadinessProbe.cs#L204-L236","documentation":"Thrown by SendRequestAsync when the JSON-RPC response correlated to the expected ID contains an 'error' member. This surfaces the remote Toolbox/MCP server-side error (method not found, invalid params, auth failure, etc.) verbatim from error.GetRawText() as an InvalidOperationException locally.","triggerScenarios":"Any initialize or tools/list request that the Toolbox MCP server answers with a JSON-RPC error object — invalid session ID, unsupported method, malformed request payload, insufficient permissions, or server-side failure.","commonSituations":"Token lacks the required Foundry scope (401/403 mapped to a JSON-RPC error); the probe sends a method the deployed server version doesn't implement; session was invalidated server-side between requests; request payload doesn't match the server's expected schema.","solutions":["Read the raw error text in the exception message — it names the exact server-side cause.","Fix authentication: ensure the access token has the correct Foundry/Toolbox scopes.","Check that the MCP method/params sent match the server's protocol version; update the Aspire.Hosting.Foundry package if the server is newer.","Inspect Toolbox server logs around the failure time for the underlying exception."],"exampleFix":"// before\n// token from stale MicrosoftEntra credentials\ntoken = await oldCredential.GetTokenAsync(scope);\n// after\ntoken = (await defaultAzureCredential.GetTokenAsync(new TokenRequestContext([\"https://ai.azure.com/.default\"]), ct)).Token;","handlingStrategy":"try-catch","validationCode":"// Before probing, verify the token is valid for the Foundry scope:\n// var tok = await credential.GetTokenAsync(new TokenRequestContext([\"https://ai.azure.com/.default\"]), ct);","typeGuard":null,"tryCatchPattern":"try\n{\n    await probe.WaitForToolsAsync(accessToken, ct);\n}\ncatch (InvalidOperationException ex) when (ex.Message.StartsWith(\"Toolbox MCP request failed:\"))\n{\n    // ex.Message carries the raw JSON-RPC error payload; log it verbatim for diagnosis.\n    logger.LogError(ex, \"Toolbox rejected an MCP request.\");\n    throw;\n}","preventionTips":["Issue access tokens with the correct Foundry/Toolbox scopes before probing.","Keep the Aspire.Hosting.Foundry package aligned with the deployed Toolbox protocol version.","Check server logs whenever a JSON-RPC error is surfaced to find the root cause."],"tags":["mcp","json-rpc","api-error","foundry-toolbox"],"backgroundTag":"api-error-response","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}