get-code-examples
Generate code examples in Node.js or Python for integrating PaymanAI features. Simplify development by providing ready-to-use snippets for specific functionalities.
Instructions
Get Node.js or Python code examples for PaymanAI integration
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| feature | Yes | The feature or functionality you need code examples for | |
| language | No | Programming language (nodejs or python) | nodejs |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"feature": {
"description": "The feature or functionality you need code examples for",
"type": "string"
},
"language": {
"default": "nodejs",
"description": "Programming language (nodejs or python)",
"enum": [
"nodejs",
"python"
],
"type": "string"
}
},
"required": [
"feature"
],
"type": "object"
}
Implementation Reference
- src/index.ts:305-395 (handler)The handler function that executes the get-code-examples tool. It fetches relevant PaymanAI documentation, extracts code blocks in the specified language (nodejs or python), filters those matching the feature, and returns formatted examples.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:294-304 (schema)Zod schema defining the input parameters for the get-code-examples tool: feature (string) and language (enum: nodejs|python, default 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)Registration of the get-code-examples tool using server.tool(), including 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, }, ], }; } );