54 lines
1.6 KiB
TypeScript
54 lines
1.6 KiB
TypeScript
|
|
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||
|
|
import { z } from "zod";
|
||
|
|
import { db } from "../db.js";
|
||
|
|
import { objects } from "../schema.js";
|
||
|
|
import { objectTypes } from "../shared-types.js";
|
||
|
|
import { toolCatch, toolErr, toolOk } from "./tool-result.js";
|
||
|
|
|
||
|
|
const objectTypeSchema = z.enum(objectTypes as unknown as [string, ...string[]]);
|
||
|
|
|
||
|
|
const createObjectInputSchema = z.object({
|
||
|
|
type: objectTypeSchema,
|
||
|
|
title: z.string().min(1).max(500),
|
||
|
|
parentId: z.string().uuid().nullable().optional(),
|
||
|
|
workspaceId: z.string().uuid(),
|
||
|
|
description: z.string().optional(),
|
||
|
|
status: z.string().optional(),
|
||
|
|
icon: z.string().optional(),
|
||
|
|
});
|
||
|
|
|
||
|
|
export function registerCreateObjectTool(mcp: McpServer): void {
|
||
|
|
mcp.registerTool(
|
||
|
|
"create_object",
|
||
|
|
{
|
||
|
|
description:
|
||
|
|
"Create a new object (task, project, document, whiteboard, group, workspace).",
|
||
|
|
inputSchema: createObjectInputSchema,
|
||
|
|
},
|
||
|
|
async (args) => {
|
||
|
|
try {
|
||
|
|
const input = createObjectInputSchema.parse(args);
|
||
|
|
const [created] = await db
|
||
|
|
.insert(objects)
|
||
|
|
.values({
|
||
|
|
type: input.type,
|
||
|
|
title: input.title,
|
||
|
|
parentId: input.parentId ?? null,
|
||
|
|
workspaceId: input.workspaceId,
|
||
|
|
description: input.description,
|
||
|
|
status: input.status,
|
||
|
|
icon: input.icon,
|
||
|
|
})
|
||
|
|
.returning();
|
||
|
|
|
||
|
|
if (!created) {
|
||
|
|
return toolErr("Failed to create object");
|
||
|
|
}
|
||
|
|
return toolOk(created);
|
||
|
|
} catch (e) {
|
||
|
|
return toolCatch(e);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
);
|
||
|
|
}
|