{"record":{"id":"98de126ba35dcc23","repo":"microsoft/semantic-kernel","slug":"please-provide-a-valid-openai-apikey-to-run-this-s","errorCode":null,"errorMessage":"Please provide a valid OpenAI:ApiKey to run this sample. See the associated README.md for more details.","messagePattern":"Please provide a valid OpenAI:ApiKey to run this sample\\. See the associated README\\.md for more details\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"dotnet/samples/Demos/ModelContextProtocolClientServer/MCPClient/Samples/BaseSample.cs","lineNumber":90,"sourceCode":"    }\n\n    /// <summary>\n    /// Creates an instance of <see cref=\"Kernel\"/> with the OpenAI chat completion service registered.\n    /// </summary>\n    /// <returns>An instance of <see cref=\"Kernel\"/>.</returns>\n    protected static Kernel CreateKernelWithChatCompletionService()\n    {\n        // Load and validate configuration\n        IConfigurationRoot config = new ConfigurationBuilder()\n            .AddUserSecrets<Program>()\n            .AddEnvironmentVariables()\n            .Build();\n\n        if (config[\"OpenAI:ApiKey\"] is not { } apiKey)\n        {\n            const string Message = \"Please provide a valid OpenAI:ApiKey to run this sample. See the associated README.md for more details.\";\n            Console.Error.WriteLine(Message);\n            throw new InvalidOperationException(Message);\n        }\n\n        string modelId = config[\"OpenAI:ChatModelId\"] ?? \"gpt-4o-mini\";\n\n        // Create kernel\n        var kernelBuilder = Kernel.CreateBuilder();\n        kernelBuilder.Services.AddOpenAIChatCompletion(modelId: modelId, apiKey: apiKey);\n\n        return kernelBuilder.Build();\n    }\n\n    /// <summary>\n    /// Displays the list of available MCP tools.\n    /// </summary>\n    /// <param name=\"tools\">The list of the tools to display.</param>\n    protected static void DisplayTools(IList<McpClientTool> tools)\n    {\n        Console.WriteLine(\"Available MCP tools:\");","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/samples/Demos/ModelContextProtocolClientServer/MCPClient/Samples/BaseSample.cs#L72-L108","documentation":"BaseSample.CreateKernelWithChatCompletionService builds config from user secrets + env vars and throws InvalidOperationException if OpenAI:ApiKey is absent. ChatModelId defaults to gpt-4o-mini if unset, but the API key is mandatory and has no default. The message is written to stderr before throwing.","triggerScenarios":"config[\"OpenAI:ApiKey\"] is null after merging user secrets and environment variables.","commonSituations":"First run without user secrets, OPENAI_API_KEY env var not exported, or user-secrets ID bound to a different project.","solutions":["dotnet user-secrets set OpenAI:ApiKey <your-key>.","Or export OPENAI__ApiKey environment variable (use the __ hierarchy separator).","Optionally set OpenAI:ChatModelId if you don't want the gpt-4o-mini default.","Verify the user-secrets ID matches this project so secrets resolve."],"exampleFix":"// before\nif (config[\"OpenAI:ApiKey\"] is not { } apiKey)\n{\n    const string Message = \"Please provide a valid OpenAI:ApiKey to run this sample...\";\n    Console.Error.WriteLine(Message);\n    throw new InvalidOperationException(Message);\n}\n\n// after (user secret)\n// dotnet user-secrets set OpenAI:ApiKey sk-...\n// dotnet user-secrets set OpenAI:ChatModelId gpt-4o","handlingStrategy":"validation","validationCode":"if (config[\"OpenAI:ApiKey\"] is not { } apiKey)\n    throw new InvalidOperationException(\n        \"Set OpenAI:ApiKey via user secrets or OPENAI__ApiKey env var.\");","typeGuard":"static bool HasOpenAiKey(IConfigurationRoot c) =>\n    !string.IsNullOrEmpty(c[\"OpenAI:ApiKey\"]);","tryCatchPattern":null,"preventionTips":["Use dotnet user-secrets for the API key locally.","Use OPENAI__ApiKey for CI/containers.","Verify the user-secrets ID matches the project."],"tags":["configuration","openai","user-secrets","samples"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}