read_pubnub_chat_sdk_docs
Retrieve PubNub Chat SDK documentation in markdown format for specific languages and topics, including code examples and usage patterns, to enhance development workflows.
Instructions
Retrieves official PubNub Chat SDK documentation for a given Chat SDK language and topic section. Call this tool whenever you need detailed Chat SDK docs, code examples, or usage patterns. Returns documentation in markdown format.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| language | Yes | Chat SDK language to retrieve documentation for | |
| topic | Yes | Chat SDK documentation topic to retrieve |
Implementation Reference
- index.js:416-437 (handler)The handler function that resolves the documentation URL from the pre-parsed chatSdkDocsUrlMap based on language and topic parameters, fetches the HTML content using loadArticle, converts it to markdown, and returns it as tool content. Handles missing URL and fetch errors gracefully.toolHandlers['read_pubnub_chat_sdk_docs'] = async ({ language, topic }) => { const url = chatSdkDocsUrlMap[language]?.[topic]; if (!url) { return { content: [ { type: 'text', text: `Documentation URL not found for language '${language}' and topic '${topic}'.` }, ], isError: true, }; } try { const markdown = await loadArticle(url); return { content: [{ type: 'text', text: markdown }], }; } catch (err) { return { content: [{ type: 'text', text: `Error fetching ${url}: ${err.message || err}` }], isError: true, }; } };
- index.js:440-447 (schema)Zod-based input schema definition for the tool, specifying required parameters 'language' and 'topic' as enums derived from parsed chat_sdk_urls.txt data.toolDefinitions['read_pubnub_chat_sdk_docs'] = { name: 'read_pubnub_chat_sdk_docs', description: 'Retrieves official PubNub Chat SDK documentation for a given Chat SDK language and topic section. Call this tool whenever you need detailed Chat SDK docs, code examples, or usage patterns. Returns documentation in markdown format.', parameters: { language: z.enum(chatSdkLanguages).describe('Chat SDK language to retrieve documentation for'), topic: z.enum(chatSdkTopics).describe('Chat SDK documentation topic to retrieve'), } };
- index.js:1363-1394 (registration)The registration function that adds the tool to the MCP server instance using serverInstance.tool(), with special conditional logic to include it only if chat SDK URL data is loaded, and excludes certain tools in --chat-sdk mode. Called for both stdio and HTTP session servers.function registerAllTools(serverInstance, chatSdkMode = false) { // Tools to exclude when in chat SDK mode const chatSdkExcludedTools = [ 'read_pubnub_sdk_docs', 'write_pubnub_app', 'read_pubnub_resources', 'manage_pubnub_account' ]; for (const toolName in toolDefinitions) { if (toolHandlers[toolName]) { // Skip excluded tools when in chat SDK mode if (chatSdkMode && chatSdkExcludedTools.includes(toolName)) { continue; } // Special handling for chat SDK docs tool if (toolName === 'read_pubnub_chat_sdk_docs' && (chatSdkLanguages.length === 0 || chatSdkTopics.length === 0)) { continue; // Skip this tool if chat SDK data isn't loaded } const toolDef = toolDefinitions[toolName]; serverInstance.tool( toolDef.name, toolDef.description, toolDef.parameters, wrapToolHandler(toolHandlers[toolName], toolName) ); } } }
- index.js:373-414 (helper)Parses the chat_sdk_urls.txt file to create chatSdkDocsUrlMap {language: {topic: url}}, and derives chatSdkLanguages and chatSdkTopics arrays used in schema enums and registration check.// Parse chat_sdk_urls.txt to generate mapping of Chat SDK documentation URLs per language and topic const chatSdkUrlsPath = pathJoin(__dirname, 'chat_sdk_urls.txt'); let chatSdkDocsUrlMap = {}; try { const chatSdkUrlsContent = fs.readFileSync(chatSdkUrlsPath, 'utf8'); const lines = chatSdkUrlsContent.split(/\n/); let currentLang = null; for (const rawLine of lines) { const line = rawLine.trim(); if (!line) { currentLang = null; continue; } if (!line.startsWith('http')) { currentLang = line.toLowerCase(); chatSdkDocsUrlMap[currentLang] = {}; } else if (currentLang) { const url = line; const urlObj = new URL(url); const pathSegments = urlObj.pathname.split('/').filter(Boolean); let topicKey; const buildIndex = pathSegments.indexOf('build'); const learnIndex = pathSegments.indexOf('learn'); if (buildIndex !== -1) { topicKey = pathSegments[pathSegments.length - 1]; } else if (learnIndex !== -1) { const remaining = pathSegments.slice(learnIndex + 1); topicKey = remaining.length > 1 ? remaining[remaining.length - 1] : remaining[0]; } else { topicKey = pathSegments[pathSegments.length - 1]; } chatSdkDocsUrlMap[currentLang][topicKey] = url; } } } catch (err) { //console.error(`Error loading chat_sdk_urls.txt: ${err}`); } const chatSdkLanguages = Object.keys(chatSdkDocsUrlMap); const chatSdkTopics = chatSdkLanguages.length > 0 ? Object.keys(chatSdkDocsUrlMap[chatSdkLanguages[0]]) : [];
- index.js:277-292 (helper)Utility function called by the handler to fetch documentation HTML from the resolved URL, extract the article element, and convert to markdown using HtmlToMarkdown class from lib/html-to-markdown.js.async function loadArticle(url) { try { const response = await fetch(url); if (!response.ok) { return `Error fetching ${url}: ${response.status} ${response.statusText}`; } const html = await response.text(); const root = parse(html); const article = root.querySelector('article'); const converter = new HtmlToMarkdown(); return converter.turndown(article?.innerHTML || ''); } catch (err) { return `Error fetching ${url}: ${err.message}`; } }