← Files Sulekha EventsARCHIVED FILE

source/scripts/debug-query.js

3.27 KB · Nov 30, 2026 · 07:53 UTC

↓ Download file

import { createServer } from 'node:http';
import { spawn } from 'node:child_process';
import { once } from 'node:events';
import { fileURLToPath } from 'node:url';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';

// A local API receiver shows exactly what the real MCP server sends.
// No production credentials or production API calls are used.
const requests = [];
const receiver = createServer((req, res) => {
  const url = new URL(req.url, 'http://localhost');
  requests.push(Object.fromEntries(url.searchParams));
  res.writeHead(200, { 'content-type': 'application/json' });
  res.end('[]');
});
receiver.listen(0, '127.0.0.1');
await once(receiver, 'listening');
const child = spawn(process.execPath, [fileURLToPath(new URL('../server.js', import.meta.url))], {
  env: { ...process.env, NODE_ENV: 'production', HOST: '127.0.0.1', PORT: '0',
    SearchApiAuth__ClientId: 'local-debug', SearchApiAuth__Secret: 'local-debug-only',
    SULEKHA_EVENTS_WEB_BASE_URL: `http://127.0.0.1:${receiver.address().port}`,
    SULEKHA_EVENTS_WEB_SEARCH_PATH: 'capture' },
  stdio: ['ignore', 'pipe', 'pipe']
});
const client = new Client({ name: 'query-debug-demo', version: '1.0.0' });
try {
  const endpoint = await new Promise((resolve, reject) => {
    let output = '';
    const timer = setTimeout(() => reject(new Error('Local server startup timed out')), 10000);
    child.stdout.on('data', chunk => {
      output += chunk;
      const match = output.match(/http:\/\/127\.0\.0\.1:\d+\/mcp/);
      if (match) { clearTimeout(timer); resolve(match[0]); }
    });
    child.once('error', error => { clearTimeout(timer); reject(error); });
    child.once('exit', code => { clearTimeout(timer); reject(new Error(`Server exited: ${code}`)); });
  });
  await client.connect(new StreamableHTTPClientTransport(new URL(endpoint)));
  const examples = [
    { rawQuery: 'show me events in PA', query: '', country: 'US', stateCode: 'PA', latitude: 40.9, longitude: -77.8, distance: 500 },
    { rawQuery: 'Find Sonu Nigam events in NJ', query: 'Sonu Nigam', country: 'US', stateCode: 'NJ', latitude: 40.1, longitude: -74.5, distance: 150 },
    { rawQuery: '  Get தமிழ் music events in Toronto!  ', query: 'தமிழ் music', country: 'CA', cityUrl: 'toronto-on', latitude: 43.6532, longitude: -79.3832 }
  ];
  console.log('LOCAL DEMO: real MCP server → local API receiver (no live API call).');
  console.log('Keyword/location extraction is supplied as demo input; this does not test platform language understanding.');
  for (const args of examples) {
    const result = await client.callTool({ name: 'search_sulekha_events', arguments: args });
    if (result.isError) throw new Error(JSON.stringify(result.content));
    const received = requests.at(-1);
    if (received.rawQuery !== args.rawQuery || received.searchtxt !== args.query) throw new Error('Query changed in transit');
    console.log(JSON.stringify({ toolInput: args, receivedByAPI: { platform: received.platform, rawQuery: received.rawQuery, searchtxt: received.searchtxt, cityurl: received.cityurl, lat: received.lat, long: received.long }, exactMatch: true }, null, 2));
  }
} finally {
  await client.close();
  child.kill();
  receiver.close();
}

SHA-256: 1eb8753a56dd7ee3beb4467d69454b5406bf1b917fc1c12c66466a606b9ac5d8