50 lines
1.7 KiB
TypeScript
50 lines
1.7 KiB
TypeScript
|
|
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||
|
|
import { eq } from "../drizzle.js";
|
||
|
|
import { z } from "zod";
|
||
|
|
import { db } from "../db.js";
|
||
|
|
import { objects } from "../schema.js";
|
||
|
|
import { toolCatch, toolErr, toolOk } from "./tool-result.js";
|
||
|
|
|
||
|
|
const updateObjectInputSchema = z.object({
|
||
|
|
id: z.string().uuid(),
|
||
|
|
title: z.string().min(1).max(500).optional(),
|
||
|
|
description: z.string().nullable().optional(),
|
||
|
|
status: z.string().nullable().optional(),
|
||
|
|
icon: z.string().nullable().optional(),
|
||
|
|
coverImage: z.string().nullable().optional(),
|
||
|
|
});
|
||
|
|
|
||
|
|
export function registerUpdateObjectTool(mcp: McpServer): void {
|
||
|
|
mcp.registerTool(
|
||
|
|
"update_object",
|
||
|
|
{
|
||
|
|
description: "Update an existing object (only provided fields are changed).",
|
||
|
|
inputSchema: updateObjectInputSchema,
|
||
|
|
},
|
||
|
|
async (args) => {
|
||
|
|
try {
|
||
|
|
const input = updateObjectInputSchema.parse(args);
|
||
|
|
const [updated] = await db
|
||
|
|
.update(objects)
|
||
|
|
.set({
|
||
|
|
...(input.title !== undefined ? { title: input.title } : {}),
|
||
|
|
...(input.description !== undefined ? { description: input.description } : {}),
|
||
|
|
...(input.status !== undefined ? { status: input.status } : {}),
|
||
|
|
...(input.icon !== undefined ? { icon: input.icon } : {}),
|
||
|
|
...(input.coverImage !== undefined ? { coverImage: input.coverImage } : {}),
|
||
|
|
updatedAt: new Date(),
|
||
|
|
})
|
||
|
|
.where(eq(objects.id, input.id))
|
||
|
|
.returning();
|
||
|
|
|
||
|
|
if (!updated) {
|
||
|
|
return toolErr(`Object not found: ${input.id}`);
|
||
|
|
}
|
||
|
|
return toolOk(updated);
|
||
|
|
} catch (e) {
|
||
|
|
return toolCatch(e);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
);
|
||
|
|
}
|