Skip to content

MCP: Add tool get_table_columns#66

Merged
amotl merged 1 commit intomainfrom
get_table_columns
Jul 27, 2025
Merged

MCP: Add tool get_table_columns#66
amotl merged 1 commit intomainfrom
get_table_columns

Conversation

@amotl
Copy link
Member

@amotl amotl commented Jul 26, 2025

About

This patch adds a tool to inquire column information. We thought get_table_metadata would be doing it, but it didn't.

References

@coderabbitai
Copy link

coderabbitai bot commented Jul 26, 2025

Warning

Rate limit exceeded

@amotl has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 20 minutes and 21 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 6d5a5ce and 33229a3.

📒 Files selected for processing (10)
  • CHANGES.md (1 hunks)
  • README.md (2 hunks)
  • cratedb_mcp/core.py (2 hunks)
  • cratedb_mcp/knowledge.py (1 hunks)
  • cratedb_mcp/prompt/instructions.md (1 hunks)
  • cratedb_mcp/tool.py (1 hunks)
  • examples/mcptools.sh (1 hunks)
  • tests/test_examples.py (1 hunks)
  • tests/test_knowledge.py (1 hunks)
  • tests/test_mcp.py (2 hunks)

Walkthrough

A new tool, get_table_columns, was introduced to retrieve schema and column information from CrateDB. This tool is registered in the core module, referenced in usage instructions, and demonstrated in example scripts. Supporting SQL queries and related tests were added or updated to ensure integration and correctness.

Changes

File(s) Change Summary
cratedb_mcp/tool.py Added get_table_columns() function to retrieve and categorize table/column metadata.
cratedb_mcp/core.py Imported and registered get_table_columns as a tool in CrateDbMcp.add_tools().
cratedb_mcp/knowledge.py Added Queries.TABLES_COLUMNS SQL query constant for column metadata retrieval.
cratedb_mcp/prompt/instructions.md Updated instructions to include a step for using get_table_columns before SQL queries.
examples/mcptools.sh Added command to call the get_table_columns tool.
tests/test_mcp.py Added test_get_table_columns() to test new tool output.
tests/test_knowledge.py Added assertion for Queries.TABLES_COLUMNS content.
tests/test_examples.py Increased script execution timeout from 15 to 20 seconds.
CHANGES.md Updated changelog to mention the addition of the get_table_columns tool.
README.md Updated tool list to include get_table_columns and reordered Text-to-SQL tools.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CrateDbMcp
    participant get_table_columns
    participant Queries

    User->>CrateDbMcp: Request table/column info via get_table_columns
    CrateDbMcp->>get_table_columns: Call function
    get_table_columns->>Queries: Use TABLES_COLUMNS SQL query
    get_table_columns->>CrateDbMcp: Return categorized metadata
    CrateDbMcp->>User: Respond with schema/column info
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~15 minutes

Suggested reviewers

  • kneth
  • surister
  • WalBeh

Poem

In the warren where schemas grow,
A new tool hops in, column names to show.
Tables and types, all neatly arrayed,
By clever code-bunnies, carefully displayed.
With every hop, our knowledge blooms—
Hooray for new tools and database rooms!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch get_table_columns

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🔭 Outside diff range comments (1)
cratedb_mcp/prompt/instructions.md (1)

5-10: Synchronise tool list with newly added get_table_columns
The numbered list still shows four tools and omits the new one, yet the rules below reference it. Add the entry to prevent user confusion.

 1. `get_table_metadata`: Return table metadata for all tables stored in CrateDB.
+2. `get_table_columns`: Return schema & column information for all tables.
 2. `query_sql`: Execute an SQL query on CrateDB and return the results. …
 3. `get_cratedb_documentation_index`: Return the table of contents …
 4. `fetch_cratedb_docs`: Given a `link` …
🧹 Nitpick comments (4)
cratedb_mcp/knowledge.py (1)

12-21: Add deterministic ordering to the query result set
Without an ORDER BY, consumers get columns in an arbitrary sequence which can differ between clusters and between successive calls. Adding an explicit sort makes downstream processing and manual inspection easier.

 FROM information_schema.columns c
+ORDER BY
+    c.table_schema,
+    c.table_name,
+    c.ordinal_position
tests/test_examples.py (1)

