Commit 65b9399
authored
Eliminate duplicate sys tool metadata in registerSysTools() (#1349)
The `registerSysTools()` function in `internal/server/unified.go` was
duplicating tool metadata (Name, Description, InputSchema) for each sys
tool—once when storing in the internal `ToolInfo` map and again when
registering with the SDK.
## Changes
- **Added `registerSysTool()` helper function** that encapsulates both
internal storage and SDK registration, accepting metadata parameters
once
- **Refactored `sys___init` and `sys___list_servers`** to use the
helper, eliminating ~22 lines of duplicated metadata definitions
## Example
Before:
```go
// Store tool info
us.tools["sys___init"] = &ToolInfo{
Name: "sys___init",
Description: "Initialize the MCPG system...",
InputSchema: map[string]interface{}{...},
// ...
}
// Register with SDK (duplicate metadata)
sdk.AddTool(us.server, &sdk.Tool{
Name: "sys___init",
Description: "Initialize the MCPG system...",
InputSchema: map[string]interface{}{...},
}, handler)
```
After:
```go
us.registerSysTool(
"sys___init",
"Initialize the MCPG system...",
map[string]interface{}{...},
handler,
)
```
This establishes a single source of truth for sys tool metadata and
reduces the risk of schema drift between internal storage and SDK
registration.
> [!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-build1598906572/b275/launcher.test
/tmp/go-build1598906572/b275/launcher.test
-test.testlogfile=/tmp/go-build1598906572/b275/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true go fmtsort/sort.go
64/pkg/tool/linu-o --output-dir /tmp/playwright--unsafeptr=false
--allowed-origin-unreachable=false
64/pkg/tool/linu/tmp/go-build1598906572/b064/vet.cfg` (dns block)
> - `invalid-host-that-does-not-exist-12345.com`
> - Triggering command: `/tmp/go-build1598906572/b260/config.test
/tmp/go-build1598906572/b260/config.test
-test.testlogfile=/tmp/go-build1598906572/b260/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true go
base64/base64.go--64 64/pkg/tool/linu-o` (dns block)
> - `nonexistent.local`
> - Triggering command: `/tmp/go-build1598906572/b275/launcher.test
/tmp/go-build1598906572/b275/launcher.test
-test.testlogfile=/tmp/go-build1598906572/b275/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true go fmtsort/sort.go
64/pkg/tool/linu-o --output-dir /tmp/playwright--unsafeptr=false
--allowed-origin-unreachable=false
64/pkg/tool/linu/tmp/go-build1598906572/b064/vet.cfg` (dns block)
> - `slow.example.com`
> - Triggering command: `/tmp/go-build1598906572/b275/launcher.test
/tmp/go-build1598906572/b275/launcher.test
-test.testlogfile=/tmp/go-build1598906572/b275/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true go fmtsort/sort.go
64/pkg/tool/linu-o --output-dir /tmp/playwright--unsafeptr=false
--allowed-origin-unreachable=false
64/pkg/tool/linu/tmp/go-build1598906572/b064/vet.cfg` (dns block)
> - `this-host-does-not-exist-12345.com`
> - Triggering command: `/tmp/go-build1598906572/b284/mcp.test
/tmp/go-build1598906572/b284/mcp.test
-test.testlogfile=/tmp/go-build1598906572/b284/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true go1.25.6 -c=4
-nolocalimports -importcfg /tmp/go-build1598906572/b191/importcfg -pack
/opt/hostedtoolcache/go/1.25.6/x64/src/net/http/internal/httpcommon/httpcommon.go`
(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>[duplicate-code] Duplicate Code Analysis
Report</issue_title>
> <issue_description>*Analysis of commit
be3b856*
>
> ## Summary
>
> This analysis found **1 significant duplication pattern** in the Go
source files. The duplication occurs in `internal/server/unified.go`
where sys tool metadata (Name, Description, InputSchema) is defined
twice per tool — once for internal `ToolInfo` storage and again for the
SDK `sdk.Tool` registration.
>
> ## Detected Patterns
>
> 1. **Sys Tool Struct Data Duplication** - Severity: Medium - See
sub-issue #1348
>
> ## Overall Impact
>
> - **Total Duplicated Lines**: ~22 lines across 2 sys tools
> - **Affected Files**: 1 (`internal/server/unified.go`)
> - **Maintainability Risk**: Medium — schema/description changes to sys
tools require updating two locations, risking drift
> - **Refactoring Priority**: Low-Medium — localized to
`registerSysTools()`, no runtime risk, but creates update burden
>
> ## Next Steps
>
> 1. Review individual pattern sub-issue for detailed analysis
> 2. Implement a `registerSysTool` helper that accepts
name/description/schema once and handles both internal storage and SDK
registration
> 3. Verify no behavioral change after refactoring with existing tests
>
> ## Analysis Metadata
>
> - **Analyzed Files**: 64 Go source files (excluding test files)
> - **Detection Method**: Semantic and structural code analysis
> - **Commit**: be3b856
> - **Analysis Date**: 2026-02-24</issue_description>
>
> ## Comments on the Issue (you are @claude[agent] in this section)
>
> <comments>
> </comments>
>
</details>
<!-- START COPILOT CODING AGENT SUFFIX -->
- Fixes #13471 file changed
Lines changed: 37 additions & 45 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
406 | 406 | | |
407 | 407 | | |
408 | 408 | | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
409 | 432 | | |
410 | 433 | | |
411 | 434 | | |
| |||
464 | 487 | | |
465 | 488 | | |
466 | 489 | | |
467 | | - | |
468 | | - | |
469 | | - | |
470 | | - | |
471 | | - | |
472 | | - | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
473 | 495 | | |
474 | 496 | | |
475 | 497 | | |
| |||
478 | 500 | | |
479 | 501 | | |
480 | 502 | | |
481 | | - | |
482 | | - | |
483 | | - | |
484 | | - | |
485 | | - | |
486 | | - | |
487 | | - | |
488 | | - | |
489 | | - | |
490 | | - | |
491 | | - | |
492 | | - | |
493 | | - | |
494 | | - | |
495 | | - | |
496 | | - | |
497 | | - | |
498 | | - | |
499 | | - | |
| 503 | + | |
| 504 | + | |
500 | 505 | | |
501 | 506 | | |
502 | 507 | | |
| |||
524 | 529 | | |
525 | 530 | | |
526 | 531 | | |
527 | | - | |
528 | | - | |
529 | | - | |
530 | | - | |
531 | | - | |
532 | | - | |
533 | | - | |
534 | | - | |
535 | | - | |
536 | | - | |
537 | | - | |
538 | | - | |
539 | | - | |
540 | | - | |
541 | | - | |
542 | | - | |
543 | | - | |
544 | | - | |
545 | | - | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
546 | 537 | | |
547 | 538 | | |
548 | 539 | | |
549 | | - | |
| 540 | + | |
| 541 | + | |
550 | 542 | | |
551 | 543 | | |
552 | 544 | | |
| |||
0 commit comments