Commit cb39742
authored
Refactor: Code organization improvements from semantic clustering analysis (#1068)
Semantic function clustering analysis identified 4 high-priority
refactoring opportunities: duplicate auth middleware application,
misplaced generic utilities, schema transformation mixed with type
definitions, and unnecessary wrapper indirection.
## Changes
### Extract auth middleware helper
- Replaced duplicate conditional auth application with
`applyAuthIfConfigured()` helper
- Eliminates 8 lines of duplication in `internal/server/transport.go`
**Before:**
```go
finalHandler := shutdownHandler
if apiKey != "" {
finalHandler = authMiddleware(apiKey, shutdownHandler.ServeHTTP)
}
// ... repeated for closeHandler
```
**After:**
```go
finalHandler := applyAuthIfConfigured(apiKey, shutdownHandler.ServeHTTP)
finalCloseHandler := applyAuthIfConfigured(apiKey, closeHandler.ServeHTTP)
```
### Move runtime error logging to dedicated file
- Created `internal/server/errors.go` for error handling utilities
- Moved `logRuntimeError()` from `auth.go` (was incorrectly placed in
auth-specific file)
### Separate schema transformation from type definitions
- Created `internal/mcp/schema.go` for 86-line `NormalizeInputSchema()`
function
- `internal/mcp/types.go` now contains only type definitions per Go
conventions
### Remove validation wrapper
- Removed thin `validateServerConfig()` wrapper that only passed `nil`
- Tests call `validateServerConfigWithCustomSchemas()` directly
## Files Changed
- Modified: `internal/server/transport.go`, `internal/server/auth.go`,
`internal/mcp/types.go`, `internal/config/validation.go`,
`internal/config/validation_test.go`
- Added: `internal/server/errors.go`, `internal/mcp/schema.go`
> [!WARNING]
>
> <details>
> <summary>Firewall rules blocked me from connecting to one or more
addresses (expand for details)</summary>
>
> #### I tried to connect to the following addresses, but was blocked by
firewall rules:
>
> - `example.com`
> - Triggering command: `/tmp/go-build1870635187/b275/launcher.test
/tmp/go-build1870635187/b275/launcher.test
-test.testlogfile=/tmp/go-build1870635187/b275/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true ain cfg s.test
--gdwarf-5 --64 -o s.test 6525�� ache/go/1.25.7/x-s -I 86_64/git
--gdwarf-5 --64 -o 256506/b165/_x00-extld=gcc` (dns block)
> - `invalid-host-that-does-not-exist-12345.com`
> - Triggering command: `/tmp/go-build1870635187/b260/config.test
/tmp/go-build1870635187/b260/config.test
-test.testlogfile=/tmp/go-build1870635187/b260/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true d -n 10
5877973+lpcox@users.noreply.github.com>
64/pkg/tool/linux_amd64/vet -p net/netip -lang=go1.25
64/pkg/tool/linux_amd64/vet -I
/opt/hostedtoolcache/go/1.25.7/x64/src/runtime/cgo 256506/b165/
x_amd64/link --gdwarf-5 --64` (dns block)
> - Triggering command: `/tmp/go-build3740779677/b001/config.test
/tmp/go-build3740779677/b001/config.test
-test.testlogfile=/tmp/go-build3740779677/b001/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -uns�� on-clustering-again
/tmp/go-build165256506/b208/vet.cfg .12/x64/bin/bash` (dns block)
> - `nonexistent.local`
> - Triggering command: `/tmp/go-build1870635187/b275/launcher.test
/tmp/go-build1870635187/b275/launcher.test
-test.testlogfile=/tmp/go-build1870635187/b275/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true ain cfg s.test
--gdwarf-5 --64 -o s.test 6525�� ache/go/1.25.7/x-s -I 86_64/git
--gdwarf-5 --64 -o 256506/b165/_x00-extld=gcc` (dns block)
> - `slow.example.com`
> - Triggering command: `/tmp/go-build1870635187/b275/launcher.test
/tmp/go-build1870635187/b275/launcher.test
-test.testlogfile=/tmp/go-build1870635187/b275/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true ain cfg s.test
--gdwarf-5 --64 -o s.test 6525�� ache/go/1.25.7/x-s -I 86_64/git
--gdwarf-5 --64 -o 256506/b165/_x00-extld=gcc` (dns block)
> - `this-host-does-not-exist-12345.com`
> - Triggering command: `/tmp/go-build1870635187/b284/mcp.test
/tmp/go-build1870635187/b284/mcp.test
-test.testlogfile=/tmp/go-build1870635187/b284/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true
rverConfigWithCustomSchemas 256506/b082/vet.cfg cfg --gdwarf-5
ut-2576538208.c -o ache/go/1.25.7/x-importcfg` (dns block)
> - Triggering command: `/tmp/go-build3437160695/b284/mcp.test
/tmp/go-build3437160695/b284/mcp.test
-test.testlogfile=/tmp/go-build3437160695/b284/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true -c=4 -nolocalimports
-importcfg /tmp/go-build1870635187/b305/importcfg -pack
/home/REDACTED/work/gh-aw-mcpg/gh-aw-mcpg/internal/version/version.go
/home/REDACTED/work/gh-aw-mcpg/gh-aw-mcpg/internal/version/version_test.go
-qui�� ain /opt/hostedtoolcache/go/1.25.7/x64/src/net x_amd64/compile
/tmp/go-build165/opt/hostedtoolcache/go/1.25.7/x64/pkg/tool/linux_amd64/compile
-imultiarch x86_64-linux-gnu/tmp/go-build1870635187/b306/_pkg_.a
x_amd64/compile` (dns block)
> - Triggering command: `/tmp/go-build880157664/b284/mcp.test
/tmp/go-build880157664/b284/mcp.test
-test.testlogfile=/tmp/go-build880157664/b284/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true
C77u/Ma68ZNCRMnumNFLzC77u /tmp/go-build165256506/b065/vet.cfg
k/_temp/ghcca-node/node/bin/sh go1.25.7 -c=4 -nolocalimports
256506/b258/importcfg -plu�� celain --ignore-submodules | head -n 10
nction
Co-authored-by: lpcox
<15877973+lpcox@users.noreply.github.com> 64/bin/git
-plugin-opt=-pas/usr/libexec/docker/cli-plugins/docker-buildx
-plugin-opt=-pasdocker-cli-plugin-metadata
-plugin-opt=-pass-through=-lpthr(create|run)
/opt/hostedtoolcache/go/1.25.7/x64/pkg/tool/linux_amd64/vet` (dns block)
>
> If you need me to access, download, or install something from one of
these locations, you can either:
>
> - Configure [Actions setup
steps](https://gh.io/copilot/actions-setup-steps) to set up my
environment, which run before the firewall is enabled
> - Add the appropriate URLs or hosts to the custom allowlist in this
repository's [Copilot coding agent
settings](https://github.com/github/gh-aw-mcpg/settings/copilot/coding_agent)
(admins only)
>
> </details>
<!-- START COPILOT ORIGINAL PROMPT -->
<details>
<summary>Original prompt</summary>
----
*This section details on the original issue you should resolve*
<issue_title>[refactor] Semantic Function Clustering Analysis - Code
Organization & Refactoring Opportunities</issue_title>
<issue_description>## Executive Summary
Analyzed **63 Go source files** (excluding tests) across **16 packages**
containing **~450 functions** to identify refactoring opportunities
through semantic function clustering and duplicate detection.
**Key Findings:**
- ✅ **Overall code quality is excellent** - well-organized with strong
use of Go patterns
- 🔧 **8 high-priority refactoring opportunities** identified (duplicate
logic, misplaced functions)
- 📊 **Function clusters are generally well-organized** by file purpose
- 7 files changed
Lines changed: 132 additions & 127 deletions
File tree
- internal
- config
- mcp
- server
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | 103 | | |
109 | 104 | | |
110 | 105 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
352 | 352 | | |
353 | 353 | | |
354 | 354 | | |
355 | | - | |
| 355 | + | |
356 | 356 | | |
357 | 357 | | |
358 | 358 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | | - | |
7 | 5 | | |
8 | 6 | | |
9 | | - | |
10 | | - | |
11 | 7 | | |
12 | 8 | | |
13 | 9 | | |
| |||
49 | 45 | | |
50 | 46 | | |
51 | 47 | | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | 4 | | |
6 | | - | |
7 | 5 | | |
8 | 6 | | |
9 | 7 | | |
| |||
48 | 46 | | |
49 | 47 | | |
50 | 48 | | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
59 | 68 | | |
60 | 69 | | |
61 | 70 | | |
| |||
116 | 125 | | |
117 | 126 | | |
118 | 127 | | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
| 128 | + | |
123 | 129 | | |
124 | 130 | | |
125 | 131 | | |
| |||
133 | 139 | | |
134 | 140 | | |
135 | 141 | | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
| 142 | + | |
140 | 143 | | |
141 | 144 | | |
142 | 145 | | |
| |||
0 commit comments