Fix: list_traces Tool QueryType Marshaling Issue #117
Closed
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.
Problem
The
list_tracestool was failing with multiple related issues:Issue 1: Invalid QueryType Value (400 Bad Request)
Issue 2: Nil Pointer Dereference Panic
Root Cause
go-swagger generates broken code when using
allOfwith enum types in OpenAPI specs. The generatedDatav1ListTracesRequeststruct has:This embedded anonymous struct doesn't marshal correctly to JSON. Instead of:
{"query_type": "SERVICE_OPERATION"}It produces:
{"query_type": {"ListTracesRequestQueryType": "SERVICE_OPERATION"}}The API expects the plain string enum value, not a nested object.
Additionally, attempts to use
unsafe.Pointerto cast between types caused memory corruption because the struct layouts were incompatible, leading to nil pointer dereferences when marshaling time fields.Solution
Implemented a complete workaround that bypasses the broken generated client code:
listTracesRequestBodythat embeds the generated model by VALUE (not pointer) and storesqueryTypeseparatelyMarshalBinary()that produces correctly formatted JSON with query_type as a plain stringCode Changes
mcp-server/pkg/tools/traces/traces.go:
httpClientandbaseURLfields toToolsstructNewTools()to extract HTTP client and base URL using reflectionlistTracesRequestBodywrapper type (embedding by value, not pointer)MarshalBinary()andValidate()methodsmcp-server/pkg/tools/traces/traces_test.go:
SERVICE_OPERATIONandTRACE_IDSquery typesTesting
go test ./mcp-server/pkg/tools/traces/... -v go build ./mcp-server/...All tests pass, confirming:
Technical Details
Why Direct HTTP Calls?
Multiple approaches were attempted:
Struct Layout
The key insight was embedding by VALUE:
This allows us to:
MarshalBinary()that's actually calledReflection Usage
Since
httptransport.Runtimehas unexported fields, we use reflection to extract:*http.Clientfor making requestsHostfield for constructing the base URLThis is safe because we have fallback defaults if reflection fails.
Notes
allOf+ enum types