#!/usr/bin/env bun
/**
* Simple verification script to test MCP server functionality
* This tests the underlying functions without using the MCP protocol
*/
import { connect, disconnect, findDocuments } from "./src/mongo";
import type { StockData, NewsItem } from "./src/business";
/**
* Converts YYYYMMDD to YYYY-MM-DD (MongoDB stock date format)
*/
function formatStockDate(dateStr: string): string {
if (!/^\d{8}$/.test(dateStr)) {
throw new Error(`Invalid date format: ${dateStr}. Expected YYYYMMDD`);
}
return `${dateStr.slice(0, 4)}-${dateStr.slice(4, 6)}-${dateStr.slice(6, 8)}`;
}
/**
* Converts YYYYMMDD to news time_published format for queries
*/
function formatNewsTime(dateStr: string, isEnd: boolean = false): string {
if (!/^\d{8}$/.test(dateStr)) {
throw new Error(`Invalid date format: ${dateStr}. Expected YYYYMMDD`);
}
return isEnd ? `${dateStr}T2359` : `${dateStr}T0000`;
}
async function verify() {
console.log("Verifying MCP Server Implementation...\n");
const client = await connect();
try {
// Test 1: Get stock prices for AAPL on 20251223
console.log("=== Test 1: Get AAPL stock prices for 20251223 ===");
const dateFormatted = formatStockDate("20251223");
const stockDocs = await findDocuments<StockData>("finance", "stock-AAPL", { date: dateFormatted });
if (stockDocs.length > 0) {
console.log("✓ Found stock data:");
console.log(JSON.stringify(stockDocs[0], null, 2));
} else {
console.log("✗ No stock data found");
}
console.log();
// Test 2: Get latest 100 IBM stock prices
console.log("=== Test 2: Get latest 100 IBM stock prices ===");
const ibmDocs = await findDocuments<StockData>("finance", "stock-IBM", {}, {
sort: { date: -1 },
limit: 100
});
console.log(`✓ Found ${ibmDocs.length} IBM stock records`);
if (ibmDocs.length > 0) {
console.log("Latest record:", JSON.stringify(ibmDocs[0], null, 2));
}
console.log();
// Test 3: Get news between dates (find any date that has news)
console.log("=== Test 3: Get any news articles ===");
const allNews = await findDocuments<NewsItem>("finance", "news", {}, { limit: 1 });
if (allNews.length > 0) {
console.log("✓ Found news data:");
console.log("Sample article:", allNews[0].title);
console.log("Published:", allNews[0].time_published);
// Now test filtering by the date we know has news
const newsDate = allNews[0].time_published.slice(0, 8); // Extract YYYYMMDD
const fromTime = formatNewsTime(newsDate, false);
const toTime = formatNewsTime(newsDate, true);
const filteredNews = await findDocuments<NewsItem>("finance", "news", {
time_published: {
$gte: fromTime,
$lte: toTime
}
});
console.log(`✓ Date filter test: Found ${filteredNews.length} articles for ${newsDate}`);
} else {
console.log("✗ No news data found in database");
}
console.log();
// Test 4: Get news with keyword filter
console.log("=== Test 4: Get news with keyword 'market' ===");
const keywordNews = await findDocuments<NewsItem>("finance", "news", {
$or: [
{ title: { $regex: "market", $options: "i" } },
{ summary: { $regex: "market", $options: "i" } }
]
}, { limit: 5 });
console.log(`✓ Found ${keywordNews.length} articles matching 'market'`);
if (keywordNews.length > 0) {
console.log("First match:", keywordNews[0].title);
}
console.log();
console.log("=== Verification Summary ===");
console.log("✓ MongoDB connection: OK");
console.log("✓ Stock price queries: OK");
console.log("✓ News queries: OK");
console.log("✓ Date formatting: OK");
console.log("✓ Keyword filtering: OK");
console.log("\nAll verifications passed! MCP server should work correctly.");
} finally {
await disconnect(client);
}
}
verify().catch((error) => {
console.error("Verification failed:", error);
process.exit(1);
});