purchase-mcp-server
Version:
Purchase and budget management server handling requisitions, purchase orders, expenses, budgets, and vendor management with ERP access for data extraction
86 lines • 3.13 kB
JavaScript
import { getMongoClient, logger, getConfig } from "syia-mcp-utils";
import { ObjectId } from "mongodb";
export class VendorToolHandler {
constructor() {
// No MongoDB client initialization needed
}
async searchVendors(arguments_) {
const { query, limit = 10 } = arguments_;
try {
const mongoClient = await getMongoClient(getConfig().mongoUri);
const db = mongoClient.db(getConfig().dbName);
const vendors = await db.collection('vendors')
.find({
$or: [
{ name: { $regex: query, $options: 'i' } },
{ category: { $regex: query, $options: 'i' } },
{ location: { $regex: query, $options: 'i' } }
]
})
.limit(limit)
.toArray();
return [{
type: "text",
text: JSON.stringify(vendors, null, 2)
}];
}
catch (error) {
logger.error('Error searching vendors:', error);
throw error;
}
}
async findRelevantVendors(arguments_) {
const { query, category, limit = 10 } = arguments_;
if (!query) {
throw new Error("Query is required");
}
try {
const mongoClient = await getMongoClient();
const db = mongoClient.db(getConfig().dbName);
const collection = db.collection('vendors');
const searchQuery = {
$or: [
{ name: { $regex: query, $options: 'i' } },
{ description: { $regex: query, $options: 'i' } }
]
};
if (category) {
searchQuery.category = category;
}
const vendors = await collection.find(searchQuery)
.sort({ relevance: -1 })
.limit(limit)
.toArray();
return [{
type: "text",
text: JSON.stringify(vendors, null, 2)
}];
}
catch (error) {
logger.error('Error finding relevant vendors:', error);
throw error;
}
}
async getVendorContactInfo(arguments_) {
const { vendorId } = arguments_;
try {
const mongoClient = await getMongoClient(getConfig().mongoUri);
const db = mongoClient.db(getConfig().dbName);
// Using ObjectId from syia-mcp-utils import
const vendor = await db.collection('vendors')
.findOne({ _id: new ObjectId(vendorId) }, { projection: { contactDetails: 1, name: 1 } });
if (!vendor) {
throw new Error(`Vendor not found: ${vendorId}`);
}
return [{
type: "text",
text: JSON.stringify(vendor, null, 2)
}];
}
catch (error) {
logger.error('Error getting vendor contact info:', error);
throw error;
}
}
}
//# sourceMappingURL=vendorTools.js.map