follow
Add a pubkey to your contact list by updating kind 3 events on the Nostr protocol. Specify the pubkey to follow and optional relays for publishing.
Instructions
Follow a pubkey (updates kind 3 contact list)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| pubkeyToFollow | Yes | Pubkey (hex or npub) to follow | |
| privateKey | No | Private key (nsec or hex). Optional when NOSTR_BUNKER_URI is configured. | |
| relays | No | Relays to publish to |
Implementation Reference
- src/tools/social-tools.ts:61-87 (handler)The handler function that executes the follow logic by modifying the contact list (NIP-02).
export async function follow({ pubkeyToFollow, privateKey, relays }: z.infer<typeof followSchema>) { const targetPubkey = normalizePubkey(pubkeyToFollow); const myPubkey = resolveSigningPubkey(privateKey); // Fetch current contact list const existing = await queryEvent( { kinds: [KINDS.CONTACT_LIST], authors: [myPubkey], limit: 1 }, relays ?? DEFAULT_RELAYS, ); let tags: string[][] = existing?.tags.filter(t => t[0] === 'p') ?? []; if (tags.some(t => t[1] === targetPubkey)) { return { message: 'Already following this pubkey', pubkey: targetPubkey }; } tags.push(['p', targetPubkey]); const template: EventTemplate = { kind: KINDS.CONTACT_LIST, content: existing?.content ?? '', tags, created_at: Math.floor(Date.now() / 1000), }; const signed = await signTemplate(template, privateKey); const result = await publishEvent(signed, relays ?? DEFAULT_RELAYS); return { event: signed, published: result }; } - src/tools/social-tools.ts:11-15 (schema)Validation schema for the 'follow' tool input.
export const followSchema = z.object({ pubkeyToFollow: z.string().describe('Pubkey (hex or npub) to follow'), privateKey: z.string().optional().describe(privateKeyDesc), relays: z.array(z.string()).optional().describe('Relays to publish to'), }); - src/index.ts:99-100 (registration)Registration of the 'follow' tool in the MCP server.
server.tool('follow', 'Follow a pubkey (updates kind 3 contact list)', followSchema.shape, async (params) => { return textResult(await follow(params));