aboutsummaryrefslogtreecommitdiffstats
path: root/apps/mcp
diff options
context:
space:
mode:
authorMohamed Bassem <me@mbassem.com>2025-04-13 13:47:40 +0000
committerMohamed Bassem <me@mbassem.com>2025-04-13 13:47:40 +0000
commit8c6cfc8f5fdab4bbdae41060518c08731720976a (patch)
tree9e0bf2cbd2e61342380f295faaec88e014c33fa9 /apps/mcp
parent46277c74d5df376e4350cb3c8035ec9dda329a7a (diff)
downloadkarakeep-8c6cfc8f5fdab4bbdae41060518c08731720976a.tar.zst
fix(mcp): compact the response of get bookmark lists
Diffstat (limited to 'apps/mcp')
-rw-r--r--apps/mcp/src/bookmarks.ts85
-rw-r--r--apps/mcp/src/lists.ts5
-rw-r--r--apps/mcp/src/shared.ts15
-rw-r--r--apps/mcp/src/tags.ts3
-rw-r--r--apps/mcp/src/utils.ts92
5 files changed, 99 insertions, 101 deletions
diff --git a/apps/mcp/src/bookmarks.ts b/apps/mcp/src/bookmarks.ts
index 04e4480d..ed2da2fd 100644
--- a/apps/mcp/src/bookmarks.ts
+++ b/apps/mcp/src/bookmarks.ts
@@ -1,89 +1,8 @@
import { CallToolResult } from "@modelcontextprotocol/sdk/types";
import { z } from "zod";
-import { KarakeepAPISchemas } from "@karakeep/sdk";
-
-import {
- karakeepClient,
- mcpServer,
- toMcpToolError,
- turndownService,
-} from "./shared";
-
-interface CompactBookmark {
- id: string;
- createdAt: string;
- title: string;
- summary: string;
- note: string;
- content:
- | {
- type: "link";
- url: string;
- description: string;
- author: string;
- publisher: string;
- }
- | {
- type: "text";
- sourceUrl: string;
- }
- | {
- type: "media";
- assetId: string;
- assetType: string;
- sourceUrl: string;
- }
- | {
- type: "unknown";
- };
- tags: string[];
-}
-
-function compactBookmark(
- bookmark: KarakeepAPISchemas["Bookmark"],
-): CompactBookmark {
- let content: CompactBookmark["content"];
- if (bookmark.content.type === "link") {
- content = {
- type: "link",
- url: bookmark.content.url,
- description: bookmark.content.description ?? "",
- author: bookmark.content.author ?? "",
- publisher: bookmark.content.publisher ?? "",
- };
- } else if (bookmark.content.type === "text") {
- content = {
- type: "text",
- sourceUrl: bookmark.content.sourceUrl ?? "",
- };
- } else if (bookmark.content.type === "asset") {
- content = {
- type: "media",
- assetId: bookmark.content.assetId,
- assetType: bookmark.content.assetType,
- sourceUrl: bookmark.content.sourceUrl ?? "",
- };
- } else {
- content = {
- type: "unknown",
- };
- }
-
- return {
- id: bookmark.id,
- createdAt: bookmark.createdAt,
- title: bookmark.title
- ? bookmark.title
- : ((bookmark.content.type === "link"
- ? bookmark.content.title
- : undefined) ?? ""),
- summary: bookmark.summary ?? "",
- note: bookmark.note ?? "",
- content,
- tags: bookmark.tags.map((t) => t.name),
- };
-}
+import { karakeepClient, mcpServer, turndownService } from "./shared";
+import { compactBookmark, toMcpToolError } from "./utils";
// Tools
mcpServer.tool(
diff --git a/apps/mcp/src/lists.ts b/apps/mcp/src/lists.ts
index 6cfe1d13..1e0f3a8a 100644
--- a/apps/mcp/src/lists.ts
+++ b/apps/mcp/src/lists.ts
@@ -1,7 +1,8 @@
import { CallToolResult } from "@modelcontextprotocol/sdk/types";
import { z } from "zod";
-import { karakeepClient, mcpServer, toMcpToolError } from "./shared";
+import { karakeepClient, mcpServer } from "./shared";
+import { compactBookmark, toMcpToolError } from "./utils";
mcpServer.tool(
"get-lists",
@@ -40,7 +41,7 @@ mcpServer.tool(
return {
content: res.data.bookmarks.map((bookmark) => ({
type: "text",
- text: JSON.stringify(bookmark),
+ text: JSON.stringify(compactBookmark(bookmark)),
})),
};
},
diff --git a/apps/mcp/src/shared.ts b/apps/mcp/src/shared.ts
index 2c553d17..a80c3620 100644
--- a/apps/mcp/src/shared.ts
+++ b/apps/mcp/src/shared.ts
@@ -1,5 +1,4 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
-import { CallToolResult } from "@modelcontextprotocol/sdk/types";
import TurndownService from "turndown";
import { createKarakeepClient } from "@karakeep/sdk";
@@ -21,17 +20,3 @@ export const mcpServer = new McpServer({
});
export const turndownService = new TurndownService();
-
-export function toMcpToolError(
- error: { code: string; message: string } | undefined,
-): CallToolResult {
- return {
- isError: true,
- content: [
- {
- type: "text",
- text: error ? JSON.stringify(error) : `Something went wrong`,
- },
- ],
- };
-}
diff --git a/apps/mcp/src/tags.ts b/apps/mcp/src/tags.ts
index a23abea0..da651a40 100644
--- a/apps/mcp/src/tags.ts
+++ b/apps/mcp/src/tags.ts
@@ -1,7 +1,8 @@
import { CallToolResult } from "@modelcontextprotocol/sdk/types";
import { z } from "zod";
-import { karakeepClient, mcpServer, toMcpToolError } from "./shared";
+import { karakeepClient, mcpServer } from "./shared";
+import { toMcpToolError } from "./utils";
mcpServer.tool(
"attach-tag-to-bookmark",
diff --git a/apps/mcp/src/utils.ts b/apps/mcp/src/utils.ts
new file mode 100644
index 00000000..26a86436
--- /dev/null
+++ b/apps/mcp/src/utils.ts
@@ -0,0 +1,92 @@
+import { CallToolResult } from "@modelcontextprotocol/sdk/types";
+
+import { KarakeepAPISchemas } from "@karakeep/sdk";
+
+export function toMcpToolError(
+ error: { code: string; message: string } | undefined,
+): CallToolResult {
+ return {
+ isError: true,
+ content: [
+ {
+ type: "text",
+ text: error ? JSON.stringify(error) : `Something went wrong`,
+ },
+ ],
+ };
+}
+
+interface CompactBookmark {
+ id: string;
+ createdAt: string;
+ title: string;
+ summary: string;
+ note: string;
+ content:
+ | {
+ type: "link";
+ url: string;
+ description: string;
+ author: string;
+ publisher: string;
+ }
+ | {
+ type: "text";
+ sourceUrl: string;
+ }
+ | {
+ type: "media";
+ assetId: string;
+ assetType: string;
+ sourceUrl: string;
+ }
+ | {
+ type: "unknown";
+ };
+ tags: string[];
+}
+
+export function compactBookmark(
+ bookmark: KarakeepAPISchemas["Bookmark"],
+): CompactBookmark {
+ let content: CompactBookmark["content"];
+ if (bookmark.content.type === "link") {
+ content = {
+ type: "link",
+ url: bookmark.content.url,
+ description: bookmark.content.description ?? "",
+ author: bookmark.content.author ?? "",
+ publisher: bookmark.content.publisher ?? "",
+ };
+ } else if (bookmark.content.type === "text") {
+ content = {
+ type: "text",
+ sourceUrl: bookmark.content.sourceUrl ?? "",
+ };
+ } else if (bookmark.content.type === "asset") {
+ content = {
+ type: "media",
+ assetId: bookmark.content.assetId,
+ assetType: bookmark.content.assetType,
+ sourceUrl: bookmark.content.sourceUrl ?? "",
+ };
+ } else {
+ content = {
+ type: "unknown",
+ };
+ }
+
+ return {
+ id: bookmark.id,
+ createdAt: bookmark.createdAt,
+ title: bookmark.title
+ ? bookmark.title
+ : ((bookmark.content.type === "link"
+ ? bookmark.content.title
+ : undefined) ?? ""),
+ summary: bookmark.summary ?? "",
+ note: bookmark.note ?? "",
+ content,
+ tags: bookmark.tags.map((t) => t.name),
+ };
+}