ubiquitous-invention/apps/mcp-server/src/resources/view-resource.ts

83 lines
2 KiB
TypeScript
Raw Normal View History

import { ResourceTemplate, type McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { eq } from "../drizzle.js";
import { db } from "../db.js";
import { views } from "../schema.js";
export function registerViewResource(mcp: McpServer): void {
mcp.registerResource(
"view",
new ResourceTemplate("view://{id}", { list: undefined }),
{
description: "View configuration, metadata, and parent object summary.",
mimeType: "application/json",
},
async (uri, variables) => {
const id = variables.id;
if (!id) {
return {
contents: [
{
uri: uri.toString(),
mimeType: "application/json",
text: JSON.stringify({ error: "Missing view id" }),
},
],
};
}
const view = await db.query.views.findFirst({
where: eq(views.id, id),
with: {
object: true,
},
});
if (!view) {
return {
contents: [
{
uri: uri.toString(),
mimeType: "application/json",
text: JSON.stringify({ error: "View not found", id }),
},
],
};
}
const parent = view.object;
const objectSummary =
parent && !Array.isArray(parent)
? {
id: parent.id,
title: parent.title,
type: parent.type,
}
: null;
const payload = {
view: {
id: view.id,
objectId: view.objectId,
viewType: view.viewType,
name: view.name,
config: view.config,
sortOrder: view.sortOrder,
createdAt: view.createdAt,
updatedAt: view.updatedAt,
},
object: objectSummary,
};
return {
contents: [
{
uri: uri.toString(),
mimeType: "application/json",
text: JSON.stringify(payload, null, 2),
},
],
};
},
);
}