all repos — mcp-todotxt @ 685c6e4b50ab4587940871ca263637628310e21d

MCP server to manage todo.txt file

tools.go (view raw)

 1package main
 2
 3import (
 4	"context"
 5	"log"
 6
 7	"github.com/modelcontextprotocol/go-sdk/mcp"
 8
 9	todo "github.com/1set/todotxt"
10)
11
12// var todoFilePath string = "./todo.txt"
13
14func getTasksList(path string) todo.TaskList {
15	if tasklist, err := todo.LoadFromPath(path); err != nil {
16		log.Fatal(err)
17		return todo.NewTaskList()
18	} else {
19		return tasklist
20	}
21}
22
23type GetTasksListPrams struct {
24}
25
26func getTasks(ctx context.Context, req *mcp.CallToolRequest, params *GetTasksListPrams) (*mcp.CallToolResult, any, error) {
27	tasksList := getTasksList(*todoFilePath)
28
29	return &mcp.CallToolResult{
30		Content: []mcp.Content{
31			&mcp.TextContent{Text: tasksList.String()},
32		},
33	}, nil, nil
34}
35
36type AddTaskParams struct {
37	TaskContent string `json:"taskContent"`
38}
39
40func addTask(ctx context.Context, req *mcp.CallToolRequest, params *AddTaskParams) (*mcp.CallToolResult, any, error) {
41	tasksList := getTasksList(*todoFilePath)
42	newTask, err := todo.ParseTask(params.TaskContent)
43
44	if err != nil {
45		log.Printf("Error on parsing task %s", params.TaskContent)
46
47		return &mcp.CallToolResult{
48			IsError: true,
49		}, nil, nil
50	} else {
51		tasksList.AddTask(newTask)
52		tasksList.WriteToPath(*todoFilePath)
53		log.Printf("New task added: %s", newTask.String())
54
55		return &mcp.CallToolResult{
56			Content: []mcp.Content{
57				&mcp.TextContent{Text: "New tasks added"},
58			},
59		}, nil, nil
60	}
61}
62
63type CheckTaskParams struct {
64	TaskIndex int `json:"taskIndex"`
65}
66
67func checkTask(ctx context.Context, req *mcp.CallToolRequest, params *CheckTaskParams) (*mcp.CallToolResult, any, error) {
68	tasksList := getTasksList(*todoFilePath)
69	tasksList[params.TaskIndex].Complete()
70	tasksList.WriteToPath(*todoFilePath)
71
72	return &mcp.CallToolResult{
73		Content: []mcp.Content{
74			&mcp.TextContent{Text: "Task checked"},
75		},
76	}, nil, nil
77}