-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathtools.go
More file actions
368 lines (331 loc) · 12.5 KB
/
tools.go
File metadata and controls
368 lines (331 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package main
import (
"context"
"fmt"
"github.com/isaacphi/mcp-language-server/internal/tools"
"github.com/mark3labs/mcp-go/mcp"
)
func (s *mcpServer) registerTools() error {
coreLogger.Debug("Registering MCP tools")
applyTextEditTool := mcp.NewTool("edit_file",
mcp.WithDescription("Apply multiple text edits to a file."),
mcp.WithArray("edits",
mcp.Required(),
mcp.Description("List of edits to apply"),
mcp.Items(map[string]any{
"type": "object",
"properties": map[string]any{
"startLine": map[string]any{
"type": "number",
"description": "Start line to replace, inclusive, one-indexed",
},
"endLine": map[string]any{
"type": "number",
"description": "End line to replace, inclusive, one-indexed",
},
"newText": map[string]any{
"type": "string",
"description": "Replacement text. Replace with the new text. Leave blank to remove lines.",
},
},
"required": []string{"startLine", "endLine"},
}),
),
mcp.WithString("filePath",
mcp.Required(),
mcp.Description("Path to the file to edit"),
),
)
s.mcpServer.AddTool(applyTextEditTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
// Extract arguments
filePath, ok := request.Params.Arguments["filePath"].(string)
if !ok {
return mcp.NewToolResultError("filePath must be a string"), nil
}
// Extract edits array
editsArg, ok := request.Params.Arguments["edits"]
if !ok {
return mcp.NewToolResultError("edits is required"), nil
}
// Type assert and convert the edits
editsArray, ok := editsArg.([]any)
if !ok {
return mcp.NewToolResultError("edits must be an array"), nil
}
var edits []tools.TextEdit
for _, editItem := range editsArray {
editMap, ok := editItem.(map[string]any)
if !ok {
return mcp.NewToolResultError("each edit must be an object"), nil
}
startLine, ok := editMap["startLine"].(float64)
if !ok {
return mcp.NewToolResultError("startLine must be a number"), nil
}
endLine, ok := editMap["endLine"].(float64)
if !ok {
return mcp.NewToolResultError("endLine must be a number"), nil
}
newText, _ := editMap["newText"].(string) // newText can be empty
edits = append(edits, tools.TextEdit{
StartLine: int(startLine),
EndLine: int(endLine),
NewText: newText,
})
}
coreLogger.Debug("Executing edit_file for file: %s", filePath)
response, err := tools.ApplyTextEdits(s.ctx, s.lspClient, filePath, edits)
if err != nil {
coreLogger.Error("Failed to apply edits: %v", err)
return mcp.NewToolResultError(fmt.Sprintf("failed to apply edits: %v", err)), nil
}
return mcp.NewToolResultText(response), nil
})
readDefinitionTool := mcp.NewTool("definition",
mcp.WithDescription("Read the source code definition of a symbol (function, type, constant, etc.) from the codebase. Returns the complete implementation code where the symbol is defined."),
mcp.WithString("symbolName",
mcp.Required(),
mcp.Description("The name of the symbol whose definition you want to find (e.g. 'mypackage.MyFunction', 'MyType.MyMethod')"),
),
)
s.mcpServer.AddTool(readDefinitionTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
// Extract arguments
symbolName, ok := request.Params.Arguments["symbolName"].(string)
if !ok {
return mcp.NewToolResultError("symbolName must be a string"), nil
}
coreLogger.Debug("Executing definition for symbol: %s", symbolName)
text, err := tools.ReadDefinition(s.ctx, s.lspClient, symbolName)
if err != nil {
coreLogger.Error("Failed to get definition: %v", err)
return mcp.NewToolResultError(fmt.Sprintf("failed to get definition: %v", err)), nil
}
return mcp.NewToolResultText(text), nil
})
findReferencesTool := mcp.NewTool("references",
mcp.WithDescription("Find all usages and references of a symbol throughout the codebase. Returns a list of all files and locations where the symbol appears."),
mcp.WithString("symbolName",
mcp.Required(),
mcp.Description("The name of the symbol to search for (e.g. 'mypackage.MyFunction', 'MyType')"),
),
)
s.mcpServer.AddTool(findReferencesTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
// Extract arguments
symbolName, ok := request.Params.Arguments["symbolName"].(string)
if !ok {
return mcp.NewToolResultError("symbolName must be a string"), nil
}
coreLogger.Debug("Executing references for symbol: %s", symbolName)
text, err := tools.FindReferences(s.ctx, s.lspClient, symbolName)
if err != nil {
coreLogger.Error("Failed to find references: %v", err)
return mcp.NewToolResultError(fmt.Sprintf("failed to find references: %v", err)), nil
}
return mcp.NewToolResultText(text), nil
})
getDiagnosticsTool := mcp.NewTool("diagnostics",
mcp.WithDescription("Get diagnostic information for a specific file from the language server."),
mcp.WithString("filePath",
mcp.Required(),
mcp.Description("The path to the file to get diagnostics for"),
),
mcp.WithBoolean("contextLines",
mcp.Description("Lines to include around each diagnostic."),
mcp.DefaultBool(false),
),
mcp.WithBoolean("showLineNumbers",
mcp.Description("If true, adds line numbers to the output"),
mcp.DefaultBool(true),
),
)
s.mcpServer.AddTool(getDiagnosticsTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
// Extract arguments
filePath, ok := request.Params.Arguments["filePath"].(string)
if !ok {
return mcp.NewToolResultError("filePath must be a string"), nil
}
contextLines := 5 // default value
if contextLinesArg, ok := request.Params.Arguments["contextLines"].(int); ok {
contextLines = contextLinesArg
}
showLineNumbers := true // default value
if showLineNumbersArg, ok := request.Params.Arguments["showLineNumbers"].(bool); ok {
showLineNumbers = showLineNumbersArg
}
coreLogger.Debug("Executing diagnostics for file: %s", filePath)
text, err := tools.GetDiagnosticsForFile(s.ctx, s.lspClient, filePath, contextLines, showLineNumbers)
if err != nil {
coreLogger.Error("Failed to get diagnostics: %v", err)
return mcp.NewToolResultError(fmt.Sprintf("failed to get diagnostics: %v", err)), nil
}
return mcp.NewToolResultText(text), nil
})
// Uncomment to add codelens tools
//
// getCodeLensTool := mcp.NewTool("get_codelens",
// mcp.WithDescription("Get code lens hints for a given file from the language server."),
// mcp.WithString("filePath",
// mcp.Required(),
// mcp.Description("The path to the file to get code lens information for"),
// ),
// )
//
// s.mcpServer.AddTool(getCodeLensTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
// // Extract arguments
// filePath, ok := request.Params.Arguments["filePath"].(string)
// if !ok {
// return mcp.NewToolResultError("filePath must be a string"), nil
// }
//
// coreLogger.Debug("Executing get_codelens for file: %s", filePath)
// text, err := tools.GetCodeLens(s.ctx, s.lspClient, filePath)
// if err != nil {
// coreLogger.Error("Failed to get code lens: %v", err)
// return mcp.NewToolResultError(fmt.Sprintf("failed to get code lens: %v", err)), nil
// }
// return mcp.NewToolResultText(text), nil
// })
//
// executeCodeLensTool := mcp.NewTool("execute_codelens",
// mcp.WithDescription("Execute a code lens command for a given file and lens index."),
// mcp.WithString("filePath",
// mcp.Required(),
// mcp.Description("The path to the file containing the code lens to execute"),
// ),
// mcp.WithNumber("index",
// mcp.Required(),
// mcp.Description("The index of the code lens to execute (from get_codelens output), 1 indexed"),
// ),
// )
//
// s.mcpServer.AddTool(executeCodeLensTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
// // Extract arguments
// filePath, ok := request.Params.Arguments["filePath"].(string)
// if !ok {
// return mcp.NewToolResultError("filePath must be a string"), nil
// }
//
// // Handle both float64 and int for index due to JSON parsing
// var index int
// switch v := request.Params.Arguments["index"].(type) {
// case float64:
// index = int(v)
// case int:
// index = v
// default:
// return mcp.NewToolResultError("index must be a number"), nil
// }
//
// coreLogger.Debug("Executing execute_codelens for file: %s index: %d", filePath, index)
// text, err := tools.ExecuteCodeLens(s.ctx, s.lspClient, filePath, index)
// if err != nil {
// coreLogger.Error("Failed to execute code lens: %v", err)
// return mcp.NewToolResultError(fmt.Sprintf("failed to execute code lens: %v", err)), nil
// }
// return mcp.NewToolResultText(text), nil
// })
hoverTool := mcp.NewTool("hover",
mcp.WithDescription("Get hover information (type, documentation) for a symbol at the specified position."),
mcp.WithString("filePath",
mcp.Required(),
mcp.Description("The path to the file to get hover information for"),
),
mcp.WithNumber("line",
mcp.Required(),
mcp.Description("The line number where the hover is requested (1-indexed)"),
),
mcp.WithNumber("column",
mcp.Required(),
mcp.Description("The column number where the hover is requested (1-indexed)"),
),
)
s.mcpServer.AddTool(hoverTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
// Extract arguments
filePath, ok := request.Params.Arguments["filePath"].(string)
if !ok {
return mcp.NewToolResultError("filePath must be a string"), nil
}
// Handle both float64 and int for line and column due to JSON parsing
var line, column int
switch v := request.Params.Arguments["line"].(type) {
case float64:
line = int(v)
case int:
line = v
default:
return mcp.NewToolResultError("line must be a number"), nil
}
switch v := request.Params.Arguments["column"].(type) {
case float64:
column = int(v)
case int:
column = v
default:
return mcp.NewToolResultError("column must be a number"), nil
}
coreLogger.Debug("Executing hover for file: %s line: %d column: %d", filePath, line, column)
text, err := tools.GetHoverInfo(s.ctx, s.lspClient, filePath, line, column)
if err != nil {
coreLogger.Error("Failed to get hover information: %v", err)
return mcp.NewToolResultError(fmt.Sprintf("failed to get hover information: %v", err)), nil
}
return mcp.NewToolResultText(text), nil
})
renameSymbolTool := mcp.NewTool("rename_symbol",
mcp.WithDescription("Rename a symbol (variable, function, class, etc.) at the specified position and update all references throughout the codebase."),
mcp.WithString("filePath",
mcp.Required(),
mcp.Description("The path to the file containing the symbol to rename"),
),
mcp.WithNumber("line",
mcp.Required(),
mcp.Description("The line number where the symbol is located (1-indexed)"),
),
mcp.WithNumber("column",
mcp.Required(),
mcp.Description("The column number where the symbol is located (1-indexed)"),
),
mcp.WithString("newName",
mcp.Required(),
mcp.Description("The new name for the symbol"),
),
)
s.mcpServer.AddTool(renameSymbolTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
// Extract arguments
filePath, ok := request.Params.Arguments["filePath"].(string)
if !ok {
return mcp.NewToolResultError("filePath must be a string"), nil
}
newName, ok := request.Params.Arguments["newName"].(string)
if !ok {
return mcp.NewToolResultError("newName must be a string"), nil
}
// Handle both float64 and int for line and column due to JSON parsing
var line, column int
switch v := request.Params.Arguments["line"].(type) {
case float64:
line = int(v)
case int:
line = v
default:
return mcp.NewToolResultError("line must be a number"), nil
}
switch v := request.Params.Arguments["column"].(type) {
case float64:
column = int(v)
case int:
column = v
default:
return mcp.NewToolResultError("column must be a number"), nil
}
coreLogger.Debug("Executing rename_symbol for file: %s line: %d column: %d newName: %s", filePath, line, column, newName)
text, err := tools.RenameSymbol(s.ctx, s.lspClient, filePath, line, column, newName)
if err != nil {
coreLogger.Error("Failed to rename symbol: %v", err)
return mcp.NewToolResultError(fmt.Sprintf("failed to rename symbol: %v", err)), nil
}
return mcp.NewToolResultText(text), nil
})
coreLogger.Info("Successfully registered all MCP tools")
return nil
}