download_markdown
Convert webpages to markdown files by providing a URL, with options to save in a specified subdirectory and generate date-stamped filenames automatically.
Instructions
Download a webpage as markdown using r.jina.ai
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| subdirectory | No | Optional subdirectory to save the file in | |
| url | Yes | URL of the webpage to download |
Input Schema (JSON Schema)
{
"properties": {
"subdirectory": {
"description": "Optional subdirectory to save the file in",
"type": "string"
},
"url": {
"description": "URL of the webpage to download",
"type": "string"
}
},
"required": [
"url"
],
"type": "object"
}
Implementation Reference
- src/index.ts:187-245 (handler)The execution handler for the 'download_markdown' tool. Validates URL input, prepends r.jina.ai, downloads markdown with axios, generates a sanitized filename with date, saves to configurable directory (optionally subdirectory), and returns success/error message.if (request.params.name === 'download_markdown') { const url = request.params.arguments?.url; const subdirectory = request.params.arguments?.subdirectory; if (!url || typeof url !== 'string') { throw new McpError( ErrorCode.InvalidParams, 'A valid URL must be provided' ); } try { // Get current download directory const config = getConfig(); // Prepend r.jina.ai to the URL const jinaUrl = `https://r.jina.ai/${url}`; // Download markdown const response = await axios.get(jinaUrl, { headers: { 'Accept': 'text/markdown' } }); // Generate filename const filename = generateFilename(url); let filepath = path.join(config.downloadDirectory, filename); // If subdirectory is specified, use it if (subdirectory && typeof subdirectory === 'string') { filepath = path.join(config.downloadDirectory, subdirectory, filename); fs.ensureDirSync(path.dirname(filepath)); } // Save markdown file await fs.writeFile(filepath, response.data); return { content: [ { type: 'text', text: `Markdown downloaded and saved as ${filename} in ${path.dirname(filepath)}` } ] }; } catch (downloadError) { console.error('Download error:', downloadError); return { content: [ { type: 'text', text: `Failed to download markdown: ${downloadError instanceof Error ? downloadError.message : 'Unknown error'}` } ], isError: true }; } }
- src/index.ts:115-131 (registration)Registration of the 'download_markdown' tool in the ListTools response, including its description and input schema definition.name: 'download_markdown', description: 'Download a webpage as markdown using r.jina.ai', inputSchema: { type: 'object', properties: { url: { type: 'string', description: 'URL of the webpage to download' }, subdirectory: { type: 'string', description: 'Optional subdirectory to save the file in' } }, required: ['url'] } },
- src/index.ts:117-130 (schema)Input schema definition for the 'download_markdown' tool, specifying required 'url' and optional 'subdirectory'.inputSchema: { type: 'object', properties: { url: { type: 'string', description: 'URL of the webpage to download' }, subdirectory: { type: 'string', description: 'Optional subdirectory to save the file in' } }, required: ['url'] }
- src/index.ts:77-81 (helper)Helper function to generate a sanitized filename from the URL with a datestamp suffix for the markdown file.function generateFilename(url: string): string { const sanitizedUrl = sanitizeFilename(url); const datestamp = new Date().toISOString().split('T')[0].replace(/-/g, ''); return `${sanitizedUrl}-${datestamp}.md`; }
- src/index.ts:69-75 (helper)Helper function to sanitize URL into a valid filename by removing protocol and replacing special chars.function sanitizeFilename(url: string): string { // Remove protocol, replace non-alphanumeric chars with dash return url .replace(/^https?:\/\//, '') .replace(/[^a-z0-9]/gi, '-') .toLowerCase(); }