Spaces:
Build error
Build error
| const fs = require('fs'); | |
| const path = require('path'); | |
| const axios = require('axios'); | |
| // MCP Server Functions | |
| async function generateImage(prompt, params = {}) { | |
| const url = `https://image.pollinations.ai/${encodeURIComponent(prompt)}?${new URLSearchParams(params).toString()}`; | |
| const response = await axios.get(url, { responseType: 'arraybuffer' }); | |
| return Buffer.from(response.data).toString('base64'); | |
| } | |
| async function generateText(prompt, model = 'openai') { | |
| const url = `https://text.pollinations.ai/${encodeURIComponent(prompt)}?model=${model}`; | |
| const response = await axios.get(url); | |
| return response.data; | |
| } | |
| async function downloadImage(url, filename) { | |
| const response = await axios.get(url, { responseType: 'arraybuffer' }); | |
| fs.writeFileSync(filename, response.data); | |
| return `Image saved to ${filename}`; | |
| } | |
| // STDIO Listener for MCP | |
| process.stdin.resume(); | |
| process.stdin.setEncoding('utf8'); | |
| let input = ''; | |
| process.stdin.on('data', chunk => { | |
| input += chunk; | |
| }); | |
| process.stdin.on('end', () => { | |
| const request = JSON.parse(input); | |
| let result; | |
| switch (request.tool) { | |
| case 'generate_image': | |
| result = generateImage(request.prompt, request.params); | |
| break; | |
| case 'generate_text': | |
| result = generateText(request.prompt, request.model); | |
| break; | |
| case 'download_image': | |
| result = downloadImage(request.url, request.filename); | |
| break; | |
| default: | |
| result = { error: 'Unknown tool' }; | |
| } | |
| console.log(JSON.stringify(result)); | |
| }); | |
| // For HTTP mode (optional for HF) | |
| const http = require('http'); | |
| const server = http.createServer((req, res) => { | |
| res.writeHead(200, { 'Content-Type': 'text/plain' }); | |
| res.end('Pollinations MCP Server Running'); | |
| }); | |
| server.listen(7860, () => { | |
| console.log('Server listening on port 7860'); | |
| }); | |