getKind1Notes
Fetch and retrieve text notes (kind 1) from the Nostr social network by specifying a user's public key. Customize results with optional relay lists and limit settings for efficient data queries.
Instructions
Get text notes (kind 1) by public key
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | Maximum number of notes to fetch | |
| pubkey | Yes | Public key of the Nostr user (hex format or npub format) | |
| relays | No | Optional list of relays to query |
Implementation Reference
- index.ts:152-226 (handler)The core handler function that implements the getKind1Notes tool logic: converts pubkey, queries Nostr relays via snstr pool for kind 1 events, sorts by recency, formats using formatNote, and returns MCP-formatted content.async ({ pubkey, limit, relays }, extra) => { // Convert npub to hex if needed const hexPubkey = npubToHex(pubkey); if (!hexPubkey) { return { content: [ { type: "text", text: "Invalid public key format. Please provide a valid hex pubkey or npub.", }, ], }; } // Generate a friendly display version of the pubkey const displayPubkey = formatPubkey(hexPubkey); const relaysToUse = relays || DEFAULT_RELAYS; // Create a fresh pool for this request const pool = getFreshPool(relaysToUse); try { console.error(`Fetching kind 1 notes for ${hexPubkey} from ${relaysToUse.join(", ")}`); // Query for text notes - snstr handles timeout internally const notes = await pool.querySync( relaysToUse, { kinds: [KINDS.Text], authors: [hexPubkey], limit, } as NostrFilter, { timeout: QUERY_TIMEOUT } ); if (!notes || notes.length === 0) { return { content: [ { type: "text", text: `No notes found for ${displayPubkey}`, }, ], }; } // Sort notes by created_at in descending order (newest first) notes.sort((a, b) => b.created_at - a.created_at); const formattedNotes = notes.map(formatNote).join("\n"); return { content: [ { type: "text", text: `Found ${notes.length} notes from ${displayPubkey}:\n\n${formattedNotes}`, }, ], }; } catch (error) { console.error("Error fetching notes:", error); return { content: [ { type: "text", text: `Error fetching notes for ${displayPubkey}: ${error instanceof Error ? error.message : "Unknown error"}`, }, ], }; } finally { // Clean up any subscriptions and close the pool await pool.close(); } }
- index.ts:148-227 (registration)Registers the getKind1Notes tool with the MCP server, specifying name, description, input schema, and inline handler function.server.tool( "getKind1Notes", "Get text notes (kind 1) by public key", getKind1NotesToolConfig, async ({ pubkey, limit, relays }, extra) => { // Convert npub to hex if needed const hexPubkey = npubToHex(pubkey); if (!hexPubkey) { return { content: [ { type: "text", text: "Invalid public key format. Please provide a valid hex pubkey or npub.", }, ], }; } // Generate a friendly display version of the pubkey const displayPubkey = formatPubkey(hexPubkey); const relaysToUse = relays || DEFAULT_RELAYS; // Create a fresh pool for this request const pool = getFreshPool(relaysToUse); try { console.error(`Fetching kind 1 notes for ${hexPubkey} from ${relaysToUse.join(", ")}`); // Query for text notes - snstr handles timeout internally const notes = await pool.querySync( relaysToUse, { kinds: [KINDS.Text], authors: [hexPubkey], limit, } as NostrFilter, { timeout: QUERY_TIMEOUT } ); if (!notes || notes.length === 0) { return { content: [ { type: "text", text: `No notes found for ${displayPubkey}`, }, ], }; } // Sort notes by created_at in descending order (newest first) notes.sort((a, b) => b.created_at - a.created_at); const formattedNotes = notes.map(formatNote).join("\n"); return { content: [ { type: "text", text: `Found ${notes.length} notes from ${displayPubkey}:\n\n${formattedNotes}`, }, ], }; } catch (error) { console.error("Error fetching notes:", error); return { content: [ { type: "text", text: `Error fetching notes for ${displayPubkey}: ${error instanceof Error ? error.message : "Unknown error"}`, }, ], }; } finally { // Clean up any subscriptions and close the pool await pool.close(); } } );
- note/note-tools.ts:16-21 (schema)Zod schema defining input parameters for the getKind1Notes tool: pubkey (required), limit (1-100, default 10), relays (optional array).// Schema for getKind1Notes tool export const getKind1NotesToolConfig = { pubkey: z.string().describe("Public key of the Nostr user (hex format or npub format)"), limit: z.number().min(1).max(100).default(10).describe("Maximum number of notes to fetch"), relays: z.array(z.string()).optional().describe("Optional list of relays to query"), };
- note/note-tools.ts:95-106 (helper)Helper function to format a single Nostr note event into a readable string, used by the handler to format output notes.export function formatNote(note: NostrEvent): string { if (!note) return ""; const created = new Date(note.created_at * 1000).toLocaleString(); return [ `ID: ${note.id}`, `Created: ${created}`, `Content: ${note.content}`, `---`, ].join("\n"); }