Skip to content

refactor: extract generic listMCPItems helper for list* MCP operations#3470

Merged
1 commit merged intomainfrom
lpcox/refactor-list-mcp-operations
Apr 9, 2026
Merged

refactor: extract generic listMCPItems helper for list* MCP operations#3470
1 commit merged intomainfrom
lpcox/refactor-list-mcp-operations

Conversation

@lpcox
Copy link
Copy Markdown
Collaborator

@lpcox lpcox commented Apr 9, 2026

Summary

Extracts a generic listMCPItems helper that eliminates the structural duplication across listTools, listResources, and listPrompts in internal/mcp/connection.go.

Problem

The three list functions shared identical boilerplate (~17 lines each):

  1. requireSession() check
  2. Debug log
  3. paginateAll() call with a type-specific fetch closure
  4. Error check
  5. marshalToResponse() with a type-specific result wrapper

If the list-operation contract ever changes (e.g., new error handling, retry logic, metrics), all three functions had to be updated in sync.

Solution

A new generic listMCPItems[Item, Result] helper centralizes the shared boilerplate. Each list method now delegates to it with only the type-specific SDK calls:

func (c *Connection) listTools() (*Response, error) {
    return listMCPItems(c, "tools",
        func(cursor string) (paginatedPage[*sdk.Tool], error) { ... },
        func(items []*sdk.Tool) *sdk.ListToolsResult { ... },
    )
}

Future list operations (e.g., listResourceTemplates) become trivial additions.

Verification

make agent-finished passes — all unit and integration tests green.

Fixes #3155
Fixes #3156

The three list functions (listTools, listResources, listPrompts) shared
identical boilerplate: session check, log, paginateAll call, and
marshalToResponse. Extract this into a generic listMCPItems helper that
accepts a fetch function and result builder, reducing ~51 lines of
duplication to a single 15-line helper.

Each list method now delegates to listMCPItems with its type-specific
SDK calls, making future additions (e.g., listResourceTemplates) a
one-liner instead of 17 lines.

Fixes #3155
Fixes #3156

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Refactors the MCP connection’s list* operations to remove duplicated session-check/log/pagination/marshal boilerplate by introducing a reusable generic helper in internal/mcp/connection.go.

Changes:

  • Added a generic listMCPItems[Item, Result] helper to centralize common list-operation logic.
  • Updated listTools, listResources, and listPrompts to delegate to the helper with type-specific SDK calls/result wrappers.
Show a summary per file
File Description
internal/mcp/connection.go Introduces listMCPItems and rewires the three list methods to use it, reducing duplication.

Copilot's findings

Tip

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

  • Files reviewed: 1/1 changed files
  • Comments generated: 1

}
return paginatedPage[*sdk.Tool]{Items: result.Tools, NextCursor: result.NextCursor}, nil
})
logConn.Printf("list %s: requesting %s list from backend serverID=%s", kind, kind, c.serverID)
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

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

The new request log prefix changed from the prior method-style prefixes (e.g., listTools:) to list tools:. If logs are grepped/alerted on by operation name, this change can be disruptive; consider keeping the previous prefix format (e.g., list%s: or passing an explicit operation name like listTools separate from kind).

Copilot uses AI. Check for mistakes.
@lpcox lpcox closed this pull request by merging all changes into main in 44b24ed Apr 9, 2026
@lpcox lpcox deleted the lpcox/refactor-list-mcp-operations branch April 9, 2026 18:47
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.

[duplicate-code] Duplicate Code Pattern: list* MCP Operations Structural Duplication [duplicate-code] Duplicate Code Analysis Report

2 participants