Skip to content

Commit cc3f2de

Browse files
committed
return timings in coder_workspace_exec
1 parent aa33071 commit cc3f2de

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

mcp/tools/tools_coder.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"io"
99
"strings"
10+
"time"
1011

1112
"github.com/google/uuid"
1213
"github.com/mark3labs/mcp-go/mcp"
@@ -217,6 +218,7 @@ func handleCoderWorkspaceExec(deps ToolDeps) mcpserver.ToolHandlerFunc {
217218
return nil, xerrors.Errorf("no connected agents for workspace %s", ws.ID)
218219
}
219220

221+
startedAt := time.Now()
220222
conn, err := workspacesdk.New(deps.Client).AgentReconnectingPTY(ctx, workspacesdk.WorkspaceAgentReconnectingPTYOpts{
221223
AgentID: agt.ID,
222224
Reconnect: uuid.New(),
@@ -229,6 +231,7 @@ func handleCoderWorkspaceExec(deps ToolDeps) mcpserver.ToolHandlerFunc {
229231
return nil, xerrors.Errorf("failed to open reconnecting PTY: %w", err)
230232
}
231233
defer conn.Close()
234+
connectedAt := time.Now()
232235

233236
var buf bytes.Buffer
234237
if _, err := io.Copy(&buf, conn); err != nil {
@@ -238,10 +241,23 @@ func handleCoderWorkspaceExec(deps ToolDeps) mcpserver.ToolHandlerFunc {
238241
return nil, xerrors.Errorf("failed to read from reconnecting PTY: %w", err)
239242
}
240243
}
244+
completedAt := time.Now()
245+
connectionTime := connectedAt.Sub(startedAt)
246+
executionTime := completedAt.Sub(connectedAt)
247+
248+
resp := map[string]string{
249+
"connection_time": connectionTime.String(),
250+
"execution_time": executionTime.String(),
251+
"output": buf.String(),
252+
}
253+
respJSON, err := json.Marshal(resp)
254+
if err != nil {
255+
return nil, xerrors.Errorf("failed to encode workspace build: %w", err)
256+
}
241257

242258
return &mcp.CallToolResult{
243259
Content: []mcp.Content{
244-
mcp.NewTextContent(strings.TrimSpace(buf.String())),
260+
mcp.NewTextContent(string(respJSON)),
245261
},
246262
}, nil
247263
}

mcp/tools/tools_coder_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,8 @@ func TestCoderTools(t *testing.T) {
190190
_ = pty.ReadLine(ctx) // skip the echo
191191

192192
// Then: the response is the output of the command.
193-
expected := makeJSONRPCTextResponse(t, randString)
194193
actual := pty.ReadLine(ctx)
195-
testutil.RequireJSONEq(t, expected, actual)
194+
require.Contains(t, actual, randString)
196195
})
197196

198197
// NOTE: this test runs after the list_workspaces tool is called.

0 commit comments

Comments
 (0)