OtterMind/Chat2DB · error · IllegalArgumentException
No database connection context found. Call list_all_datasour
Error message
No database connection context found. Call list_all_datasources first, then provide dataSourceId/databaseName.
What it means
Thrown by the AI tool connection resolver when neither an explicit dataSourceId argument, nor a usable ConnectionProfile (from toolContext), yields a non-null resolvedDataSourceId. The message instructs the caller to first list datasources, because the tool layer cannot infer which datasource the SQL/metadata tool should target.
Source
Thrown at chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/ai/AiToolServiceImpl.java:720
String resolvedSchemaName = schemaName;
if (Objects.isNull(resolvedDataSourceId) && contextProfile != null) {
resolvedDataSourceId = contextProfile.getDataSourceId();
}
if (StringUtils.isBlank(resolvedDatabaseName) && contextProfile != null) {
resolvedDatabaseName = contextProfile.getDatabaseName();
}
if (StringUtils.isBlank(resolvedSchemaName) && contextProfile != null) {
resolvedSchemaName = contextProfile.getSchemaName();
}
if (Objects.nonNull(resolvedDataSourceId)) {
final Long scopedDataSourceId = resolvedDataSourceId;
final String scopedDatabaseName = resolvedDatabaseName;
final String scopedSchemaName = resolvedSchemaName;
return invokeWithRequestContext(toolContext,
() -> buildProfile(scopedDataSourceId, scopedDatabaseName, scopedSchemaName));
}
throw new IllegalArgumentException(
"No database connection context found. Call list_all_datasources first, then provide dataSourceId/databaseName.");
}
private ConnectionProfile resolveConnectionProfile(AiToolContextRequest toolContext) {
if (Objects.isNull(toolContext)) {
return null;
}
if (toolContext.getConnectionProfile() != null) {
return toolContext.getConnectionProfile();
}
Long dataSourceId = toolContext.getDataSourceId();
if (Objects.isNull(dataSourceId)) {
return null;
}
String databaseName = toolContext.getDatabaseName();
String schemaName = toolContext.getSchemaName();
return buildProfile(dataSourceId, databaseName, schemaName);
}View on GitHub (pinned to 5ee1e990e7)
Solutions
- Call the list_all_datasources tool first and capture a valid dataSourceId from its response.
- Pass dataSourceId (and databaseName/schemaName when needed) into the next tool request's toolContext.
- Ensure the client sets toolContext.dataSourceId or connectionProfile from the active datasource session before invoking any SQL/metadata tool.
- If using a pre-established connection, populate toolContext.connectionProfile so resolveConnectionProfile returns non-null.
Example fix
// before
run_sql({ sql: "SELECT 1" }) // no datasource context
// after
const ds = (await list_all_datasources())[0];
run_sql({ sql: "SELECT 1", dataSourceId: ds.id, databaseName: ds.database }); Defensive patterns
Strategy: validation
Validate before calling
public void requireDataSourceContext(AiToolContextRequest ctx, Long dataSourceId) {
boolean hasArg = dataSourceId != null;
boolean hasCtx = ctx != null && (ctx.getDataSourceId() != null
|| (ctx.getConnectionProfile() != null && ctx.getConnectionProfile().getDataSourceId() != null));
if (!hasArg && !hasCtx) {
throw new IllegalStateException("No datasource context; call list_all_datasources first");
}
} Try / catch
try {
return aiToolService.runSql(toolContext, sql);
} catch (IllegalArgumentException e) {
if (e.getMessage().contains("No database connection context found")) {
// prompt the model/user to call list_all_datasources and retry with dataSourceId
return needDatasourcePrompt();
}
throw e;
} Prevention
- Inject the active datasource session id into every AI tool request at the client boundary.
- Make list_all_datasources the first tool in any new chat flow.
- Add a client-side guard that refuses to dispatch SQL/metadata tools without a dataSourceId.
When it happens
Trigger: An AI tool invocation (e.g. run_sql, list_schemas) is dispatched with a toolContext whose connectionProfile is null and whose dataSourceId is null, while the per-call dataSourceId argument is also null/blank.
Common situations: An LLM tool call omits dataSourceId after the model forgot to call list_all_datasources first; the MCP/tool client did not forward the active datasource session into AiToolContextRequest; a brand-new chat session with no datasource selected.
Related errors
- No database connection context found. Provide dataSourceId/d
- database.delete.invalidDataSource
- datasource_not_found
- invalid_connection_test_args
- invalid_datasource_create_args
AI-assisted analysis of OtterMind/Chat2DB@5ee1e990e7 (2026-08-14).
Data as JSON: /api/errors/a3601989d8cc5c6c.
Report an issue: GitHub.