Minor updates to duckduckgo
This commit is contained in:
parent
a9c0bafaaf
commit
124ff86101
1 changed files with 87 additions and 78 deletions
|
|
@ -7,10 +7,33 @@ import {
|
||||||
ListResourcesRequestSchema,
|
ListResourcesRequestSchema,
|
||||||
ListToolsRequestSchema,
|
ListToolsRequestSchema,
|
||||||
ReadResourceRequestSchema,
|
ReadResourceRequestSchema,
|
||||||
|
Tool,
|
||||||
} from "@modelcontextprotocol/sdk/types.js";
|
} from "@modelcontextprotocol/sdk/types.js";
|
||||||
import fetch from "node-fetch";
|
import fetch from "node-fetch";
|
||||||
import { JSDOM } from "jsdom";
|
import { JSDOM } from "jsdom";
|
||||||
|
|
||||||
|
const SEARCH_TOOL: Tool = {
|
||||||
|
name: "duckduckgo_search",
|
||||||
|
description:
|
||||||
|
"Performs a search using DuckDuckGo and returns the top search results. " +
|
||||||
|
"Returns titles, snippets, and URLs of the search results. ",
|
||||||
|
inputSchema: {
|
||||||
|
type: "object",
|
||||||
|
properties: {
|
||||||
|
query: {
|
||||||
|
type: "string",
|
||||||
|
description: "The search query to look up",
|
||||||
|
},
|
||||||
|
numResults: {
|
||||||
|
type: "number",
|
||||||
|
description: "Number of results to return (default: 10)",
|
||||||
|
default: 10,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
required: ["query"],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
const server = new Server(
|
const server = new Server(
|
||||||
{
|
{
|
||||||
name: "example-servers/duckduckgo",
|
name: "example-servers/duckduckgo",
|
||||||
|
|
@ -19,14 +42,12 @@ const server = new Server(
|
||||||
{
|
{
|
||||||
capabilities: {
|
capabilities: {
|
||||||
tools: {},
|
tools: {},
|
||||||
resources: {}, // Required since we're using ListResourcesRequestSchema
|
resources: {},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
// Add Resources List Handler - Important for showing up in the tools list
|
server.setRequestHandler(ListResourcesRequestSchema, async () => ({
|
||||||
server.setRequestHandler(ListResourcesRequestSchema, async () => {
|
|
||||||
return {
|
|
||||||
resources: [
|
resources: [
|
||||||
{
|
{
|
||||||
uri: "duckduckgo://search",
|
uri: "duckduckgo://search",
|
||||||
|
|
@ -34,10 +55,8 @@ server.setRequestHandler(ListResourcesRequestSchema, async () => {
|
||||||
name: "DuckDuckGo Search Results",
|
name: "DuckDuckGo Search Results",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
}));
|
||||||
});
|
|
||||||
|
|
||||||
// Add Read Resource Handler
|
|
||||||
server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
|
server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
|
||||||
if (request.params.uri.toString() === "duckduckgo://search") {
|
if (request.params.uri.toString() === "duckduckgo://search") {
|
||||||
return {
|
return {
|
||||||
|
|
@ -53,43 +72,11 @@ server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
|
||||||
throw new Error("Resource not found");
|
throw new Error("Resource not found");
|
||||||
});
|
});
|
||||||
|
|
||||||
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
||||||
return {
|
tools: [SEARCH_TOOL],
|
||||||
tools: [
|
}));
|
||||||
{
|
|
||||||
name: "search",
|
|
||||||
description:
|
|
||||||
"Performs a search using DuckDuckGo and returns the top search results. " +
|
|
||||||
"Returns titles, snippets, and URLs of the search results. " +
|
|
||||||
"Use this tool when you need to search for current information on the internet.",
|
|
||||||
inputSchema: {
|
|
||||||
type: "object",
|
|
||||||
properties: {
|
|
||||||
query: {
|
|
||||||
type: "string",
|
|
||||||
description: "The search query to look up",
|
|
||||||
},
|
|
||||||
numResults: {
|
|
||||||
type: "number",
|
|
||||||
description: "Number of results to return (default: 5)",
|
|
||||||
default: 5,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
required: ["query"],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
||||||
if (request.params.name === "search") {
|
|
||||||
try {
|
|
||||||
const { query, numResults = 5 } = request.params.arguments as {
|
|
||||||
query: string;
|
|
||||||
numResults?: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
async function performSearch(query: string, numResults: number = 10) {
|
||||||
const url = `https://html.duckduckgo.com/html/?q=${encodeURIComponent(query)}`;
|
const url = `https://html.duckduckgo.com/html/?q=${encodeURIComponent(query)}`;
|
||||||
const headers = {
|
const headers = {
|
||||||
"User-Agent":
|
"User-Agent":
|
||||||
|
|
@ -123,6 +110,19 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
||||||
|
if (request.params.name === "duckduckgo_search") {
|
||||||
|
try {
|
||||||
|
const { query, numResults = 10 } = request.params.arguments as {
|
||||||
|
query: string;
|
||||||
|
numResults?: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
const results = await performSearch(query, numResults);
|
||||||
|
|
||||||
const formattedResults = results
|
const formattedResults = results
|
||||||
.map(
|
.map(
|
||||||
(result) =>
|
(result) =>
|
||||||
|
|
@ -137,6 +137,7 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
||||||
text: formattedResults || "No results found.",
|
text: formattedResults || "No results found.",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
isError: false,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return {
|
return {
|
||||||
|
|
@ -151,7 +152,15 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(`Unknown tool: ${request.params.name}`);
|
return {
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: `Unknown tool: ${request.params.name}`,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
isError: true,
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
async function runServer() {
|
async function runServer() {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue