-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp-api-middleware.js
More file actions
219 lines (194 loc) · 6.14 KB
/
mcp-api-middleware.js
File metadata and controls
219 lines (194 loc) · 6.14 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// MCP API Key Middleware - Shared module for all 3 MCP servers
// Validates API keys, enforces tier limits, tracks free tier usage
const fs = require('fs');
const crypto = require('crypto');
const KEYS_FILE = '/root/family-data/mcp-api-keys.json';
const SIGNUP_URL = 'https://api.100levelup.com/mcp-pricing/';
// Tier limits
const TIER_LIMITS = {
free: { daily_calls: 10 },
pro: { daily_calls: 1000 },
team: { daily_calls: 5000 },
enterprise: { daily_calls: 999999 }
};
// Free tool definitions per server
const FREE_TOOLS = {
'levels-of-self': ['get_scenario', 'get_game_info'],
'palyan-ai-ops': ['get_business_ops', 'get_family_info'],
'nervous-system': ['get_framework', 'get_nervous_system_info', 'drift_audit', 'security_audit', 'page_health', 'mcp_analyzer', 'self_check']
};
// Free tier daily limits per server (for free tools, no key needed)
const FREE_TIER_LIMITS = {
'levels-of-self': 10,
'palyan-ai-ops': 5,
'nervous-system': 10
};
function loadKeys() {
try {
return JSON.parse(fs.readFileSync(KEYS_FILE, 'utf8'));
} catch (e) {
return { keys: {}, free_tier_usage: {} };
}
}
function saveKeys(data) {
fs.writeFileSync(KEYS_FILE, JSON.stringify(data, null, 2));
}
function getClientIP(req) {
return req.headers['x-forwarded-for']?.split(',')[0]?.trim() ||
req.headers['x-real-ip'] ||
req.socket?.remoteAddress ||
'unknown';
}
function todayUTC() {
return new Date().toISOString().split('T')[0];
}
// Reset daily counters at midnight UTC
function getFreeTierCount(data, ip, serverName) {
const key = `${ip}:${serverName}`;
const usage = data.free_tier_usage[key];
if (!usage || usage.date !== todayUTC()) {
return 0;
}
return usage.count || 0;
}
function incrementFreeTier(data, ip, serverName) {
const key = `${ip}:${serverName}`;
const today = todayUTC();
if (!data.free_tier_usage[key] || data.free_tier_usage[key].date !== today) {
data.free_tier_usage[key] = { date: today, count: 0 };
}
data.free_tier_usage[key].count++;
saveKeys(data);
}
function getKeyDailyCount(data, apiKey) {
const keyData = data.keys[apiKey];
if (!keyData) return 0;
if (!keyData.usage_date || keyData.usage_date !== todayUTC()) {
return 0;
}
return keyData.usage_count || 0;
}
function incrementKeyUsage(data, apiKey) {
const today = todayUTC();
if (!data.keys[apiKey].usage_date || data.keys[apiKey].usage_date !== today) {
data.keys[apiKey].usage_date = today;
data.keys[apiKey].usage_count = 0;
}
data.keys[apiKey].usage_count++;
saveKeys(data);
}
// Generate a new API key
function generateAPIKey() {
return crypto.randomBytes(32).toString('hex');
}
// Create a new API key entry
function createAPIKey(tier, email, stripeCustomerId) {
const data = loadKeys();
const key = generateAPIKey();
data.keys[key] = {
tier: tier,
daily_calls: TIER_LIMITS[tier]?.daily_calls || TIER_LIMITS.free.daily_calls,
created_at: new Date().toISOString(),
stripe_customer_id: stripeCustomerId || null,
email: email || null,
active: true
};
saveKeys(data);
return key;
}
// Main validation function - call this before handling any tool call
// Returns: { allowed: true } or { allowed: false, status: 401|429, message: string }
function validateRequest(req, serverName, toolName) {
const data = loadKeys();
const ip = getClientIP(req);
// Localhost bypass - internal calls (Tamara, bridge, smoke tests) skip rate limiting
const isLocal = ip === '127.0.0.1' || ip === '::1' || ip === '::ffff:127.0.0.1' || ip === 'localhost';
if (isLocal) {
return { allowed: true, tier: 'internal' };
}
const freeTools = FREE_TOOLS[serverName] || [];
const isFreeTool = freeTools.includes(toolName);
// Get API key from header or query param
const apiKey = req.headers['x-api-key'] ||
new URL(req.url, 'http://localhost').searchParams.get('api_key');
// If API key provided, validate it
if (apiKey) {
const keyData = data.keys[apiKey];
if (!keyData) {
return {
allowed: false,
status: 401,
message: 'Invalid API key. Get a valid key at ' + SIGNUP_URL
};
}
if (!keyData.active) {
return {
allowed: false,
status: 401,
message: 'API key is deactivated. Contact support or get a new key at ' + SIGNUP_URL
};
}
// Check daily limit for this key
const dailyLimit = TIER_LIMITS[keyData.tier]?.daily_calls || keyData.daily_calls;
const currentCount = getKeyDailyCount(data, apiKey);
if (currentCount >= dailyLimit) {
return {
allowed: false,
status: 429,
message: `Daily limit reached (${dailyLimit} calls/day for ${keyData.tier} tier). Resets at midnight UTC. Upgrade at ${SIGNUP_URL}`
};
}
// Valid key, within limits
incrementKeyUsage(data, apiKey);
return { allowed: true, tier: keyData.tier };
}
// No API key - check if this is a free tool
if (!isFreeTool) {
return {
allowed: false,
status: 401,
message: `The tool "${toolName}" requires an API key. Free tools for this server: ${freeTools.join(', ')}. Get your API key at ${SIGNUP_URL}`
};
}
// Free tool, check rate limit by IP
const freeLimit = FREE_TIER_LIMITS[serverName] || 10;
const freeCount = getFreeTierCount(data, ip, serverName);
if (freeCount >= freeLimit) {
return {
allowed: false,
status: 429,
message: `Free tier limit reached (${freeLimit} calls/day). Get an API key for unlimited access at ${SIGNUP_URL}`
};
}
// Free tool within limits
incrementFreeTier(data, ip, serverName);
return { allowed: true, tier: 'free' };
}
// MCP error response for blocked requests
function mcpErrorResponse(id, validation) {
return JSON.stringify({
jsonrpc: '2.0',
id,
result: {
content: [{
type: 'text',
text: JSON.stringify({
error: validation.status === 429 ? 'rate_limit_exceeded' : 'authentication_required',
message: validation.message,
upgrade_url: SIGNUP_URL
}, null, 2)
}]
}
});
}
module.exports = {
validateRequest,
mcpErrorResponse,
createAPIKey,
generateAPIKey,
loadKeys,
saveKeys,
FREE_TOOLS,
TIER_LIMITS,
SIGNUP_URL
};