refactor: extract generic listMCPItems helper for list* MCP operations#3470
Merged
1 commit merged intomainfrom Apr 9, 2026
Merged
refactor: extract generic listMCPItems helper for list* MCP operations#34701 commit merged intomainfrom
1 commit merged intomainfrom
Conversation
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>
This was referenced Apr 9, 2026
Contributor
There was a problem hiding this comment.
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, andlistPromptsto 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) |
There was a problem hiding this comment.
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Extracts a generic
listMCPItemshelper that eliminates the structural duplication acrosslistTools,listResources, andlistPromptsininternal/mcp/connection.go.Problem
The three list functions shared identical boilerplate (~17 lines each):
requireSession()checkpaginateAll()call with a type-specific fetch closuremarshalToResponse()with a type-specific result wrapperIf 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:Future list operations (e.g.,
listResourceTemplates) become trivial additions.Verification
make agent-finishedpasses — all unit and integration tests green.Fixes #3155
Fixes #3156