When code has repeated “plumbing” (string templating/formatting, pagination/envelopes, client setup, session/ctx extraction, embedding serialization, etc.), move it into a shared helper/utility and keep stable imports/constants at module scope. This reduces drift, improves readability, and keeps formatting behavior consistent across tools.
Rules:
format_response or a dedicated formatter) instead of re-implementing in each tool.__init__.py into a dedicated module (e.g., factory.py or config_builder.py) for testability and style consistency.Example (hoist and extract):
# module scope (not inside each function)
import json
from .rum_queries import build_rum_report # example shared import
async def _run_tool(ctx, action: str, **kwargs):
# shared choke point / formatter logic here
report = await build_rum_report(action=action, **kwargs)
return json.dumps(report, indent=2)
# tool handlers call the shared helper; no repeated imports/formatting
async def errors_tool(app_monitor_name: str, start_time: str, end_time: str) -> str:
return await _run_tool(None, 'errors', app_monitor_name=app_monitor_name,
start_time=start_time, end_time=end_time)
Apply this standard during refactors: if you can point to the same pattern being repeated in multiple functions/files (even if “almost the same”), extract it and reuse it everywhere.