fix(mcp-kb): SQL HAVING clause without GROUP BY breaks semantic search #73

Closed
opened 2026-01-03 23:57:01 +00:00 by cardosofelipe · 0 comments

Bug Description

In mcp-servers/knowledge-base/database.py, the _format_where_clause method uses HAVING without a GROUP BY clause, which breaks PostgreSQL queries when semantic search is combined with filters.

Location

mcp-servers/knowledge-base/database.py:393-395

# BUG: HAVING without GROUP BY
if min_score:
    having_clauses.append(f"1 - (embedding <=> $1) >= {min_score}")

Expected Behavior

The query should properly filter by minimum similarity score without breaking PostgreSQL syntax.

Actual Behavior

Queries with min_score parameter will fail with PostgreSQL syntax errors.

Fix Required

Either:

  1. Add proper GROUP BY clause when using HAVING
  2. Use a subquery/CTE approach
  3. Move the condition to WHERE with proper vector distance calculation
  • Found during PR #72 deep review
  • Issue #57 (Knowledge Base MCP Server)
## Bug Description In `mcp-servers/knowledge-base/database.py`, the `_format_where_clause` method uses `HAVING` without a `GROUP BY` clause, which breaks PostgreSQL queries when semantic search is combined with filters. ### Location `mcp-servers/knowledge-base/database.py:393-395` ```python # BUG: HAVING without GROUP BY if min_score: having_clauses.append(f"1 - (embedding <=> $1) >= {min_score}") ``` ### Expected Behavior The query should properly filter by minimum similarity score without breaking PostgreSQL syntax. ### Actual Behavior Queries with `min_score` parameter will fail with PostgreSQL syntax errors. ### Fix Required Either: 1. Add proper `GROUP BY` clause when using `HAVING` 2. Use a subquery/CTE approach 3. Move the condition to `WHERE` with proper vector distance calculation ### Related - Found during PR #72 deep review - Issue #57 (Knowledge Base MCP Server)
Sign in to join this conversation.