Skip to content

Commit 127cc83

Browse files
committed
feat: enhance MCP server and finalize ecosystem integrations
1 parent d73c496 commit 127cc83

1 file changed

Lines changed: 118 additions & 5 deletions

File tree

src/mcp-server.ts

Lines changed: 118 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ const TOOLS = [
5252
},
5353
{
5454
name: "retrieve_memory",
55-
description: "Recall public data from decentralized storage using a CID.",
55+
description: "Recall public data from decentralized storage using a CID. Optionally provide a UCAN token if access is delegated.",
5656
inputSchema: {
5757
type: "object",
5858
properties: {
59-
cid: { type: "string", description: "The CID of the memory to labels." },
59+
cid: { type: "string", description: "The CID of the memory to fetch." },
60+
ucan: { type: "string", description: "Optional: Base64 encoded UCAN delegation token." },
6061
},
6162
required: ["cid"],
6263
},
@@ -96,6 +97,61 @@ const TOOLS = [
9697
required: ["target_did"],
9798
},
9899
},
100+
{
101+
name: "read_semantic_memory",
102+
description: "Read the agent's core semantic memory (like memory.md) which contains stable facts and preferences.",
103+
inputSchema: { type: "object", properties: {}, required: [] },
104+
},
105+
{
106+
name: "update_semantic_memory",
107+
description: "Update the agent's core semantic memory with new facts or refined preferences.",
108+
inputSchema: {
109+
type: "object",
110+
properties: {
111+
markdown: { type: "string", description: "The complete markdown content for semantic memory." },
112+
},
113+
required: ["markdown"],
114+
},
115+
},
116+
{
117+
name: "read_daily_log",
118+
description: "Read the agent's episodic memory (daily log) for a specific date.",
119+
inputSchema: {
120+
type: "object",
121+
properties: {
122+
date: { type: "string", description: "The date in YYYY-MM-DD format." },
123+
},
124+
required: ["date"],
125+
},
126+
},
127+
{
128+
name: "append_daily_log",
129+
description: "Append a new entry or fact to the agent's episodic memory (daily log) for a specific date.",
130+
inputSchema: {
131+
type: "object",
132+
properties: {
133+
date: { type: "string", description: "The date in YYYY-MM-DD format." },
134+
markdown: { type: "string", description: "The markdown content to append." },
135+
},
136+
required: ["date", "markdown"],
137+
},
138+
},
139+
{
140+
name: "list_sessions",
141+
description: "List all known session namespaces (chat contexts) in the agent's decentralized registry.",
142+
inputSchema: { type: "object", properties: {}, required: [] },
143+
},
144+
{
145+
name: "resolve_session",
146+
description: "Find the CID and latest data for a specific session namespace.",
147+
inputSchema: {
148+
type: "object",
149+
properties: {
150+
namespace: { type: "string", description: "The namespace to resolve (e.g., 'CHAT_ALICE')." },
151+
},
152+
required: ["namespace"],
153+
},
154+
},
99155
];
100156

101157
server.setRequestHandler(ListToolsRequestSchema, async () => ({
@@ -126,8 +182,8 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
126182

127183
case "retrieve_memory": {
128184
if (!activeAgent) throw new Error("Agent not initialized. Call init_agent first.");
129-
const { cid } = args as { cid: string };
130-
const data = await activeAgent.retrievePublicMemory(cid);
185+
const { cid, ucan } = args as { cid: string; ucan?: string };
186+
const data = await activeAgent.retrievePublicMemory(cid, ucan);
131187
return {
132188
content: [{ type: "text", text: JSON.stringify(data, null, 2) }],
133189
};
@@ -166,12 +222,69 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
166222
};
167223
}
168224

225+
case "read_semantic_memory": {
226+
if (!activeAgent) throw new Error("Agent not initialized. Call init_agent first.");
227+
const markdown = await activeAgent.readSemanticMemory();
228+
return { content: [{ type: "text", text: markdown }] };
229+
}
230+
231+
case "update_semantic_memory": {
232+
if (!activeAgent) throw new Error("Agent not initialized. Call init_agent first.");
233+
const { markdown } = args as { markdown: string };
234+
const cid = await activeAgent.updateSemanticMemory(markdown);
235+
return { content: [{ type: "text", text: `Semantic memory updated successfully. CID: ${cid}` }] };
236+
}
237+
238+
case "read_daily_log": {
239+
if (!activeAgent) throw new Error("Agent not initialized. Call init_agent first.");
240+
const { date } = args as { date: string };
241+
const markdown = await activeAgent.readDailyLog(date);
242+
return { content: [{ type: "text", text: markdown }] };
243+
}
244+
245+
case "append_daily_log": {
246+
if (!activeAgent) throw new Error("Agent not initialized. Call init_agent first.");
247+
const { date, markdown } = args as { date: string; markdown: string };
248+
const cid = await activeAgent.appendDailyLog(date, markdown);
249+
return { content: [{ type: "text", text: `Daily log appended successfully. CID: ${cid}` }] };
250+
}
251+
252+
case "save_session_snapshot": {
253+
if (!activeAgent) throw new Error("Agent not initialized. Call init_agent first.");
254+
const { session_id, markdown } = args as { session_id: string; markdown: string };
255+
const cid = await activeAgent.saveSessionSnapshot(session_id, markdown);
256+
return { content: [{ type: "text", text: `Session snapshot saved. CID: ${cid}` }] };
257+
}
258+
259+
case "list_sessions": {
260+
if (!activeAgent) throw new Error("Agent not initialized. Call init_agent first.");
261+
const sessions = await activeAgent.listNamespaces();
262+
return {
263+
content: [{ type: "text", text: `Known Sessions:\n${sessions.map(s => `- ${s}`).join("\n")}` }],
264+
};
265+
}
266+
267+
case "resolve_session": {
268+
if (!activeAgent) throw new Error("Agent not initialized. Call init_agent first.");
269+
const { namespace } = args as { namespace: string };
270+
const cid = await activeAgent.getNamespaceCid(namespace);
271+
if (!cid) {
272+
return { content: [{ type: "text", text: `Namespace '${namespace}' not found.` }], isError: true };
273+
}
274+
const data = await activeAgent.retrievePublicMemory(cid);
275+
return {
276+
content: [{ type: "text", text: `Namespace: ${namespace}\nCID: ${cid}\n\nData:\n${JSON.stringify(data, null, 2)}` }],
277+
};
278+
}
279+
169280
default:
170281
throw new Error(`Unknown tool: ${name}`);
171282
}
172283
} catch (error) {
284+
const err = error as Error;
285+
console.error(`[MCP Error in ${name}]:`, err.stack || err.message);
173286
return {
174-
content: [{ type: "text", text: `Error: ${(error as Error).message}` }],
287+
content: [{ type: "text", text: `Error: ${err.message}` }],
175288
isError: true,
176289
};
177290
}

0 commit comments

Comments
 (0)