Skip to main content
Glama

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
NameRequiredDescriptionDefault
contentYesMessage content
recipientPubkeyYesRecipient pubkey (hex or npub)
privateKeyNoPrivate key (nsec or hex). Optional when NOSTR_BUNKER_URI is configured.
relaysNoRelays to publish to

Implementation Reference

  • 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 };
    }
  • 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));
    });

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jorgenclaw/nostr-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server