Skip to content

Eliminate duplicate code in server HTTP endpoint registration#1186

Merged
lpcox merged 2 commits intomainfrom
claude/fix-duplicate-code-issues-again
Feb 20, 2026
Merged

Eliminate duplicate code in server HTTP endpoint registration#1186
lpcox merged 2 commits intomainfrom
claude/fix-duplicate-code-issues-again

Conversation

@Claude
Copy link
Contributor

@Claude Claude AI commented Feb 20, 2026

The server package contained ~20 lines of duplicated code across routed.go and transport.go for auth middleware application and common HTTP endpoint registration.

Changes

  • Extract registerCommonEndpoints() helper (handlers.go:87-104)
    Consolidates OAuth discovery, health check, and close endpoint registration for both routed and unified modes. Previously duplicated across both server creation functions.

  • Standardize on applyAuthIfConfigured() helper
    Replaced inline auth conditionals in routed.go with existing helper from transport.go. Both modes now use identical auth application logic.

Before

// routed.go - inline duplication
finalHandler := shutdownHandler
if apiKey != "" {
    finalHandler = authMiddleware(apiKey, shutdownHandler.ServeHTTP)
}

// Separate endpoint registration in both files
mux.Handle("/.well-known/oauth-authorization-server", withResponseLogging(handleOAuthDiscovery()))
mux.Handle("/health", withResponseLogging(HandleHealth(unifiedServer)))
// ... repeated in transport.go

After

// Both routed.go and transport.go
registerCommonEndpoints(mux, unifiedServer, apiKey)
finalHandler := applyAuthIfConfigured(apiKey, shutdownHandler.ServeHTTP)

Changes to shared endpoint behavior now only require updates in one location.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • example.com
    • Triggering command: /tmp/go-build1821963260/b275/launcher.test /tmp/go-build1821963260/b275/launcher.test -test.testlogfile=/tmp/go-build1821963260/b275/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true v0.12.18/builtin.go v0.12.18/code.go 64/pkg/tool/linux_amd64/compile go1.25.6 -c=4 -nolocalimports 64/pkg/tool/linux_amd64/compile -I g_.a -I cal/bin/as --gdwarf-5 ernal/sys -o ortcfg (dns block)
  • invalid-host-that-does-not-exist-12345.com
    • Triggering command: /tmp/go-build1821963260/b260/config.test /tmp/go-build1821963260/b260/config.test -test.testlogfile=/tmp/go-build1821963260/b260/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true 64/src/net x5Bl6W7wQ 64/pkg/tool/linux_amd64/vet -p internal/fmtsortrev-parse -lang=go1.25 64/pkg/tool/linuHEAD ortc�� se httptrace/trace.go 64/pkg/tool/linux_amd64/vet -o /dev/null /tmp/cc13LNSQ.s 64/pkg/tool/linux_amd64/vet (dns block)
  • nonexistent.local
    • Triggering command: /tmp/go-build1821963260/b275/launcher.test /tmp/go-build1821963260/b275/launcher.test -test.testlogfile=/tmp/go-build1821963260/b275/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true v0.12.18/builtin.go v0.12.18/code.go 64/pkg/tool/linux_amd64/compile go1.25.6 -c=4 -nolocalimports 64/pkg/tool/linux_amd64/compile -I g_.a -I cal/bin/as --gdwarf-5 ernal/sys -o ortcfg (dns block)
  • slow.example.com
    • Triggering command: /tmp/go-build1821963260/b275/launcher.test /tmp/go-build1821963260/b275/launcher.test -test.testlogfile=/tmp/go-build1821963260/b275/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true v0.12.18/builtin.go v0.12.18/code.go 64/pkg/tool/linux_amd64/compile go1.25.6 -c=4 -nolocalimports 64/pkg/tool/linux_amd64/compile -I g_.a -I cal/bin/as --gdwarf-5 ernal/sys -o ortcfg (dns block)
  • this-host-does-not-exist-12345.com
    • Triggering command: /tmp/go-build1821963260/b284/mcp.test /tmp/go-build1821963260/b284/mcp.test -test.testlogfile=/tmp/go-build1821963260/b284/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true eutil.test 3538032/b048/vet.cfg ortcfg.link --gdwarf-5 --64 -o L8U9Qyx1I3DkwMKxtw/MSkguOXFE0oGgHEAD -uns�� submodules | head -n 10 /tmp/go-build3313538032/b012/vet.cfg g_.a show-toplevel --global (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>[duplicate-code] Duplicate Code Analysis Report</issue_title>
<issue_description>## Summary

Analysis of internal/server/ Go files found 2 significant duplication patterns in the HTTP server creation code. Both affect routed.go and transport.go.

Detected Patterns

This analysis found 2 significant duplication patterns:

  1. applyAuthIfConfigured helper not used in routed.go - Severity: Medium - See sub-issue [duplicate-code] Duplicate Code Pattern: applyAuthIfConfigured helper not reused in routed.go #1160
  2. Common HTTP mux route registration duplicated - Severity: Medium - See sub-issue [duplicate-code] Duplicate Code Pattern: Common HTTP mux setup duplicated between routed.go and transport.go #1159

Overall Impact

  • Total Duplicated Lines: ~20 lines of duplicated logic
  • Affected Files: 2 files (internal/server/routed.go, internal/server/transport.go)
  • Maintainability Risk: Medium — changes to auth or shared endpoint behavior require updates in two places
  • Refactoring Priority: Low-Medium — straightforward consolidation with minimal risk

Next Steps

  1. Review individual pattern sub-issues for detailed analysis
  2. Prioritize refactoring based on severity and impact
  3. Create implementation plan for highest priority patterns

Analysis Metadata

  • Analyzed Files: 3 Go files (routed.go, transport.go, unified.go)
  • Detection Method: Pattern search and semantic analysis
  • Commit: 4cd522d
  • Analysis Date: 2026-02-20</issue_description>

Comments on the Issue (you are @claude[agent] in this section)

- Replace inline auth conditionals in routed.go with applyAuthIfConfigured helper
- Extract registerCommonEndpoints helper to consolidate OAuth, health, and close endpoint registration
- Both routed.go and transport.go now use shared helpers, eliminating ~20 lines of duplication

Fixes #1160 and #1159

Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
@Claude Claude AI changed the title [WIP] Fix duplicate code in HTTP server creation Eliminate duplicate code in server HTTP endpoint registration Feb 20, 2026
@Claude Claude AI requested a review from lpcox February 20, 2026 19:42
@lpcox lpcox marked this pull request as ready for review February 20, 2026 19:53
Copilot AI review requested due to automatic review settings February 20, 2026 19:53
@lpcox lpcox merged commit eaae2b7 into main Feb 20, 2026
17 checks passed
@lpcox lpcox deleted the claude/fix-duplicate-code-issues-again branch February 20, 2026 19:53
Copy link
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

This PR refactors internal/server HTTP server setup to remove duplicated endpoint registration and authentication-wrapping logic, keeping routed and unified (streamable HTTP) modes consistent and easier to maintain.

Changes:

  • Centralized OAuth discovery, /health, and /close registration into a new registerCommonEndpoints() helper.
  • Standardized conditional auth wrapping in routed mode by using the existing applyAuthIfConfigured() helper.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
internal/server/transport.go Replaces inlined common endpoint registrations with registerCommonEndpoints() in unified mode.
internal/server/routed.go Uses registerCommonEndpoints() and switches to applyAuthIfConfigured() for per-route auth wrapping.
internal/server/handlers.go Adds registerCommonEndpoints() implementation to register OAuth discovery, health, and close endpoints consistently.

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

lpcox added a commit that referenced this pull request Feb 20, 2026
Investigation of smoke test run #22238913503 reveals the Serena MCP test
failure is a false negative - the server operates correctly but the
Copilot agent cannot discover its tools due to incorrect tool naming.

## Root Cause

Serena MCP server initializes successfully:
- Loads 40 tools, exposes 23 in 'codex' context (excludes
`create_text_file`, `read_file`, `execute_shell_command`,
`prepare_for_new_conversation`, `replace_content`)
- Auto-configures project, detects Go as primary language (53.2%)
- Registers in gateway alongside other MCP servers
- Both `activate_project` and `find_symbol` are available in the exposed
toolset

Agent reports tools unavailable without attempting invocation:
```
rpc safeoutputs→tools/call missing_tool
  reason: "The Serena MCP server tools (activate_project, find_symbol) are not available"
```

**Issue**: The smoke test referenced tools without the server name
prefix (`activate_project`, `find_symbol`), but the Copilot agent
framework expects the `server-toolname` pattern for proper tool
discovery in routed mode.

## Changes Made

Updated `.github/workflows/smoke-copilot.md` to use correct tool naming
convention:
- Changed `activate_project` → `serena-activate_project`
- Changed `find_symbol` → `serena-find_symbol`

This matches the naming pattern used by other MCP tools in the workflow
(e.g., `safeinputs-gh`, `safeinputs-make`) where tools are prefixed with
their MCP server name using hyphens.

## Analysis

**Confirmed Working**:
- Container launch: `ghcr.io/github/serena-mcp-server:latest`
- Server initialization: v0.1.4, process id=1
- Project configuration:
`/home/runner/work/gh-aw-mcpg/gh-aw-mcpg/.serena/project.yml`
- Gateway registration: Listed in startup logs with 6 total servers

**Fixed Issue**:
- Tool naming now follows the `server-toolname` convention required by
the agent framework for tool discoverability
- Recompiled workflow lock file with gh-aw v0.47.4

**Note**: Playwright test failure (network timeout to github.com) is
expected due to firewall constraints, not a code issue.

<!-- START COPILOT ORIGINAL PROMPT -->



<details>

<summary>Original prompt</summary>

> 
> ----
> 
> *This section details on the original issue you should resolve*
> 
> <issue_title>Smoke Test: Copilot - 22238913503</issue_title>
> <issue_description>## Smoke Test Results
> 
> | Test | Status |
> |------|--------|
> | GitHub MCP Testing (last 2 merged PRs) | ✅ |
> | Safe Inputs GH CLI Testing | ✅ |
> | Serena MCP Testing | ❌ (not available) |
> | Make Build Testing | ✅ |
> | Playwright Testing | ❌ (network timeout) |
> | File Writing Testing | ✅ |
> | Bash Tool Testing | ✅ |
> | Discussion Interaction Testing | ✅ |
> | Agentic Workflows MCP Testing | ✅ |
> 
> **Overall Status: PASS** (7/9 - 2 tests failed due to environment
constraints)
> 
> ### Test Details
> 
> - **GitHub MCP**: Found PRs #1186 "Eliminate
duplicate code in server HTTP endpoint registration" and
#1173 "Addressing PR comments"
> - **GH CLI**: Queried PRs successfully (no open PRs)
> - **Serena MCP**: ❌ Tools not available in this environment
> - **Build**: `make build` succeeded, `awmg` binary created
> - **Playwright**: ❌ Network timeout (github.com blocked by firewall)
> - **File Writing**: Created
`/tmp/gh-aw/agent/smoke-test-copilot-22238913503.txt`
> - **Bash**: Verified file contents
> - **Discussion**: Got discussion #45, added comment
> - **Agentic Workflows**: smoke-copilot workflow is compiled and active
(triggers: PR label, reaction, schedule every 12h)
> 
> **Run URL**:
https://github.com/github/gh-aw-mcpg/actions/runs/22238913503
> **Timestamp**: 2026-02-20T19:58:14Z
> 
> 
> 
> 
> > 📰 *BREAKING: Report filed by [Smoke
Copilot](https://github.com/github/gh-aw-mcpg/actions/runs/22238913503)*
> > - [x] expires <!-- gh-aw-expires: 2026-02-20T22:02:03.746Z --> on
Feb 20, 2026, 10:02 PM UTC
> 
> <!-- gh-aw-agentic-workflow: Smoke Copilot, engine: copilot, run:
https://github.com/github/gh-aw-mcpg/actions/runs/22238913503 -->
> 
> <!-- gh-aw-workflow-id: smoke-copilot --></issue_description>
> 
> <agent_instructions>Try to understand why the serena test is
failing</agent_instructions>
> 
> ## Comments on the Issue (you are @claude[agent] in this section)
> 
> <comments>
> </comments>
> 


</details>



<!-- START COPILOT CODING AGENT SUFFIX -->

- Fixes #1190
lpcox added a commit that referenced this pull request Feb 20, 2026
- [x] Fix Serena tools configuration in smoke-copilot.md (change nested
languages structure to array format)
- [x] Recompile smoke-copilot workflow to regenerate lock file
- [x] Verify the fix matches working configurations in other workflows
- [x] Store memory about correct Serena tool configuration format

<!-- START COPILOT ORIGINAL PROMPT -->



<details>

<summary>Original prompt</summary>

> 
> ----
> 
> *This section details on the original issue you should resolve*
> 
> <issue_title>Smoke Test: Copilot - 22243401416</issue_title>
> <issue_description>## Smoke Test Results
> 
> | Test | Status |
> |------|--------|
> | GitHub MCP - Last 2 merged PRs | ✅ PR #1195 "Fix
Serena MCP test tool discovery in smoke test workflow", PR
#1186 "Eliminate duplicate code in server HTTP endpoint
registration" |
> | Safe Inputs GH CLI - 2 PRs | ✅ PR #1196 "[WIP]
Refactor semantic function clustering for code organization" |
> | Serena MCP - activate_project + find_symbol | ❌ Tools not available
in this agent context |
> | Make Build | ✅ `make build` succeeded, `awmg` binary produced |
> | Playwright - github.com title | ✅ Title contains "GitHub" |
> | File Writing | ✅
`/tmp/gh-aw/agent/smoke-test-copilot-22243401416.txt` created |
> | Bash Tool | ✅ File read back successfully |
> | Discussion Interaction | ✅ Comment added to discussion
#45 |
> | Agentic Workflows MCP Status | ✅ smoke-copilot: compiled=Yes,
triggers=PR/reaction/schedule |
> 
> ## Overall Status: ⚠️ PARTIAL PASS (8/9 tests passed, Serena tools
unavailable)
> 
> **Run URL**:
https://github.com/github/gh-aw-mcpg/actions/runs/22243401416
> **Timestamp**: 2026-02-20T22:21 UTC
> 
> 
> 
> 
> > 📰 *BREAKING: Report filed by [Smoke
Copilot](https://github.com/github/gh-aw-mcpg/actions/runs/22243401416)*
> > - [x] expires <!-- gh-aw-expires: 2026-02-21T00:23:33.836Z --> on
Feb 21, 2026, 12:23 AM UTC
> 
> <!-- gh-aw-agentic-workflow: Smoke Copilot, engine: copilot, run:
https://github.com/github/gh-aw-mcpg/actions/runs/22243401416 -->
> 
> <!-- gh-aw-workflow-id: smoke-copilot --></issue_description>
> 
> <agent_instructions>Figure out why serena is not
passing</agent_instructions>
> 
> ## Comments on the Issue (you are @claude[agent] in this section)
> 
> <comments>
> </comments>
> 


</details>



<!-- START COPILOT CODING AGENT SUFFIX -->

- Fixes #1198
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 Analysis Report

3 participants