UNPKG

@iriseller/mcp-server

Version:

Model Context Protocol (MCP) server providing access to IRISeller's AI sales intelligence platform with 7 AI agents, multi-CRM integration, advanced sales workflows, email automation (detection/sending/campaigns), and robust asynchronous agent execution h

93 lines (90 loc) 3.88 kB
import axios from 'axios'; export class WebSearchService { claudeApiKey; claudeApiUrl = 'https://api.anthropic.com/v1/messages'; constructor() { this.claudeApiKey = process.env.ANTHROPIC_API_KEY || process.env.CLAUDE_API_KEY || ''; if (!this.claudeApiKey) { console.warn('[WebSearchService] No Claude API key found. Web search will not be available.'); } } async search(request) { if (!this.claudeApiKey) { throw new Error('Claude API key not configured. Please set ANTHROPIC_API_KEY or CLAUDE_API_KEY environment variable.'); } const { query, max_uses = 3, allowed_domains, blocked_domains, user_location } = request; try { // Use Claude's native web search capability instead of invalid tool // Make a simple request without tools and let Claude search naturally const searchPrompt = `Please search the web for: ${query} Search for the most current and accurate information${allowed_domains?.length ? ` from these domains: ${allowed_domains.join(', ')}` : ''}. Provide a comprehensive summary with specific details and cite your sources.`; // Make request to Claude API without invalid tools const response = await axios.post(this.claudeApiUrl, { model: "claude-sonnet-4-20250514", // Claude Sonnet 4.0 max_tokens: 4000, // Increased for Claude 4.0 capabilities temperature: 0.2, // Slightly higher for more creative search responses messages: [ { role: "user", content: searchPrompt } ] }, { headers: { 'Content-Type': 'application/json', 'x-api-key': this.claudeApiKey, 'anthropic-version': '2023-06-01' }, timeout: 60000 }); const result = response.data; const content = result.content?.[0]?.text || ''; // Extract citations if available const citations = this.extractCitations(result); return { query, results: citations, answer: content, citations, provider: 'claude_web_search', timestamp: new Date().toISOString() }; } catch (error) { console.error('[WebSearchService] Claude API error:', error.response?.data || error.message); if (error.response?.status === 401) { throw new Error('Invalid Claude API key. Please check your ANTHROPIC_API_KEY environment variable.'); } else if (error.response?.status === 429) { throw new Error('Claude API rate limit exceeded. Please try again later.'); } else if (error.response?.status === 400) { throw new Error(`Claude API request error: ${error.response.data?.error?.message || 'Invalid request'}`); } throw new Error(`Web search failed: ${error.message}`); } } extractCitations(result) { // Extract citations from Claude's response // This will depend on the actual response format from Claude's web search const citations = []; if (result.content) { result.content.forEach((content) => { if (content.citations) { citations.push(...content.citations); } }); } return citations; } isAvailable() { return !!this.claudeApiKey; } getConfiguration() { return { hasApiKey: !!this.claudeApiKey, model: "claude-3-5-sonnet-latest" }; } }