-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp-server.js
More file actions
executable file
·64 lines (53 loc) · 1.87 KB
/
mcp-server.js
File metadata and controls
executable file
·64 lines (53 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env bun
const { Server } = require("@modelcontextprotocol/sdk/server/index.js");
const { StdioServerTransport } = require("@modelcontextprotocol/sdk/server/stdio.js");
const { CallToolRequestSchema, ListToolsRequestSchema } = require("@modelcontextprotocol/sdk/types.js");
const path = require('path');
const { search } = require(path.join(__dirname, 'lib', 'client'));
const server = new Server(
{ name: "isearch", version: "2.1.0" },
{ capabilities: { tools: {} } }
);
server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [{
name: "google_search",
description: "Search Google for real-time information, documentation, or current events.",
inputSchema: {
type: "object",
properties: {
query: { type: "string", description: "The search query" }
},
required: ["query"]
}
}]
}));
server.setRequestHandler(CallToolRequestSchema, async (req) => {
if (req.params.name !== "google_search") {
return { content: [{ type: "text", text: `Unknown tool: ${req.params.name}` }], isError: true };
}
const q = req.params.arguments?.query;
if (!q) {
return { content: [{ type: "text", text: "Query is required" }], isError: true };
}
try {
const r = await search(q);
if (r.error) {
return { content: [{ type: "text", text: `Error: ${r.error}` }], isError: true };
}
let text = r.markdown || 'No results found.';
if (r.timeMs != null) {
text += `\n\n---\n_${r.timeMs}ms${r.fromCache ? ' (cached)' : ''}_`;
}
return { content: [{ type: "text", text }], isError: false };
} catch (e) {
return { content: [{ type: "text", text: `System error: ${e.message}` }], isError: true };
}
});
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
}
main().catch(e => {
console.error('MCP error:', e);
process.exit(1);
});