-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
129 lines (114 loc) · 3.75 KB
/
Copy pathserver.js
File metadata and controls
129 lines (114 loc) · 3.75 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import express from "express";
import fetch from "node-fetch";
import cors from "cors";
const app = express();
app.use(cors({
origin: '*',
methods: ['GET', 'POST', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization', 'x-session-id']
}));
app.use(express.json());
const GRAPH = "https://graph.facebook.com/v20.0";
const ACCESS_TOKEN = process.env.META_TOKEN;
app.get("/api/interests", async (req, res) => {
try {
const q = (req.query.q || "").trim();
if (!q) return res.json({ data: [] });
const limit = req.query.limit || '1000';
const url = `${GRAPH}/search?type=adinterest&q=${encodeURIComponent(q)}&limit=${limit}&access_token=${ACCESS_TOKEN}`;
const response = await fetch(url);
const data = await response.json();
const interests = (data.data || []).map(item => ({
id: item.id,
name: item.name,
path: item.path || [],
audience_size_min: item.audience_size_lower_bound || 0,
audience_size_max: item.audience_size_upper_bound || 0
}));
res.json({ data: interests });
} catch (error) {
res.json({ data: [], error: error.message });
}
});
app.get("/mcp", (req, res) => {
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
const sessionId = Math.random().toString(36).slice(2);
res.write(`data: ${JSON.stringify({
jsonrpc: "2.0",
method: "initialize",
params: {
sessionId,
serverInfo: { name: "FB Interest Finder", version: "1.0.0" },
capabilities: { tools: {} }
}
})}\n\n`);
req.on('close', () => res.end());
});
app.post("/mcp", async (req, res) => {
const { method, params, id } = req.body;
if (method === 'initialize') {
return res.json({
jsonrpc: "2.0", id,
result: {
serverInfo: { name: "FB Interest Finder", version: "1.0.0" },
capabilities: { tools: {} },
protocolVersion: "2024-11-05"
}
});
}
if (method === 'tools/list') {
return res.json({
jsonrpc: "2.0", id,
result: {
tools: [{
name: "search_facebook_interests",
description: "Search Facebook interests for Meta ad targeting. Returns interest names and audience sizes.",
inputSchema: {
type: "object",
properties: {
query: {
type: "string",
description: "Interest keyword to search e.g. cricket, crypto, Zerodha"
}
},
required: ["query"]
}
}]
}
});
}
if (method === 'tools/call') {
const query = params?.arguments?.query;
if (!query) {
return res.json({
jsonrpc: "2.0", id,
result: { content: [{ type: "text", text: "Error: query is required" }] }
});
}
try {
const url = `${GRAPH}/search?type=adinterest&q=${encodeURIComponent(query)}&limit=20&access_token=${ACCESS_TOKEN}`;
const response = await fetch(url);
const data = await response.json();
const interests = (data.data || []).map(item => ({
name: item.name,
audience_size: item.audience_size_lower_bound
? `${(item.audience_size_lower_bound / 1000000).toFixed(1)}M - ${(item.audience_size_upper_bound / 1000000).toFixed(1)}M`
: 'Unknown',
path: item.path || []
}));
return res.json({
jsonrpc: "2.0", id,
result: { content: [{ type: "text", text: JSON.stringify(interests, null, 2) }] }
});
} catch (e) {
return res.json({
jsonrpc: "2.0", id,
result: { content: [{ type: "text", text: `Error: ${e.message}` }] }
});
}
}
return res.json({ jsonrpc: "2.0", id, error: { code: -32601, message: "Method not found" } });
});
export default app;