@@ -214,21 +214,9 @@ func handleCoderWorkspaceExec(client *codersdk.Client) mcpserver.ToolHandlerFunc
214
214
215
215
// Attempt to fetch the workspace. We may get a UUID or a name, so try to
216
216
// handle both.
217
- var ws codersdk.Workspace
218
- if wsid ,err := uuid .Parse (wsArg );err == nil {
219
- ws ,err = client .Workspace (ctx ,wsid )
220
- if err != nil {
221
- return nil ,xerrors .Errorf ("failed to fetch workspace: %w" ,err )
222
- }
223
- }else {
224
- owner ,workspaceName ,err := splitNamedWorkspace (wsArg )
225
- if err != nil {
226
- return nil ,xerrors .Errorf ("failed to split workspace name: %w" ,err )
227
- }
228
- ws ,err = client .WorkspaceByOwnerAndName (ctx ,owner ,workspaceName , codersdk.WorkspaceOptions {})
229
- if err != nil {
230
- return nil ,xerrors .Errorf ("failed to fetch workspace: %w" ,err )
231
- }
217
+ ws ,err := getWorkspaceByIDOrOwnerName (ctx ,client ,wsArg )
218
+ if err != nil {
219
+ return nil ,xerrors .Errorf ("failed to fetch workspace: %w" ,err )
232
220
}
233
221
234
222
// Ensure the workspace is started.
@@ -277,19 +265,9 @@ func handleCoderWorkspaceExec(client *codersdk.Client) mcpserver.ToolHandlerFunc
277
265
}
278
266
}
279
267
280
- // COPYPASTA: from cli/root.go
281
- func splitNamedWorkspace (identifier string ) (owner string ,workspaceName string ,err error ) {
282
- parts := strings .Split (identifier ,"/" )
283
-
284
- switch len (parts ) {
285
- case 1 :
286
- owner = codersdk .Me
287
- workspaceName = parts [0 ]
288
- case 2 :
289
- owner = parts [0 ]
290
- workspaceName = parts [1 ]
291
- default :
292
- return "" ,"" ,xerrors .Errorf ("invalid workspace name: %q should be in the format <workspace> or <owner>/<workspace>" ,identifier )
268
+ func getWorkspaceByIDOrOwnerName (ctx context.Context ,client * codersdk.Client ,identifier string ) (codersdk.Workspace ,error ) {
269
+ if wsid ,err := uuid .Parse (identifier );err == nil {
270
+ return client .Workspace (ctx ,wsid )
293
271
}
294
- return owner , workspaceName , nil
272
+ return client . WorkspaceByOwnerAndName ( ctx , codersdk . Me , identifier , codersdk. WorkspaceOptions {})
295
273
}