{"record":{"id":"4887f134eb39e241","repo":"microsoft/semantic-kernel","slug":"please-provide-a-valid-openai-apikey-to-run-this-s-4887f1","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/MCPServer/Program.cs","lineNumber":74,"sourceCode":"\nawait builder.Build().RunAsync();\n\n/// <summary>\n/// Gets configuration.\n/// </summary>\nstatic (string EmbeddingModelId, string ChatModelId, string ApiKey) GetConfiguration()\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 embeddingModelId = config[\"OpenAI:EmbeddingModelId\"] ?? \"text-embedding-3-small\";\n\n    string chatModelId = config[\"OpenAI:ChatModelId\"] ?? \"gpt-4o-mini\";\n\n    return (embeddingModelId, chatModelId, apiKey);\n}\nstatic ResourceTemplateDefinition CreateVectorStoreSearchResourceTemplate(Kernel? kernel = null)\n{\n    return new ResourceTemplateDefinition\n    {\n        Kernel = kernel,\n        ResourceTemplate = new()\n        {\n            UriTemplate = \"vectorStore://{collection}/{prompt}\",\n            Name = \"Vector Store Record Retrieval\",\n            Description = \"Retrieves relevant records from the vector store based on the provided prompt.\"","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/samples/Demos/ModelContextProtocolClientServer/MCPServer/Program.cs#L56-L92","documentation":"MCPServer's GetConfiguration builds config from user secrets + env vars and throws InvalidOperationException if OpenAI:ApiKey is absent. EmbeddingModelId and ChatModelId have defaults (text-embedding-3-small, gpt-4o-mini), but the API key is mandatory. The message is written to stderr first, then thrown.","triggerScenarios":"config[\"OpenAI:ApiKey\"] is null after merging user secrets and environment variables.","commonSituations":"Server run without user secrets set, OPENAI__ApiKey not exported, or the user-secrets ID bound to a different assembly.","solutions":["dotnet user-secrets set OpenAI:ApiKey <key>.","Or export OPENAI__ApiKey (and optionally OPENAI__EmbeddingModelId / OPENAI__ChatModelId) env vars.","Verify the user-secrets ID matches this MCPServer project.","Confirm the key is valid for the models you reference."],"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\n// dotnet user-secrets set OpenAI:EmbeddingModelId text-embedding-3-small","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 set OpenAI:ApiKey locally.","Use OPENAI__ApiKey (and optional OPENAI__ChatModelId/OPENAI__EmbeddingModelId) for CI.","Confirm the user-secrets ID matches this MCPServer project."],"tags":["configuration","openai","user-secrets","mcp-server","samples"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}