Skip to main content
Glama

Hacker News Companion MCP

by georgeck
utils.js1.19 kB
/** * Utility functions for the HN Companion MCP */ /** * Extract a post ID from a Hacker News URL * @param url - HN URL (e.g., https://news.ycombinator.com/item?id=43448075) * @returns The post ID or null if not found */ export function extractPostIdFromUrl(url) { try { const urlObj = new URL(url); if (urlObj.hostname === 'news.ycombinator.com' || urlObj.hostname === 'hn.algolia.com') { const params = new URLSearchParams(urlObj.search); return params.get('id'); } return null; } catch (error) { return null; } } /** * Check if a string is a valid post ID * @param id - Post ID to validate * @returns True if the ID is valid */ function isValidPostId(id) { return /^\d+$/.test(id); } /** * Get post ID from either a URL or direct ID * @param input - URL or post ID * @returns Extracted post ID or null if invalid */ export function getPostId(input) { // Check if input is a URL if (input.startsWith('http')) { return extractPostIdFromUrl(input); } // Check if input is a valid post ID if (isValidPostId(input)) { return input; } return null; }

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/georgeck/hn-companion-mcp'

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