refactor: extract generic executeBackendRequest[T] to eliminate duplicate backend call pattern by Copilot · Pull Request #8153 · github/gh-aw-mcpg · GitHub
Skip to content

refactor: extract generic executeBackendRequest[T] to eliminate duplicate backend call pattern#8153

Merged
lpcox merged 3 commits into
mainfrom
copilot/duplicate-code-fix-backend-request
Jun 27, 2026
Merged

refactor: extract generic executeBackendRequest[T] to eliminate duplicate backend call pattern#8153
lpcox merged 3 commits into
mainfrom
copilot/duplicate-code-fix-backend-request

Conversation

Copilot AI commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

The prompt handler in registerPromptsFromBackend duplicated the connect → send → error-check → unmarshal pattern that executeBackendToolCall was introduced to centralize, creating two diverging implementations with inconsistent error messages.

Changes

  • internal/server/unified.go: Adds executeBackendRequest[T any] — a generic helper that owns the full backend RPC lifecycle for any method/result type. Refactors executeBackendToolCall into a thin wrapper delegating to it:
func executeBackendRequest[T any](ctx context.Context, l *launcher.Launcher, serverID, sessionID, method string, params map[string]interface{}) (T, error) {
    var zero T
    conn, err := launcher.GetOrLaunchForSession(l, serverID, sessionID)
    // ... connect → send → error-check → unmarshal
    return result, nil
}

func executeBackendToolCall(...) (interface{}, error) {
    logUnified.Printf("executeBackendToolCall: serverID=%s, toolName=%s", serverID, toolName)
    return executeBackendRequest[interface{}](ctx, l, serverID, sessionID, "tools/call", map[string]interface{}{
        "name": toolName, "arguments": args,
    })
}
  • internal/server/tool_registry.go: Replaces the ~20-line inline duplicate in the prompt handler closure with a single call:
result, err := executeBackendRequest[sdk.GetPromptResult](ctx, us.launcher, serverIDCopy, sessionID, "prompts/get", params)

Future backend capability types (e.g. resources/read) can now reuse executeBackendRequest with zero boilerplate, and error handling improvements apply uniformly across all backend call sites.

GitHub Advanced Security started work on behalf of lpcox June 26, 2026 22:41 View session
GitHub Advanced Security finished work on behalf of lpcox June 26, 2026 22:41
GitHub Advanced Security started work on behalf of lpcox June 26, 2026 22:48 View session
Copilot AI changed the title [WIP] Fix duplicate code pattern in prompt handler refactor: extract generic executeBackendRequest[T] to eliminate duplicate backend call pattern Jun 26, 2026
Copilot finished work on behalf of lpcox June 26, 2026 22:48
Copilot AI requested a review from lpcox June 26, 2026 22:48
GitHub Advanced Security finished work on behalf of lpcox June 26, 2026 22:48
@lpcox lpcox marked this pull request as ready for review June 27, 2026 15:28
Copilot AI review requested due to automatic review settings June 27, 2026 15:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors unified-mode backend RPC invocation by introducing a generic executeBackendRequest[T] helper to centralize the backend connection/request/error/unmarshal lifecycle, then reuses it to remove duplicated logic in prompt handling.

Changes:

  • Added executeBackendRequest[T any] to unify backend request execution and JSON unmarshalling into typed results.
  • Refactored executeBackendToolCall into a thin wrapper around executeBackendRequest.
  • Simplified prompt handler registration to use the shared helper instead of duplicating request/unmarshal code.
Show a summary per file
File Description
internal/server/unified.go Introduces executeBackendRequest[T] and refactors executeBackendToolCall to delegate to it.
internal/server/tool_registry.go Replaces duplicated prompt backend call logic with a call to executeBackendRequest[sdk.GetPromptResult].

Review details

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Low

Comment thread internal/server/unified.go Outdated
Comment on lines +220 to +222
if response.Error != nil {
logUnified.Printf("executeBackendToolCall: backend error: serverID=%s, toolName=%s, code=%d", serverID, toolName, response.Error.Code)
return nil, fmt.Errorf("backend error: code=%d, message=%s", response.Error.Code, response.Error.Message)
logUnified.Printf("executeBackendRequest: backend error: serverID=%s, method=%s, code=%d", serverID, method, response.Error.Code)
return zero, fmt.Errorf("backend error %s: code=%d, message=%s", method, response.Error.Code, response.Error.Message)
Comment on lines +420 to 423
result, err := executeBackendRequest[sdk.GetPromptResult](ctx, us.launcher, serverIDCopy, sessionID, "prompts/get", params)
if err != nil {
return nil, err
}
@lpcox

lpcox commented Jun 27, 2026

Copy link
Copy Markdown
Collaborator

Copilot finished work on behalf of lpcox June 27, 2026 15:42
GitHub Advanced Security started work on behalf of lpcox June 27, 2026 15:44 View session
GitHub Advanced Security finished work on behalf of lpcox June 27, 2026 15:44
@lpcox lpcox merged commit 715fde1 into main Jun 27, 2026
27 checks passed
@lpcox lpcox deleted the copilot/duplicate-code-fix-backend-request branch June 27, 2026 15:51
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.

3 participants