8-8: Avoid hard-coded timeout magic numbers
Bumping the constant works short-term, but the script may keep growing. Consider deriving the timeout from an env var or marking the test @pytest.mark.slow and using pytest-timeout to centralise configuration.

examples/mcptools.sh (1)

31-31: Quote tool name to guard against shell alias expansion
mcpt is aliased, but if someone defines a shell function named get_table_columns, unquoted arguments can mis-behave. For absolute safety:

-mcpt call get_table_columns cratedb-mcp serve
+mcpt call "get_table_columns" cratedb-mcp serve
tests/test_mcp.py (1)

61-64: Test implementation is appropriate but could be more robust.

The test correctly verifies the function output contains expected schema names. However, consider checking the structure of the returned dictionary for more comprehensive validation.

Consider enhancing the test to verify the expected dictionary structure:

 def test_get_table_columns():
     response = str(get_table_columns())
     assert "information_schema" in response
     assert "pg_catalog" in response
+    
+    # Verify the returned structure
+    result = get_table_columns()
+    assert isinstance(result, dict)
+    expected_keys = {"user", "information_schema", "pg_catalog", "sys"}
+    assert set(result.keys()) == expected_keys
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f34b392 and 9be6544.

📒 Files selected for processing (9)
  • CHANGES.md (1 hunks)
  • cratedb_mcp/core.py (2 hunks)
  • cratedb_mcp/knowledge.py (1 hunks)
  • cratedb_mcp/prompt/instructions.md (1 hunks)
  • cratedb_mcp/tool.py (1 hunks)
  • examples/mcptools.sh (1 hunks)
  • tests/test_examples.py (1 hunks)
  • tests/test_knowledge.py (1 hunks)
  • tests/test_mcp.py (2 hunks)
🧰 Additional context used
🧠 Learnings (5)
📓 Common learnings
Learnt from: amotl
PR: crate/cratedb-mcp#17
File: CHANGES.md:5-5
Timestamp: 2025-05-16T13:35:04.487Z
Learning: For the cratedb-mcp repository, version numbers and release dates in CHANGES.md are updated by a dedicated "Release vX.Y.Z" commit as part of the release process, not during documentation preparation PRs.
CHANGES.md (2)

Learnt from: amotl
PR: #17
File: CHANGES.md:5-5
Timestamp: 2025-05-16T13:35:04.487Z
Learning: For the cratedb-mcp repository, version numbers and release dates in CHANGES.md are updated by a dedicated "Release vX.Y.Z" commit as part of the release process, not during documentation preparation PRs.

Learnt from: amotl
PR: crate/cratedb-mcp#0
File: :0-0
Timestamp: 2025-05-17T14:55:23.400Z
Learning: In the cratedb-mcp repository, the backlog.md document is intended as a functional planning document that doesn't need to adhere to strict formatting standards. Avoid pointing out formatting or layout shortcomings in this file during reviews.

cratedb_mcp/prompt/instructions.md (3)

Learnt from: amotl
PR: #17
File: CHANGES.md:5-5
Timestamp: 2025-05-16T13:35:04.487Z
Learning: For the cratedb-mcp repository, version numbers and release dates in CHANGES.md are updated by a dedicated "Release vX.Y.Z" commit as part of the release process, not during documentation preparation PRs.

Learnt from: amotl
PR: #54
File: tests/test_cli.py:0-0
Timestamp: 2025-07-20T17:17:10.660Z
Learning: In the cratedb-mcp project, tests should avoid asserting against mcp.settings internal state as these assertions will break with later versions of FastMCP. Tests should focus on verifying that functions are called with correct parameters rather than checking internal implementation details.

Learnt from: amotl
PR: crate/cratedb-mcp#0
File: :0-0
Timestamp: 2025-05-17T14:55:23.400Z
Learning: In the cratedb-mcp repository, the backlog.md document is intended as a functional planning document that doesn't need to adhere to strict formatting standards. Avoid pointing out formatting or layout shortcomings in this file during reviews.

examples/mcptools.sh (1)

