{"record":{"id":"17a316cdbe31dcbc","repo":"OtterMind/Chat2DB","slug":"no-database-connection-context-found-provide-data","errorCode":null,"errorMessage":"No database connection context found. Provide dataSourceId/databaseName.","messagePattern":"No database connection context found\\. Provide dataSourceId/databaseName\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/ai/AiToolServiceImpl.java","lineNumber":745,"sourceCode":"        }\n        if (toolContext.getConnectionProfile() != null) {\n            return toolContext.getConnectionProfile();\n        }\n        Long dataSourceId = toolContext.getDataSourceId();\n        if (Objects.isNull(dataSourceId)) {\n            return null;\n        }\n        String databaseName = toolContext.getDatabaseName();\n        String schemaName = toolContext.getSchemaName();\n        return buildProfile(dataSourceId, databaseName, schemaName);\n    }\n\n    private ConnectionProfile requireConnectInfo(AiToolContextRequest toolContext) {\n        ConnectionProfile profile = resolveConnectionProfile(toolContext);\n        if (Objects.nonNull(profile)) {\n            return profile;\n        }\n        throw new IllegalArgumentException(\"No database connection context found. Provide dataSourceId/databaseName.\");\n    }\n\n    private ConnectionProfile buildProfile(Long dataSourceId, String databaseName, String schemaName) {\n        DbConnectionContextRequest param = new DbConnectionContextRequest();\n        param.setDataSourceId(dataSourceId);\n        param.setDatabaseName(databaseName);\n        param.setSchemaName(schemaName);\n        return connectionContextService.buildProfile(param);\n    }\n}\n","sourceCodeStart":727,"sourceCodeEnd":756,"githubUrl":"https://github.com/OtterMind/Chat2DB/blob/5ee1e990e73fbcae1969dc554be254fedb3ab888/chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/ai/AiToolServiceImpl.java#L727-L756","documentation":"Thrown by requireConnectInfo when resolveConnectionProfile returns null — i.e. toolContext is null, or it has no connectionProfile and no dataSourceId. This is the hard precondition used by tools that must execute against an already-resolved connection and cannot build one lazily.","triggerScenarios":"A tool path calling requireConnectInfo(toolContext) where the request carries neither connectionProfile nor dataSourceId, and the caller did not establish a session first.","commonSituations":"Same family as error 61 but the stricter variant: the tool has no optional-resolution fallback, so any missing connection input fails immediately. Common when an MCP caller forwards an empty toolContext object.","solutions":["Populate toolContext.dataSourceId (plus databaseName/schemaName) before calling the tool.","Alternatively, set toolContext.connectionProfile to a resolved ConnectionProfile from a prior buildProfile call.","Verify the client is forwarding the active datasource session into every tool request that requires it."],"exampleFix":"// before\nrequest.setToolContext(new AiToolContextRequest()); // empty\n\n// after\nAiToolContextRequest ctx = new AiToolContextRequest();\nctx.setDataSourceId(activeDsId);\nctx.setDatabaseName(activeDb);\nrequest.setToolContext(ctx);","handlingStrategy":"validation","validationCode":"public boolean hasConnectInfo(AiToolContextRequest ctx) {\n    if (ctx == null) return false;\n    return ctx.getConnectionProfile() != null || ctx.getDataSourceId() != null;\n}\n// call: if (!hasConnectInfo(ctx)) throw new IllegalStateException(\"connection required\");","typeGuard":"public boolean hasResolvedConnection(AiToolContextRequest ctx) {\n    return ctx != null && (ctx.getConnectionProfile() != null || ctx.getDataSourceId() != null);\n}","tryCatchPattern":"try {\n    return aiToolService.requireConnectInfo(toolContext);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().startsWith(\"No database connection context found.\")) {\n        return Optional.empty(); // or prompt for connection input\n    }\n    throw e;\n}","preventionTips":["Never dispatch a connection-requiring tool with an empty toolContext.","Validate connection inputs at the API/transport boundary before reaching the service.","Centralize connection resolution so all tools share one guard."],"tags":["ai-tool","datasource","validation","context"],"backgroundTag":null,"analyzedSha":"5ee1e990e73fbcae1969dc554be254fedb3ab888","analyzedAt":"2026-08-14T07:05:03.077Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}