Skip to content

Commit 6ebb683

Browse files
committed
🐛 fix(mcp): 统一跨工具驱动值 JSON 序列化
1 parent 16e17e5 commit 6ebb683

4 files changed

Lines changed: 179 additions & 75 deletions

File tree

‎src/dbjavagenix/database/atomic_codegen_tools.py‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
from ..core.exceptions import DatabaseConnectionError, MCPServiceError
3232
from ..database.connection_manager import connection_manager
33+
from ..utils.json_serialization import dumps as _json_dumps
3334

3435
logger = logging.getLogger(__name__)
3536

@@ -257,23 +258,21 @@ async def handle_codegen_build_context(arguments: Dict[str, Any]) -> List[TextCo
257258
],
258259
},
259260
}
260-
return [TextContent(type="text", text=json.dumps(result, ensure_ascii=False, indent=2))]
261+
return [TextContent(type="text", text=_json_dumps(result, indent=2))]
261262

262263
except (DatabaseConnectionError, MCPServiceError) as e:
263264
return [
264265
TextContent(
265266
type="text",
266-
text=json.dumps({"error": str(e), "stage": "build_context"}, ensure_ascii=False),
267+
text=_json_dumps({"error": str(e), "stage": "build_context"}),
267268
)
268269
]
269270
except Exception as e: # noqa: BLE001
270271
logger.error(f"codegen_build_context unexpected error: {e}")
271272
return [
272273
TextContent(
273274
type="text",
274-
text=json.dumps(
275-
{"error": f"unexpected: {e}", "stage": "build_context"}, ensure_ascii=False
276-
),
275+
text=_json_dumps({"error": f"unexpected: {e}", "stage": "build_context"}),
277276
)
278277
]
279278

@@ -298,19 +297,18 @@ async def handle_codegen_render_dto(arguments: Dict[str, Any]) -> List[TextConte
298297
"""Render the Java 21 record DTO exposed by the sb35-java21 template family."""
299298
context = _extract_context(arguments)
300299
if not isinstance(context, dict):
301-
return [TextContent(type="text", text=json.dumps({"error": "context missing or invalid"}))]
300+
return [TextContent(type="text", text=_json_dumps({"error": "context missing or invalid"}))]
302301

303302
if context.get("templateCategory") != "sb35-java21":
304303
return [
305304
TextContent(
306305
type="text",
307-
text=json.dumps(
306+
text=_json_dumps(
308307
{
309308
"files": [],
310309
"language": "java",
311310
"note": "template_category does not provide a record DTO",
312311
},
313-
ensure_ascii=False,
314312
indent=2,
315313
),
316314
)
@@ -329,7 +327,7 @@ async def handle_codegen_render_mapper(arguments: Dict[str, Any]) -> List[TextCo
329327
"""
330328
context = _extract_context(arguments)
331329
if not isinstance(context, dict):
332-
return [TextContent(type="text", text=json.dumps({"error": "context missing or invalid"}))]
330+
return [TextContent(type="text", text=_json_dumps({"error": "context missing or invalid"}))]
333331

334332
category = context.get("templateCategory", "")
335333
templates: List[str] = []
@@ -347,13 +345,12 @@ async def handle_codegen_render_mapper(arguments: Dict[str, Any]) -> List[TextCo
347345
return [
348346
TextContent(
349347
type="text",
350-
text=json.dumps(
348+
text=_json_dumps(
351349
{
352350
"files": [],
353351
"language": "java",
354352
"note": f"template_category={category} 不需要 mapper 层(BaseMapper/JpaRepository 内置)",
355353
},
356-
ensure_ascii=False,
357354
indent=2,
358355
),
359356
)
@@ -390,7 +387,7 @@ async def _render_single_layer(
390387
return [
391388
TextContent(
392389
type="text",
393-
text=json.dumps({"error": "context missing or not a dict"}, ensure_ascii=False),
390+
text=_json_dumps({"error": "context missing or not a dict"}),
394391
)
395392
]
396393

@@ -403,13 +400,12 @@ async def _render_single_layer(
403400
return [
404401
TextContent(
405402
type="text",
406-
text=json.dumps(
403+
text=_json_dumps(
407404
{
408405
"error": "unsupported template category",
409406
"template_category": category,
410407
"supported_categories": supported_categories,
411408
},
412-
ensure_ascii=False,
413409
indent=2,
414410
),
415411
)
@@ -476,7 +472,7 @@ async def _render_single_layer(
476472

477473
content = TextContent(
478474
type="text",
479-
text=json.dumps({"files": files, "language": "java"}, ensure_ascii=False, indent=2),
475+
text=_json_dumps({"files": files, "language": "java"}, indent=2),
480476
)
481477
return [attach_meta(content, diff_meta)]
482478

0 commit comments

Comments
 (0)