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

Commite440e69

Browse files
committed
simplify toolbox impl, rename to deps
1 parentcaeecf8 commite440e69

File tree

3 files changed

+80
-113
lines changed

3 files changed

+80
-113
lines changed

‎cli/exp_mcp.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,21 +400,23 @@ func mcpServerHandler(inv *serpent.Invocation, client *codersdk.Client, instruct
400400
)
401401

402402
// Create a new context for the tools with all relevant information.
403-
tb:=toolsdk.NewToolbox(client)
403+
tb:= toolsdk.Deps{
404+
CoderClient:client,
405+
}
404406
// Get the workspace agent token from the environment.
405407
varhasAgentClientbool
406408
ifagentToken,err:=getAgentToken(fs);err==nil&&agentToken!="" {
407409
hasAgentClient=true
408410
agentClient:=agentsdk.New(client.URL)
409411
agentClient.SetSessionToken(agentToken)
410-
tb=tb.WithAgentClient(agentClient)
412+
tb.AgentClient=agentClient
411413
}else {
412414
cliui.Warnf(inv.Stderr,"CODER_AGENT_TOKEN is not set, task reporting will not be available")
413415
}
414416
ifappStatusSlug=="" {
415417
cliui.Warnf(inv.Stderr,"CODER_MCP_APP_STATUS_SLUG is not set, task reporting will not be available.")
416418
}else {
417-
tb=tb.WithAppStatusSlug(appStatusSlug)
419+
tb.AppStatusSlug=appStatusSlug
418420
}
419421

