fix: adjust websearch tool to emphasize that it ISNT 2024, give more info as to current date
This commit is contained in:
@@ -36,109 +36,115 @@ interface McpSearchResponse {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const WebSearchTool = Tool.define("websearch", {
|
export const WebSearchTool = Tool.define("websearch", async () => {
|
||||||
description: DESCRIPTION,
|
return {
|
||||||
parameters: z.object({
|
get description() {
|
||||||
query: z.string().describe("Websearch query"),
|
return DESCRIPTION.replace("{{date}}", new Date().toISOString().slice(0, 10))
|
||||||
numResults: z.number().optional().describe("Number of search results to return (default: 8)"),
|
},
|
||||||
livecrawl: z
|
parameters: z.object({
|
||||||
.enum(["fallback", "preferred"])
|
query: z.string().describe("Websearch query"),
|
||||||
.optional()
|
numResults: z.number().optional().describe("Number of search results to return (default: 8)"),
|
||||||
.describe(
|
livecrawl: z
|
||||||
"Live crawl mode - 'fallback': use live crawling as backup if cached content unavailable, 'preferred': prioritize live crawling (default: 'fallback')",
|
.enum(["fallback", "preferred"])
|
||||||
),
|
.optional()
|
||||||
type: z
|
.describe(
|
||||||
.enum(["auto", "fast", "deep"])
|
"Live crawl mode - 'fallback': use live crawling as backup if cached content unavailable, 'preferred': prioritize live crawling (default: 'fallback')",
|
||||||
.optional()
|
),
|
||||||
.describe("Search type - 'auto': balanced search (default), 'fast': quick results, 'deep': comprehensive search"),
|
type: z
|
||||||
contextMaxCharacters: z
|
.enum(["auto", "fast", "deep"])
|
||||||
.number()
|
.optional()
|
||||||
.optional()
|
.describe(
|
||||||
.describe("Maximum characters for context string optimized for LLMs (default: 10000)"),
|
"Search type - 'auto': balanced search (default), 'fast': quick results, 'deep': comprehensive search",
|
||||||
}),
|
),
|
||||||
async execute(params, ctx) {
|
contextMaxCharacters: z
|
||||||
await ctx.ask({
|
.number()
|
||||||
permission: "websearch",
|
.optional()
|
||||||
patterns: [params.query],
|
.describe("Maximum characters for context string optimized for LLMs (default: 10000)"),
|
||||||
always: ["*"],
|
}),
|
||||||
metadata: {
|
async execute(params, ctx) {
|
||||||
query: params.query,
|
await ctx.ask({
|
||||||
numResults: params.numResults,
|
permission: "websearch",
|
||||||
livecrawl: params.livecrawl,
|
patterns: [params.query],
|
||||||
type: params.type,
|
always: ["*"],
|
||||||
contextMaxCharacters: params.contextMaxCharacters,
|
metadata: {
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
const searchRequest: McpSearchRequest = {
|
|
||||||
jsonrpc: "2.0",
|
|
||||||
id: 1,
|
|
||||||
method: "tools/call",
|
|
||||||
params: {
|
|
||||||
name: "web_search_exa",
|
|
||||||
arguments: {
|
|
||||||
query: params.query,
|
query: params.query,
|
||||||
type: params.type || "auto",
|
numResults: params.numResults,
|
||||||
numResults: params.numResults || API_CONFIG.DEFAULT_NUM_RESULTS,
|
livecrawl: params.livecrawl,
|
||||||
livecrawl: params.livecrawl || "fallback",
|
type: params.type,
|
||||||
contextMaxCharacters: params.contextMaxCharacters,
|
contextMaxCharacters: params.contextMaxCharacters,
|
||||||
},
|
},
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
const controller = new AbortController()
|
|
||||||
const timeoutId = setTimeout(() => controller.abort(), 25000)
|
|
||||||
|
|
||||||
try {
|
|
||||||
const headers: Record<string, string> = {
|
|
||||||
accept: "application/json, text/event-stream",
|
|
||||||
"content-type": "application/json",
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await fetch(`${API_CONFIG.BASE_URL}${API_CONFIG.ENDPOINTS.SEARCH}`, {
|
|
||||||
method: "POST",
|
|
||||||
headers,
|
|
||||||
body: JSON.stringify(searchRequest),
|
|
||||||
signal: AbortSignal.any([controller.signal, ctx.abort]),
|
|
||||||
})
|
})
|
||||||
|
|
||||||
clearTimeout(timeoutId)
|
const searchRequest: McpSearchRequest = {
|
||||||
|
jsonrpc: "2.0",
|
||||||
if (!response.ok) {
|
id: 1,
|
||||||
const errorText = await response.text()
|
method: "tools/call",
|
||||||
throw new Error(`Search error (${response.status}): ${errorText}`)
|
params: {
|
||||||
|
name: "web_search_exa",
|
||||||
|
arguments: {
|
||||||
|
query: params.query,
|
||||||
|
type: params.type || "auto",
|
||||||
|
numResults: params.numResults || API_CONFIG.DEFAULT_NUM_RESULTS,
|
||||||
|
livecrawl: params.livecrawl || "fallback",
|
||||||
|
contextMaxCharacters: params.contextMaxCharacters,
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
const responseText = await response.text()
|
const controller = new AbortController()
|
||||||
|
const timeoutId = setTimeout(() => controller.abort(), 25000)
|
||||||
|
|
||||||
// Parse SSE response
|
try {
|
||||||
const lines = responseText.split("\n")
|
const headers: Record<string, string> = {
|
||||||
for (const line of lines) {
|
accept: "application/json, text/event-stream",
|
||||||
if (line.startsWith("data: ")) {
|
"content-type": "application/json",
|
||||||
const data: McpSearchResponse = JSON.parse(line.substring(6))
|
}
|
||||||
if (data.result && data.result.content && data.result.content.length > 0) {
|
|
||||||
return {
|
const response = await fetch(`${API_CONFIG.BASE_URL}${API_CONFIG.ENDPOINTS.SEARCH}`, {
|
||||||
output: data.result.content[0].text,
|
method: "POST",
|
||||||
title: `Web search: ${params.query}`,
|
headers,
|
||||||
metadata: {},
|
body: JSON.stringify(searchRequest),
|
||||||
|
signal: AbortSignal.any([controller.signal, ctx.abort]),
|
||||||
|
})
|
||||||
|
|
||||||
|
clearTimeout(timeoutId)
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const errorText = await response.text()
|
||||||
|
throw new Error(`Search error (${response.status}): ${errorText}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const responseText = await response.text()
|
||||||
|
|
||||||
|
// Parse SSE response
|
||||||
|
const lines = responseText.split("\n")
|
||||||
|
for (const line of lines) {
|
||||||
|
if (line.startsWith("data: ")) {
|
||||||
|
const data: McpSearchResponse = JSON.parse(line.substring(6))
|
||||||
|
if (data.result && data.result.content && data.result.content.length > 0) {
|
||||||
|
return {
|
||||||
|
output: data.result.content[0].text,
|
||||||
|
title: `Web search: ${params.query}`,
|
||||||
|
metadata: {},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
output: "No search results found. Please try a different query.",
|
output: "No search results found. Please try a different query.",
|
||||||
title: `Web search: ${params.query}`,
|
title: `Web search: ${params.query}`,
|
||||||
metadata: {},
|
metadata: {},
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
clearTimeout(timeoutId)
|
clearTimeout(timeoutId)
|
||||||
|
|
||||||
if (error instanceof Error && error.name === "AbortError") {
|
if (error instanceof Error && error.name === "AbortError") {
|
||||||
throw new Error("Search request timed out")
|
throw new Error("Search request timed out")
|
||||||
}
|
}
|
||||||
|
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -9,3 +9,6 @@ Usage notes:
|
|||||||
- Search types: 'auto' (balanced), 'fast' (quick results), 'deep' (comprehensive search)
|
- Search types: 'auto' (balanced), 'fast' (quick results), 'deep' (comprehensive search)
|
||||||
- Configurable context length for optimal LLM integration
|
- Configurable context length for optimal LLM integration
|
||||||
- Domain filtering and advanced search options available
|
- Domain filtering and advanced search options available
|
||||||
|
|
||||||
|
Today's date is {{date}}. You MUST use this year when searching for recent information or current events
|
||||||
|
- Example: If today is 2025-07-15 and the user asks for "latest AI news", search for "AI news 2025", NOT "AI news 2024"
|
||||||
|
|||||||
Reference in New Issue
Block a user