add nip-04 mcp tool

This commit is contained in:
hzrd149
2025-04-07 14:18:53 +01:00
parent 60a46e16e7
commit a2a3972bdd
2 changed files with 61 additions and 0 deletions

View File

@@ -8,3 +8,4 @@ import "./profile.js";
import "./signer.js"; import "./signer.js";
import "./social.js"; import "./social.js";
import "./sql.js"; import "./sql.js";
import "./messages.js";

View File

@@ -0,0 +1,60 @@
import { mergeRelaySets } from "applesauce-core/helpers";
import { includeSingletonTag, setEncryptedContent } from "applesauce-factory/operations/event";
import { kinds } from "nostr-tools";
import { lastValueFrom, toArray } from "rxjs";
import { z } from "zod";
import { LOOKUP_RELAYS } from "../../../env.js";
import bakeryConfig from "../../bakery-config.js";
import { asyncLoader } from "../../loaders.js";
import { ownerAccount$, ownerFactory, ownerSigner } from "../../owner-signer.js";
import pool from "../../pool.js";
import { pubkeyInput } from "../common.js";
import mcpServer from "../server.js";
mcpServer.tool(
"send_message",
"Send a encrypted direct message to a user",
{
pubkey: pubkeyInput.describe("The pubkey of the user to send the message to"),
message: z.string().describe("The message to send"),
},
async ({ pubkey, message }) => {
if (!ownerAccount$.value) return { content: [{ type: "text", text: "No nostr signer found" }] };
// Create a new NIP-04 message
const draft = await ownerFactory.build(
{
kind: kinds.EncryptedDirectMessage,
},
includeSingletonTag(["p", pubkey]),
setEncryptedContent(pubkey, message, "nip04"),
);
// Sign the event
const event = await ownerFactory.sign(draft);
// Get users inboxes
const otherInboxes = await asyncLoader.inboxes(pubkey, bakeryConfig.data.lookup_relays ?? LOOKUP_RELAYS);
if (otherInboxes.length === 0) {
return {
content: [{ type: "text", text: "The user has no DM inboxes configured" }],
};
}
const ownerInboxes = await asyncLoader.inboxes(
await ownerSigner.getPublicKey(),
bakeryConfig.data.lookup_relays ?? LOOKUP_RELAYS,
);
// publish the event to the owner and the other user
const results = await lastValueFrom(pool.event(mergeRelaySets(ownerInboxes, otherInboxes), event).pipe(toArray()));
return {
content: [
{ type: "text", text: `Message sent to ${results.length} relays` },
{ type: "text", text: results.map((r) => `${r.from} (${r.ok}) ${r.message ?? ""}`).join("\n") },
],
};
},
);