Skip to content

Commit ef8c29f

Browse files
committed
added explicit timeout
1 parent 6f30a54 commit ef8c29f

File tree

1 file changed

+51
-5
lines changed

1 file changed

+51
-5
lines changed

src/clients/mongodb-client.ts

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { MongoClient, Db } from "mongodb";
22
import { config } from "dotenv";
33
import mongoose from "mongoose";
44
import { glob } from "glob";
5+
import fs from 'fs';
6+
import path from 'path';
7+
import { tmpdir } from 'os';
58

69

710
config();
@@ -12,6 +15,7 @@ class MongoDBClient {
1215
private db: Db | null = null;
1316
private models: Record<string, mongoose.Model<any>> = {};
1417
private initialized: boolean = false;
18+
private tempSchemaDir: string | null = null;
1519

1620
private constructor() {}
1721

@@ -22,26 +26,62 @@ class MongoDBClient {
2226
return MongoDBClient.instance;
2327
}
2428

29+
private async copySchemaFiles(sourcePath: string): Promise<string> {
30+
const tempDir = path.join(tmpdir(), 'mcp-schemas-' + Date.now());
31+
await fs.promises.mkdir(tempDir, { recursive: true });
32+
33+
const files = await glob(`${sourcePath}/**/*.{js,ts}`);
34+
35+
for (const file of files) {
36+
const relativePath = path.relative(sourcePath, file);
37+
const targetPath = path.join(tempDir, relativePath);
38+
39+
await fs.promises.mkdir(path.dirname(targetPath), { recursive: true });
40+
await fs.promises.copyFile(file, targetPath);
41+
}
42+
43+
return tempDir;
44+
}
45+
46+
private async cleanupTempDir(): Promise<void> {
47+
if (this.tempSchemaDir) {
48+
try {
49+
await fs.promises.rm(this.tempSchemaDir, { recursive: true, force: true });
50+
} catch (error) {
51+
console.error('Error cleaning up temporary schema directory:', error);
52+
}
53+
}
54+
}
55+
2556
public async initialize(): Promise<void> {
2657
if (this.initialized) return;
2758

2859
try {
2960
const uri = process.env.MONGODB_URI || "mongodb://localhost:27017/mcp-database";
3061
const schemaPath = process.env.SCHEMA_PATH;
3162

63+
console.error(`Attempting to connect to MongoDB at: ${uri}`);
64+
console.error(`Schema path: ${schemaPath}`);
65+
3266
// Connect with MongoDB driver
3367
this.mongoClient = new MongoClient(uri);
3468
await this.mongoClient.connect();
3569
this.db = this.mongoClient.db();
3670
console.error(`Connected to MongoDB using native driver`);
3771

3872
// Connect with Mongoose
39-
await mongoose.connect(uri);
73+
await mongoose.connect(uri, {
74+
serverSelectionTimeoutMS: 5000,
75+
socketTimeoutMS: 45000,
76+
});
4077
console.error(`Connected to MongoDB using Mongoose`);
4178

4279
// Load schemas if path is provided
4380
if (schemaPath) {
44-
await this.loadSchemas(schemaPath);
81+
console.error(`Loading schemas from: ${schemaPath}`);
82+
// Copy schema files to temp directory if running via npx
83+
this.tempSchemaDir = await this.copySchemaFiles(schemaPath);
84+
await this.loadSchemas(this.tempSchemaDir);
4585
} else {
4686
console.error("No schema path provided. Running in schemaless mode.");
4787
}
@@ -55,9 +95,11 @@ class MongoDBClient {
5595

5696
private async loadSchemas(schemaPath: string): Promise<void> {
5797
try {
58-
// Use the absolute path directly
98+
console.error(`Starting schema loading from path: ${schemaPath}`);
5999
const schemaFiles = await glob(`${schemaPath}/**/*.{js,ts}`);
60100

101+
console.error(`Found schema files:`, schemaFiles);
102+
61103
if (schemaFiles.length === 0) {
62104
console.error(`No schema files found in ${schemaPath}. Running in schemaless mode.`);
63105
return;
@@ -67,20 +109,23 @@ class MongoDBClient {
67109

68110
for (const file of schemaFiles) {
69111
try {
70-
// Use require for npm package compatibility
112+
console.error(`Attempting to load schema file: ${file}`);
71113
const moduleImport = require(file);
72114
const schema = moduleImport.default;
73115

74116
if (schema && schema.modelName) {
75117
this.models[schema.modelName.toLowerCase()] = schema;
76-
console.error(`Loaded schema for: ${schema.modelName}`);
118+
console.error(`Successfully loaded schema for: ${schema.modelName}`);
119+
} else {
120+
console.error(`Schema file ${file} does not export a valid model`);
77121
}
78122
} catch (err) {
79123
console.error(`Error loading schema file ${file}:`, err);
80124
}
81125
}
82126

83127
console.error(`Loaded ${Object.keys(this.models).length} schemas.`);
128+
console.error(`Available models:`, Object.keys(this.models));
84129
} catch (error) {
85130
console.error("Error loading schemas:", error);
86131
console.error("Running in schemaless mode.");
@@ -133,6 +178,7 @@ class MongoDBClient {
133178
await mongoose.connection.close();
134179
}
135180

181+
await this.cleanupTempDir();
136182
this.initialized = false;
137183
}
138184
}

0 commit comments

Comments
 (0)