fix(mcp-kb): address critical issues from deep review
- Fix SQL HAVING clause bug by using CTE approach (closes #73) - Add /mcp JSON-RPC 2.0 endpoint for tool execution (closes #74) - Add /mcp/tools endpoint for tool discovery (closes #75) - Add content size limits to prevent DoS attacks (closes #78) - Add comprehensive tests for new endpoints 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -345,8 +345,9 @@ class DatabaseManager:
|
||||
"""
|
||||
try:
|
||||
async with self.acquire() as conn:
|
||||
# Build query with optional filters
|
||||
query = """
|
||||
# Build query with optional filters using CTE to filter by similarity
|
||||
# We use a CTE to compute similarity once, then filter in outer query
|
||||
inner_query = """
|
||||
SELECT
|
||||
id, project_id, collection, content, embedding,
|
||||
chunk_type, source_path, start_line, end_line,
|
||||
@@ -361,18 +362,21 @@ class DatabaseManager:
|
||||
param_idx = 3
|
||||
|
||||
if collection:
|
||||
query += f" AND collection = ${param_idx}"
|
||||
inner_query += f" AND collection = ${param_idx}"
|
||||
params.append(collection)
|
||||
param_idx += 1
|
||||
|
||||
if file_types:
|
||||
file_type_values = [ft.value for ft in file_types]
|
||||
query += f" AND file_type = ANY(${param_idx})"
|
||||
inner_query += f" AND file_type = ANY(${param_idx})"
|
||||
params.append(file_type_values)
|
||||
param_idx += 1
|
||||
|
||||
query += f"""
|
||||
HAVING 1 - (embedding <=> $1) >= ${param_idx}
|
||||
# Wrap in CTE and filter by threshold in outer query
|
||||
query = f"""
|
||||
WITH scored AS ({inner_query})
|
||||
SELECT * FROM scored
|
||||
WHERE similarity >= ${param_idx}
|
||||
ORDER BY similarity DESC
|
||||
LIMIT ${param_idx + 1}
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user