420422
// Register tools based on the allowlist (if specified)
@@ -695,7 +697,7 @@ func getAgentToken(fs afero.Fs) (string, error) {
695697

696698
// mcpFromSDK adapts a toolsdk.Tool to go-mcp's server.ServerTool.
697699
// It assumes that the tool responds with a valid JSON object.
698-
funcmcpFromSDK(sdkTool toolsdk.Tool[any,any],tb toolsdk.Toolbox) server.ServerTool {
700+
funcmcpFromSDK(sdkTool toolsdk.Tool[any,any],tb toolsdk.Deps) server.ServerTool {
699701
// NOTE: some clients will silently refuse to use tools if there is an issue
700702
// with the tool's schema or configuration.
701703
ifsdkTool.Schema.Properties==nil {

‎codersdk/toolsdk/toolsdk.go

Lines changed: 45 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,15 @@ import (
1313
"github.com/coder/coder/v2/codersdk/agentsdk"
1414
)
1515

16-
// Toolbox provides access to tool dependencies.
17-
typeToolboxinterface {
18-
CoderClient()*codersdk.Client
19-
AgentClient() (*agentsdk.Client,bool)
20-
AppStatusSlug() (string,bool)
21-
22-
WithAgentClient(*agentsdk.Client)Toolbox
23-
WithAppStatusSlug(string)Toolbox
24-
}
25-
26-
// toolbox is the concrete implementation of Toolbox.
27-
typetoolboxstruct {
28-
coderClient*codersdk.Client
29-
agentClient*agentsdk.Client
30-
appStatusSlugstring
31-
}
32-
33-
// NewToolbox constructs a Toolbox with a required CoderClient.
34-
funcNewToolbox(coder*codersdk.Client)Toolbox {
35-
return&toolbox{coderClient:coder}
36-
}
37-
38-
func (tb*toolbox)CoderClient()*codersdk.Client {
39-
returntb.coderClient
40-
}
41-
42-
func (tb*toolbox)AgentClient() (*agentsdk.Client,bool) {
43-
returntb.agentClient,tb.agentClient!=nil
44-
}
45-
46-
func (tb*toolbox)AppStatusSlug() (string,bool) {
47-
returntb.appStatusSlug,tb.appStatusSlug!=""
48-
}
49-
50-
func (tb*toolbox)WithAgentClient(agent*agentsdk.Client)Toolbox {
51-
tb.agentClient=agent
52-
returntb
53-
}
54-
55-
func (tb*toolbox)WithAppStatusSlug(slugstring)Toolbox {
56-
tb.appStatusSlug=slug
57-
returntb
16+
// Deps provides access to tool dependencies.
17+
typeDepsstruct {
18+
CoderClient*codersdk.Client
19+
AgentClient*agentsdk.Client
20+
AppStatusSlugstring
5821
}
5922

6023
// HandlerFunc is a function that handles a tool call.
61-
typeHandlerFunc[Arg,Retany]func(tbToolbox,argsArg) (Ret,error)
24+
typeHandlerFunc[Arg,Retany]func(tbDeps,argsArg) (Ret,error)
6225

6326
typeTool[Arg,Retany]struct {
6427
aisdk.Tool
@@ -69,7 +32,7 @@ type Tool[Arg, Ret any] struct {
6932
func (tTool[Arg,Ret])Generic()Tool[any,any] {
7033
returnTool[any,any]{
7134
Tool:t.Tool,
72-
Handler:func(tbToolbox,argsany) (any,error) {
35+
Handler:func(tbDeps,argsany) (any,error) {
7336
typedArg,ok:=args.(Arg)
7437
if!ok {
7538
returnnil,xerrors.Errorf("developer error: invalid argument type for tool %s",t.Tool.Name)
@@ -152,7 +115,7 @@ type UploadTarFileArgs struct {
152115

153116
// WithRecover wraps a HandlerFunc to recover from panics and return an error.
154117
funcWithRecover[Arg,Retany](hHandlerFunc[Arg,Ret])HandlerFunc[Arg,Ret] {
155-
returnfunc(tbToolbox,argsArg) (retRet,errerror) {
118+
returnfunc(tbDeps,argsArg) (retRet,errerror) {
156119
deferfunc() {
157120
ifr:=recover();r!=nil {
158121
err=xerrors.Errorf("tool handler panic: %v",r)
@@ -220,17 +183,15 @@ var (
220183
Required: []string{"summary","link","state"},
221184
},
222185
},
223-
Handler:func(tbToolbox,argsReportTaskArgs) (string,error) {
224-
agentClient,ok:=tb.AgentClient()
225-
if!ok {
186+
Handler:func(tbDeps,argsReportTaskArgs) (string,error) {
187+
iftb.AgentClient==nil {
226188
return"",xerrors.New("tool unavailable as CODER_AGENT_TOKEN or CODER_AGENT_TOKEN_FILE not set")
227189
}
228-
appStatusSlug,ok:=tb.AppStatusSlug()
229-
if!ok {
190+
iftb.AppStatusSlug=="" {
230191
return"",xerrors.New("workspace app status slug not found in toolbox")
231192
}
232-
iferr:=agentClient.PatchAppStatus(context.TODO(), agentsdk.PatchAppStatus{
233-
AppSlug:appStatusSlug,
193+
iferr:=tb.AgentClient.PatchAppStatus(context.TODO(), agentsdk.PatchAppStatus{
194+
AppSlug:tb.AppStatusSlug,
234195
Message:args.Summary,
235196
URI:args.Link,
236197
State:codersdk.WorkspaceAppStatusState(args.State),
@@ -256,12 +217,12 @@ This returns more data than list_workspaces to reduce token usage.`,
256217
Required: []string{"workspace_id"},
257218
},
258219
},
259-
Handler:func(tbToolbox,argsGetWorkspaceArgs) (codersdk.Workspace,error) {
220+
Handler:func(tbDeps,argsGetWorkspaceArgs) (codersdk.Workspace,error) {
260221
wsID,err:=uuid.Parse(args.WorkspaceID)
261222
iferr!=nil {
262223
return codersdk.Workspace{},xerrors.New("workspace_id must be a valid UUID")
263224
}
264-
returntb.CoderClient().Workspace(context.TODO(),wsID)
225+
returntb.CoderClient.Workspace(context.TODO(),wsID)
265226
},
266227
}
267228

@@ -296,7 +257,7 @@ is provisioned correctly and the agent can connect to the control plane.
296257
Required: []string{"user","template_version_id","name","rich_parameters"},
297258
},
298259
},
299-
Handler:func(tbToolbox,argsCreateWorkspaceArgs) (codersdk.Workspace,error) {
260+
Handler:func(tbDeps,argsCreateWorkspaceArgs) (codersdk.Workspace,error) {
300261
tvID,err:=uuid.Parse(args.TemplateVersionID)
301262
iferr!=nil {
302263
return codersdk.Workspace{},xerrors.New("template_version_id must be a valid UUID")
@@ -311,7 +272,7 @@ is provisioned correctly and the agent can connect to the control plane.
311272
Value:v,
312273
})
313274
}
314-
workspace,err:=tb.CoderClient().CreateUserWorkspace(context.TODO(),args.User, codersdk.CreateWorkspaceRequest{
275+
workspace,err:=tb.CoderClient.CreateUserWorkspace(context.TODO(),args.User, codersdk.CreateWorkspaceRequest{
315276
TemplateVersionID:tvID,
316277
Name:args.Name,
317278
RichParameterValues:buildParams,
@@ -336,12 +297,12 @@ is provisioned correctly and the agent can connect to the control plane.
336297
},
337298
},
338299
},
339-
Handler:func(tbToolbox,argsListWorkspacesArgs) ([]MinimalWorkspace,error) {
300+
Handler:func(tbDeps,argsListWorkspacesArgs) ([]MinimalWorkspace,error) {
340301
owner:=args.Owner
341302
ifowner=="" {
342303
owner=codersdk.Me
343304
}
344-
workspaces,err:=tb.CoderClient().Workspaces(context.TODO(), codersdk.WorkspaceFilter{
305+
workspaces,err:=tb.CoderClient.Workspaces(context.TODO(), codersdk.WorkspaceFilter{
345306
Owner:owner,
346307
})
347308
iferr!=nil {
@@ -373,8 +334,8 @@ is provisioned correctly and the agent can connect to the control plane.
373334
Required: []string{},
374335
},
375336
},
376-
Handler:func(tbToolbox,_NoArgs) ([]MinimalTemplate,error) {
377-
templates,err:=tb.CoderClient().Templates(context.TODO(), codersdk.TemplateFilter{})
337+
Handler:func(tbDeps,_NoArgs) ([]MinimalTemplate,error) {
338+
templates,err:=tb.CoderClient.Templates(context.TODO(), codersdk.TemplateFilter{})
378339
iferr!=nil {
379340
returnnil,err
380341
}
@@ -406,12 +367,12 @@ is provisioned correctly and the agent can connect to the control plane.
406367
Required: []string{"template_version_id"},
407368
},
408369
},
409-
Handler:func(tbToolbox,argsListTemplateVersionParametersArgs) ([]codersdk.TemplateVersionParameter,error) {
370+
Handler:func(tbDeps,argsListTemplateVersionParametersArgs) ([]codersdk.TemplateVersionParameter,error) {
410371
templateVersionID,err:=uuid.Parse(args.TemplateVersionID)
411372
iferr!=nil {
412373
returnnil,xerrors.Errorf("template_version_id must be a valid UUID: %w",err)
413374
}
414-
parameters,err:=tb.CoderClient().TemplateVersionRichParameters(context.TODO(),templateVersionID)
375+
parameters,err:=tb.CoderClient.TemplateVersionRichParameters(context.TODO(),templateVersionID)
415376
iferr!=nil {
416377
returnnil,err
417378
}
@@ -428,8 +389,8 @@ is provisioned correctly and the agent can connect to the control plane.
428389
Required: []string{},
429390
},
430391
},
431-
Handler:func(tbToolbox,_NoArgs) (codersdk.User,error) {
432-
returntb.CoderClient().User(context.TODO(),"me")
392+
Handler:func(tbDeps,_NoArgs) (codersdk.User,error) {
393+
returntb.CoderClient.User(context.TODO(),"me")
433394
},
434395
}
435396

@@ -455,7 +416,7 @@ is provisioned correctly and the agent can connect to the control plane.
455416
Required: []string{"workspace_id","transition"},
456417
},
457418
},
458-
Handler:func(tbToolbox,argsCreateWorkspaceBuildArgs) (codersdk.WorkspaceBuild,error) {
419+
Handler:func(tbDeps,argsCreateWorkspaceBuildArgs) (codersdk.WorkspaceBuild,error) {
459420
workspaceID,err:=uuid.Parse(args.WorkspaceID)
460421
iferr!=nil {
461422
return codersdk.WorkspaceBuild{},xerrors.Errorf("workspace_id must be a valid UUID: %w",err)
@@ -474,7 +435,7 @@ is provisioned correctly and the agent can connect to the control plane.
474435
iftemplateVersionID!=uuid.Nil {
475436
cbr.TemplateVersionID=templateVersionID
476437
}
477-
returntb.CoderClient().CreateWorkspaceBuild(context.TODO(),workspaceID,cbr)
438+
returntb.CoderClient.CreateWorkspaceBuild(context.TODO(),workspaceID,cbr)
478439
},
479440
}
480441

@@ -936,8 +897,8 @@ The file_id provided is a reference to a tar file you have uploaded containing t
936897
Required: []string{"file_id"},
937898
},
938899
},
939-
Handler:func(tbToolbox,argsCreateTemplateVersionArgs) (codersdk.TemplateVersion,error) {
940-
me,err:=tb.CoderClient().User(context.TODO(),"me")
900+
Handler:func(tbDeps,argsCreateTemplateVersionArgs) (codersdk.TemplateVersion,error) {
901+
me,err:=tb.CoderClient.User(context.TODO(),"me")
941902
iferr!=nil {
942903
return codersdk.TemplateVersion{},err
943904
}
@@ -949,7 +910,7 @@ The file_id provided is a reference to a tar file you have uploaded containing t
949910
iferr!=nil {
950911
return codersdk.TemplateVersion{},xerrors.Errorf("template_id must be a valid UUID: %w",err)
951912
}
952-
templateVersion,err:=tb.CoderClient().CreateTemplateVersion(context.TODO(),me.OrganizationIDs[0], codersdk.CreateTemplateVersionRequest{
913+
templateVersion,err:=tb.CoderClient.CreateTemplateVersion(context.TODO(),me.OrganizationIDs[0], codersdk.CreateTemplateVersionRequest{
953914
Message:"Created by AI",
954915
StorageMethod:codersdk.ProvisionerStorageMethodFile,
955916
FileID:fileID,
@@ -978,12 +939,12 @@ The file_id provided is a reference to a tar file you have uploaded containing t
978939
Required: []string{"workspace_agent_id"},
979940
},
980941
},
981-
Handler:func(tbToolbox,argsGetWorkspaceAgentLogsArgs) ([]string,error) {
942+
Handler:func(tbDeps,argsGetWorkspaceAgentLogsArgs) ([]string,error) {
982943
workspaceAgentID,err:=uuid.Parse(args.WorkspaceAgentID)
983944
iferr!=nil {
984945
returnnil,xerrors.Errorf("workspace_agent_id must be a valid UUID: %w",err)
985946
}
986-
logs,closer,err:=tb.CoderClient().WorkspaceAgentLogsAfter(context.TODO(),workspaceAgentID,0,false)
947+
logs,closer,err:=tb.CoderClient.WorkspaceAgentLogsAfter(context.TODO(),workspaceAgentID,0,false)
987948
iferr!=nil {
988949
returnnil,err
989950
}
@@ -1013,12 +974,12 @@ The file_id provided is a reference to a tar file you have uploaded containing t
1013974
Required: []string{"workspace_build_id"},
1014975
},
1015976
},
1016-
Handler:func(tbToolbox,argsGetWorkspaceBuildLogsArgs) ([]string,error) {
977+
Handler:func(tbDeps,argsGetWorkspaceBuildLogsArgs) ([]string,error) {
1017978
workspaceBuildID,err:=uuid.Parse(args.WorkspaceBuildID)
1018979
iferr!=nil {
1019980
returnnil,xerrors.Errorf("workspace_build_id must be a valid UUID: %w",err)
1020981
}
1021-
logs,closer,err:=tb.CoderClient().WorkspaceBuildLogsAfter(context.TODO(),workspaceBuildID,0)
982+
logs,closer,err:=tb.CoderClient.WorkspaceBuildLogsAfter(context.TODO(),workspaceBuildID,0)
1022983
iferr!=nil {
1023984
returnnil,err
1024985
}
@@ -1044,13 +1005,13 @@ The file_id provided is a reference to a tar file you have uploaded containing t
10441005
Required: []string{"template_version_id"},
10451006
},
10461007
},
1047-
Handler:func(tbToolbox,argsGetTemplateVersionLogsArgs) ([]string,error) {
1008+
Handler:func(tbDeps,argsGetTemplateVersionLogsArgs) ([]string,error) {
10481009
templateVersionID,err:=uuid.Parse(args.TemplateVersionID)
10491010
iferr!=nil {
10501011
returnnil,xerrors.Errorf("template_version_id must be a valid UUID: %w",err)
10511012
}
10521013

1053-
logs,closer,err:=tb.CoderClient().TemplateVersionLogsAfter(context.TODO(),templateVersionID,0)
1014+
logs,closer,err:=tb.CoderClient.TemplateVersionLogsAfter(context.TODO(),templateVersionID,0)
10541015
iferr!=nil {
10551016
returnnil,err
10561017
}
@@ -1079,7 +1040,7 @@ The file_id provided is a reference to a tar file you have uploaded containing t
10791040
Required: []string{"template_id","template_version_id"},
10801041
},
10811042
},
1082-
Handler:func(tbToolbox,argsUpdateTemplateActiveVersionArgs) (string,error) {
1043+
Handler:func(tbDeps,argsUpdateTemplateActiveVersionArgs) (string,error) {
10831044
templateID,err:=uuid.Parse(args.TemplateID)
10841045
iferr!=nil {
10851046
return"",xerrors.Errorf("template_id must be a valid UUID: %w",err)
@@ -1088,7 +1049,7 @@ The file_id provided is a reference to a tar file you have uploaded containing t
10881049
iferr!=nil {
10891050
return"",xerrors.Errorf("template_version_id must be a valid UUID: %w",err)
10901051
}
1091-
err=tb.CoderClient().UpdateActiveTemplateVersion(context.TODO(),templateID, codersdk.UpdateActiveTemplateVersion{
1052+
err=tb.CoderClient.UpdateActiveTemplateVersion(context.TODO(),templateID, codersdk.UpdateActiveTemplateVersion{
10921053
ID:templateVersionID,
10931054
})
10941055
iferr!=nil {
@@ -1112,7 +1073,7 @@ The file_id provided is a reference to a tar file you have uploaded containing t
11121073
Required: []string{"mime_type","files"},
11131074
},
11141075
},
1115-
Handler:func(tbToolbox,argsUploadTarFileArgs) (codersdk.UploadResponse,error) {
1076+
Handler:func(tbDeps,argsUploadTarFileArgs) (codersdk.UploadResponse,error) {
11161077
pipeReader,pipeWriter:=io.Pipe()
11171078
gofunc() {
11181079
deferpipeWriter.Close()
@@ -1137,7 +1098,7 @@ The file_id provided is a reference to a tar file you have uploaded containing t
11371098
}
11381099
}()
11391100

1140-
resp,err:=tb.CoderClient().Upload(context.TODO(),codersdk.ContentTypeTar,pipeReader)
1101+
resp,err:=tb.CoderClient.Upload(context.TODO(),codersdk.ContentTypeTar,pipeReader)
11411102
iferr!=nil {
11421103
return codersdk.UploadResponse{},err
11431104
}
@@ -1172,16 +1133,16 @@ The file_id provided is a reference to a tar file you have uploaded containing t
11721133
Required: []string{"name","display_name","description","version_id"},
11731134
},
11741135
},
1175-
Handler:func(tbToolbox,argsCreateTemplateArgs) (codersdk.Template,error) {
1176-
me,err:=tb.CoderClient().User(context.TODO(),"me")
1136+
Handler:func(tbDeps,argsCreateTemplateArgs) (codersdk.Template,error) {
1137+
me,err:=tb.CoderClient.User(context.TODO(),"me")
11771138
iferr!=nil {
11781139
return codersdk.Template{},err
11791140
}
11801141
versionID,err:=uuid.Parse(args.VersionID)
11811142
iferr!=nil {
11821143
return codersdk.Template{},xerrors.Errorf("version_id must be a valid UUID: %w",err)
11831144
}
1184-
template,err:=tb.CoderClient().CreateTemplate(context.TODO(),me.OrganizationIDs[0], codersdk.CreateTemplateRequest{
1145+
template,err:=tb.CoderClient.CreateTemplate(context.TODO(),me.OrganizationIDs[0], codersdk.CreateTemplateRequest{
11851146
Name:args.Name,
11861147
DisplayName:args.DisplayName,
11871148
Description:args.Description,
@@ -1206,12 +1167,12 @@ The file_id provided is a reference to a tar file you have uploaded containing t
12061167
},
12071168
},
12081169
},
1209-
Handler:func(tbToolbox,argsDeleteTemplateArgs) (string,error) {
1170+
Handler:func(tbDeps,argsDeleteTemplateArgs) (string,error) {
12101171
templateID,err:=uuid.Parse(args.TemplateID)
12111172
iferr!=nil {
12121173
return"",xerrors.Errorf("template_id must be a valid UUID: %w",err)
12131174
}
1214-
err=tb.CoderClient().DeleteTemplate(context.TODO(),templateID)
1175+
err=tb.CoderClient.DeleteTemplate(context.TODO(),templateID)
12151176
iferr!=nil {
12161177
return"",err
12171178
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp