@@ -56,118 +56,130 @@ func NewOpenForUserTool() mcp.Tool {
5656 }
5757 }
5858
59-// MakeOpenForUserHandler creates a handler for Open tool.
60-// programPtr should point to the tea.Program instance (can be set after creation).
61-func MakeOpenForUserHandler(programPtr **tea.Program) mcp.ToolHandler {
62- return func(ctx context.Context, arguments json.RawMessage) (*mcp.ToolResult, error) {
63- var args OpenForUserArgs
64- if err := json.Unmarshal(arguments, &args); err != nil {
65- return &mcp.ToolResult{
66- Content: []mcp.ContentBlock{{Type: "text", Text: fmt.Sprintf("Invalid arguments: %v", err)}},
67- IsError: true,
68- }, nil
69- }
59+// OpenForUserHandler handles Open tool calls and sends messages to the TUI program.
60+type OpenForUserHandler struct {
61+ program *tea.Program
62+}
7063
71- if args.File == "" {
72- return &mcp.ToolResult{
73- Content: []mcp.ContentBlock{{Type: "text", Text: "File path cannot be empty"}},
74- IsError: true,
75- }, nil
76- }
64+// NewOpenForUserHandler creates a handler for Open tool.
65+func NewOpenForUserHandler() *OpenForUserHandler {
66+ return &OpenForUserHandler{}
67+}
7768
78- // Security: Convert to absolute path and validate
79- absPath, err := filepath.Abs(args.File)
80- if err != nil {
81- return &mcp.ToolResult{
82- Content: []mcp.ContentBlock{{Type: "text", Text: fmt.Sprintf("Invalid file path: %v", err)}},
83- IsError: true,
84- }, nil
69+// SetProgram sets the tea.Program instance after it's created.
70+func (h *OpenForUserHandler) SetProgram(program *tea.Program) {
71+ h.program = program
72+}
73+
74+// Handle implements the mcp.ToolHandler interface.
75+func (h *OpenForUserHandler) Handle(ctx context.Context, arguments json.RawMessage) (*mcp.ToolResult, error) {
76+ var args OpenForUserArgs
77+ if err := json.Unmarshal(arguments, &args); err != nil {
78+ return &mcp.ToolResult{
79+ Content: []mcp.ContentBlock{{Type: "text", Text: fmt.Sprintf("Invalid arguments: %v", err)}},
80+ IsError: true,
81+ }, nil
82+ }
83+
84+ if args.File == "" {
85+ return &mcp.ToolResult{
86+ Content: []mcp.ContentBlock{{Type: "text", Text: "File path cannot be empty"}},
87+ IsError: true,
88+ }, nil
89+ }
90+
91+ // Security: Convert to absolute path and validate
92+ absPath, err := filepath.Abs(args.File)
93+ if err != nil {
94+ return &mcp.ToolResult{
95+ Content: []mcp.ContentBlock{{Type: "text", Text: fmt.Sprintf("Invalid file path: %v", err)}},
96+ IsError: true,
97+ }, nil
98+ }
99+
100+ // Get current working directory for validation
101+ workingDir, err := os.Getwd()
102+ if err != nil {
103+ return &mcp.ToolResult{
104+ Content: []mcp.ContentBlock{{Type: "text", Text: fmt.Sprintf("Failed to get working directory: %v", err)}},
105+ IsError: true,
106+ }, nil
107+ }
108+
109+ // Security: Prevent path traversal - only allow files within or below working directory
110+ relPath, err := filepath.Rel(workingDir, absPath)
111+ if err != nil || strings.HasPrefix(relPath, "..") || filepath.IsAbs(relPath) {
112+ return &mcp.ToolResult{
113+ Content: []mcp.ContentBlock{{Type: "text", Text: "Access denied: path outside working directory"}},
114+ IsError: true,
115+ }, nil
116+ }
117+
118+ // Read file content
119+ content, err := os.ReadFile(absPath)
120+ if err != nil {
121+ return &mcp.ToolResult{
122+ Content: []mcp.ContentBlock{{Type: "text", Text: fmt.Sprintf("Failed to read file: %v", err)}},
123+ IsError: true,
124+ }, nil
125+ }
126+
127+ lines := strings.Split(string(content), "\n")
128+
129+ // Extract line range if specified
130+ var selectedContent string
131+ if args.Start > 0 || args.End > 0 {
132+ start := args.Start
133+ end := args.End
134+
135+ // Default start to 1 if not specified
136+ if start <= 0 {
137+ start = 1
85138 }
86139
87- // Get current working directory for validation
88- workingDir, err := os.Getwd()
89- if err != nil {
140+ // Validate start is in range
141+ if start < 1 || start > len(lines) {
90142 return &mcp.ToolResult{
91- Content: []mcp.ContentBlock{{Type: "text", Text: fmt.Sprintf("Failed to get working directory: %v", err)}},
143+ Content: []mcp.ContentBlock{{Type: "text", Text: fmt.Sprintf("Start line %d out of range (file has %d lines)", start, len(lines))}},
92144 IsError: true,
93145 }, nil
94146 }
95147
96- // Security: Prevent path traversal - only allow files within or below working directory
97- relPath, err := filepath.Rel(workingDir, absPath)
98- if err != nil || strings.HasPrefix(relPath, "..") || filepath.IsAbs(relPath) {
99- return &mcp.ToolResult{
100- Content: []mcp.ContentBlock{{Type: "text", Text: "Access denied: path outside working directory"}},
101- IsError: true,
102- }, nil
148+ // Default end to end of file if not specified or out of range
149+ if end <= 0 || end > len(lines) {
150+ end = len(lines)
103151 }
104152
105- // Read file content
106- content, err := os.ReadFile(absPath)
107- if err != nil {
153+ // Validate range
154+ if start > end {
108155 return &mcp.ToolResult{
109- Content: []mcp.ContentBlock{{Type: "text", Text: fmt.Sprintf("Failed to read file: %v", err)}},
156+ Content: []mcp.ContentBlock{{Type: "text", Text: fmt.Sprintf("Invalid range: start (%d) > end (%d)", start, end)}},
110157 IsError: true,
111158 }, nil
112159 }
113160
114- lines := strings.Split(string(content), "\n")
115-
116- // Extract line range if specified
117- var selectedContent string
118- if args.Start > 0 || args.End > 0 {
119- start := args.Start
120- end := args.End
121-
122- // Default start to 1 if not specified
123- if start <= 0 {
124- start = 1
125- }
126-
127- // Validate start is in range
128- if start < 1 || start > len(lines) {
129- return &mcp.ToolResult{
130- Content: []mcp.ContentBlock{{Type: "text", Text: fmt.Sprintf("Start line %d out of range (file has %d lines)", start, len(lines))}},
131- IsError: true,
132- }, nil
133- }
134-
135- // Default end to end of file if not specified or out of range
136- if end <= 0 || end > len(lines) {
137- end = len(lines)
138- }
139-
140- // Validate range
141- if start > end {
142- return &mcp.ToolResult{
143- Content: []mcp.ContentBlock{{Type: "text", Text: fmt.Sprintf("Invalid range: start (%d) > end (%d)", start, end)}},
144- IsError: true,
145- }, nil
146- }
147-
148- // Extract range (convert to 0-indexed)
149- selectedContent = strings.Join(lines[start-1:end], "\n")
150- } else {
151- selectedContent = string(content)
152- }
153-
154- // Detect language from file extension
155- language := detectLanguage(args.File)
161+ // Extract range (convert to 0-indexed)
162+ selectedContent = strings.Join(lines[start-1:end], "\n")
163+ } else {
164+ selectedContent = string(content)
165+ }
156166
157- // Send message to TUI to update editor
158- if programPtr != nil && *programPtr != nil {
159- (*programPtr).Send(OpenForUserMsg{
160- Content: selectedContent,
161- Language: language,
162- FilePath: args.File,
163- })
164- }
167+ // Detect language from file extension
168+ language := detectLanguage(args.File)
165169
166- return &mcp.ToolResult{
167- Content: []mcp.ContentBlock{{Type: "text", Text: fmt.Sprintf("Opened %s in editor", args.File)}},
168- IsError: false,
169- }, nil
170+ // Send message to TUI to update editor
171+ if h.program != nil {
172+ h.program.Send(OpenForUserMsg{
173+ Content: selectedContent,
174+ Language: language,
175+ FilePath: args.File,
176+ })
170177 }
178+
179+ return &mcp.ToolResult{
180+ Content: []mcp.ContentBlock{{Type: "text", Text: fmt.Sprintf("Opened %s in editor", args.File)}},
181+ IsError: false,
182+ }, nil
171183 }
172184
173185 // detectLanguage returns the Chroma language identifier based on file extension.