Skip to content

chore(codersdk/toolsdk): improve static analyzability of toolsdk.Tools #17562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 29, 2025
Prev Previous commit
Next Next commit
chore(codersdk/toolsdk): add tool deps in toolbox struct instead of c…
…ontext
  • Loading branch information
johnstcn committed Apr 29, 2025
commit a7784ea2340183a52db02632bdf34b9f106a57bc
16 changes: 8 additions & 8 deletions cli/exp_mcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,21 +400,21 @@ func mcpServerHandler(inv *serpent.Invocation, client *codersdk.Client, instruct
)

// Create a new context for the tools with all relevant information.
clientCtx := toolsdk.WithClient(ctx, client)
tb := toolsdk.NewToolbox(client)
// Get the workspace agent token from the environment.
var hasAgentClient bool
if agentToken, err := getAgentToken(fs); err == nil && agentToken != "" {
hasAgentClient = true
agentClient := agentsdk.New(client.URL)
agentClient.SetSessionToken(agentToken)
clientCtx = toolsdk.WithAgentClient(clientCtx, agentClient)
tb = tb.WithAgentClient(agentClient)
} else {
cliui.Warnf(inv.Stderr, "CODER_AGENT_TOKEN is not set, task reporting will not be available")
}
if appStatusSlug == "" {
cliui.Warnf(inv.Stderr, "CODER_MCP_APP_STATUS_SLUG is not set, task reporting will not be available.")
} else {
clientCtx = toolsdk.WithWorkspaceAppStatusSlug(clientCtx, appStatusSlug)
tb = tb.WithAppStatusSlug(appStatusSlug)
}

// Register tools based on the allowlist (if specified)
Expand All @@ -427,15 +427,15 @@ func mcpServerHandler(inv *serpent.Invocation, client *codersdk.Client, instruct
if len(allowedTools) == 0 || slices.ContainsFunc(allowedTools, func(t string) bool {
return t == tool.Tool.Name
}) {
mcpSrv.AddTools(mcpFromSDK(tool))
mcpSrv.AddTools(mcpFromSDK(tool, tb))
}
}

srv := server.NewStdioServer(mcpSrv)
done := make(chan error)
go func() {
defer close(done)
srvErr := srv.Listen(clientCtx, invStdin, invStdout)
srvErr := srv.Listen(ctx, invStdin, invStdout)
done <- srvErr
}()

Expand Down Expand Up @@ -695,7 +695,7 @@ func getAgentToken(fs afero.Fs) (string, error) {

// mcpFromSDK adapts a toolsdk.Tool to go-mcp's server.ServerTool.
// It assumes that the tool responds with a valid JSON object.
func mcpFromSDK(sdkTool toolsdk.Tool[any, any]) server.ServerTool {
func mcpFromSDK(sdkTool toolsdk.Tool[any, any], tb toolsdk.Toolbox) server.ServerTool {
// NOTE: some clients will silently refuse to use tools if there is an issue
// with the tool's schema or configuration.
if sdkTool.Schema.Properties == nil {
Expand All @@ -711,8 +711,8 @@ func mcpFromSDK(sdkTool toolsdk.Tool[any, any]) server.ServerTool {
Required: sdkTool.Schema.Required,
},
},
Handler: func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
result, err := sdkTool.Handler(ctx, request.Params.Arguments)
Handler: func(_ context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
result, err := sdkTool.Handler(tb, request.Params.Arguments)
if err != nil {
return nil, err
}
Expand Down
Loading