Skip to content

[test-improver] Improve tests for internal/server (registerToolsFromBackendContext) - #11107

Merged
lpcox merged 2 commits into
mainfrom
test-improver/register-tools-from-backend-bc5336238e9a3cc5
Aug 13, 2026
Merged

[test-improver] Improve tests for internal/server (registerToolsFromBackendContext)#11107
lpcox merged 2 commits into
mainfrom
test-improver/register-tools-from-backend-bc5336238e9a3cc5

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

File analyzed

internal/server/register_tools_from_backend_test.go

Why this file

  • Package internal/server is large and central; this test file already used testify (require/assert) consistently, but the tool-registration handler closure created inside registerToolsFromBackendContext (in internal/server/tool_registry.go) was never invoked directly by any test — it was only reachable indirectly through full MCP transport/integration tests, leaving its argument-parsing, session-auto-init, and error-handling branches uncovered.
  • Not modified in the recent 3 commits touching this package; no open [test-improver] PR existed.

Improvements made

Added TestRegisterToolsFromBackend_HandlerInvocation, which registers a real tool from a mock HTTP backend and then calls the returned tool.Handler closure directly with three sub-cases (t.Run):

  1. Valid arguments — exercises argument parsing, session auto-initialization (requireSession), and a full round-trip call to the backend via callBackendTool, asserting a non-nil result/data.
  2. Malformed JSON arguments — exercises the mcp.ParseToolArguments error path, verifying the handler returns an error CallToolResult without ever reaching the backend.
  3. Nil Params — exercises the "no arguments" default path in ParseToolArguments.

