Skip to content

Conversation

@amotl
Copy link
Member

@amotl amotl commented May 16, 2025

About

Just a bit of refactoring before the first release, adding classes DocumentationIndex and Settings, to bundle relevant topic matters.

@coderabbitai
Copy link

coderabbitai bot commented May 16, 2025

"""

Walkthrough

This change removes the hishel HTTP client caching library and related caching logic from the codebase. It refactors configuration constants into a Settings class with static methods, centralizes URL permission logic within the DocumentationIndex class, and updates tests to manipulate environment variables instead of module-level flags.

Changes

File(s) Change Summary
cratedb_mcp/__main__.py Removed all usage and setup of the hishel HTTP client caching library, updated documentation fetching to use the client from documentation_index, and switched URL permission checks to use a method on documentation_index.
cratedb_mcp/knowledge.py Refactored DocumentationIndex to add a settings attribute, a permitted_urls list, a url_permitted method, and updated the HTTP client and caching logic. Removed the standalone URL permission function.
cratedb_mcp/settings.py Moved configuration constants into a new Settings class with static methods for retrieving settings, handling environment variables and warnings internally. Removed module-level constants.
cratedb_mcp/util/sql.py Replaced import of PERMIT_ALL_STATEMENTS with the Settings class and updated logic to use the new settings API for SQL permission checks.
tests/test_util.py Updated tests to manipulate environment variables for SQL statement permission, split the test into two cases, and added assertions for warnings. Added imports for pytest and os.

Sequence Diagram(s)

sequenceDiagram
    participant Main
    participant DocumentationIndex
    participant Settings

    Main->>DocumentationIndex: fetch_cratedb_docs(url)
    DocumentationIndex->>DocumentationIndex: url_permitted(url)
    alt URL permitted
        DocumentationIndex->>DocumentationIndex: client.get(url)
    else URL not permitted
        DocumentationIndex-->>Main: Raise error or deny
    end
Loading

Possibly related PRs

Suggested reviewers

  • surister

Poem

In the warren of code, a cache was once found,
But now it's been lifted, no hishel around.
Settings hop in, with methods so neat,
URLs checked by the docs at their seat.
The tests sniff the breeze for warnings anew—
This code garden's fresh, and the carrots grew! 🥕
"""


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 1190f6a and 90aac8b.

📒 Files selected for processing (3)
  • cratedb_mcp/settings.py (1 hunks)
  • cratedb_mcp/util/sql.py (2 hunks)
  • tests/test_util.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • tests/test_util.py
  • cratedb_mcp/settings.py
  • cratedb_mcp/util/sql.py
✨ Finishing Touches
  • 📝 Generate Docstrings

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 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.

@amotl amotl requested review from bmunkholm, kneth and surister May 16, 2025 10:17
@amotl amotl force-pushed the use-about-package branch from 35727ca to 06980f5 Compare May 16, 2025 11:13
Base automatically changed from use-about-package to main May 16, 2025 11:14
@amotl amotl force-pushed the refactor-document-index branch from b84425a to 02b21a2 Compare May 16, 2025 11:24
@amotl amotl marked this pull request as ready for review May 16, 2025 11:25
@bmunkholm bmunkholm removed their request for review May 16, 2025 11:34
@bmunkholm
Copy link

LGTM as a 1:1 refactor, but I'm not into the context so will let others approve.

Introduce `DocumentationIndex` and `Settings` classes.
@amotl amotl force-pushed the refactor-document-index branch from 02b21a2 to 1190f6a Compare May 16, 2025 12:03
@amotl amotl changed the title Refactoring: More OO, less module-level code. Here: DocumentationIndex Refactoring: More OO, less module-level code May 16, 2025
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

🧹 Nitpick comments (1)
cratedb_mcp/util/sql.py (1)

13-14: Instance creation for static methods

Creating an instance of Settings is unnecessary since you're only using static methods. Consider using the class directly.

-settings = Settings()
-

Then update line 27 to use:

-    is_dql = SqlStatementClassifier(expression=expression, permit_all=settings.permit_all_statements()).is_dql
+    is_dql = SqlStatementClassifier(expression=expression, permit_all=Settings.permit_all_statements()).is_dql
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between acd3f85 and 1190f6a.

📒 Files selected for processing (5)
  • cratedb_mcp/__main__.py (2 hunks)
  • cratedb_mcp/knowledge.py (2 hunks)
  • cratedb_mcp/settings.py (1 hunks)
  • cratedb_mcp/util/sql.py (2 hunks)
  • tests/test_util.py (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
cratedb_mcp/util/sql.py (1)
cratedb_mcp/settings.py (2)
  • Settings (12-48)
  • permit_all_statements (18-34)
tests/test_util.py (1)
cratedb_mcp/util/sql.py (1)
  • sql_is_permitted (16-32)
🔇 Additional comments (13)
cratedb_mcp/settings.py (1)

12-48: Looks good: Encapsulating settings into class methods

The refactoring to a Settings class with static methods is a good OO approach. This provides a centralized location for configuration and handles environment variable parsing and validation in a cleaner way.

A couple minor points:

  1. Consider addressing the TODOs for tests (lines 31 and 45) now rather than deferring them
  2. The error message in line 31's comment about integers is misleading since you're converting to boolean
cratedb_mcp/__main__.py (2)

4-5: Clean import refactoring

Good job simplifying the imports by removing hishel and related constants.


35-37: Proper encapsulation of URL permission and HTTP client

The refactoring correctly moves URL permission checks and HTTP client usage into the DocumentationIndex class, which aligns with the OO approach.

cratedb_mcp/util/sql.py (2)

8-8: Good update of import to use Settings class

The import was correctly updated to use the new Settings class rather than a module-level constant.


27-27: Updated to use Settings method appropriately

The code correctly uses the new permit_all_statements() method from the Settings class.

tests/test_util.py (3)

1-4: Added necessary imports for environment variable testing

Good addition of the required imports for the updated tests.


20-26: Good test for environment variable success case

This test properly verifies that:

  1. Setting the environment variable to "true" enables all SQL statements
  2. A warning is emitted when all statements are permitted

This aligns with the refactoring to environment variable-based configuration.


28-34: Good test for invalid environment variable

This test properly verifies that:

  1. Setting an invalid value for the environment variable causes the permission to be denied
  2. An appropriate warning is emitted about the invalid environment variable value

This ensures proper error handling in the refactored code.

cratedb_mcp/knowledge.py (5)

2-3: Good modularization with improved imports.

The addition of typing and structured imports from the new Settings module aligns well with the objective of making the code more object-oriented.

Also applies to: 5-5, 8-9


116-124: Well-structured class attributes for configuration.

Moving the settings and permitted URLs into class-level attributes implements good encapsulation. The list of permitted URLs is clearly defined with appropriate documentation, making the security boundaries explicit.


125-132: Good constructor implementation with improved HTTP client configuration.

The initialization of the Hishel HTTP client with caching is well-structured, using the settings object to retrieve the cache TTL. This approach properly integrates the caching logic into the class and makes the dependency on the settings explicit.


136-140: Appropriate cache configuration with dynamic TTL.

Using the settings object to configure the TTL for the cache is a good practice. Setting the TTL to slightly less than the docs cache TTL is a thoughtful detail to prevent edge cases where the cache might expire while being used.


143-159: Good encapsulation of URL permission logic.

Moving the URL permission logic from a standalone function to an instance method increases cohesion and aligns with object-oriented principles. The method is well-documented with clear purpose, parameters, and return values.

@amotl amotl force-pushed the refactor-document-index branch from 1190f6a to 90aac8b Compare May 16, 2025 12:06
@amotl amotl merged commit 546cd65 into main May 16, 2025
6 checks passed
@amotl amotl deleted the refactor-document-index branch May 16, 2025 12:48
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.

3 participants