Skip to content

Conversation

petrtsatsin-branch
Copy link
Contributor

@petrtsatsin-branch petrtsatsin-branch commented Jun 6, 2025

Summary
This PR enhances the MCP Trino HTTP server by improving query validation logic and fixing table schema retrieval for fully qualified table names. The changes allow complex SELECT queries to be properly recognized as read-only and support various table name formats in schema retrieval operations.

Changes

  1. Enhanced Query Validation Logic
    Improved the isReadOnlyQuery function to correctly identify SELECT\n queries as read-only
    Added normalization of whitespace by replacing newlines and carriage returns with spaces
    Ensured proper detection of SQL keywords regardless of spacing
    Added detection of embedded write operations within SELECT queries
    Added protection against SQL injection attempts using multiple statements (semicolons)
  2. Fixed Table Schema Retrieval
    Enhanced GetTableSchema function to properly handle fully qualified table names:
    Supports catalog.schema.table format without prepending defaults
    Supports schema.table format with default catalog
    Maintains backward compatibility with simple table names
    Prevents "Too many dots in table name" errors when using fully qualified table names
  3. Added Comprehensive Unit Tests
    Created a new test file client_test.go with extensive test cases for isReadOnlyQuery
    Tests cover various query formats, whitespace variations, and embedded write operations
    Added tests for case insensitivity and SQL injection attempts
    Problem Solved
    Previously, SELECT\n queries were incorrectly rejected as non-read-only
    Queries with newlines or unusual spacing were not properly recognized
    Table schema retrieval failed when using fully qualified table names
    Testing
    Added unit tests for query validation logic
    Manually tested with various query formats
    Verified schema retrieval works with different table name formats

Summary by CodeRabbit

  • New Features
    • Enhanced support for flexible table name formats when retrieving table schemas, allowing use of fully qualified names.
  • Bug Fixes
    • Improved detection of read-only SQL queries, ensuring more accurate handling of query types.
  • Tests
    • Added comprehensive tests for read-only query detection to ensure correct behavior across various SQL statements.
  • Chores
    • Added a new command to build and run tests inside a Docker container for easier testing in isolated environments.

Copy link

coderabbitai bot commented Jun 6, 2025

Walkthrough

The changes introduce a new docker-test target to the Makefile for running tests in a Docker container. The isReadOnlyQuery function in the Trino client is enhanced for more robust SQL read-only detection, and GetTableSchema now supports fully qualified table names. Comprehensive tests for isReadOnlyQuery are added.

Changes

File(s) Change Summary
Makefile Added docker-test phony target and commands to build and run tests using Dockerfile.test.
internal/trino/client.go Enhanced isReadOnlyQuery for stricter SQL normalization and detection; updated GetTableSchema to support flexible table name formats.
internal/trino/client_test.go Added TestIsReadOnlyQuery with extensive test cases for read-only SQL detection logic.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Makefile
    participant Docker
    participant TestSuite

    User->>Makefile: make docker-test
    Makefile->>Docker: Build image (Dockerfile.test)
    Docker-->>Makefile: Image built
    Makefile->>Docker: Run container with test suite
    Docker->>TestSuite: Execute tests
    TestSuite-->>Docker: Test results
    Docker-->>User: Output test results
Loading
sequenceDiagram
    participant Client
    participant isReadOnlyQuery
    participant GetTableSchema

    Client->>isReadOnlyQuery: Check if SQL query is read-only
    isReadOnlyQuery-->>Client: true/false

    Client->>GetTableSchema: Request table schema with flexible table name
    GetTableSchema->>GetTableSchema: Parse table string (catalog.schema.table)
    GetTableSchema-->>Client: Return schema or error
Loading

Poem

In the warren, tests now run in Docker’s embrace,
SQL queries checked with meticulous grace.
Table names parsed, no matter the form,
Read-only or write, the logic’s reform.
With every command, the code grows more bright—
A rabbit’s delight in each test’s green light! 🐇


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:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • 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.

@tuannvm
Copy link
Owner

tuannvm commented Jun 25, 2025

