sendDmNip04
Send encrypted direct messages on Nostr using NIP-04 protocol. Securely communicate with recipients by providing message content and recipient public key.
Instructions
Send a NIP-04 direct message
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | Message content | |
| recipientPubkey | Yes | Recipient pubkey (hex or npub) | |
| privateKey | No | Private key (nsec or hex). Optional when NOSTR_BUNKER_URI is configured. | |
| relays | No | Relays to publish to |
Implementation Reference
- src/tools/dm-tools.ts:78-106 (handler)The handler function `sendDmNip04Fn` that performs NIP-04 DM encryption and publishes the event.
export async function sendDmNip04Fn({ content, recipientPubkey, privateKey, relays }: z.infer<typeof sendDmNip04Schema>) { const recipient = normalizePubkey(recipientPubkey); let encrypted: string; if (isBunkerMode()) { encrypted = await nip04EncryptWithBunker(recipient, content); } else { if (!privateKey) throw new Error('privateKey is required when NOSTR_BUNKER_URI is not configured'); const sk = normalizePrivateKey(privateKey); encrypted = await nip04.encrypt(sk, recipient, content); } const template: EventTemplate = { kind: KINDS.DM, content: encrypted, tags: [['p', recipient]], created_at: Math.floor(Date.now() / 1000), }; let signed: VerifiedEvent; if (isBunkerMode()) { signed = await signEventWithBunker(template); } else { signed = finalizeEvent(template, normalizePrivateKey(privateKey!)); } const result = await publishEvent(signed, relays ?? DEFAULT_RELAYS); return { event: signed, published: result }; } - src/tools/dm-tools.ts:32-37 (schema)The Zod schema definition for `sendDmNip04` inputs.
export const sendDmNip04Schema = z.object({ content: z.string().describe('Message content'), recipientPubkey: z.string().describe('Recipient pubkey (hex or npub)'), privateKey: z.string().optional().describe(privateKeyDesc), relays: z.array(z.string()).optional().describe('Relays to publish to'), }); - src/index.ts:133-134 (registration)Registration of the `sendDmNip04` tool in the MCP server instance.
server.tool('sendDmNip04', 'Send a NIP-04 direct message', sendDmNip04Schema.shape, async (params) => { return textResult(await sendDmNip04Fn(params));