Learnt from: amotl
PR: #54
File: tests/test_cli.py:0-0
Timestamp: 2025-07-20T17:17:10.660Z
Learning: In the cratedb-mcp project, tests should avoid asserting against mcp.settings internal state as these assertions will break with later versions of FastMCP. Tests should focus on verifying that functions are called with correct parameters rather than checking internal implementation details.

tests/test_mcp.py (1)

Learnt from: amotl
PR: #54
File: tests/test_cli.py:0-0
Timestamp: 2025-07-20T17:17:10.660Z
Learning: In the cratedb-mcp project, tests should avoid asserting against mcp.settings internal state as these assertions will break with later versions of FastMCP. Tests should focus on verifying that functions are called with correct parameters rather than checking internal implementation details.

🧬 Code Graph Analysis (5)
tests/test_knowledge.py (1)
cratedb_mcp/knowledge.py (1)
  • Queries (11-101)
examples/mcptools.sh (2)
cratedb_mcp/tool.py (1)
  • get_table_columns (31-62)
cratedb_mcp/cli.py (1)
  • serve (73-94)
tests/test_mcp.py (1)
cratedb_mcp/tool.py (1)
  • get_table_columns (31-62)
cratedb_mcp/core.py (1)
cratedb_mcp/tool.py (1)
  • get_table_columns (31-62)
cratedb_mcp/tool.py (1)
cratedb_mcp/knowledge.py (1)
  • Queries (11-101)
🔇 Additional comments (6)
CHANGES.md (1)

6-6: Changelog entry LGTM
Entry is concise and follows the existing unreleased-section pattern.

tests/test_knowledge.py (1)

15-15: LGTM! Test assertion follows established pattern.

The new assertion correctly verifies that the TABLES_COLUMNS query references the expected information_schema.columns table, maintaining consistency with the existing test structure.

tests/test_mcp.py (1)

7-7: LGTM! Import follows alphabetical ordering.

The import is correctly added in alphabetical order with the other function imports.

cratedb_mcp/core.py (2)

13-13: LGTM! Import correctly added in alphabetical order.

The import follows the established pattern and maintains alphabetical ordering within the import statement.


50-56: LGTM! Tool registration follows established pattern.

The tool registration is correctly implemented using the same pattern as other tools, with appropriate tags and placement within the text-to-SQL section.

cratedb_mcp/tool.py (1)

31-62: LGTM! Well-implemented function with comprehensive documentation.

The function implementation is solid with several strengths:

  • Comprehensive docstring explaining purpose, return fields, and categorization
  • Clean categorization of schemas into user tables vs. system tables
  • Proper use of existing query_sql() function for execution and error handling
  • Logical structure that returns organized results by schema type

The implementation follows established patterns in the codebase and provides valuable functionality for schema discovery.

@amotl amotl requested review from WalBeh and surister July 26, 2025 02:46
@amotl amotl marked this pull request as ready for review July 26, 2025 02:46
@amotl amotl requested a review from hammerhead July 26, 2025 02:49
@amotl amotl force-pushed the get_table_columns branch from 43c5c17 to 50b3b00 Compare July 27, 2025 15:22
@amotl amotl force-pushed the refactor-tool-descriptions branch from f34b392 to 9142a8e Compare July 27, 2025 15:59
Base automatically changed from refactor-tool-descriptions to main July 27, 2025 15:59
@amotl amotl force-pushed the get_table_columns branch from 50b3b00 to 8a2d7a8 Compare July 27, 2025 16:00
@amotl amotl force-pushed the get_table_columns branch from 8a2d7a8 to 6d5a5ce Compare July 27, 2025 16:04
@amotl amotl force-pushed the get_table_columns branch from 6d5a5ce to 0008fe8 Compare July 27, 2025 16:07
@amotl amotl force-pushed the get_table_columns branch from 0008fe8 to 33229a3 Compare July 27, 2025 16:09
@amotl amotl merged commit 34988ab into main Jul 27, 2025
10 checks passed
@amotl amotl deleted the get_table_columns branch July 27, 2025 16:37
Copy link
Contributor

@surister surister left a comment

Choose a reason for hiding this comment

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

LGTM, thanks

}

response = {}
for variant, where in variants.items():
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick: Could have used dict comprehension:

return {
    variant: query_sql( Queries.TABLES_COLUMNS.format(where) )
    for variant, where in variants.items()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants