Skip to content

Commit 684fa0e

Browse files
authored
Enhance configuration management and signal handling in mcpproxy (#2)
* Enhance configuration management and signal handling in mcpproxy - Added automatic creation of default configuration file and directory on first-time setup. - Improved configuration loading priority with fallback mechanisms. - Implemented robust signal handling for graceful shutdown, including context-aware operations and logging. - Updated server background operations to respect cancellation context for better resource management. * Refactor cache and logging structures for consistency - Renamed CacheRecord to Record and CacheStats to Stats for clarity. - Updated related methods and types to reflect the new naming conventions. - Improved error handling in various functions by deferring error checks. - Enhanced logging synchronization to ensure proper resource management. * Refactor function signatures and improve error handling - Updated function signatures in main.go and manager.go for clarity by removing unused parameters. - Enhanced error handling in mcp.go and client.go by ensuring proper return values. - Introduced constants for operation names in mcp.go to improve readability and maintainability. - Removed deprecated test fixtures in fixtures_test.go to streamline the codebase. * Refactor configuration defaults and improve error handling - Introduced a constant for the default port in config.go to enhance maintainability. - Updated DefaultConfig to use the new constant for setting the Listen address. - Improved error handling in loader.go by changing file write permissions for security. - Added a testServerConnection method in e2e_test.go to verify server readiness more effectively. - Refactored function signatures in various files for clarity and consistency. * Enhance cross-platform build configuration and testing documentation - Updated .goreleaser.yaml to define builds for Linux, Windows, and macOS with appropriate settings. - Added Windows Testing Guide to README.md for improved clarity on running tests in Windows environments. - Modified GitHub Actions workflows to support platform-specific test execution and binary builds. - Introduced scripts for running unit tests on Windows with proper argument handling. - Refactored main.go to support tray functionality conditionally based on build tags. * Refactor GitHub Actions workflows to streamline build configurations - Removed redundant CGO_ENABLED environment variable from jobs in e2e-tests.yml. - Added CGO_ENABLED=0 to build commands in unit-tests.yml for cross-compilation consistency. - Enhanced logging in manager.go by changing warning to debug level for disconnected clients. - Optimized GetTotalToolCount method to minimize network calls during shutdown. * Improve e2e test logging and concurrency handling - Updated e2e-tests.yml to run mcpproxy in the background for log file creation. - Enhanced e2e_test.go to count log messages more reliably and verify goroutine presence. - Adjusted assertions to allow for tolerance in concurrent logging scenarios. * Enhance e2e test setup for cross-platform compatibility - Updated e2e-tests.yml to build mcpproxy binary for both Windows and Unix environments. - Modified TestE2E_MCPProxyWithLogging to dynamically determine binary name based on OS. - Improved TestE2E_ConcurrentLogging to use unique log filenames and adjusted concurrency settings for Windows. * Refactor mcpproxy process handling in e2e-tests.yml - Updated e2e-tests.yml to store the mcpproxy process in a variable before stopping it, improving clarity and maintainability of the workflow. * Update log path and compliance check in e2e-tests.yml - Changed log path to use the LOCALAPPDATA environment variable for better compatibility. - Updated compliance check to match specific OS standard compliance information. * Update log output in e2e-tests.yml for Windows compatibility - Changed log path output to use the LOCALAPPDATA environment variable. - Updated OS standard message to reflect Windows Application Data Guidelines. * Refactor Windows log testing in e2e-tests.yml for improved error handling - Updated PowerShell script to include try-catch for mcpproxy process management. - Reorganized log directory and file existence checks for clarity and reliability. - Enhanced log content verification to ensure compliance with OS standards. * Refactor server context handling in server.go to prevent race conditions - Updated background initialization methods to use read locks for accessing server context. - Added cancellation of the old context in StartServer to avoid potential race conditions. - Improved tool re-indexing logic after configuration reload to ensure thread safety. * Add MCP Schema Compatibility Summary and Update Gemini Compatibility Documentation - Introduced COMPATIBILITY_SUMMARY.md detailing schema changes for `upstream_servers` and `call_tool` tools, transitioning to JSON string parameters for improved compatibility. - Created GEMINI_COMPATIBILITY.md to address known issues with Gemini 2.5 Pro Preview 06-05 and provide updated usage examples and recommendations. - Refactored mcp.go to support new JSON string parameters while maintaining backward compatibility with legacy object formats. * Update compatibility documentation and refactor MCP parameters to JSON format - Modified COMPATIBILITY_SUMMARY.md to include changes for `env`, `headers`, and `patch` parameters to JSON strings. - Updated GEMINI_COMPATIBILITY.md with examples reflecting the new JSON string format for `upstream_servers`. - Refactored mcp.go to handle new JSON string parameters while ensuring backward compatibility with legacy formats.
1 parent d36728a commit 684fa0e

37 files changed

Lines changed: 1581 additions & 687 deletions

.github/workflows/e2e-tests.yml

Lines changed: 83 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ jobs:
3434
path: |
3535
~/.cache/go-build
3636
~/go/pkg/mod
37+
~/AppData/Local/go-build
3738
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
3839
restore-keys: |
3940
${{ runner.os }}-go-${{ matrix.go-version }}-
@@ -45,6 +46,18 @@ jobs:
4546
- name: Verify dependencies
4647
run: go mod verify
4748

49+
- name: Build mcpproxy binary for tests (Windows)
50+
if: matrix.os == 'windows-latest'
51+
env:
52+
CGO_ENABLED: "0"
53+
run: go build -tags nogui -o mcpproxy.exe ./cmd/mcpproxy
54+
55+
- name: Build mcpproxy binary for tests (Unix)
56+
if: matrix.os != 'windows-latest'
57+
env:
58+
CGO_ENABLED: "0"
59+
run: go build -tags nogui -o mcpproxy ./cmd/mcpproxy
60+
4861
- name: Run unit tests
4962
run: go test -v -race -timeout 30s ./internal/...
5063

@@ -77,7 +90,6 @@ jobs:
7790

7891
env:
7992
GO111MODULE: "on"
80-
CGO_ENABLED: "0"
8193

8294
steps:
8395
- name: Checkout code
@@ -89,6 +101,8 @@ jobs:
89101
go-version: "1.23.10"
90102

91103
- name: Build mcpproxy
104+
env:
105+
CGO_ENABLED: "0"
92106
run: go build -tags nogui -o mcpproxy ./cmd/mcpproxy
93107

94108
- name: Test mcpproxy binary
@@ -112,7 +126,17 @@ jobs:
112126
fi
113127
fi
114128
115-
- name: Run tests with coverage
129+
- name: Run tests with coverage (Windows)
130+
if: matrix.os == 'windows-latest'
131+
shell: pwsh
132+
run: |
133+
go test -v -race '-coverprofile=coverage.out' -covermode=atomic ./internal/server -run TestE2E
134+
go test -v -race '-coverprofile=coverage-logs.out' -covermode=atomic ./internal/logs -run TestE2E
135+
go tool cover '-html=coverage.out' -o coverage.html
136+
go tool cover '-html=coverage-logs.out' -o coverage-logs.html
137+
138+
- name: Run tests with coverage (Unix)
139+
if: matrix.os != 'windows-latest'
116140
run: |
117141
go test -v -race -coverprofile=coverage.out -covermode=atomic ./internal/server -run TestE2E
118142
go test -v -race -coverprofile=coverage-logs.out -covermode=atomic ./internal/logs -run TestE2E
@@ -133,7 +157,6 @@ jobs:
133157

134158
env:
135159
GO111MODULE: "on"
136-
CGO_ENABLED: "0"
137160

138161
strategy:
139162
matrix:
@@ -164,7 +187,16 @@ jobs:
164187
- name: Run logging E2E tests
165188
run: go test -v -race -timeout 5m ./internal/logs -run TestE2E
166189

167-
- name: Build mcpproxy for logging test
190+
- name: Build mcpproxy for logging test (Windows)
191+
if: matrix.os == 'windows-latest'
192+
env:
193+
CGO_ENABLED: "0"
194+
run: go build -tags nogui -o mcpproxy.exe ./cmd/mcpproxy
195+
196+
- name: Build mcpproxy for logging test (Unix)
197+
if: matrix.os != 'windows-latest'
198+
env:
199+
CGO_ENABLED: "0"
168200
run: go build -tags nogui -o mcpproxy ./cmd/mcpproxy
169201

170202
- name: Test OS-specific log directory creation (Unix)
@@ -175,7 +207,11 @@ jobs:
175207
echo "OS Standard: ${{ matrix.log_standard }}"
176208
177209
# Run mcpproxy briefly to create log files
178-
timeout 5s ./mcpproxy --log-to-file --log-level info --listen :0 --tray=false || true
210+
./mcpproxy --log-to-file --log-level info --listen :0 --tray=false &
211+
MCPPROXY_PID=$!
212+
sleep 5
213+
kill $MCPPROXY_PID 2>/dev/null || true
214+
wait $MCPPROXY_PID 2>/dev/null || true
179215
180216
# Check if log directory was created
181217
if [ -d "${{ matrix.log_path_check }}" ]; then
@@ -212,52 +248,58 @@ jobs:
212248
213249
- name: Test OS-specific log directory creation (Windows)
214250
if: matrix.os == 'windows-latest'
215-
shell: powershell
251+
shell: pwsh
216252
run: |
217253
Write-Host "Testing logging on Windows"
218-
Write-Host "Expected log path: ${{ matrix.log_path_check }}"
219-
Write-Host "OS Standard: ${{ matrix.log_standard }}"
254+
Write-Host "Expected log path: $env:LOCALAPPDATA\mcpproxy\logs"
255+
Write-Host "OS Standard: Windows Application Data Guidelines"
220256
221257
# Run mcpproxy briefly to create log files
222-
Start-Process -FilePath "./mcpproxy.exe" -ArgumentList "--log-to-file", "--log-level", "info", "--listen", ":0", "--tray=false" -NoNewWindow -PassThru | ForEach-Object { Start-Sleep -Seconds 5; $_ | Stop-Process -Force }
258+
try {
259+
$process = Start-Process -FilePath "./mcpproxy.exe" -ArgumentList "--log-to-file", "--log-level", "info", "--listen", ":0", "--tray=false" -NoNewWindow -PassThru
260+
Start-Sleep -Seconds 5
261+
$process | Stop-Process -Force -ErrorAction SilentlyContinue
262+
} catch {
263+
Write-Host "Error starting/stopping mcpproxy: $_"
264+
}
223265
224266
# Check if log directory was created
225-
$logPath = "${{ matrix.log_path_check }}"
226-
if (Test-Path $logPath) {
227-
Write-Host "✓ Log directory created successfully"
228-
Get-ChildItem $logPath
229-
230-
# Check if log file exists and has content
231-
$logFile = Join-Path $logPath "mcpproxy.log"
232-
if (Test-Path $logFile) {
233-
Write-Host "✓ Log file created successfully"
234-
$fileSize = (Get-Item $logFile).Length
235-
Write-Host "Log file size: $fileSize bytes"
236-
Write-Host "First few lines:"
237-
Get-Content $logFile -Head 3
238-
239-
# Verify log contains expected content
240-
$content = Get-Content $logFile -Raw
241-
if ($content -match "Log directory configured") {
242-
Write-Host "✓ Log contains expected startup messages"
243-
} else {
244-
Write-Host "⚠ Log missing expected startup messages"
245-
}
246-
247-
if ($content -match "${{ matrix.log_standard }}") {
248-
Write-Host "✓ Log contains OS standard compliance information"
249-
} else {
250-
Write-Host "⚠ Log missing OS standard compliance information"
251-
}
252-
} else {
253-
Write-Host "✗ Log file not created"
254-
exit 1
255-
}
256-
} else {
267+
$logPath = "$env:LOCALAPPDATA\mcpproxy\logs"
268+
if (-not (Test-Path $logPath)) {
257269
Write-Host "✗ Log directory not created"
258270
exit 1
259271
}
260272
273+
Write-Host "✓ Log directory created successfully"
274+
Get-ChildItem $logPath
275+
276+
# Check if log file exists and has content
277+
$logFile = Join-Path $logPath "mcpproxy.log"
278+
if (-not (Test-Path $logFile)) {
279+
Write-Host "✗ Log file not created"
280+
exit 1
281+
}
282+
283+
Write-Host "✓ Log file created successfully"
284+
$fileSize = (Get-Item $logFile).Length
285+
Write-Host "Log file size: $fileSize bytes"
286+
Write-Host "First few lines:"
287+
Get-Content $logFile -Head 3
288+
289+
# Verify log contains expected content
290+
$content = Get-Content $logFile -Raw
291+
if ($content -match "Log directory configured") {
292+
Write-Host "✓ Log contains expected startup messages"
293+
} else {
294+
Write-Host "⚠ Log missing expected startup messages"
295+
}
296+
297+
if ($content -match "Windows Application Data Guidelines") {
298+
Write-Host "✓ Log contains OS standard compliance information"
299+
} else {
300+
Write-Host "⚠ Log missing OS standard compliance information"
301+
}
302+
261303
- name: Test log rotation
262304
if: matrix.os == 'ubuntu-latest'
263305
run: |

.github/workflows/pr-build.yml

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,40 @@ jobs:
2828
- name: Cache Go modules
2929
uses: actions/cache@v4
3030
with:
31-
path: ~/go/pkg/mod
31+
path: |
32+
~/.cache/go-build
33+
~/go/pkg/mod
34+
~/AppData/Local/go-build
3235
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
3336
restore-keys: |
3437
${{ runner.os }}-go-${{ matrix.go-version }}-
3538
3639
- name: Download dependencies
3740
run: go mod download
3841

39-
- name: Run tests
42+
- name: Run tests (with nogui for CI)
4043
run: go test -tags nogui -v ./...
4144

42-
- name: Build binary
45+
- name: Build binary for Linux
46+
if: runner.os == 'Linux'
4347
shell: bash
4448
run: |
4549
VERSION=pr-${{ github.event.number }}-${{ github.sha }}
46-
if [[ "${{ runner.os }}" == "Windows" ]]; then
47-
go build -tags nogui -ldflags "-X main.version=${VERSION}" -o mcpproxy.exe ./cmd/mcpproxy
48-
else
49-
go build -tags nogui -ldflags "-X main.version=${VERSION}" -o mcpproxy ./cmd/mcpproxy
50-
fi
50+
CGO_ENABLED=0 go build -ldflags "-X main.version=${VERSION}" -o mcpproxy ./cmd/mcpproxy
51+
52+
- name: Build binary for macOS
53+
if: runner.os == 'macOS'
54+
shell: bash
55+
run: |
56+
VERSION=pr-${{ github.event.number }}-${{ github.sha }}
57+
go build -ldflags "-X main.version=${VERSION}" -o mcpproxy ./cmd/mcpproxy
58+
59+
- name: Build binary for Windows
60+
if: runner.os == 'Windows'
61+
shell: bash
62+
run: |
63+
VERSION=pr-${{ github.event.number }}-${{ github.sha }}
64+
CGO_ENABLED=0 go build -ldflags "-X main.version=${VERSION}" -o mcpproxy.exe ./cmd/mcpproxy
5165
5266
- name: Test version output
5367
shell: bash
@@ -57,26 +71,3 @@ jobs:
5771
else
5872
./mcpproxy --version
5973
fi
60-
61-
goreleaser-check:
62-
runs-on: ubuntu-latest
63-
64-
env:
65-
GO111MODULE: "on"
66-
67-
steps:
68-
- name: Checkout
69-
uses: actions/checkout@v4
70-
with:
71-
fetch-depth: 0
72-
73-
- name: Set up Go
74-
uses: actions/setup-go@v5
75-
with:
76-
go-version: "1.23.10"
77-
78-
- name: Check GoReleaser config
79-
uses: goreleaser/goreleaser-action@v5
80-
with:
81-
version: v2.3.2
82-
args: check

0 commit comments

Comments
 (0)