const http = require('http');
const fs = require('fs');
const path = require('path');
const MCP_PORT = 3000;
const PRESIDENTS_FILE = path.join(__dirname, '../document/presidents.md');
// Helper to send JSON-RPC requests
function sendJsonRpc(method, params) {
return new Promise((resolve, reject) => {
const payload = JSON.stringify({
jsonrpc: "2.0",
method: "tools/call",
params: {
name: method,
arguments: params
},
id: Date.now()
});
const options = {
hostname: '127.0.0.1',
port: MCP_PORT,
path: '/messages',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(payload)
}
};
const req = http.request(options, (res) => {
let body = '';
res.on('data', chunk => body += chunk);
res.on('end', () => {
if (res.statusCode >= 200 && res.statusCode < 300) {
try {
const json = JSON.parse(body);
if (json.error) reject(json.error);
else resolve(json.result);
} catch (e) {
reject(new Error(`Invalid JSON: ${body}`));
}
} else {
reject(new Error(`HTTP Error ${res.statusCode}: ${body}`));
}
});
});
req.on('error', reject);
req.write(payload);
req.end();
});
}
async function main() {
try {
// 1. Read Presidents File
console.log("Reading presidents.md...");
const content = fs.readFileSync(PRESIDENTS_FILE, 'utf-8');
const lines = content.split('\n').filter(line => line.trim().length > 0);
console.log(`Found ${lines.length} lines. Ingesting...`);
// 2. Ingest line by line (or chunks)
// For demonstration, we ingest each president as a separate item to allow specific retrieval
for (const line of lines) {
if (line.trim().startsWith('#')) continue; // Skip header
process.stdout.write(`Adding: ${line.substring(0, 30)}... `);
await sendJsonRpc('save_document', {
content: line.trim(),
document_name: 'document/presidents.md'
});
console.log("OK");
}
console.log("\nIngestion complete. Waiting a moment for DB commit if needed...\n");
await new Promise(r => setTimeout(r, 1000));
// 3. Perform Searches
const queries = [
"Who was the first president?",
"President during the Civil War",
"President in 2005",
"Lincoln"
];
for (const query of queries) {
console.log(`\nQuery: "${query}"`);
const result = await sendJsonRpc('find_documents', {
content: query,
limit: 3
});
// Parse the inner generic content structure of MCP
const textContent = result.content[0].text;
const items = JSON.parse(textContent);
items.forEach((item, idx) => {
console.log(` ${idx + 1}. [${item.distance.toFixed(4)}] ${item.content}`);
});
}
} catch (error) {
console.error("Error:", error);
}
}
main();