Skip to main content
Glama

createKeypair

Generates a Nostr-compatible keypair in specified formats (hex, npub, or both) to facilitate secure interactions with the Nostr social network via the Nostr MCP Server.

Instructions

Generate a new Nostr keypair

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
formatNoFormat to return keys in: hex only, npub only, or bothboth

Implementation Reference

  • The core handler function that generates a new Nostr keypair using snstr's generateKeypair(), formats the output based on the requested format (hex, npub, or both), and returns the keys.
    /** * Generate a new Nostr keypair */ export async function createKeypair( format: "both" | "hex" | "npub" = "both" ): Promise<{ publicKey?: string, privateKey?: string, npub?: string, nsec?: string }> { try { // Generate a new keypair const keys = await generateKeypair(); const result: { publicKey?: string, privateKey?: string, npub?: string, nsec?: string } = {}; if (format === "hex" || format === "both") { result.publicKey = keys.publicKey; result.privateKey = keys.privateKey; } if (format === "npub" || format === "both") { result.npub = encodePublicKey(keys.publicKey); result.nsec = encodePrivateKey(keys.privateKey); } return result; } catch (error) { throw new Error(`Failed to generate keypair: ${error instanceof Error ? error.message : "Unknown error"}`); } }
  • Zod schema definition for the createKeypair tool input parameters, specifying the output format.
    // Schema for createKeypair tool export const createKeypairToolConfig = { format: z.enum(["both", "hex", "npub"]).default("both").describe("Format to return keys in: hex only, npub only, or both"), };
  • index.ts:1156-1202 (registration)
    MCP server tool registration for 'createKeypair', which wraps the handler, formats the response as MCP content, and handles errors.
    server.tool( "createKeypair", "Generate a new Nostr keypair", createKeypairToolConfig, async ({ format }) => { try { const result = await createKeypair(format); let response = "New Nostr keypair generated:\n\n"; if (result.publicKey) { response += `Public Key (hex): ${result.publicKey}\n`; } if (result.privateKey) { response += `Private Key (hex): ${result.privateKey}\n`; } if (result.npub) { response += `Public Key (npub): ${result.npub}\n`; } if (result.nsec) { response += `Private Key (nsec): ${result.nsec}\n`; } response += "\n⚠️ IMPORTANT: Store your private key securely! This is the only copy and cannot be recovered if lost."; return { content: [ { type: "text", text: response, }, ], }; } catch (error) { console.error("Error in createKeypair tool:", error); return { content: [ { type: "text", text: `Error generating keypair: ${error instanceof Error ? error.message : "Unknown error"}`, }, ], }; } }, );

Other Tools

Related Tools

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/AustinKelsay/nostr-mcp-server'

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