Skip to content

Commit f6cfda9

Browse files
committed
Just return the tool and handler directly instead of assigning to variables first.
This stops copilot complaining in review that the variables are unused.
1 parent 2e5f092 commit f6cfda9

File tree

1 file changed

+115
-124
lines changed

1 file changed

+115
-124
lines changed

pkg/github/dynamic_tools.go

Lines changed: 115 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -21,149 +21,140 @@ func ToolsetEnum(toolsetGroup *toolsets.ToolsetGroup) []any {
2121
}
2222

2323
func EnableToolset(s *mcp.Server, toolsetGroup *toolsets.ToolsetGroup, t translations.TranslationHelperFunc) (mcp.Tool, mcp.ToolHandlerFor[map[string]any, any]) {
24-
tool := mcp.Tool{
25-
Name: "enable_toolset",
26-
Description: t("TOOL_ENABLE_TOOLSET_DESCRIPTION", "Enable one of the sets of tools the GitHub MCP server provides, use get_toolset_tools and list_available_toolsets first to see what this will enable"),
27-
Annotations: &mcp.ToolAnnotations{
28-
Title: t("TOOL_ENABLE_TOOLSET_USER_TITLE", "Enable a toolset"),
29-
// Not modifying GitHub data so no need to show a warning
30-
ReadOnlyHint: true,
31-
},
32-
InputSchema: &jsonschema.Schema{
33-
Type: "object",
34-
Properties: map[string]*jsonschema.Schema{
35-
"toolset": {
36-
Type: "string",
37-
Description: "The name of the toolset to enable",
38-
Enum: ToolsetEnum(toolsetGroup),
24+
return mcp.Tool{
25+
Name: "enable_toolset",
26+
Description: t("TOOL_ENABLE_TOOLSET_DESCRIPTION", "Enable one of the sets of tools the GitHub MCP server provides, use get_toolset_tools and list_available_toolsets first to see what this will enable"),
27+
Annotations: &mcp.ToolAnnotations{
28+
Title: t("TOOL_ENABLE_TOOLSET_USER_TITLE", "Enable a toolset"),
29+
// Not modifying GitHub data so no need to show a warning
30+
ReadOnlyHint: true,
31+
},
32+
InputSchema: &jsonschema.Schema{
33+
Type: "object",
34+
Properties: map[string]*jsonschema.Schema{
35+
"toolset": {
36+
Type: "string",
37+
Description: "The name of the toolset to enable",
38+
Enum: ToolsetEnum(toolsetGroup),
39+
},
3940
},
41+
Required: []string{"toolset"},
4042
},
41-
Required: []string{"toolset"},
4243
},
43-
}
44+
mcp.ToolHandlerFor[map[string]any, any](func(_ context.Context, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
45+
// We need to convert the toolsets back to a map for JSON serialization
46+
toolsetName, err := RequiredParam[string](args, "toolset")
47+
if err != nil {
48+
return utils.NewToolResultError(err.Error()), nil, nil
49+
}
50+
toolset := toolsetGroup.Toolsets[toolsetName]
51+
if toolset == nil {
52+
return utils.NewToolResultError(fmt.Sprintf("Toolset %s not found", toolsetName)), nil, nil
53+
}
54+
if toolset.Enabled {
55+
return utils.NewToolResultText(fmt.Sprintf("Toolset %s is already enabled", toolsetName)), nil, nil
56+
}
57+
58+
toolset.Enabled = true
4459

45-
handler := mcp.ToolHandlerFor[map[string]any, any](func(_ context.Context, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
46-
// We need to convert the toolsets back to a map for JSON serialization
47-
toolsetName, err := RequiredParam[string](args, "toolset")
48-
if err != nil {
49-
return utils.NewToolResultError(err.Error()), nil, nil
50-
}
51-
toolset := toolsetGroup.Toolsets[toolsetName]
52-
if toolset == nil {
53-
return utils.NewToolResultError(fmt.Sprintf("Toolset %s not found", toolsetName)), nil, nil
54-
}
55-
if toolset.Enabled {
56-
return utils.NewToolResultText(fmt.Sprintf("Toolset %s is already enabled", toolsetName)), nil, nil
57-
}
58-
59-
toolset.Enabled = true
60-
61-
// caution: this currently affects the global tools and notifies all clients:
62-
//
63-
// Send notification to all initialized sessions
64-
// s.sendNotificationToAllClients("notifications/tools/list_changed", nil)
65-
for _, serverTool := range toolset.GetActiveTools() {
66-
serverTool.RegisterFunc(s)
67-
}
68-
69-
return utils.NewToolResultText(fmt.Sprintf("Toolset %s enabled", toolsetName)), nil, nil
70-
})
71-
72-
return tool, handler
60+
// caution: this currently affects the global tools and notifies all clients:
61+
//
62+
// Send notification to all initialized sessions
63+
// s.sendNotificationToAllClients("notifications/tools/list_changed", nil)
64+
for _, serverTool := range toolset.GetActiveTools() {
65+
serverTool.RegisterFunc(s)
66+
}
67+
68+
return utils.NewToolResultText(fmt.Sprintf("Toolset %s enabled", toolsetName)), nil, nil
69+
})
7370
}
7471

7572
func ListAvailableToolsets(toolsetGroup *toolsets.ToolsetGroup, t translations.TranslationHelperFunc) (mcp.Tool, mcp.ToolHandlerFor[map[string]any, any]) {
76-
tool := mcp.Tool{
77-
Name: "list_available_toolsets",
78-
Description: t("TOOL_LIST_AVAILABLE_TOOLSETS_DESCRIPTION", "List all available toolsets this GitHub MCP server can offer, providing the enabled status of each. Use this when a task could be achieved with a GitHub tool and the currently available tools aren't enough. Call get_toolset_tools with these toolset names to discover specific tools you can call"),
79-
Annotations: &mcp.ToolAnnotations{
80-
Title: t("TOOL_LIST_AVAILABLE_TOOLSETS_USER_TITLE", "List available toolsets"),
81-
ReadOnlyHint: true,
82-
},
83-
InputSchema: &jsonschema.Schema{
84-
Type: "object",
85-
Properties: map[string]*jsonschema.Schema{},
73+
return mcp.Tool{
74+
Name: "list_available_toolsets",
75+
Description: t("TOOL_LIST_AVAILABLE_TOOLSETS_DESCRIPTION", "List all available toolsets this GitHub MCP server can offer, providing the enabled status of each. Use this when a task could be achieved with a GitHub tool and the currently available tools aren't enough. Call get_toolset_tools with these toolset names to discover specific tools you can call"),
76+
Annotations: &mcp.ToolAnnotations{
77+
Title: t("TOOL_LIST_AVAILABLE_TOOLSETS_USER_TITLE", "List available toolsets"),
78+
ReadOnlyHint: true,
79+
},
80+
InputSchema: &jsonschema.Schema{
81+
Type: "object",
82+
Properties: map[string]*jsonschema.Schema{},
83+
},
8684
},
87-
}
88-
89-
handler := mcp.ToolHandlerFor[map[string]any, any](func(_ context.Context, _ *mcp.CallToolRequest, _ map[string]any) (*mcp.CallToolResult, any, error) {
90-
// We need to convert the toolsetGroup back to a map for JSON serialization
91-
92-
payload := []map[string]string{}
93-
94-
for name, ts := range toolsetGroup.Toolsets {
95-
{
96-
t := map[string]string{
97-
"name": name,
98-
"description": ts.Description,
99-
"can_enable": "true",
100-
"currently_enabled": fmt.Sprintf("%t", ts.Enabled),
85+
mcp.ToolHandlerFor[map[string]any, any](func(_ context.Context, _ *mcp.CallToolRequest, _ map[string]any) (*mcp.CallToolResult, any, error) {
86+
// We need to convert the toolsetGroup back to a map for JSON serialization
87+
88+
payload := []map[string]string{}
89+
90+
for name, ts := range toolsetGroup.Toolsets {
91+
{
92+
t := map[string]string{
93+
"name": name,
94+
"description": ts.Description,
95+
"can_enable": "true",
96+
"currently_enabled": fmt.Sprintf("%t", ts.Enabled),
97+
}
98+
payload = append(payload, t)
10199
}
102-
payload = append(payload, t)
103100
}
104-
}
105101

106-
r, err := json.Marshal(payload)
107-
if err != nil {
108-
return nil, nil, fmt.Errorf("failed to marshal features: %w", err)
109-
}
110-
111-
return utils.NewToolResultText(string(r)), nil, nil
112-
})
102+
r, err := json.Marshal(payload)
103+
if err != nil {
104+
return nil, nil, fmt.Errorf("failed to marshal features: %w", err)
105+
}
113106

114-
return tool, handler
107+
return utils.NewToolResultText(string(r)), nil, nil
108+
})
115109
}
116110

117111
func GetToolsetsTools(toolsetGroup *toolsets.ToolsetGroup, t translations.TranslationHelperFunc) (mcp.Tool, mcp.ToolHandlerFor[map[string]any, any]) {
118-
tool := mcp.Tool{
119-
Name: "get_toolset_tools",
120-
Description: t("TOOL_GET_TOOLSET_TOOLS_DESCRIPTION", "Lists all the capabilities that are enabled with the specified toolset, use this to get clarity on whether enabling a toolset would help you to complete a task"),
121-
Annotations: &mcp.ToolAnnotations{
122-
Title: t("TOOL_GET_TOOLSET_TOOLS_USER_TITLE", "List all tools in a toolset"),
123-
ReadOnlyHint: true,
124-
},
125-
InputSchema: &jsonschema.Schema{
126-
Type: "object",
127-
Properties: map[string]*jsonschema.Schema{
128-
"toolset": {
129-
Type: "string",
130-
Description: "The name of the toolset you want to get the tools for",
131-
Enum: ToolsetEnum(toolsetGroup),
112+
return mcp.Tool{
113+
Name: "get_toolset_tools",
114+
Description: t("TOOL_GET_TOOLSET_TOOLS_DESCRIPTION", "Lists all the capabilities that are enabled with the specified toolset, use this to get clarity on whether enabling a toolset would help you to complete a task"),
115+
Annotations: &mcp.ToolAnnotations{
116+
Title: t("TOOL_GET_TOOLSET_TOOLS_USER_TITLE", "List all tools in a toolset"),
117+
ReadOnlyHint: true,
118+
},
119+
InputSchema: &jsonschema.Schema{
120+
Type: "object",
121+
Properties: map[string]*jsonschema.Schema{
122+
"toolset": {
123+
Type: "string",
124+
Description: "The name of the toolset you want to get the tools for",
125+
Enum: ToolsetEnum(toolsetGroup),
126+
},
132127
},
128+
Required: []string{"toolset"},
133129
},
134-
Required: []string{"toolset"},
135130
},
136-
}
137-
138-
handler := mcp.ToolHandlerFor[map[string]any, any](func(_ context.Context, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
139-
// We need to convert the toolsetGroup back to a map for JSON serialization
140-
toolsetName, err := RequiredParam[string](args, "toolset")
141-
if err != nil {
142-
return utils.NewToolResultError(err.Error()), nil, nil
143-
}
144-
toolset := toolsetGroup.Toolsets[toolsetName]
145-
if toolset == nil {
146-
return utils.NewToolResultError(fmt.Sprintf("Toolset %s not found", toolsetName)), nil, nil
147-
}
148-
payload := []map[string]string{}
149-
150-
for _, st := range toolset.GetAvailableTools() {
151-
tool := map[string]string{
152-
"name": st.Tool.Name,
153-
"description": st.Tool.Description,
154-
"can_enable": "true",
155-
"toolset": toolsetName,
131+
mcp.ToolHandlerFor[map[string]any, any](func(_ context.Context, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
132+
// We need to convert the toolsetGroup back to a map for JSON serialization
133+
toolsetName, err := RequiredParam[string](args, "toolset")
134+
if err != nil {
135+
return utils.NewToolResultError(err.Error()), nil, nil
136+
}
137+
toolset := toolsetGroup.Toolsets[toolsetName]
138+
if toolset == nil {
139+
return utils.NewToolResultError(fmt.Sprintf("Toolset %s not found", toolsetName)), nil, nil
140+
}
141+
payload := []map[string]string{}
142+
143+
for _, st := range toolset.GetAvailableTools() {
144+
tool := map[string]string{
145+
"name": st.Tool.Name,
146+
"description": st.Tool.Description,
147+
"can_enable": "true",
148+
"toolset": toolsetName,
149+
}
150+
payload = append(payload, tool)
156151
}
157-
payload = append(payload, tool)
158-
}
159-
160-
r, err := json.Marshal(payload)
161-
if err != nil {
162-
return nil, nil, fmt.Errorf("failed to marshal features: %w", err)
163-
}
164152

165-
return utils.NewToolResultText(string(r)), nil, nil
166-
})
153+
r, err := json.Marshal(payload)
154+
if err != nil {
155+
return nil, nil, fmt.Errorf("failed to marshal features: %w", err)
156+
}
167157

168-
return tool, handler
158+
return utils.NewToolResultText(string(r)), nil, nil
159+
})
169160
}

0 commit comments

Comments
 (0)