cd ~/mcp-platform/orchestrator

cat > index.js << 'EOF'
import express from 'express';
const app = express();
app.use(express.json());

const S2_API_KEY = "s2k-zQ59GazS7hOCxietPyjWfq1I9daEXiZOanAqHniN";
const BRAVE_API_KEY = "BSAcKz8Yw7wHwvrAtusBzmH3fvm52UM";

async function searchPapers(query) {
    const url = `https://api.semanticscholar.org/graph/v1/paper/search?query=${encodeURIComponent(query)}&limit=5&fields=title,year`;
    const response = await fetch(url, { headers: { 'x-api-key': S2_API_KEY } });
    const data = await response.json();
    return data.data || [];
}

async function webSearch(query) {
    const url = `https://api.search.brave.com/res/v1/web/search?q=${encodeURIComponent(query)}&count=5`;
    const response = await fetch(url, { headers: { 'X-Subscription-Token': BRAVE_API_KEY } });
    const data = await response.json();
    return data.web?.results || [];
}

// Simulación de búsqueda de patentes (mientras scholar-mcp se estabiliza)
async function patentSearch(query) {
    return [{ title: `Patente simulada para: ${query}`, year: '2024' }];
}

app.get('/health', (req, res) => res.json({ status: 'ok' }));

app.post('/mcp', async (req, res) => {
    const { method, params, id } = req.body;
    console.log('Request:', method, id);
    
    if (method === 'tools/list') {
        return res.json({
            jsonrpc: '2.0',
            id: id,
            result: {
                tools: [
                    { name: 'ping', description: 'Pong' },
                    { name: 'search_papers', description: 'Search academic papers' },
                    { name: 'web_search', description: 'Web search' },
                    { name: 'search_patents', description: 'Search patents (simulado)' }
                ]
            }
        });
    }
    
    if (method === 'tools/call') {
        const name = params?.name;
        const args = params?.arguments || {};
        
        if (name === 'ping') {
            return res.json({ jsonrpc: '2.0', id: id, result: { content: [{ type: 'text', text: 'pong' }] } });
        }
        
        if (name === 'search_papers') {
            try {
                const papers = await searchPapers(args.query);
                let text = 'Papers found: ' + papers.length + '\n';
                for (const p of papers.slice(0, 5)) {
                    text += '- ' + p.title + ' (' + (p.year || 'N/A') + ')\n';
                }
                return res.json({ jsonrpc: '2.0', id: id, result: { content: [{ type: 'text', text: text }] } });
            } catch (err) {
                return res.json({ jsonrpc: '2.0', id: id, result: { content: [{ type: 'text', text: 'Error: ' + err.message }], isError: true } });
            }
        }
        
        if (name === 'web_search') {
            try {
                const results = await webSearch(args.query);
                let text = 'Web results: ' + results.length + '\n';
                for (const r of results.slice(0, 5)) {
                    text += '- ' + r.title + '\n';
                }
                return res.json({ jsonrpc: '2.0', id: id, result: { content: [{ type: 'text', text: text }] } });
            } catch (err) {
                return res.json({ jsonrpc: '2.0', id: id, result: { content: [{ type: 'text', text: 'Error: ' + err.message }], isError: true } });
            }
        }
        
        if (name === 'search_patents') {
            try {
                const patents = await patentSearch(args.query);
                let text = 'Patents found: ' + patents.length + '\n';
                for (const p of patents.slice(0, 5)) {
                    text += '- ' + p.title + ' (' + (p.year || 'N/A') + ')\n';
                }
                return res.json({ jsonrpc: '2.0', id: id, result: { content: [{ type: 'text', text: text }] } });
            } catch (err) {
                return res.json({ jsonrpc: '2.0', id: id, result: { content: [{ type: 'text', text: 'Error: ' + err.message }], isError: true } });
            }
        }
    }
    
    res.json({ jsonrpc: '2.0', id: id, error: { code: -32600, message: 'Method not found' } });
});

app.listen(3000, '0.0.0.0', () => console.log('Server running on port 3000'));
EOF

# Verificar sintaxis
node -c index.js

# Reconstruir
cd ~/mcp-platform
docker compose build orchestrator --no-cache
docker compose up -d orchestrator

# Probar
sleep 3
echo "=== Tools List ==="
curl -X POST http://localhost:3000/mcp -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"tools/list","id":1}'

echo -e "\n=== Search Papers ==="
curl -X POST http://localhost:3000/mcp -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"tools/call","params":{"name":"search_papers","arguments":{"query":"machine learning"}},"id":20}'

echo -e "\n=== Web Search ==="
curl -X POST http://localhost:3000/mcp -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"tools/call","params":{"name":"web_search","arguments":{"query":"latest tech"}},"id":21}'

echo -e "\n=== Patent Search (simulado) ==="
curl -X POST http://localhost:3000/mcp -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"tools/call","params":{"name":"search_patents","arguments":{"query":"graphene"}},"id":30}'