Skip to content

Commit d4ded06

Browse files
Copilotlpcox
andauthored
refactor mcp response logging into shared helper
Agent-Logs-Url: https://github.com/github/gh-aw-mcpg/sessions/019072d6-0f6b-49f5-a866-73c69fbf0832 Co-authored-by: lpcox <[email protected]>
1 parent 653d423 commit d4ded06

2 files changed

Lines changed: 45 additions & 8 deletions

File tree

internal/mcp/connection.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,17 @@ func logInboundRPCResponse(serverID string, payload []byte, err error, shouldAtt
444444
}
445445
}
446446

447+
// logInboundRPCResponseFromResult marshals a response payload, logs the inbound response, and
448+
// returns the original result and error unchanged.
449+
func logInboundRPCResponseFromResult(serverID string, result *Response, err error, shouldAttachTags bool, snapshot *AgentTagsSnapshot) (*Response, error) {
450+
var responsePayload []byte
451+
if result != nil {
452+
responsePayload, _ = json.Marshal(result)
453+
}
454+
logInboundRPCResponse(serverID, responsePayload, err, shouldAttachTags, snapshot)
455+
return result, err
456+
}
457+
447458
// SendRequest sends a JSON-RPC request and waits for the response
448459
// The serverID parameter is used for logging to associate the request with a backend server
449460
func (c *Connection) SendRequest(method string, params interface{}) (*Response, error) {
@@ -481,14 +492,7 @@ func (c *Connection) SendRequestWithServerID(ctx context.Context, method string,
481492
result, err = c.callSDKMethod(method, params)
482493
}
483494

484-
// Log the response from backend server
485-
var responsePayload []byte
486-
if result != nil {
487-
responsePayload, _ = json.Marshal(result)
488-
}
489-
logInboundRPCResponse(serverID, responsePayload, err, shouldAttachAgentTags, snapshot)
490-
491-
return result, err
495+
return logInboundRPCResponseFromResult(serverID, result, err, shouldAttachAgentTags, snapshot)
492496
}
493497

494498
// callSDKMethod calls the appropriate SDK method based on the method name
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package mcp
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestLogInboundRPCResponseFromResult_ReturnsResultAndError(t *testing.T) {
11+
assert := assert.New(t)
12+
13+
expectedErr := errors.New("expected error")
14+
expectedResult := &Response{
15+
JSONRPC: "2.0",
16+
ID: 1,
17+
Result: []byte(`{"ok":true}`),
18+
}
19+
20+
result, err := logInboundRPCResponseFromResult("test-server", expectedResult, expectedErr, false, nil)
21+
22+
assert.Same(expectedResult, result)
23+
assert.ErrorIs(err, expectedErr)
24+
}
25+
26+
func TestLogInboundRPCResponseFromResult_AllowsNilResult(t *testing.T) {
27+
assert := assert.New(t)
28+
29+
result, err := logInboundRPCResponseFromResult("test-server", nil, nil, false, nil)
30+
31+
assert.Nil(result)
32+
assert.NoError(err)
33+
}

0 commit comments

Comments
 (0)