#!/usr/bin/env node
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import {
CallToolRequestSchema,
ErrorCode,
ListToolsRequestSchema,
McpError,
} from '@modelcontextprotocol/sdk/types.js';
import axios from 'axios';
import * as cheerio from 'cheerio';
interface SearchResult {
title: string;
url: string;
description: string;
}
const isValidSearchArgs = (args: any): args is { query: string; limit?: number } =>
typeof args === 'object' &&
args !== null &&
typeof args.query === 'string' &&
(args.limit === undefined || typeof args.limit === 'number');
class WebSearchServer {
private server: Server;
constructor() {
this.server = new Server(
{
name: 'web-search',
version: '0.1.0',
},
{
capabilities: {
tools: {},
},
}
);
this.setupToolHandlers();
this.server.onerror = (error) => console.error('[MCP Error]', error);
process.on('SIGINT', async () => {
await this.server.close();
process.exit(0);
});
}
private setupToolHandlers() {
this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [
{
name: 'search',
description: 'Search the web using Google (no API key required)',
inputSchema: {
type: 'object',
properties: {
query: {
type: 'string',
description: 'Search query',
},
limit: {
type: 'number',
description: 'Maximum number of results to return (default: 5)',
minimum: 1,
maximum: 10,
},
},
required: ['query'],
},
},
],
}));
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
if (request.params.name !== 'search') {
throw new McpError(
ErrorCode.MethodNotFound,
`Unknown tool: ${request.params.name}`
);
}
if (!isValidSearchArgs(request.params.arguments)) {
throw new McpError(
ErrorCode.InvalidParams,
'Invalid search arguments'
);
}
const query = request.params.arguments.query;
const limit = Math.min(request.params.arguments.limit || 5, 10);
try {
const results = await this.performSearch(query, limit);
return {
content: [
{
type: 'text',
text: JSON.stringify(results, null, 2),
},
],
};
} catch (error) {
if (axios.isAxiosError(error)) {
return {
content: [
{
type: 'text',
text: `Search error: ${error.message}`,
},
],
isError: true,
};
}
throw error;
}
});
}
private async performSearch(query: string, limit: number): Promise<SearchResult[]> {
// Add a small delay to avoid being detected as a bot
await new Promise(resolve => setTimeout(resolve, 1000 + Math.random() * 1000));
// Try DuckDuckGo first as it's more scraping-friendly
try {
return await this.searchDuckDuckGo(query, limit);
} catch (error) {
console.error('DuckDuckGo search failed, trying Google:', error);
return await this.searchGoogle(query, limit);
}
}
private async searchDuckDuckGo(query: string, limit: number): Promise<SearchResult[]> {
const response = await axios.get('https://html.duckduckgo.com/html/', {
params: { q: query },
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
}
});
const $ = cheerio.load(response.data);
const results: SearchResult[] = [];
$('.result').each((i, element) => {
if (i >= limit) return false;
const titleElement = $(element).find('.result__title a');
const snippetElement = $(element).find('.result__snippet');
const url = titleElement.attr('href');
const title = titleElement.text().trim();
if (url && title) {
results.push({
title: title,
url: url.startsWith('//') ? 'https:' + url : url,
description: snippetElement.text().trim() || '',
});
}
});
return results;
}
private async searchGoogle(query: string, limit: number): Promise<SearchResult[]> {
const response = await axios.get('https://www.google.com/search', {
params: { q: query },
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1'
}
});
const $ = cheerio.load(response.data);
const results: SearchResult[] = [];
// Debug: Log the HTML to see what we're getting
console.error('Response status:', response.status);
console.error('Response length:', response.data.length);
// Try multiple selectors as Google changes them frequently
const searchSelectors = [
'div.g', // Traditional selector
'[data-sokoban-container]', // Newer selector
'.MjjYud', // Another common selector
'.tF2Cxc' // Alternative selector
];
let foundResults = false;
for (const selector of searchSelectors) {
$(selector).each((i, element) => {
if (i >= limit) return false;
// Try multiple title selectors
const titleElement = $(element).find('h3').first() ||
$(element).find('[role="heading"]').first() ||
$(element).find('a h3').first();
// Try multiple link selectors
const linkElement = $(element).find('a[href^="http"]').first() ||
$(element).find('a').first();
// Try multiple snippet selectors
const snippetElement = $(element).find('.VwiC3b').first() ||
$(element).find('[data-snf]').first() ||
$(element).find('.s').first() ||
$(element).find('[style*="line-height"]').first();
if (titleElement.length && linkElement.length) {
const url = linkElement.attr('href');
const title = titleElement.text().trim();
if (url && url.startsWith('http') && title) {
results.push({
title: title,
url: url,
description: snippetElement.text().trim() || '',
});
foundResults = true;
}
}
});
if (foundResults) break; // Stop trying other selectors if we found results
}
// Debug logging
if (results.length === 0) {
console.error('No results found. Available elements:');
console.error('div.g count:', $('div.g').length);
console.error('[data-sokoban-container] count:', $('[data-sokoban-container]').length);
console.error('.MjjYud count:', $('.MjjYud').length);
console.error('.tF2Cxc count:', $('.tF2Cxc').length);
// Log a sample of the HTML for debugging
console.error('Sample HTML (first 1000 chars):', response.data.substring(0, 1000));
}
return results;
}
async run() {
const transport = new StdioServerTransport();
await this.server.connect(transport);
console.error('Web Search MCP server running on stdio');
}
}
const server = new WebSearchServer();
server.run().catch(console.error);