{"record":{"id":"ec7d2ce1e0d28cde","repo":"microsoft/semantic-kernel","slug":"azureai-project-client-not-found","errorCode":null,"errorMessage":"AzureAI project client not found.","messagePattern":"AzureAI project client not found\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Agents/AzureAI/Extensions/AgentDefinitionExtensions.cs","lineNumber":168,"sourceCode":"            {\n#pragma warning disable CA2000 // Dispose objects before losing scope, not relevant because the HttpClient is created and may be used elsewhere\n                var httpClient = HttpClientProvider.GetHttpClient(kernel.Services);\n#pragma warning restore CA2000 // Dispose objects before losing scope\n\n                var tokenCredential = kernel.Services.GetRequiredService<TokenCredential>();\n                AIProjectClientOptions options =\n                    new()\n                    {\n                        Transport = new HttpClientPipelineTransport(httpClient),\n                        RetryPolicy = new ClientRetryPolicy(maxRetries: 0) // Disable retry policy if a custom HttpClient is provided.\n                    };\n                return new AIProjectClient(new Uri(endpoint), tokenCredential, options);\n            }\n        }\n\n        // Return the client registered on the kernel\n        var client = kernel.GetAllServices<AIProjectClient>().FirstOrDefault();\n        return client ?? throw new InvalidOperationException(\"AzureAI project client not found.\");\n    }\n\n    #region private\n    private static CodeInterpreterToolResource? GetCodeInterpreterToolResource(this AgentDefinition agentDefinition)\n    {\n        Verify.NotNull(agentDefinition);\n\n        CodeInterpreterToolResource? resource = null;\n\n        var codeInterpreter = agentDefinition.GetFirstToolDefinition(CodeInterpreterType);\n        if (codeInterpreter is not null)\n        {\n            var fileIds = codeInterpreter.GetFileIds();\n            var dataSources = codeInterpreter.GetDataSources();\n            if (fileIds is not null || dataSources is not null)\n            {\n                resource = new CodeInterpreterToolResource();\n                if (fileIds is not null)","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Agents/AzureAI/Extensions/AgentDefinitionExtensions.cs#L150-L186","documentation":"Thrown by AgentDefinitionExtensions.GetProjectsClient when it cannot obtain an Azure AI Project client. The method first tries to build an AIProjectClient from the agent definition's Model.Connection endpoint; only if that connection (or its endpoint) is absent does it fall back to kernel.GetAllServices<AIProjectClient>(). If neither path yields a client, it throws InvalidOperationException. This is a startup/configuration error, not a transient network failure.","triggerScenarios":"Calling GetProjectsClient(agentDefinition, kernel) where agentDefinition.Model.Connection is null or lacks an 'endpoint' key, AND the Kernel has no AIProjectClient registered as a service. Also triggered when a TokenCredential is missing (that path throws earlier, but missing registration is the direct cause here).","commonSituations":"Developer forgot to register AIProjectClient on the kernel builder (e.g., missing AddAzureAIAgent / services.AddSingleton<AIProjectClient>). Agent YAML/JSON template missing the connection.endpoint field. Running in a DI container where the Azure AI services were never added. Misconfigured KernelFactory in tests.","solutions":["Register an AIProjectClient on the kernel: builder.Services.AddSingleton<AIProjectClient>(new AIProjectClient(new Uri(endpoint), credential));","Alternatively, set the connection endpoint in the agent definition model so GetProjectsClient builds the client itself.","Ensure a TokenCredential is also registered (kernel.Services.GetRequiredService<TokenCredential> is called on that path).","Verify the agent definition file's connection section is parsed correctly (check Model.Model.Connection?.ExtensionData for the 'endpoint' key)."],"exampleFix":"// before\nvar kernel = Kernel.CreateBuilder().Build();\n// ... agent uses AzureAI tools -> throws 160\n\n// after\nvar kernelBuilder = Kernel.CreateBuilder();\nkernelBuilder.Services.AddSingleton(new AIProjectClient(new Uri(endpoint), tokenCredential));\nvar kernel = kernelBuilder.Build();","handlingStrategy":"validation","validationCode":"// Before calling code that uses AzureAI tools, verify a client is resolvable.\nbool hasConnectionEndpoint =\n    agentDefinition?.Model?.Connection?.ExtensionData.TryGetValue(\"endpoint\", out _) ?? false;\nbool hasRegisteredClient = kernel.GetAllServices<AIProjectClient>().Any();\nif (!hasConnectionEndpoint && !hasRegisteredClient)\n{\n    throw new InvalidOperationException(\n        \"No AIProjectClient available. Register one on the kernel or set connection.endpoint.\");\n}","typeGuard":"static bool HasProjectsClient(AgentDefinition def, Kernel kernel) =>\n    (def?.Model?.Connection?.ExtensionData.TryGetValue(\"endpoint\", out _) ?? false)\n    || kernel.GetAllServices<AIProjectClient>().Any();","tryCatchPattern":"try { var client = agentDefinition.GetProjectsClient(kernel); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"project client not found\"))\n{ /* register AIProjectClient, or fail fast with actionable guidance */ }","preventionTips":["Register AIProjectClient once in your kernel/DI composition root.","Validate the agent definition connection section at load time.","In tests, register a mock/stub AIProjectClient on the kernel."],"tags":["azureai","configuration","dependency-injection","agent","startup"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}