refactor: consolidate method-dispatch boilerplate in operation/* #8

Open
opened 2026-05-23 18:30:42 +00:00 by Klaus · 0 comments
Owner

Problem

Many tools in operation/* follow the same shape: a single *_read / *_write tool with a method enum that fans out to ~5–10 sub-handlers. The fan-out is reimplemented per module — each does its own switch, its own param extraction, its own error wrapping. Adding a new common behavior (e.g. consistent logging, pagination caps, the %w sweep) requires editing the same shape dozens of times.

Examples: operation/issue/issue.go, operation/label/label.go, operation/milestone/milestone.go, operation/repo/*, operation/pull/pull.go.

Suggested direction

Introduce a small dispatcher in pkg/tool:

type MethodHandler func(ctx, req) (*mcp.Result, error)
func Dispatch(handlers map[string]MethodHandler) ToolFn { ... }

Each module registers a map[string]MethodHandler instead of a hand-rolled switch. Cross-cutting concerns (logging, validation, error wrapping) live in the dispatcher, applied once.

## Problem Many tools in `operation/*` follow the same shape: a single `*_read` / `*_write` tool with a `method` enum that fans out to ~5–10 sub-handlers. The fan-out is reimplemented per module — each does its own switch, its own param extraction, its own error wrapping. Adding a new common behavior (e.g. consistent logging, pagination caps, the `%w` sweep) requires editing the same shape dozens of times. Examples: `operation/issue/issue.go`, `operation/label/label.go`, `operation/milestone/milestone.go`, `operation/repo/*`, `operation/pull/pull.go`. ## Suggested direction Introduce a small dispatcher in `pkg/tool`: ```go type MethodHandler func(ctx, req) (*mcp.Result, error) func Dispatch(handlers map[string]MethodHandler) ToolFn { ... } ``` Each module registers a `map[string]MethodHandler` instead of a hand-rolled switch. Cross-cutting concerns (logging, validation, error wrapping) live in the dispatcher, applied once.
Klaus referenced this issue from a commit 2026-05-23 18:47:28 +00:00
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Klaus/gitea-mcp#8