analyzeNip19
Decode and analyze NIP-19 entities or hex strings to identify their type and extract detailed contents, simplifying interaction with the Nostr social network.
Instructions
Analyze any NIP-19 entity or hex string to understand its type and contents
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| input | Yes | The NIP-19 entity or hex string to analyze |
Implementation Reference
- utils/conversion.ts:340-389 (handler)Core implementation of NIP-19 analysis: decodes input as NIP-19 entity or validates as hex, applies security filtering to complex types, returns type and safe data.export function analyzeNip19Entity(input: string): ConversionResult { try { const cleanInput = input.trim(); // Check if hex if (/^[0-9a-fA-F]{64}$/.test(cleanInput)) { return { success: true, originalType: 'hex', message: 'Valid 64-character hex string', data: cleanInput.toLowerCase() }; } // Try to decode as NIP-19 if (cleanInput.includes('1')) { const decoded = decode(cleanInput as `${string}1${string}`); // Apply security filtering for complex types let safeData = decoded.data; if (['nprofile', 'nevent', 'naddr'].includes(decoded.type)) { if (decoded.type === 'nprofile') { safeData = filterProfile(decoded.data); } else if (decoded.type === 'nevent') { safeData = filterEvent(decoded.data); } else if (decoded.type === 'naddr') { safeData = filterAddress(decoded.data); } } return { success: true, originalType: decoded.type, message: `Valid ${decoded.type} entity`, data: safeData }; } return { success: false, message: 'Input is not a valid NIP-19 entity or hex string' }; } catch (error) { return { success: false, message: `Analysis failed: ${error instanceof Error ? error.message : 'Unknown error'}` }; } }
- utils/nip19-tools.ts:14-17 (schema)Zod input schema for the analyzeNip19 tool.// Schema for analyzeNip19 tool export const analyzeNip19ToolConfig = { input: z.string().describe("The NIP-19 entity or hex string to analyze"), };
- index.ts:1051-1095 (registration)MCP server.tool registration for analyzeNip19, including description, schema, and inline handler that calls analyzeNip19 wrapper and formats response.server.tool( "analyzeNip19", "Analyze any NIP-19 entity or hex string to understand its type and contents", analyzeNip19ToolConfig, async ({ input }) => { try { const result = await analyzeNip19(input); if (result.success) { let response = `Analysis successful!\n\n`; response += `Type: ${result.type}\n\n`; response += formatAnalysisResult(result.type!, result.data); return { content: [ { type: "text", text: response, }, ], }; } else { return { content: [ { type: "text", text: `Analysis failed: ${result.message}`, }, ], }; } } catch (error) { console.error("Error in analyzeNip19 tool:", error); return { content: [ { type: "text", text: `Error during analysis: ${error instanceof Error ? error.message : "Unknown error"}`, }, ], }; } }, );
- utils/nip19-tools.ts:69-94 (helper)Wrapper function for analyzeNip19Entity that handles async/await and standardizes error responses.export async function analyzeNip19( input: string ): Promise<{ success: boolean, message: string, type?: string, data?: any }> { try { const result = analyzeNip19Entity(input); if (!result.success) { return { success: false, message: result.message || 'Analysis failed' }; } return { success: true, message: result.message || 'Analysis successful', type: result.originalType, data: result.data }; } catch (error) { return { success: false, message: `Error during analysis: ${error instanceof Error ? error.message : 'Unknown error'}` }; } }