Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
PORT=3100
API_KEYS=your-api-key-1,your-api-key-2
NODE_ENV=production

# Search provider: "duckduckgo" (default) or "tavily"
SEARCH_PROVIDER=duckduckgo
# Required when SEARCH_PROVIDER=tavily
TAVILY_API_KEY=
147 changes: 142 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@hono/node-server": "^1.19.9",
"@modelcontextprotocol/sdk": "^1.27.1",
"@mozilla/readability": "^0.6.0",
"@tavily/core": "^0.6.4",
"@types/better-sqlite3": "^7.6.13",
"@types/jsdom": "^28.0.0",
"@types/node": "^25.3.3",
Expand Down
6 changes: 3 additions & 3 deletions src/mcp-server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
import { searchDuckDuckGo } from "./services/search.js";
import { search } from "./services/search.js";
import { extractContent } from "./services/extract.js";
import { getWeather } from "./services/weather.js";
import { getStockQuote, getExchangeRate } from "./services/finance.js";
Expand All @@ -15,14 +15,14 @@ const server = new McpServer({
// Search tool
server.tool(
"search",
"Search the web using DuckDuckGo",
"Search the web (DuckDuckGo or Tavily depending on SEARCH_PROVIDER)",
{
query: z.string().describe("Search query"),
count: z.number().min(1).max(10).default(5).describe("Number of results"),
lang: z.string().default("en").describe("Language code"),
},
async ({ query, count, lang }) => {
const results = await searchDuckDuckGo({ query, count, lang });
const results = await search({ query, count, lang });
return {
content: [{ type: "text" as const, text: JSON.stringify(results, null, 2) }],
};
Expand Down
6 changes: 3 additions & 3 deletions src/mcp-sse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
import { z } from "zod";
import type { IncomingMessage, ServerResponse } from "node:http";

import { searchDuckDuckGo } from "./services/search.js";
import { search } from "./services/search.js";
import { extractContent } from "./services/extract.js";
import { getWeather } from "./services/weather.js";
import { getStockQuote, getExchangeRate } from "./services/finance.js";
Expand All @@ -28,10 +28,10 @@ function createMcpServer(): McpServer {

server.tool(
"search",
"Search the web using DuckDuckGo",
"Search the web (DuckDuckGo or Tavily depending on SEARCH_PROVIDER)",
{ query: z.string(), count: z.number().min(1).max(10).default(5), lang: z.string().default("en") },
async ({ query, count, lang }) => ({
content: [{ type: "text" as const, text: JSON.stringify(await searchDuckDuckGo({ query, count, lang }), null, 2) }],
content: [{ type: "text" as const, text: JSON.stringify(await search({ query, count, lang }), null, 2) }],
})
);

Expand Down
4 changes: 2 additions & 2 deletions src/routes/search.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Hono } from "hono";
import { searchDuckDuckGo } from "../services/search.js";
import { search } from "../services/search.js";
import { successResponse, errorResponse } from "../utils/response.js";
import { requireString, optionalNumber, optionalString } from "../utils/validation.js";

Expand All @@ -15,7 +15,7 @@ searchRouter.post("/v1/search", async (c) => {
const count = optionalNumber(body.count, 5, 1, 10);
const lang = optionalString(body.lang, "en");

const results = await searchDuckDuckGo({ query, count, lang });
const results = await search({ query, count, lang });
return c.json(successResponse(results, endpoint, startTime));
} catch (err) {
const message = err instanceof Error ? err.message : "Search failed";
Expand Down
30 changes: 30 additions & 0 deletions src/services/search.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { tavily } from "@tavily/core";

export interface SearchResult {
title: string;
url: string;
Expand All @@ -10,6 +12,34 @@ export interface SearchInput {
lang?: string;
}

/**
* Provider-aware search dispatch.
* Uses Tavily when SEARCH_PROVIDER=tavily and TAVILY_API_KEY is set,
* otherwise falls back to DuckDuckGo.
*/
export async function search(input: SearchInput): Promise<SearchResult[]> {
const provider = (process.env.SEARCH_PROVIDER || "duckduckgo").toLowerCase();
if (provider === "tavily" && process.env.TAVILY_API_KEY) {
return searchTavily(input);
}
return searchDuckDuckGo(input);
}

export async function searchTavily(input: SearchInput): Promise<SearchResult[]> {
const { query, count = 5 } = input;
const client = tavily({ apiKey: process.env.TAVILY_API_KEY! });
const response = await client.search(query, {
maxResults: count,
searchDepth: "basic",
topic: "general",
});
return (response.results || []).map((r: { title: string; url: string; content: string }) => ({
title: r.title,
url: r.url,
snippet: r.content,
}));
}

export async function searchDuckDuckGo(input: SearchInput): Promise<SearchResult[]> {
const { query, count = 5, lang = "en" } = input;

Expand Down