publishNostrEvent
Publish a signed Nostr event to selected relays. Supply the event object with signature and optional relay URLs to broadcast the event across the network.
Instructions
Publish a signed event to relays
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| signedEvent | Yes | Signed event to publish | |
| relays | No | Relays to publish to |
Implementation Reference
- src/index.ts:83-85 (registration)Registers the MCP tool 'publishNostrEvent' on the server, wiring it to the handler and schema.
server.tool('publishNostrEvent', 'Publish a signed event to relays', publishNostrEventSchema.shape, async (params) => { return textResult(await publishNostrEvent(params)); }); - src/tools/event-tools.ts:27-38 (schema)Zod schema defining the input validation for publishNostrEvent: 'signedEvent' (full event object with id, kind, content, tags, created_at, pubkey, sig) and optional 'relays' array.
export const publishNostrEventSchema = z.object({ signedEvent: z.object({ id: z.string(), kind: z.number(), content: z.string(), tags: z.array(z.array(z.string())), created_at: z.number(), pubkey: z.string(), sig: z.string(), }).describe('Signed event to publish'), relays: z.array(z.string()).optional().describe('Relays to publish to'), }); - src/tools/event-tools.ts:65-67 (handler)The main handler function that publishes a signed Nostr event to the specified relays (or defaults). Delegates to the 'publishEvent' helper.
export async function publishNostrEvent({ signedEvent, relays }: z.infer<typeof publishNostrEventSchema>) { return publishEvent(signedEvent as any, relays ?? DEFAULT_RELAYS); } - src/utils/pool.ts:36-64 (helper)Core helper function that does the actual publishing: sends the event to each relay using SimplePool.publish, collects successes and failures, and throws if none succeeded.
export async function publishEvent( event: Event, relays: string[] = DEFAULT_RELAYS, ): Promise<{ successes: string[]; failures: string[] }> { const pool = getPool(); const successes: string[] = []; const failures: string[] = []; const results = await Promise.allSettled( relays.map(async (relay) => { await pool.publish([relay], event); return relay; }), ); for (const result of results) { if (result.status === 'fulfilled') { successes.push(result.value); } else { failures.push(String(result.reason)); } } if (successes.length === 0) { throw new Error(`Failed to publish to any relay: ${failures.join(', ')}`); } return { successes, failures }; } - src/utils/constants.ts:1-8 (helper)Default relay list used when no relays are provided to publishNostrEvent.
export const DEFAULT_RELAYS = [ 'wss://relay.damus.io', 'wss://relay.primal.net', 'wss://nos.lol', 'wss://relay.nostr.band', 'wss://purplepag.es', 'wss://relay.snort.social', ];