import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
import { z } from "zod";
import { getTodos, createTodo } from "./things.js";
const server = new Server(
{
name: "things3-mcp",
version: "0.1.0",
},
{
capabilities: {
tools: {},
},
}
);
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: [
{
name: "todo_list",
description: "List todos from a specific Things 3 list",
inputSchema: {
type: "object",
properties: {
listName: {
type: "string",
enum: ["Inbox", "Today", "Anytime", "Upcoming", "Someday", "Logbook", "Trash"],
description: "The name of the list to fetch todos from. Defaults to Today."
}
}
},
},
{
name: "todo_create",
description: "Create a new todo in Things 3",
inputSchema: {
type: "object",
properties: {
title: {
type: "string",
description: "The title of the todo"
},
listName: {
type: "string",
enum: ["Inbox", "Today", "Anytime", "Upcoming", "Someday"],
description: "The list to add the todo to. Defaults to Inbox."
},
notes: {
type: "string",
description: "Notes for the todo"
}
},
required: ["title"]
},
}
],
};
});
server.setRequestHandler(CallToolRequestSchema, async (request) => {
try {
const { name, arguments: args } = request.params;
if (name === "todo_list") {
const { listName } = z.object({
listName: z.string().optional().default("Today"),
}).parse(args);
const todos = await getTodos(listName);
return {
content: [
{
type: "text",
text: JSON.stringify(todos, null, 2),
},
],
};
}
if (name === "todo_create") {
const { title, listName, notes } = z.object({
title: z.string(),
listName: z.string().optional().default("Inbox"),
notes: z.string().optional(),
}).parse(args);
const id = await createTodo(title, listName, notes);
return {
content: [
{
type: "text",
text: `Created todo with ID: ${id}`,
},
],
};
}
throw new Error(`Unknown tool: ${name}`);
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
return {
content: [
{
type: "text",
text: `Error: ${errorMessage}`,
},
],
isError: true,
};
}
});
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("Things 3 MCP Server running on stdio");
}
main().catch((error) => {
console.error("Fatal error in main():", error);
process.exit(1);
});