Skip to content

Conversation

@HxyScotthuang
Copy link
Collaborator

Description

This PR implements a comprehensive SQL Toolkit for CAMEL-AI, providing native database/SQL support as requested in #3566.

Features Implemented:

  • ✅ DuckDB and SQLite database support
  • ✅ Read-only and read-write operation modes (Allow only SELECT in Read-only without alterning database)
  • ✅ Schema discovery with primary/foreign key detection
  • ✅ Full SQL query execution (SELECT, INSERT, UPDATE, DELETE, JOIN, CTE)
  • ✅ Security features (SQL injection protection via identifier quoting)
  • ✅ Comprehensive test suite (49+ tests)
  • ✅ 12 detailed examples including ChatAgent integration
  • ✅ File-based and in-memory database support

Files Added:

  • camel/toolkits/sql_toolkit.py - Main toolkit implementation
  • test/toolkits/test_sql_toolkit.py - Comprehensive test suite
  • examples/toolkits/sql_toolkit.py - Usage examples

Files Modified:

  • camel/toolkits/__init__.py - Added SQLToolkit export

Testing:

  • All 49 tests pass
  • Tests cover initialization, query execution, read-only mode, schema discovery, SQL injection protection
  • Both DuckDB and SQLite are tested

Fixes #3566

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 13, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch sql-toolkit

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions bot added the Review Required PR need to be reviewed label Dec 13, 2025
@HxyScotthuang HxyScotthuang changed the title Sql toolkit Implementing SQL toolkit Dec 13, 2025
@HxyScotthuang
Copy link
Collaborator Author

Seems there is a pre-commit error. Will investigate first.

Copy link
Member

@Wendong-Fan Wendong-Fan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @HxyScotthuang 's contribution, left some comments below, please run pre-commit run --all-files locally to check pre-commit issue, I created one enhance PR #3568 based on comment below, feel free to review

Comment on lines 39 to 40
database_type (str, optional): Type of database to use. Currently
supports "duckdb" and "sqlite". (default: :obj:`"duckdb"`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be better to use Literal for limited type value

Comment on lines 235 to 240
raise ValueError(
"Write operations are not allowed in read-only mode. "
"The query contains write operations (INSERT, UPDATE, DELETE, "
"DROP, CREATE, ALTER, TRUNCATE, etc.). Only SELECT queries "
"are permitted."
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be better to return error message instead of raise error, since this toolkit would be used by agent, we just need to pass the key information to agent instead of stop the running, same for other error raising part

Comment on lines +53 to +54
# SQL keywords that indicate write operations
_WRITE_KEYWORDS: ClassVar[List[str]] = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are more commands that can also write to files like COPY

"column_name": row["name"],
"column_type": row["type"],
"null": "YES" if row["notnull"] == 0 else "NO",
"key": "PRI" if row["pk"] == 1 else None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would not work as expected if there's composite primary key, we need to check whether row["pk"] > 0

self._validate_query_mode(query)

try:
logger.debug(f"Executing query: {query[:100]}...")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better show the whole query

Comment on lines 315 to 320
# Query did not return results or is a write operation
# (INSERT, UPDATE, DELETE, etc.)
# Commit the transaction
self._connection.commit()
logger.debug("Non-SELECT query executed successfully")
return []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we shouldn't handle this silently and return [], we need to provide meaningful information to agent

Comment on lines 327 to 328
except Exception:
pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, shouldn't handle silently

"""
if self.database_type == "duckdb":
try:
import duckdb
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to add dependency into .toml file and run uv lock to update the lock file

@Wendong-Fan Wendong-Fan removed the Review Required PR need to be reviewed label Dec 13, 2025
@Wendong-Fan Wendong-Fan added this to the Sprint 44 milestone Dec 13, 2025
Wendong-Fan and others added 3 commits December 13, 2025 21:43
Merge the sql-toolkit and sql-toolkit_wd branch to main, supporting DuckDB and sqlite3 database toolkit support.
@Wendong-Fan Wendong-Fan changed the title Implementing SQL toolkit feat: Implementing SQL toolkit Dec 14, 2025
@Wendong-Fan Wendong-Fan merged commit de6676c into master Dec 14, 2025
12 of 13 checks passed
@Wendong-Fan Wendong-Fan deleted the sql-toolkit branch December 14, 2025 06:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

[Feature Request] Database/SQL support as toolkit

3 participants