@petrtsatsin-branch could you sign DCO, check https://github.com/tuannvm/mcp-trino/pull/50/checks?check_run_id=43647570913

Thanks!

…ons.

Signed-off-by: Petr Tsatsin <petr.tsatsin@branch.io>
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: 1

🧹 Nitpick comments (1)
Makefile (1)

54-58: Consider caching layers / adding --target for faster docker-test runs (optional)

docker-test rebuilds the entire image every invocation. If the Dockerfile.test uses a multi-stage build (common for Go projects), adding --target test and ensuring proper layer caching will speed up local and CI runs:

-docker build -f Dockerfile.test -t $(BINARY_NAME)-test:$(VERSION) .
+docker build --target test -f Dockerfile.test -t $(BINARY_NAME)-test:$(VERSION) .

This is purely a QoL improvement and can be skipped if the build is already fast enough.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d784c1e and c9f8ba8.

📒 Files selected for processing (3)
  • Makefile (2 hunks)
  • internal/trino/client.go (2 hunks)
  • internal/trino/client_test.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • internal/trino/client_test.go
  • internal/trino/client.go
🧰 Additional context used
🪛 checkmake (0.2.2)
Makefile

[warning] 1-1: Missing required phony target "all"

(minphony)


[warning] 60-60: Target "all" should be declared PHONY.

(phonydeclared)

Makefile Outdated
@@ -1,4 +1,4 @@
.PHONY: build test clean run-dev release-snapshot run-docker run docker-compose-up docker-compose-down lint
.PHONY: build test clean run-dev release-snapshot run-docker run docker-compose-up docker-compose-down lint docker-test
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add all to the .PHONY list to satisfy checkmake and avoid accidental file collisions

The all target is defined later in the file (line 60) but is not declared as phony. This triggers the checkmake warnings (minphony, phonydeclared) and, more importantly, prevents make from ignoring a real file named all in the working directory.

-.PHONY: build test clean run-dev release-snapshot run-docker run docker-compose-up docker-compose-down lint docker-test
+.PHONY: build test clean run-dev release-snapshot run-docker run docker-compose-up docker-compose-down lint docker-test all
🧰 Tools
🪛 checkmake (0.2.2)

[warning] 1-1: Missing required phony target "all"

(minphony)

🤖 Prompt for AI Agents
In the Makefile at line 1, add the target `all` to the `.PHONY` declaration list
to prevent checkmake warnings and avoid conflicts with any real file named `all`
in the directory. This ensures that `make` treats `all` as a phony target and
always executes its commands.

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

♻️ Duplicate comments (1)
Makefile (1)

1-1: all target still missing from .PHONY list
This was flagged in the previous review and is still unresolved. Declaring all as phony silences checkmake warnings and prevents collisions with a real file named all.

-.PHONY: build build-dxt pack-dxt test clean run-dev release-snapshot run-docker run docker-compose-up docker-compose-down lint docker-test
+.PHONY: build build-dxt pack-dxt test clean run-dev release-snapshot run-docker run docker-compose-up docker-compose-down lint docker-test all
🧹 Nitpick comments (1)
Makefile (1)

73-77: Minor enhancements for the docker-test target

  1. Add --pull=missing (or similar) to ensure the base image is up-to-date when CI runs infrequently.
  2. Consider passing the binary version as a build-arg instead of baking it into the tag only – this makes the version available inside the Dockerfile if needed for caching layers or metadata.

Nit-level; feel free to ignore if unnecessary.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c9f8ba8 and a19bd45.

📒 Files selected for processing (1)
  • Makefile (2 hunks)
🧰 Additional context used
🪛 checkmake (0.2.2)
Makefile

[warning] 1-1: Missing required phony target "all"

(minphony)


[warning] 79-79: Target "all" should be declared PHONY.

(phonydeclared)

@tuannvm
Copy link
Owner

tuannvm commented Jul 1, 2025

@claude review PR

Copy link
Contributor

claude bot commented Jul 1, 2025

Claude encountered an error —— View job

Failed with exit code 128

I'll analyze this and get back to you.

@tuannvm tuannvm merged commit bd3b77e into tuannvm:main Jul 2, 2025
7 checks passed
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