|
| 1 | +import type { Tool } from '@modelcontextprotocol/sdk/types.js'; |
| 2 | +import type { JsonSchemaProperty } from './tools.js'; |
| 3 | + |
| 4 | +/** Public MCP base; paths under it may include an API key segment — never echo secrets in docs. */ |
| 5 | +const BITREFILL_MCP_PUBLIC_BASE = 'https://api.bitrefill.com/mcp'; |
| 6 | + |
| 7 | +export interface GenerateLlmContextOptions { |
| 8 | + /** MCP server URL used for this session (shown in the header; API key segment is redacted). */ |
| 9 | + mcpUrl?: string; |
| 10 | + /** CLI program name (default `bitrefill`). */ |
| 11 | + programName?: string; |
| 12 | +} |
| 13 | + |
| 14 | +function sanitizeMcpUrlForDocs(url: string): string { |
| 15 | + if (url === BITREFILL_MCP_PUBLIC_BASE) return url; |
| 16 | + if (url.startsWith(`${BITREFILL_MCP_PUBLIC_BASE}/`)) { |
| 17 | + return `${BITREFILL_MCP_PUBLIC_BASE}/<API_KEY>`; |
| 18 | + } |
| 19 | + return url; |
| 20 | +} |
| 21 | + |
| 22 | +function sortedPropertyEntries(tool: Tool): [string, JsonSchemaProperty][] { |
| 23 | + const schema = tool.inputSchema as { |
| 24 | + properties?: Record<string, JsonSchemaProperty>; |
| 25 | + }; |
| 26 | + if (!schema.properties) return []; |
| 27 | + return Object.entries(schema.properties).sort(([a], [b]) => |
| 28 | + a.localeCompare(b) |
| 29 | + ); |
| 30 | +} |
| 31 | + |
| 32 | +/** JSON.stringify for use as a shell argument (bash/zsh). */ |
| 33 | +function shellArgFromString(value: string): string { |
| 34 | + return JSON.stringify(value); |
| 35 | +} |
| 36 | + |
| 37 | +function exampleArgumentValue(prop: JsonSchemaProperty): string { |
| 38 | + if (Array.isArray(prop.enum) && prop.enum.length > 0) { |
| 39 | + const first = prop.enum[0]; |
| 40 | + return typeof first === 'string' |
| 41 | + ? shellArgFromString(first) |
| 42 | + : String(first); |
| 43 | + } |
| 44 | + const t = prop.type; |
| 45 | + switch (t) { |
| 46 | + case 'number': |
| 47 | + case 'integer': |
| 48 | + return '1'; |
| 49 | + case 'boolean': |
| 50 | + return 'true'; |
| 51 | + case 'array': |
| 52 | + return `'[]'`; |
| 53 | + case 'object': |
| 54 | + return `'{}'`; |
| 55 | + case 'string': |
| 56 | + default: |
| 57 | + return shellArgFromString('example'); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +function buildCliExample( |
| 62 | + tool: Tool, |
| 63 | + programName: string, |
| 64 | + entries: [string, JsonSchemaProperty][] |
| 65 | +): string { |
| 66 | + if (entries.length === 0) return `${programName} ${tool.name}`; |
| 67 | + |
| 68 | + const schema = tool.inputSchema as { required?: string[] }; |
| 69 | + const required = new Set(schema.required ?? []); |
| 70 | + |
| 71 | + const parts: string[] = [`${programName} ${tool.name}`]; |
| 72 | + for (const [name, prop] of entries) { |
| 73 | + if (!required.has(name)) continue; |
| 74 | + parts.push(`--${name} ${exampleArgumentValue(prop)}`); |
| 75 | + } |
| 76 | + for (const [name, prop] of entries) { |
| 77 | + if (required.has(name)) continue; |
| 78 | + parts.push(`--${name} ${exampleArgumentValue(prop)}`); |
| 79 | + break; |
| 80 | + } |
| 81 | + return parts.join(' '); |
| 82 | +} |
| 83 | + |
| 84 | +function buildMcpToolsCallExample( |
| 85 | + tool: Tool, |
| 86 | + entries: [string, JsonSchemaProperty][] |
| 87 | +): string { |
| 88 | + const args: Record<string, unknown> = {}; |
| 89 | + const schema = tool.inputSchema as { required?: string[] }; |
| 90 | + const required = new Set(schema.required ?? []); |
| 91 | + |
| 92 | + for (const [name, prop] of entries) { |
| 93 | + if (!required.has(name)) continue; |
| 94 | + args[name] = exampleJsonValue(prop); |
| 95 | + } |
| 96 | + if (Object.keys(args).length === 0 && entries.length > 0) { |
| 97 | + const [name, prop] = entries[0]; |
| 98 | + args[name] = exampleJsonValue(prop); |
| 99 | + } |
| 100 | + |
| 101 | + return JSON.stringify( |
| 102 | + { |
| 103 | + method: 'tools/call', |
| 104 | + params: { |
| 105 | + name: tool.name, |
| 106 | + arguments: args, |
| 107 | + }, |
| 108 | + }, |
| 109 | + null, |
| 110 | + 2 |
| 111 | + ); |
| 112 | +} |
| 113 | + |
| 114 | +function exampleJsonValue(prop: JsonSchemaProperty): unknown { |
| 115 | + if (Array.isArray(prop.enum) && prop.enum.length > 0) { |
| 116 | + return prop.enum[0]; |
| 117 | + } |
| 118 | + const t = prop.type; |
| 119 | + switch (t) { |
| 120 | + case 'number': |
| 121 | + case 'integer': |
| 122 | + return 1; |
| 123 | + case 'boolean': |
| 124 | + return true; |
| 125 | + case 'array': |
| 126 | + return []; |
| 127 | + case 'object': |
| 128 | + return {}; |
| 129 | + case 'string': |
| 130 | + default: |
| 131 | + return 'example'; |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +function formatParameterTable( |
| 136 | + tool: Tool, |
| 137 | + entries: [string, JsonSchemaProperty][] |
| 138 | +): string { |
| 139 | + if (entries.length === 0) { |
| 140 | + return '_No parameters._\n'; |
| 141 | + } |
| 142 | + |
| 143 | + const schema = tool.inputSchema as { required?: string[] }; |
| 144 | + const required = new Set(schema.required ?? []); |
| 145 | + |
| 146 | + const rows = entries.map(([name, prop]) => { |
| 147 | + const req = required.has(name) ? 'yes' : 'no'; |
| 148 | + const ty = prop.type ?? '—'; |
| 149 | + const desc = (prop.description ?? '—').replace(/\|/g, '\\|'); |
| 150 | + return `| \`${name}\` | ${ty} | ${req} | ${desc} |`; |
| 151 | + }); |
| 152 | + |
| 153 | + return [ |
| 154 | + '| Name | Type | Required | Description |', |
| 155 | + '| --- | --- | --- | --- |', |
| 156 | + ...rows, |
| 157 | + '', |
| 158 | + ].join('\n'); |
| 159 | +} |
| 160 | + |
| 161 | +/** |
| 162 | + * Markdown describing MCP tools (from `tools/list`): names, descriptions, |
| 163 | + * parameter tables, JSON Schema, CLI examples, and `tools/call` JSON. |
| 164 | + */ |
| 165 | +export function generateLlmContextMarkdown( |
| 166 | + tools: Tool[], |
| 167 | + options?: GenerateLlmContextOptions |
| 168 | +): string { |
| 169 | + const programName = options?.programName ?? 'bitrefill'; |
| 170 | + const sorted = [...tools].sort((a, b) => a.name.localeCompare(b.name)); |
| 171 | + |
| 172 | + const lines: string[] = [ |
| 173 | + '# Bitrefill MCP — LLM context', |
| 174 | + '', |
| 175 | + 'Generated by `' + |
| 176 | + programName + |
| 177 | + ' llm-context`. Add this to **CLAUDE.md**, **Cursor rules**, or **`.github/copilot-instructions.md`** so agents know how to use the Bitrefill API via MCP or this CLI.', |
| 178 | + '', |
| 179 | + ]; |
| 180 | + |
| 181 | + if (options?.mcpUrl) { |
| 182 | + lines.push('## Connection'); |
| 183 | + lines.push(''); |
| 184 | + lines.push( |
| 185 | + `- MCP URL used for this run: \`${sanitizeMcpUrlForDocs(options.mcpUrl)}\` (override with \`MCP_URL\` or \`--api-key\` / \`BITREFILL_API_KEY\`).` |
| 186 | + ); |
| 187 | + lines.push(''); |
| 188 | + } |
| 189 | + |
| 190 | + lines.push('## Tools'); |
| 191 | + lines.push(''); |
| 192 | + |
| 193 | + for (const tool of sorted) { |
| 194 | + const entries = sortedPropertyEntries(tool); |
| 195 | + lines.push(`### \`${tool.name}\``); |
| 196 | + lines.push(''); |
| 197 | + lines.push(tool.description?.trim() || '_No description._'); |
| 198 | + lines.push(''); |
| 199 | + lines.push('#### Parameters'); |
| 200 | + lines.push(''); |
| 201 | + lines.push(formatParameterTable(tool, entries)); |
| 202 | + lines.push('#### Input schema (JSON Schema)'); |
| 203 | + lines.push(''); |
| 204 | + lines.push('```json'); |
| 205 | + lines.push(JSON.stringify(tool.inputSchema ?? {}, null, 2)); |
| 206 | + lines.push('```'); |
| 207 | + lines.push(''); |
| 208 | + |
| 209 | + if (tool.outputSchema) { |
| 210 | + lines.push('#### Output schema (JSON Schema)'); |
| 211 | + lines.push(''); |
| 212 | + lines.push('```json'); |
| 213 | + lines.push(JSON.stringify(tool.outputSchema, null, 2)); |
| 214 | + lines.push('```'); |
| 215 | + lines.push(''); |
| 216 | + } |
| 217 | + |
| 218 | + lines.push('#### Example: CLI'); |
| 219 | + lines.push(''); |
| 220 | + lines.push('```bash'); |
| 221 | + lines.push(buildCliExample(tool, programName, entries)); |
| 222 | + lines.push('```'); |
| 223 | + lines.push(''); |
| 224 | + lines.push('#### Example: MCP `tools/call`'); |
| 225 | + lines.push(''); |
| 226 | + lines.push('```json'); |
| 227 | + lines.push(buildMcpToolsCallExample(tool, entries)); |
| 228 | + lines.push('```'); |
| 229 | + lines.push(''); |
| 230 | + lines.push('---'); |
| 231 | + lines.push(''); |
| 232 | + } |
| 233 | + |
| 234 | + return lines.join('\n'); |
| 235 | +} |
0 commit comments