Spaces:
Build error
Build error
Create index.js
Browse files
index.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const fs = require('fs');
|
| 2 |
+
const path = require('path');
|
| 3 |
+
const axios = require('axios');
|
| 4 |
+
|
| 5 |
+
// MCP Server Functions
|
| 6 |
+
async function generateImage(prompt, params = {}) {
|
| 7 |
+
const url = `https://image.pollinations.ai/${encodeURIComponent(prompt)}?${new URLSearchParams(params).toString()}`;
|
| 8 |
+
const response = await axios.get(url, { responseType: 'arraybuffer' });
|
| 9 |
+
return Buffer.from(response.data).toString('base64');
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
async function generateText(prompt, model = 'openai') {
|
| 13 |
+
const url = `https://text.pollinations.ai/${encodeURIComponent(prompt)}?model=${model}`;
|
| 14 |
+
const response = await axios.get(url);
|
| 15 |
+
return response.data;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
async function downloadImage(url, filename) {
|
| 19 |
+
const response = await axios.get(url, { responseType: 'arraybuffer' });
|
| 20 |
+
fs.writeFileSync(filename, response.data);
|
| 21 |
+
return `Image saved to ${filename}`;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
// STDIO Listener for MCP
|
| 25 |
+
process.stdin.resume();
|
| 26 |
+
process.stdin.setEncoding('utf8');
|
| 27 |
+
|
| 28 |
+
let input = '';
|
| 29 |
+
process.stdin.on('data', chunk => {
|
| 30 |
+
input += chunk;
|
| 31 |
+
});
|
| 32 |
+
|
| 33 |
+
process.stdin.on('end', () => {
|
| 34 |
+
const request = JSON.parse(input);
|
| 35 |
+
let result;
|
| 36 |
+
switch (request.tool) {
|
| 37 |
+
case 'generate_image':
|
| 38 |
+
result = generateImage(request.prompt, request.params);
|
| 39 |
+
break;
|
| 40 |
+
case 'generate_text':
|
| 41 |
+
result = generateText(request.prompt, request.model);
|
| 42 |
+
break;
|
| 43 |
+
case 'download_image':
|
| 44 |
+
result = downloadImage(request.url, request.filename);
|
| 45 |
+
break;
|
| 46 |
+
default:
|
| 47 |
+
result = { error: 'Unknown tool' };
|
| 48 |
+
}
|
| 49 |
+
console.log(JSON.stringify(result));
|
| 50 |
+
});
|
| 51 |
+
|
| 52 |
+
// For HTTP mode (optional for HF)
|
| 53 |
+
const http = require('http');
|
| 54 |
+
const server = http.createServer((req, res) => {
|
| 55 |
+
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
| 56 |
+
res.end('Pollinations MCP Server Running');
|
| 57 |
+
});
|
| 58 |
+
server.listen(7860, () => {
|
| 59 |
+
console.log('Server listening on port 7860');
|
| 60 |
+
});
|