sendDmNip04
Send encrypted direct messages on Nostr using NIP-04 protocol to communicate privately with other users through specified relays.
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 implementation of sendDmNip04Fn which handles encrypting, signing, and publishing the NIP-04 direct message.
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 input validation of the sendDmNip04 tool.
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-135 (registration)Registration of the 'sendDmNip04' tool with the MCP server.
server.tool('sendDmNip04', 'Send a NIP-04 direct message', sendDmNip04Schema.shape, async (params) => { return textResult(await sendDmNip04Fn(params)); });