api-client.ts•2.57 kB
import { QdrantClient } from '@qdrant/js-client-rest';
import { chromium } from 'playwright';
import { McpError, ErrorCode } from '@modelcontextprotocol/sdk/types.js';
import { EmbeddingService } from './embeddings.js';
export class ApiClient {
qdrantClient: QdrantClient;
embeddingService: EmbeddingService;
browser: any;
constructor(qdrantUrl: string, qdrantApiKey: string) {
// Initialize Qdrant client with cloud configuration
this.qdrantClient = new QdrantClient({
url: qdrantUrl,
apiKey: qdrantApiKey,
});
// Initialize embedding service with Ollama provider
this.embeddingService = EmbeddingService.createFromConfig({
provider: 'ollama',
model: 'nomic-embed-text'
});
}
async initBrowser() {
if (!this.browser) {
this.browser = await chromium.launch();
}
}
async cleanup() {
if (this.browser) {
await this.browser.close();
}
}
async getEmbeddings(text: string): Promise<number[]> {
try {
return await this.embeddingService.generateEmbeddings(text);
} catch (error) {
throw new McpError(
ErrorCode.InternalError,
`Failed to generate embeddings: ${error}`
);
}
}
async initCollection(COLLECTION_NAME: string) {
try {
const collections = await this.qdrantClient.getCollections();
const exists = collections.collections.some(c => c.name === COLLECTION_NAME);
if (!exists) {
await this.qdrantClient.createCollection(COLLECTION_NAME, {
vectors: {
size: 768, // nomic-embed-text embedding size
distance: 'Cosine',
},
// Add optimized settings for cloud deployment
optimizers_config: {
default_segment_number: 2,
memmap_threshold: 20000,
},
replication_factor: 2,
});
}
} catch (error) {
if (error instanceof Error) {
if (error.message.includes('unauthorized')) {
throw new McpError(
ErrorCode.InvalidRequest,
'Failed to authenticate with Qdrant cloud. Please check your API key.'
);
} else if (error.message.includes('ECONNREFUSED') || error.message.includes('ETIMEDOUT')) {
throw new McpError(
ErrorCode.InternalError,
'Failed to connect to Qdrant cloud. Please check your QDRANT_URL.'
);
}
}
throw new McpError(
ErrorCode.InternalError,
`Failed to initialize Qdrant cloud collection: ${error}`
);
}
}
}