-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
97 lines (82 loc) · 2.65 KB
/
server.js
File metadata and controls
97 lines (82 loc) · 2.65 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const express = require('express');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
// Tool definitions
const tools = [
{
name: 'search',
description: 'Search for information on a topic',
endpoint: '/tools/search',
method: 'POST',
parameters: {
query: { type: 'string', description: 'Search query', required: true }
}
},
{
name: 'calculate',
description: 'Perform basic math calculations',
endpoint: '/tools/calculate',
method: 'POST',
parameters: {
expression: { type: 'string', description: 'Math expression (e.g. "2 + 2")', required: true }
}
}
];
// List available tools
app.get('/tools', (req, res) => {
res.json({ tools });
});
// Search tool (mock)
app.post('/tools/search', (req, res) => {
const { query } = req.body;
if (!query || !query.trim()) {
return res.status(400).json({ error: 'Query is required' });
}
// Mock search results
const results = [
{
title: `Result 1 for "${query}"`,
snippet: `This is a relevant result about ${query}. It contains useful information that an AI agent can use.`,
url: `https://example.com/result-1?q=${encodeURIComponent(query)}`
},
{
title: `Result 2 for "${query}"`,
snippet: `Another result about ${query} with additional context and details.`,
url: `https://example.com/result-2?q=${encodeURIComponent(query)}`
},
{
title: `Result 3 for "${query}"`,
snippet: `A third resource about ${query} providing a different perspective.`,
url: `https://example.com/result-3?q=${encodeURIComponent(query)}`
}
];
res.json({ query, results });
});
// Calculate tool
app.post('/tools/calculate', (req, res) => {
const { expression } = req.body;
if (!expression || !expression.trim()) {
return res.status(400).json({ error: 'Expression is required' });
}
try {
// Only allow safe math characters
const sanitized = expression.replace(/[^0-9+\-*/().%\s]/g, '');
if (sanitized !== expression.trim()) {
return res.status(400).json({ error: 'Invalid characters in expression' });
}
// Evaluate the math expression
const result = Function('"use strict"; return (' + sanitized + ')')();
if (typeof result !== 'number' || !isFinite(result)) {
return res.status(400).json({ error: 'Invalid expression' });
}
res.json({ expression, result });
} catch (err) {
res.status(400).json({ error: 'Failed to evaluate expression' });
}
});
app.listen(PORT, () => {
console.log(`Tool server running on port ${PORT}`);
});