Existing tests and assertions were left untouched (all still use manual if err != nil checks only within httptest HTTP handler funcs, which run on separate goroutines where require/assert calls are unsafe — this pattern is preserved and called out with a clarifying comment on the new test's handler).

Coverage before/after (internal/server package)

  • registerToolsFromBackendContext: 72.5% → 91.3%
  • Package total: 93.7% → 94.5%

Test output

go test -run TestRegisterToolsFromBackend -v ./internal/server/   → all pass (14 tests, including 3 new subtests)
go test -count=3 ./internal/server/                                → ok (19.3s, no flakes)
go vet ./internal/server/                                          → clean
gofmt -l internal/server/register_tools_from_backend_test.go       → no output (already formatted)

Generated by Test Improver · auto · 176.9 AIC · ⊞ 8.1K ·

Add TestRegisterToolsFromBackend_HandlerInvocation which invokes the
registered tool.Handler closure directly to exercise argument parsing,
session auto-initialization, and both the success and malformed-JSON
error paths of the closure created in registerToolsFromBackendContext.
Also covers the nil Params case (ParseToolArguments defaults to empty
arguments).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox
lpcox marked this pull request as ready for review August 13, 2026 14:50
Copilot AI balanced review requested due to automatic review settings August 13, 2026 14:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds direct test coverage for registered backend tool handlers.

Changes:

  • Tests valid, malformed, and nil tool arguments.
  • Exercises backend invocation and session initialization paths.
Show a summary per file
File Description
internal/server/register_tools_from_backend_test.go Adds handler invocation tests using an HTTP mock backend.

Review details

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Suppressed comments (1)

internal/server/register_tools_from_backend_test.go:931

  • This does not verify the advertised nil-params default: the backend returns the same constant response regardless of the request, so the test also passes if nil params are translated incorrectly. Record the tools/call payload and assert that its arguments value is the expected empty object.
		result, data, err := tool.Handler(context.Background(), req, nil)
		require.NoError(err)
		assert.NotNil(result)
		assert.NotNil(data)
  • Files reviewed: 1/1 changed files
  • Comments generated: 3
  • Review effort level: Balanced

Comment on lines +805 to +806
require := require.New(t)
assert := assert.New(t)
Comment on lines +921 to +923
result, _, err := tool.Handler(context.Background(), req, nil)
require.Error(err, "malformed JSON arguments should produce an error")
assert.NotNil(result, "an error CallToolResult should still be returned")
Comment on lines +908 to +911
result, data, err := tool.Handler(context.Background(), req, nil)
require.NoError(err)
assert.NotNil(result)
assert.NotNil(data)
@github-actions

Copy link
Copy Markdown
Contributor Author

🔒 mcpg Read-Only Stress — gVisor

Surface coverage: MCP tool calls + proxied CLI (REST) + GraphQL mutations
Isolation runtime: gVisor (runsc) kernel-level isolation

Part Surface Op Result Expected Status
A MCP reads (list_issues/PRs/commits, get_file) data returned ALLOWED
B MCP writes (reaction/star/issue/comment/branch/file/PR) Error [-32602]: unknown tool BLOCKED
C CLI reads (github MCP bridge list_issues) data returned ALLOWED
D CLI REST writes (reaction/star/issue/comment/file) no GH_TOKEN — auth refused BLOCKED
E CLI GraphQL mutations (addReaction/addStar/createIssue) no GH_TOKEN — auth refused BLOCKED

Overall: PASS

References: §31653312791

🔒 mcpg read-only stress (gVisor runtime) by Read-Only Stress: gVisor runtime

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@lpcox

lpcox commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator

@copilot address review feedback

Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>

Copilot AI commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

@copilot address review feedback

Addressed in commit d39c9ee. I updated the handler-invocation test to use subtest-local asserters, verify malformed JSON returns IsError and does not call backend tools/call, assert forwarded backend arguments for valid/nil params, and verify session auto-init behavior with explicit session IDs.

Copilot AI requested a review from lpcox August 13, 2026 15:52
@github-actions

Copy link
Copy Markdown
Contributor Author

🔒 mcpg Read-Only Stress — default AWF

Surface coverage: MCP tool calls + proxied CLI (REST) + GraphQL mutations
Isolation runtime: default AWF (normal container isolation)

Part Surface Op Result Expected Status
A MCP reads (list_issues/PRs/commits, get_file_contents) data returned ALLOWED
B MCP writes (reaction/star/issue/comment/branch/file/PR) unknown tool [-32602] BLOCKED
C CLI reads (list_issues, get_file_contents) data returned ALLOWED
D CLI REST writes (reaction/star/issue/comment/file) exit 4 — no GH_TOKEN BLOCKED
E CLI GraphQL mutations (addReaction/addStar/createIssue) exit 4 — no GH_TOKEN BLOCKED

Overall: PASS

Part B refusals are gateway-enforced (Error [-32602]: unknown tool) — write tools are absent from the gateway's exposed tool set. Parts D/E blocked by unauthenticated gh CLI (no GH_TOKEN in this sandbox).

References: §31717560202

🔒 mcpg read-only stress (default AWF runtime) by Read-Only Stress: default runtime

@github-actions

Copy link
Copy Markdown
Contributor Author

🔒 mcpg Read-Only Stress — docker-sbx

Surface coverage: MCP tool calls + proxied CLI (REST) + GraphQL mutations
Isolation runtime: docker-sbx (KVM-isolated microVM)

Part Surface Op Result Expected Status
A MCP reads (issues/PRs/file/commits) data returned ALLOWED
B MCP writes (reaction/star/issue/comment/branch/file/PR) Error [-32602]: unknown tool (gateway refuses) BLOCKED
C CLI reads (issues/file via github CLI) data returned ALLOWED
D CLI REST writes (reaction/star/issue/comment/file) 401 Bad credentials BLOCKED
E CLI GraphQL mutations (addReaction/addStar/createIssue) 401 Bad credentials BLOCKED

Overall: PASS

Run: §31717560169

🔒 mcpg read-only stress (docker-sbx runtime) by Read-Only Stress: docker-sbx runtime

@lpcox
lpcox merged commit 252f081 into main Aug 13, 2026
36 of 37 checks passed
@lpcox
lpcox deleted the test-improver/register-tools-from-backend-bc5336238e9a3cc5 branch August 13, 2026 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants