7
7
"errors"
8
8
"io"
9
9
"strings"
10
+ "time"
10
11
11
12
"github.com/google/uuid"
12
13
"github.com/mark3labs/mcp-go/mcp"
@@ -217,6 +218,7 @@ func handleCoderWorkspaceExec(deps ToolDeps) mcpserver.ToolHandlerFunc {
217
218
return nil ,xerrors .Errorf ("no connected agents for workspace %s" ,ws .ID )
218
219
}
219
220
221
+ startedAt := time .Now ()
220
222
conn ,err := workspacesdk .New (deps .Client ).AgentReconnectingPTY (ctx , workspacesdk.WorkspaceAgentReconnectingPTYOpts {
221
223
AgentID :agt .ID ,
222
224
Reconnect :uuid .New (),
@@ -229,6 +231,7 @@ func handleCoderWorkspaceExec(deps ToolDeps) mcpserver.ToolHandlerFunc {
229
231
return nil ,xerrors .Errorf ("failed to open reconnecting PTY: %w" ,err )
230
232
}
231
233
defer conn .Close ()
234
+ connectedAt := time .Now ()
232
235
233
236
var buf bytes.Buffer
234
237
if _ ,err := io .Copy (& buf ,conn );err != nil {
@@ -238,10 +241,23 @@ func handleCoderWorkspaceExec(deps ToolDeps) mcpserver.ToolHandlerFunc {
238
241
return nil ,xerrors .Errorf ("failed to read from reconnecting PTY: %w" ,err )
239
242
}
240
243
}
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
+ }
241
257
242
258
return & mcp.CallToolResult {
243
259
Content : []mcp.Content {
244
- mcp .NewTextContent (strings . TrimSpace ( buf . String () )),
260
+ mcp .NewTextContent (string ( respJSON )),
245
261
},
246
262
},nil
247
263
}