Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit27f90bc

Browse files
committed
feat: add list_apps MCP tool
1 parent59c02e7 commit27f90bc

File tree

2 files changed

+192
-0
lines changed

2 files changed

+192
-0
lines changed

‎codersdk/toolsdk/toolsdk.go‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const (
5050
ToolNameWorkspaceEditFile="coder_workspace_edit_file"
5151
ToolNameWorkspaceEditFiles="coder_workspace_edit_files"
5252
ToolNameWorkspacePortForward="coder_workspace_port_forward"
53+
ToolNameWorkspaceListApps="coder_workspace_list_apps"
5354
ToolNameCreateTask="coder_create_task"
5455
ToolNameDeleteTask="coder_delete_task"
5556
ToolNameListTasks="coder_list_tasks"
@@ -227,6 +228,7 @@ var All = []GenericTool{
227228
WorkspaceEditFile.Generic(),
228229
WorkspaceEditFiles.Generic(),
229230
WorkspacePortForward.Generic(),
231+
WorkspaceListApps.Generic(),
230232
CreateTask.Generic(),
231233
DeleteTask.Generic(),
232234
ListTasks.Generic(),
@@ -1756,6 +1758,57 @@ var WorkspacePortForward = Tool[WorkspacePortForwardArgs, WorkspacePortForwardRe
17561758
},
17571759
}
17581760

1761+
typeWorkspaceListAppsArgsstruct {
1762+
Workspacestring`json:"workspace"`
1763+
}
1764+
1765+
typeWorkspaceListAppstruct {
1766+
Namestring`json:"name"`
1767+
URLstring`json:"url"`
1768+
}
1769+
1770+
typeWorkspaceListAppsResponsestruct {
1771+
Apps []WorkspaceListApp`json:"apps"`
1772+
}
1773+
1774+
varWorkspaceListApps=Tool[WorkspaceListAppsArgs,WorkspaceListAppsResponse]{
1775+
Tool: aisdk.Tool{
1776+
Name:ToolNameWorkspaceListApps,
1777+
Description:`List the URLs of Coder apps running in a workspace for a single agent.`,
1778+
Schema: aisdk.Schema{
1779+
Properties:map[string]any{
1780+
"workspace":map[string]any{
1781+
"type":"string",
1782+
"description":workspaceDescription,
1783+
},
1784+
},
1785+
Required: []string{"workspace"},
1786+
},
1787+
},
1788+
UserClientOptional:true,
1789+
Handler:func(ctx context.Context,depsDeps,argsWorkspaceListAppsArgs) (WorkspaceListAppsResponse,error) {
1790+
workspaceName:=NormalizeWorkspaceInput(args.Workspace)
1791+
_,workspaceAgent,err:=findWorkspaceAndAgent(ctx,deps.coderClient,workspaceName)
1792+
iferr!=nil {
1793+
returnWorkspaceListAppsResponse{},xerrors.Errorf("failed to find workspace: %w",err)
1794+
}
1795+
1796+
varresWorkspaceListAppsResponse
1797+
for_,app:=rangeworkspaceAgent.Apps {
1798+
name:=app.DisplayName
1799+
ifname=="" {
1800+
name=app.Slug
1801+
}
1802+
res.Apps=append(res.Apps,WorkspaceListApp{
1803+
Name:name,
1804+
URL:app.URL,
1805+
})
1806+
}
1807+
1808+
returnres,nil
1809+
},
1810+
}
1811+
17591812
typeCreateTaskArgsstruct {
17601813
Promptstring`json:"prompt"`
17611814
TemplateVersionIDstring`json:"template_version_id"`

‎codersdk/toolsdk/toolsdk_test.go‎

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,145 @@ func TestTools(t *testing.T) {
11471147
})
11481148
}
11491149
})
1150+
1151+
t.Run("WorkspaceListApps",func(t*testing.T) {
1152+
t.Parallel()
1153+
1154+
_=dbfake.WorkspaceBuild(t,store, database.WorkspaceTable{
1155+
Name:"list-app-workspace-one-agent",
1156+
OrganizationID:owner.OrganizationID,
1157+
OwnerID:member.ID,
1158+
}).WithAgent(func(agents []*proto.Agent) []*proto.Agent {
1159+
agents[0].Apps= []*proto.App{
1160+
{
1161+
Slug:"zero",
1162+
Url:"http://zero.dev.coder.com",
1163+
},
1164+
}
1165+
returnagents
1166+
}).Do()
1167+
1168+
// nolint:gocritic // This is in a test package and does not end up in the build
1169+
_=dbfake.WorkspaceBuild(t,store, database.WorkspaceTable{
1170+
Name:"list-app-workspace-multi-agent",
1171+
OrganizationID:owner.OrganizationID,
1172+
OwnerID:member.ID,
1173+
}).WithAgent(func(agents []*proto.Agent) []*proto.Agent {
1174+
agents[0].Apps= []*proto.App{
1175+
{
1176+
Slug:"one",
1177+
Url:"http://one.dev.coder.com",
1178+
},
1179+
{
1180+
Slug:"two",
1181+
Url:"http://two.dev.coder.com",
1182+
},
1183+
{
1184+
Slug:"three",
1185+
Url:"http://three.dev.coder.com",
1186+
},
1187+
}
1188+
agents=append(agents,&proto.Agent{
1189+
Id:uuid.NewString(),
1190+
Name:"dev2",
1191+
Auth:&proto.Agent_Token{
1192+
Token:uuid.NewString(),
1193+
},
1194+
Env:map[string]string{},
1195+
Apps: []*proto.App{
1196+
{
1197+
Slug:"four",
1198+
Url:"http://four.dev.coder.com",
1199+
},
1200+
},
1201+
})
1202+
returnagents
1203+
}).Do()
1204+
1205+
tests:= []struct {
1206+
namestring
1207+
args toolsdk.WorkspaceListAppsArgs
1208+
expected []toolsdk.WorkspaceListApp
1209+
errorstring
1210+
}{
1211+
{
1212+
name:"NonExistentWorkspace",
1213+
args: toolsdk.WorkspaceListAppsArgs{
1214+
Workspace:"list-appp-workspace-does-not-exist",
1215+
},
1216+
error:"failed to find workspace",
1217+
},
1218+
{
1219+
name:"OneAgentOneApp",
1220+
args: toolsdk.WorkspaceListAppsArgs{
1221+
Workspace:"list-app-workspace-one-agent",
1222+
},
1223+
expected: []toolsdk.WorkspaceListApp{
1224+
{
1225+
Name:"zero",
1226+
URL:"http://zero.dev.coder.com",
1227+
},
1228+
},
1229+
},
1230+
{
1231+
name:"MultiAgent",
1232+
args: toolsdk.WorkspaceListAppsArgs{
1233+
Workspace:"list-app-workspace-multi-agent",
1234+
},
1235+
error:"multiple agents found, please specify the agent name",
1236+
},
1237+
{
1238+
name:"MultiAgentOneApp",
1239+
args: toolsdk.WorkspaceListAppsArgs{
1240+
Workspace:"list-app-workspace-multi-agent.dev2",
1241+
},
1242+
expected: []toolsdk.WorkspaceListApp{
1243+
{
1244+
Name:"four",
1245+
URL:"http://four.dev.coder.com",
1246+
},
1247+
},
1248+
},
1249+
{
1250+
name:"MultiAgentMultiApp",
1251+
args: toolsdk.WorkspaceListAppsArgs{
1252+
Workspace:"list-app-workspace-multi-agent.dev",
1253+
},
1254+
expected: []toolsdk.WorkspaceListApp{
1255+
{
1256+
Name:"one",
1257+
URL:"http://one.dev.coder.com",
1258+
},
1259+
{
1260+
Name:"three",
1261+
URL:"http://three.dev.coder.com",
1262+
},
1263+
{
1264+
Name:"two",
1265+
URL:"http://two.dev.coder.com",
1266+
},
1267+
},
1268+
},
1269+
}
1270+
1271+
for_,tt:=rangetests {
1272+
t.Run(tt.name,func(t*testing.T) {
1273+
t.Parallel()
1274+
1275+
tb,err:=toolsdk.NewDeps(memberClient)
1276+
require.NoError(t,err)
1277+
1278+
res,err:=testTool(t,toolsdk.WorkspaceListApps,tb,tt.args)
1279+
iftt.error!="" {
1280+
require.Error(t,err)
1281+
require.ErrorContains(t,err,tt.error)
1282+
}else {
1283+
require.NoError(t,err)
1284+
require.Equal(t,tt.expected,res.Apps)
1285+
}
1286+
})
1287+
}
1288+
})
11501289
}
11511290

11521291
// TestedTools keeps track of which tools have been tested.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp