Skip to content

chore-mcp-server-god-module-handler-extraction: extract MCP tool handlers#73

Merged
emp3thy merged 1 commit into
mainfrom
ralph/chore-mcp-server-god-module-handler-extraction-2026-06-10
Jun 11, 2026
Merged

chore-mcp-server-god-module-handler-extraction: extract MCP tool handlers#73
emp3thy merged 1 commit into
mainfrom
ralph/chore-mcp-server-god-module-handler-extraction-2026-06-10

Conversation

@emp3thy

@emp3thy emp3thy commented Jun 10, 2026

Copy link
Copy Markdown
Owner

Extracts the 22 inline tool handlers from the 1617-LOC better_memory/mcp/server.py god module into per-domain handler classes (handlers/ package: observations, reflections, episodes, semantics, knowledge, sessions) registered via a {tool_name: handler} registry, reducing _call_tool to a pure lookup. Tool name/schema declarations move to mcp/tools.py (set unchanged, 22 tools), JSON payload builders to mcp/serializers.py, the synthesize audit log to mcp/synth_audit.py, and run_best_effort/session-id helpers to mcp/_util.py. All services — including SemanticMemoryService, previously instantiated inline in four separate handlers — are now constructed once in create_server() and injected. server.py is down to 378 LOC of wiring.

Closes chore-mcp-server-god-module-handler-extraction-2026-06-10.

Test plan

  • Full pytest suite: exit 0, no failures (run with BETTER_MEMORY_PROJECT cleared — the Ralph executor's env var leaks into project-inference tests; pre-existing, unrelated)
  • ruff check better_memory/mcp tests/mcp: 5 pre-existing errors in untouched test files (clean-tree baseline: 6)
  • pyright better_memory/mcp: 0 errors
  • Test diffs are import-path updates only; README MCP-tools schema pointer updated server.py → tools.py, other docs unaffected

PBI bundle: .ralph/current/chore-mcp-server-god-module-handler-extraction-2026-06-10 on ralph-queue.

🤖 Generated with Claude Code

…-10 — extract tool handlers from server.py god module

Split the 1617-LOC mcp/server.py into focused modules:

- handlers/ package: one stateless handler class per tool domain
  (observations, reflections, episodes, semantics, knowledge, sessions),
  merged into a {tool_name: handler} registry by build_registry(), which
  raises on duplicate tool names. _call_tool is now a pure lookup.
- tools.py: the 22 Tool() name/schema declarations (unchanged set).
- serializers.py: JSON payload builders for the synthesize tools.
- synth_audit.py: synthesize audit-log append + context manager.
- _util.py: run_best_effort + session-id resolution helpers.
- server.py (378 LOC): wiring only — create_server constructs each
  service once, including SemanticMemoryService, previously instantiated
  inline in four separate handlers.

Test changes are import-path updates only. README MCP-tools section now
points at tools.py for schemas; other docs unaffected.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Claude BugBot Analysis

No bugs found in the changes. This PR is a pure structural refactoring that extracts tool handlers, serializers, synth-audit helpers, and tool definitions into dedicated modules under better_memory/mcp/. All moved code is behaviorally identical to the originals: default_reasons covers all four outcome enum values, best-effort wrappers are copied verbatim, the registry dispatch correctly delegates to the same handler logic, and both _resolve_session_id and _tool_definitions are actively used in server.py at lines 210 and 277 respectively.

No bugs were detected in this PR.

@emp3thy
emp3thy merged commit 06e5f45 into main Jun 11, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant