get-code-examples
Retrieve Node.js or Python code examples for PaymanAI integration features to help developers implement specific functionality in their projects.
Instructions
Get Node.js or Python code examples for PaymanAI integration
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| feature | Yes | The feature or functionality you need code examples for | |
| language | No | Programming language (nodejs or python) | nodejs |
Implementation Reference
- src/index.ts:305-395 (handler)The handler function that implements the get-code-examples tool. It searches documentation for relevant code blocks in the specified language matching the feature, extracts them using regex, and formats them into a response.async ({ feature, language }) => { log(`Getting ${language} code example for: "${feature}"`); const potentialTopics = Object.entries(pathMap) .filter( ([topic]) => topic.toLowerCase().includes(feature.toLowerCase()) || topicMetadata[topic].title .toLowerCase() .includes(feature.toLowerCase()) ) .map(([topic]) => topic); const topicsToSearch = potentialTopics.length > 0 ? potentialTopics : Object.keys(pathMap); const examplesPromises = topicsToSearch.map(async (topic) => { const path = pathMap[topic]; const content = await fetchDocMarkdown(path); const codeBlockRegex = language === "nodejs" ? /```(?:javascript|typescript|js|nodejs|node)([\s\S]*?)```/g : /```(?:python|py)([\s\S]*?)```/g; const matches = [...content.matchAll(codeBlockRegex)]; const relevantBlocks = matches .map((match) => match[1].trim()) .filter( (code) => code.toLowerCase().includes(feature.toLowerCase()) || content .substring( Math.max(0, content.indexOf(code) - 300), content.indexOf(code) ) .toLowerCase() .includes(feature.toLowerCase()) ); if (relevantBlocks.length === 0) return null; return { topic, title: topicMetadata[topic].title, examples: relevantBlocks, }; }); const allExamples = (await Promise.all(examplesPromises)).filter( Boolean ); if (allExamples.length === 0) { return { content: [ { type: "text", text: `No ${language} code examples found for "${feature}". Try searching for a different feature or check the full documentation using get-documentation.`, }, ], }; } let responseText = `# ${language.toUpperCase()} Code Examples for "${feature}"\n\n`; allExamples.forEach((topicExamples) => { if (!topicExamples) return; responseText += `## From ${topicExamples.title}\n\n`; topicExamples.examples.forEach((code, index) => { responseText += `### Example ${index + 1}\n\n`; responseText += `\`\`\`${ language === "nodejs" ? "javascript" : "python" }\n${code}\n\`\`\`\n\n`; }); responseText += `*For more context, check the full documentation: use get-documentation with topic "${topicExamples.topic}".*\n\n---\n\n`; }); return { content: [ { type: "text", text: responseText, }, ], }; }
- src/index.ts:295-304 (schema)Zod schema defining the input parameters for the tool: feature (required string) and language (optional enum defaulting to nodejs).feature: z .string() .describe( "The feature or functionality you need code examples for" ), language: z .enum(["nodejs", "python"]) .default("nodejs") .describe("Programming language (nodejs or python)"), },
- src/index.ts:291-396 (registration)The server.tool call that registers the get-code-examples tool with the MCP server, providing name, description, input schema, and handler function.server.tool( "get-code-examples", "Get Node.js or Python code examples for PaymanAI integration", { feature: z .string() .describe( "The feature or functionality you need code examples for" ), language: z .enum(["nodejs", "python"]) .default("nodejs") .describe("Programming language (nodejs or python)"), }, async ({ feature, language }) => { log(`Getting ${language} code example for: "${feature}"`); const potentialTopics = Object.entries(pathMap) .filter( ([topic]) => topic.toLowerCase().includes(feature.toLowerCase()) || topicMetadata[topic].title .toLowerCase() .includes(feature.toLowerCase()) ) .map(([topic]) => topic); const topicsToSearch = potentialTopics.length > 0 ? potentialTopics : Object.keys(pathMap); const examplesPromises = topicsToSearch.map(async (topic) => { const path = pathMap[topic]; const content = await fetchDocMarkdown(path); const codeBlockRegex = language === "nodejs" ? /```(?:javascript|typescript|js|nodejs|node)([\s\S]*?)```/g : /```(?:python|py)([\s\S]*?)```/g; const matches = [...content.matchAll(codeBlockRegex)]; const relevantBlocks = matches .map((match) => match[1].trim()) .filter( (code) => code.toLowerCase().includes(feature.toLowerCase()) || content .substring( Math.max(0, content.indexOf(code) - 300), content.indexOf(code) ) .toLowerCase() .includes(feature.toLowerCase()) ); if (relevantBlocks.length === 0) return null; return { topic, title: topicMetadata[topic].title, examples: relevantBlocks, }; }); const allExamples = (await Promise.all(examplesPromises)).filter( Boolean ); if (allExamples.length === 0) { return { content: [ { type: "text", text: `No ${language} code examples found for "${feature}". Try searching for a different feature or check the full documentation using get-documentation.`, }, ], }; } let responseText = `# ${language.toUpperCase()} Code Examples for "${feature}"\n\n`; allExamples.forEach((topicExamples) => { if (!topicExamples) return; responseText += `## From ${topicExamples.title}\n\n`; topicExamples.examples.forEach((code, index) => { responseText += `### Example ${index + 1}\n\n`; responseText += `\`\`\`${ language === "nodejs" ? "javascript" : "python" }\n${code}\n\`\`\`\n\n`; }); responseText += `*For more context, check the full documentation: use get-documentation with topic "${topicExamples.topic}".*\n\n---\n\n`; }); return { content: [ { type: "text", text: responseText, }, ], }; } );