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 {
217218return nil ,xerrors .Errorf ("no connected agents for workspace %s" ,ws .ID )
218219}
219220
221+ startedAt := time .Now ()
220222conn ,err := workspacesdk .New (deps .Client ).AgentReconnectingPTY (ctx , workspacesdk.WorkspaceAgentReconnectingPTYOpts {
221223AgentID :agt .ID ,
222224Reconnect :uuid .New (),
@@ -229,6 +231,7 @@ func handleCoderWorkspaceExec(deps ToolDeps) mcpserver.ToolHandlerFunc {
229231return nil ,xerrors .Errorf ("failed to open reconnecting PTY: %w" ,err )
230232}
231233defer conn .Close ()
234+ connectedAt := time .Now ()
232235
233236var buf bytes.Buffer
234237if _ ,err := io .Copy (& buf ,conn );err != nil {
@@ -238,10 +241,23 @@ func handleCoderWorkspaceExec(deps ToolDeps) mcpserver.ToolHandlerFunc {
238241return 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
242258return & mcp.CallToolResult {
243259Content : []mcp.Content {
244- mcp .NewTextContent (strings . TrimSpace ( buf . String () )),
260+ mcp .NewTextContent (string ( respJSON )),
245261},
246262},nil
247263}