[test] Add tests for mcp.callSDKMethod and related dispatch functions#1889
Merged
[test] Add tests for mcp.callSDKMethod and related dispatch functions#1889
Conversation
Improve test coverage for the Connection type in the mcp package by exercising previously untested code paths: - GetAgentTagsSnapshotFromContext: all branches (nil ctx, missing key, wrong type, nil pointer, valid snapshot, empty snapshot) - callSDKMethod default branch: unsupported method names return a descriptive error without requiring a session - callSDKMethod resource/prompt cases: resources/list, resources/read, prompts/list, prompts/get, tools/list, tools/call all reach their switch cases and return the expected requireSession error when no SDK session is configured (these cases were never reached by prior tests which used the plain JSON-RPC path exclusively) - SendRequestWithServerID stdio path: isHTTP=false exercises the callSDKMethod branch that was previously untouched - SendRequestWithServerID shouldAttachAgentTags=true: plain JSON-RPC and stdio paths with a DIFC sink server ID in context exercise the LogRPCRequestWithAgentSnapshot / LogRPCResponseWithAgentSnapshot logging branches Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds targeted unit tests in internal/mcp to exercise previously untested dispatch/logging branches around MCP SDK method routing and DIFC agent-tag snapshot extraction, improving confidence in the central request path selection logic.
Changes:
- Introduces tests covering
GetAgentTagsSnapshotFromContextbranch behavior. - Adds switch-coverage tests for
Connection.callSDKMethod(supported cases + unsupported/default). - Exercises
SendRequestWithServerIDstdio path and agent-tags logging branches across HTTP plain-JSON and stdio flows.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Comment on lines
+306
to
+307
| w.WriteHeader(http.StatusInternalServerError) | ||
| w.Header().Set("Content-Type", "application/json") |
Comment on lines
+327
to
+333
| // Either a Go error or a JSON-RPC error response is acceptable; either way the | ||
| // shouldAttachAgentTags logging branch was exercised. | ||
| if err != nil { | ||
| assert.Nil(t, resp) | ||
| } else { | ||
| require.NotNil(t, resp) | ||
| } |
| "jsonrpc": "2.0", | ||
| "id": req["id"], | ||
| "result": map[string]interface{}{ | ||
| "protocolVersion": "2024-11-05", |
Comment on lines
+174
to
+177
| require.NoError(t, err) | ||
|
|
||
| var req map[string]interface{} | ||
| require.NoError(t, json.Unmarshal(body, &req)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Test Coverage Improvement:
mcp.callSDKMethodand related dispatch functionsFunction Analyzed
internal/mcpcallSDKMethod(switch dispatch for all MCP SDK methods)GetAgentTagsSnapshotFromContext,SendRequestWithServerID(stdio and agent-tags branches)Why This Function?
callSDKMethodis the central dispatcher for all MCP SDK method calls (tools/list,tools/call,resources/list,resources/read,prompts/list,prompts/get, and thedefaulterror case). Despite being called on every non-plain-JSON request, it was never reached by any prior unit test because all existing tests useNewHTTPConnectionwith custom headers, which selects the plain JSON-RPC transport path and bypassescallSDKMethodentirely.Similarly,
GetAgentTagsSnapshotFromContexthad zero unit-test coverage and theshouldAttachAgentTags = truelogging branches inSendRequestWithServerIDwere never exercised.Tests Added
New file:
internal/mcp/sdk_method_dispatch_test.goGetAgentTagsSnapshotFromContext– all 6 branches: nil context, no key, wrong type, nil pointer value, valid snapshot, empty snapshotcallSDKMethoddefault/unsupported case – 6 distinct method names that hit thedefault:branch and return"unsupported method: (method)"errorscallSDKMethodresource/prompt routing –resources/list,resources/read,prompts/list,prompts/get,tools/list,tools/calleach exercise their switch case and return the expectedrequireSessionerror when no SDK session is availableSendRequestWithServerIDstdio path –isHTTP=falsebranch exercised via direct struct construction, covering both unsupported-method and nil-session error pathsshouldAttachAgentTags = truelogging branches – HTTP plain-JSON success, HTTP plain-JSON error, stdio path, and non-sink-server context all covered usingSetDIFCSinkServerIDs+AgentTagsSnapshotContextKeycontext valueCoverage Before / After
callSDKMethodswitch casesGetAgentTagsSnapshotFromContextbranchesSendRequestWithServerIDstdio pathshouldAttachAgentTags = trueloggingTest Execution
Tests were authored to match existing package conventions:
package mcp(white-box tests accessing unexported methods)t.Runsub-testsrequirefor fatal assertions,assertfor non-fatal onest.Cleanupfor global state resets (SetDIFCSinkServerIDs)newTestConnectionhelper for minimalConnectionstruct constructionnewPlainJSONTestServerhelper for reusable plain JSON-RPC mock serverGenerated by Test Coverage Improver
Next candidates:
initializeHTTPSessionsession-ID-fallback path,cleanupIdleConnectionsalready-closed branchWarning
The following domain was blocked by the firewall during workflow execution:
proxy.golang.orgTo allow these domains, add them to the
network.allowedlist in your workflow frontmatter:See Network Configuration for more information.