-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathverify.js
More file actions
291 lines (265 loc) · 9.96 KB
/
Copy pathverify.js
File metadata and controls
291 lines (265 loc) · 9.96 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import fs from "node:fs";
import path from "node:path";
import os from "node:os";
import * as p from "@clack/prompts";
import {
VERSION,
PORTKEY_GATEWAY,
PORTKEY_DASHBOARD,
c,
ok,
err,
warn,
info,
dim,
jsonRead,
mask,
findProjectRoot,
getConfigPath,
readExistingConfig,
settingsReadMcp,
} from "../../utils.js";
/** Minimal JSON-RPC initialize (streamable HTTP) — same shape clients use to open a session. */
const MCP_INIT_BODY = JSON.stringify({
jsonrpc: "2.0",
method: "initialize",
params: {
protocolVersion: "2025-03-26",
capabilities: {},
clientInfo: { name: "portkey-cli-verify", version: VERSION },
},
id: 1,
});
function collectRemoteMcpEntries(projectRoot) {
const out = [];
const claudeJson = path.join(os.homedir(), ".claude.json");
const consider = (label, name, cfg) => {
if (!cfg || typeof cfg.url !== "string" || !cfg.url.trim()) return;
const t = String(cfg.type || "http").toLowerCase();
if (t === "stdio") return;
out.push({ label, name, url: cfg.url.trim(), headers: cfg.headers && typeof cfg.headers === "object" ? cfg.headers : {} });
};
if (projectRoot) {
const mcpJson = path.join(projectRoot, ".mcp.json");
if (fs.existsSync(mcpJson)) {
for (const [name, cfg] of Object.entries(settingsReadMcp(mcpJson)))
consider(`repo .mcp.json`, name, cfg);
}
}
if (fs.existsSync(claudeJson)) {
if (projectRoot) {
for (const [name, cfg] of Object.entries(
settingsReadMcp(claudeJson, { scope: "local", projectPath: projectRoot })
))
consider(`~/.claude.json (this project)`, name, cfg);
}
for (const [name, cfg] of Object.entries(settingsReadMcp(claudeJson, { scope: "user" })))
consider(`~/.claude.json (all projects)`, name, cfg);
}
return out;
}
async function probeOneMcp({ label, name, url, headers }) {
const h = {
"Content-Type": "application/json",
Accept: "application/json, text/event-stream",
...headers,
};
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 15000);
try {
const res = await fetch(url, {
method: "POST",
headers: h,
body: MCP_INIT_BODY,
signal: controller.signal,
});
const text = await res.text();
const lower = text.toLowerCase();
const oauthHint =
lower.includes("authorization_url") ||
lower.includes("authorize") ||
lower.includes("oauth");
let rpcOk = false;
if (res.ok) {
try {
const j = JSON.parse(text);
rpcOk = j && (j.result !== undefined || (j.error === undefined && j.jsonrpc === "2.0"));
} catch {
rpcOk = text.includes('"result"') || text.includes("event:");
}
}
return { status: res.status, ok: res.ok && rpcOk, text: text.slice(0, 400), oauthHint };
} catch (e) {
return {
status: 0,
ok: false,
text: e.name === "AbortError" ? "timeout after 15s" : e.message,
oauthHint: false,
};
} finally {
clearTimeout(timer);
}
}
/**
* POST MCP `initialize` to every remote entry in .mcp.json / ~/.claude.json using saved headers.
* Distinguishes gateway reachability from Claude Code UI state (`/mcp` can still differ).
*/
export async function doVerifyMcp() {
p.intro(`${c.bold}Verifying MCP endpoints${c.reset}`);
const projectRoot = findProjectRoot();
const entries = collectRemoteMcpEntries(projectRoot);
if (entries.length === 0) {
err("No remote MCP servers found. Add some with: portkey mcp add");
p.outro(`${c.dim}Create one at https://app.portkey.ai/mcp-servers${c.reset}`);
return;
}
info(`Checking ${entries.length} remote MCP entr${entries.length === 1 ? "y" : "ies"} (initialize handshake)`);
console.log();
for (const e of entries) {
const where = `${c.dim}[${e.label}]${c.reset}`;
const r = await probeOneMcp(e);
if (r.ok) {
ok(`${e.name} ${where} HTTP ${r.status}`);
} else if (r.status === 401 || r.status === 403) {
err(`${e.name} ${where} HTTP ${r.status} — check x-portkey-api-key and workspace MCP access`);
} else if (r.oauthHint) {
warn(`${e.name} ${where} may need upstream OAuth (Linear/Slack): complete flow in Claude /mcp or Portkey dashboard`);
dim(r.text.slice(0, 200));
} else if (r.status === 0) {
err(`${e.name} ${where} ${r.text}`);
} else {
err(`${e.name} ${where} HTTP ${r.status}`);
dim(r.text.slice(0, 200));
}
}
console.log();
dim("If this passes but Claude shows \"failed\", run: claude --debug (see MCP connection logs)");
p.outro(`${c.dim}Docs: ${PORTKEY_DASHBOARD}/docs/product/mcp-gateway/mcp-clients${c.reset}`);
}
export async function doVerify(args = {}) {
if (args.verifyMcp) return doVerifyMcp();
p.intro(`${c.bold}Verifying Portkey Gateway${c.reset}`);
// ── Resolve API key ───────────────────────────────────────────────────────
let pk = process.env.PORTKEY_API_KEY || process.env.ANTHROPIC_AUTH_TOKEN || "";
if (!pk) {
const projectRoot = findProjectRoot();
const checkOrder = [
getConfigPath("project-local", projectRoot),
getConfigPath("project-shared", projectRoot),
getConfigPath("global", projectRoot),
].filter(Boolean);
for (const f of checkOrder) {
if (!fs.existsSync(f)) continue;
const val = jsonRead(f, "env.ANTHROPIC_AUTH_TOKEN");
if (val) { pk = val; break; }
}
}
if (!pk) {
err("No Portkey API key found — set ANTHROPIC_AUTH_TOKEN in config or run: portkey setup");
return;
}
info(`API key: ${mask(pk)}`);
// ── Resolve routing headers ───────────────────────────────────────────────
let customHeaders = process.env.ANTHROPIC_CUSTOM_HEADERS || "";
if (!customHeaders) {
const projectRoot = findProjectRoot();
const checkOrder = [
getConfigPath("project-local", projectRoot),
getConfigPath("project-shared", projectRoot),
getConfigPath("global", projectRoot),
].filter(Boolean);
for (const f of checkOrder) {
if (!fs.existsSync(f)) continue;
const val = jsonRead(f, "env.ANTHROPIC_CUSTOM_HEADERS");
if (val) { customHeaders = val; break; }
}
}
const routingHeaders = {};
if (customHeaders.includes("x-portkey-config:")) {
const configVal = customHeaders.match(/x-portkey-config:(\S+)/)?.[1] || "";
routingHeaders["x-portkey-config"] = configVal;
info(`Routing via config: ${configVal}`);
} else if (customHeaders.includes("x-portkey-provider:")) {
const provVal = customHeaders.match(/x-portkey-provider:(\S+)/)?.[1] || "";
if (!provVal || provVal === "@") {
err("Provider slug is empty — run: portkey setup");
return;
}
routingHeaders["x-portkey-provider"] = provVal;
info(`Routing via provider: ${provVal}`);
} else {
err("No routing header found (x-portkey-provider or x-portkey-config) — run: portkey setup");
return;
}
// ── Resolve gateway + test model ─────────────────────────────────────────
// `readExistingConfig()` only reads settings.json layers. When the user picked
// location "env" during `portkey setup`, the model lives in their shell rc as
// ANTHROPIC_MODEL — check process.env first so verify works for that path too.
const existing = readExistingConfig();
const gateway = process.env.ANTHROPIC_BASE_URL || existing.gateway || PORTKEY_GATEWAY;
const testModel = process.env.ANTHROPIC_MODEL || existing.model;
if (!testModel) {
err("No model configured — run: portkey setup");
return;
}
info(`Gateway: ${gateway}`);
console.log();
// ── Fire test request ─────────────────────────────────────────────────────
const s = p.spinner();
s.start("Sending test request...");
try {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 15000);
const start = Date.now();
const res = await fetch(`${gateway}/v1/chat/completions`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-portkey-api-key": pk,
Authorization: `Bearer ${pk}`,
...routingHeaders,
},
body: JSON.stringify({
model: testModel,
max_tokens: 5,
messages: [{ role: "user", content: "Say ok" }],
}),
signal: controller.signal,
});
clearTimeout(timeout);
const latencyMs = Date.now() - start;
const status = res.status;
switch (status) {
case 200:
s.stop(`Gateway responded in ${latencyMs}ms`);
ok("Routing works! Claude Code is ready to use through Portkey.");
break;
case 401:
s.stop(`401 — authentication failed (${latencyMs}ms)`);
try { dim((await res.text()).slice(0, 200)); } catch {}
err("Check your Portkey API key or run: portkey setup");
break;
case 422:
s.stop(`422 — Portkey reachable, provider rejected request (${latencyMs}ms)`);
try { dim((await res.text()).slice(0, 200)); } catch {}
ok("Gateway is reachable. The provider may not support this test model.");
break;
case 429:
s.stop(`429 — rate limited (${latencyMs}ms)`);
ok("Gateway is reachable — rate limit hit, but routing works.");
break;
default:
s.stop(`HTTP ${status} (${latencyMs}ms)`);
try { dim((await res.text()).slice(0, 200)); } catch {}
}
} catch (e) {
s.stop("Connection failed");
if (e.name === "AbortError") {
err(`Request timed out after 15s — check gateway: ${gateway}`);
} else {
err(`Could not reach ${gateway}`);
dim(e.message);
}
}
p.outro(`${c.dim}Dashboard: ${PORTKEY_DASHBOARD}/logs${c.reset}`);
}