Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/dbjavagenix/database/mcp_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ async def handle_db_connect_test(arguments: Dict[str, Any]) -> List[TextContent]
f"- Host: {config.host}:{config.port}\n"
f"- Type: {config.type.value}\n\n"
f"Use this connection_id for subsequent database operations.\n\n"
f"Raw Response: {response}"
f"Raw Response: {json.dumps(response, ensure_ascii=False)}"
)]

except DatabaseConnectionError as e:
Expand Down Expand Up @@ -849,7 +849,10 @@ async def handle_db_query_table_exists(arguments: Dict[str, Any]) -> List[TextCo
status = "exists" if exists else "does not exist"
return [TextContent(
type="text",
text=f"Table '{table}' {status} in database '{database}'\n\nRaw Response: {response}"
text=(
f"Table '{table}' {status} in database '{database}'\n\n"
f"Raw Response: {json.dumps(response, ensure_ascii=False)}"
)
)]

except (DatabaseConnectionError, DatabaseQueryError) as e:
Expand Down Expand Up @@ -925,7 +928,7 @@ async def handle_db_query_execute(arguments: Dict[str, Any]) -> List[TextContent
else:
result_text = "Query executed successfully. No rows returned."

result_text += f"\n\nRaw Response: {response}"
result_text += f"\n\nRaw Response: {json.dumps(response, ensure_ascii=False)}"

return [TextContent(
type="text",
Expand Down Expand Up @@ -1114,7 +1117,7 @@ async def handle_db_table_describe(arguments: Dict[str, Any]) -> List[TextConten
for imp in sorted(java_imports):
result_text += f"import {imp};\n"

result_text += f"\nRaw Response: {response}"
result_text += f"\nRaw Response: {json.dumps(response, ensure_ascii=False)}"

return [TextContent(
type="text",
Expand Down Expand Up @@ -1250,7 +1253,7 @@ async def handle_db_table_columns(arguments: Dict[str, Any]) -> List[TextContent
result_text += f" Comment: {row['COLUMN_COMMENT']}\n"
result_text += "\n"

result_text += f"Raw Response: {response}"
result_text += f"Raw Response: {json.dumps(response, ensure_ascii=False)}"

return [TextContent(
type="text",
Expand Down Expand Up @@ -1352,7 +1355,7 @@ async def handle_db_table_primary_keys(arguments: Dict[str, Any]) -> List[TextCo
else:
result_text = f"No primary keys found for table {database}.{table}\n"

result_text += f"\nRaw Response: {response}"
result_text += f"\nRaw Response: {json.dumps(response, ensure_ascii=False)}"

return [TextContent(
type="text",
Expand Down Expand Up @@ -1494,7 +1497,7 @@ async def handle_db_table_foreign_keys(arguments: Dict[str, Any]) -> List[TextCo
else:
result_text = f"No foreign keys found for table {database}.{table}\n"

result_text += f"Raw Response: {response}"
result_text += f"Raw Response: {json.dumps(response, ensure_ascii=False)}"

return [TextContent(
type="text",
Expand Down Expand Up @@ -1650,7 +1653,7 @@ async def handle_db_table_indexes(arguments: Dict[str, Any]) -> List[TextContent
else:
result_text = f"No indexes found for table {database}.{table}\n"

result_text += f"Raw Response: {response}"
result_text += f"Raw Response: {json.dumps(response, ensure_ascii=False)}"

return [TextContent(
type="text",
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/test_mcp_error_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,46 @@ def fail_query(*_args, **_kwargs):
assert payload["success"] is False
assert payload["error"] == "query_failed"
assert payload["message"] == "[DB_QUERY_FAILED] database unavailable"


@pytest.mark.asyncio
async def test_query_execute_success_raw_response_is_json(monkeypatch):
monkeypatch.setattr(
mcp_tools.connection_manager,
"execute_query",
lambda *_args, **_kwargs: [{"id": 1, "name": "Alice"}],
)

response = await mcp_tools.handle_db_query_execute(
{"connection_id": "sqlite-1", "query": "SELECT 1", "limit": 10}
)

payload = _raw_payload(response)
assert payload["success"] is True
assert payload["data"] == [{"id": 1, "name": "Alice"}]


@pytest.mark.asyncio
async def test_table_exists_success_raw_response_is_json(monkeypatch):
monkeypatch.setattr(
mcp_tools.connection_manager,
"get_connection_info",
lambda _id: SimpleNamespace(type=DatabaseType.SQLITE),
)
monkeypatch.setattr(
mcp_tools.connection_manager,
"execute_query",
lambda *_args, **_kwargs: [{"count": 1}],
)

response = await mcp_tools.handle_db_query_table_exists(
{"connection_id": "sqlite-1", "database": "app", "table": "users"}
)

payload = _raw_payload(response)
assert payload == {
"success": True,
"database": "app",
"table": "users",
"exists": True,
}
Loading