@tonyclaw/llm-inspector 1.13.0 → 1.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. package/.output/nitro.json +1 -1
  2. package/.output/public/assets/index-B5q3Llgm.css +1 -0
  3. package/.output/public/assets/index-C6tbslcs.js +105 -0
  4. package/.output/public/assets/{main-C3tLo75s.js → main-C1k6vRnH.js} +1 -1
  5. package/.output/server/_libs/cfworker__json-schema.mjs +1 -0
  6. package/.output/server/_libs/lucide-react.mjs +60 -54
  7. package/.output/server/_libs/modelcontextprotocol__server.mjs +9738 -0
  8. package/.output/server/_libs/zod.mjs +79 -16
  9. package/.output/server/_ssr/{index-C8VC13EA.mjs → index-AxruZp16.mjs} +495 -77
  10. package/.output/server/_ssr/index.mjs +2 -2
  11. package/.output/server/_ssr/{router-D5ccnemB.mjs → router-DtleGqN8.mjs} +650 -29
  12. package/.output/server/_tanstack-start-manifest_v-B1WAHWIa.mjs +4 -0
  13. package/.output/server/index.mjs +26 -26
  14. package/README.md +98 -0
  15. package/package.json +3 -1
  16. package/src/components/proxy-viewer/CompareDrawer.tsx +583 -91
  17. package/src/components/proxy-viewer/formats/openai/ResponseView.tsx +74 -4
  18. package/src/lib/serverPort.ts +41 -0
  19. package/src/mcp/loopback.ts +76 -0
  20. package/src/mcp/previewExtractor.ts +166 -0
  21. package/src/mcp/server.ts +320 -0
  22. package/src/mcp/toolHandlers.ts +259 -0
  23. package/src/proxy/formats/openai/schemas.ts +19 -0
  24. package/src/proxy/handler.ts +23 -2
  25. package/src/proxy/openaiOrphanToolStrip.ts +148 -0
  26. package/src/proxy/schemas.ts +1 -0
  27. package/src/routes/api/mcp.ts +25 -0
  28. package/.output/public/assets/index-B0anmGQr.css +0 -1
  29. package/.output/public/assets/index-H_thmL2_.js +0 -105
  30. package/.output/server/_tanstack-start-manifest_v-DUbXa1lt.mjs +0 -4
@@ -8,7 +8,8 @@ import { C as Conf } from "../_libs/conf.mjs";
8
8
  import { randomUUID } from "crypto";
9
9
  import { exec } from "node:child_process";
10
10
  import { promisify } from "node:util";
11
- import { o as object, s as string, _ as _enum, u as union, a as array, b as boolean, n as number, d as discriminatedUnion, l as literal, r as record, c as _null, e as lazy, f as unknown } from "../_libs/zod.mjs";
11
+ import { M as McpServer, W as WebStandardStreamableHTTPServerTransport } from "../_libs/modelcontextprotocol__server.mjs";
12
+ import { d as object, b as string, _ as _enum, u as union, a as array, c as boolean, n as number, g as discriminatedUnion, l as literal, r as record, h as _null, k as lazy, e as unknown } from "../_libs/zod.mjs";
12
13
  import "../_libs/tiny-warning.mjs";
13
14
  import "../_libs/tanstack__router-core.mjs";
14
15
  import "../_libs/cookie-es.mjs";
@@ -44,8 +45,8 @@ import "../_libs/debounce-fn.mjs";
44
45
  import "../_libs/mimic-function.mjs";
45
46
  import "../_libs/semver.mjs";
46
47
  import "../_libs/uint8array-extras.mjs";
47
- const appCss = "/assets/index-B0anmGQr.css";
48
- const Route$h = createRootRoute({
48
+ const appCss = "/assets/index-B5q3Llgm.css";
49
+ const Route$i = createRootRoute({
49
50
  head: () => ({
50
51
  meta: [
51
52
  { charSet: "utf-8" },
@@ -68,8 +69,8 @@ function RootDocument({ children }) {
68
69
  ] })
69
70
  ] });
70
71
  }
71
- const $$splitComponentImporter = () => import("./index-C8VC13EA.mjs");
72
- const Route$g = createFileRoute("/")({
72
+ const $$splitComponentImporter = () => import("./index-AxruZp16.mjs");
73
+ const Route$h = createFileRoute("/")({
73
74
  component: lazyRouteComponent($$splitComponentImporter, "component")
74
75
  });
75
76
  const LOG_DIR_ENV = process.env["LOG_DIR"];
@@ -546,9 +547,19 @@ const OpenAIFunctionCall = object({
546
547
  name: string(),
547
548
  arguments: string()
548
549
  });
550
+ const OpenAIToolCallSchema = object({
551
+ index: number().optional(),
552
+ id: string().optional(),
553
+ type: literal("function").optional(),
554
+ function: object({
555
+ name: string().optional(),
556
+ arguments: string().optional()
557
+ })
558
+ });
549
559
  OpenAIMessage.extend({
550
560
  content: union([string(), array(object({ type: literal("text"), text: string() }))]).optional(),
551
- function_call: OpenAIFunctionCall.optional()
561
+ function_call: OpenAIFunctionCall.optional(),
562
+ tool_calls: array(OpenAIToolCallSchema).optional()
552
563
  });
553
564
  const OpenAIToolDefinition = object({
554
565
  type: literal("function"),
@@ -603,7 +614,8 @@ const OpenAIChoice = object({
603
614
  // Some providers use 'thinking' field in message
604
615
  think: string().optional(),
605
616
  // MiniMax uses 'think' field in message
606
- function_call: object({ name: string(), arguments: string() }).nullable().optional()
617
+ function_call: object({ name: string(), arguments: string() }).nullable().optional(),
618
+ tool_calls: array(OpenAIToolCallSchema).optional()
607
619
  }).optional(),
608
620
  delta: OpenAIChoiceDelta.optional(),
609
621
  finish_reason: string().nullable()
@@ -2056,7 +2068,7 @@ function isClaudeCodeBillingHeaderBlock(text) {
2056
2068
  const trimmed = text.trimStart();
2057
2069
  return trimmed.toLowerCase().startsWith(BILLING_HEADER_PREFIX);
2058
2070
  }
2059
- function getOwnProperty(obj, key) {
2071
+ function getOwnProperty$1(obj, key) {
2060
2072
  const desc = Object.getOwnPropertyDescriptor(obj, key);
2061
2073
  return desc?.value;
2062
2074
  }
@@ -2066,8 +2078,8 @@ function isObjectWithSystem(value) {
2066
2078
  }
2067
2079
  function isBillingHeaderTextBlock(block) {
2068
2080
  if (block === null || typeof block !== "object" || Array.isArray(block)) return false;
2069
- const typeVal = getOwnProperty(block, "type");
2070
- const textVal = getOwnProperty(block, "text");
2081
+ const typeVal = getOwnProperty$1(block, "type");
2082
+ const textVal = getOwnProperty$1(block, "text");
2071
2083
  if (typeof typeVal !== "string" || typeVal !== "text") return false;
2072
2084
  if (typeof textVal !== "string") return false;
2073
2085
  return isClaudeCodeBillingHeaderBlock(textVal);
@@ -2105,6 +2117,82 @@ function stripClaudeCodeBillingHeader(rawBody) {
2105
2117
  }
2106
2118
  return { body: JSON.stringify(parsed), removed };
2107
2119
  }
2120
+ const ROLE_ASSISTANT = "assistant";
2121
+ const ROLE_TOOL = "tool";
2122
+ function getOwnProperty(obj, key) {
2123
+ const desc = Object.getOwnPropertyDescriptor(obj, key);
2124
+ return desc?.value;
2125
+ }
2126
+ function isPlainObject(value) {
2127
+ return typeof value === "object" && value !== null && !Array.isArray(value);
2128
+ }
2129
+ function isObjectWithMessages(value) {
2130
+ if (!isPlainObject(value)) return false;
2131
+ if (!Object.prototype.hasOwnProperty.call(value, "messages")) return false;
2132
+ const messages = getOwnProperty(value, "messages");
2133
+ return Array.isArray(messages);
2134
+ }
2135
+ function collectAssistantToolCallIds(message, into) {
2136
+ const toolCalls = getOwnProperty(message, "tool_calls");
2137
+ if (!Array.isArray(toolCalls)) return;
2138
+ for (const tc of toolCalls) {
2139
+ if (!isPlainObject(tc)) continue;
2140
+ const id = getOwnProperty(tc, "id");
2141
+ if (typeof id === "string" && id.length > 0) {
2142
+ into.add(id);
2143
+ }
2144
+ }
2145
+ }
2146
+ function findOrphanToolMessageIndices(messages) {
2147
+ const knownToolCallIds = /* @__PURE__ */ new Set();
2148
+ const indices = [];
2149
+ const orphanIds = [];
2150
+ for (let i = 0; i < messages.length; i++) {
2151
+ const msg = messages[i];
2152
+ if (!isPlainObject(msg)) continue;
2153
+ const role = getOwnProperty(msg, "role");
2154
+ if (role === ROLE_ASSISTANT) {
2155
+ collectAssistantToolCallIds(msg, knownToolCallIds);
2156
+ continue;
2157
+ }
2158
+ if (role === ROLE_TOOL) {
2159
+ const id = getOwnProperty(msg, "tool_call_id");
2160
+ if (typeof id !== "string" || id.length === 0) {
2161
+ indices.push(i);
2162
+ orphanIds.push(null);
2163
+ continue;
2164
+ }
2165
+ if (!knownToolCallIds.has(id)) {
2166
+ indices.push(i);
2167
+ orphanIds.push(id);
2168
+ }
2169
+ }
2170
+ }
2171
+ return { indices, orphanIds };
2172
+ }
2173
+ function stripOpenAIOrphanToolMessages(rawBody) {
2174
+ let parsed;
2175
+ try {
2176
+ parsed = JSON.parse(rawBody);
2177
+ } catch {
2178
+ return { body: rawBody, removed: 0, orphanIds: [] };
2179
+ }
2180
+ if (!isObjectWithMessages(parsed)) {
2181
+ return { body: rawBody, removed: 0, orphanIds: [] };
2182
+ }
2183
+ const messages = parsed.messages;
2184
+ const { indices, orphanIds } = findOrphanToolMessageIndices(messages);
2185
+ if (indices.length === 0) {
2186
+ return { body: rawBody, removed: 0, orphanIds: [] };
2187
+ }
2188
+ const dropSet = new Set(indices);
2189
+ const kept = [];
2190
+ for (let i = 0; i < messages.length; i++) {
2191
+ if (!dropSet.has(i)) kept.push(messages[i]);
2192
+ }
2193
+ parsed.messages = kept;
2194
+ return { body: JSON.stringify(parsed), removed: indices.length, orphanIds };
2195
+ }
2108
2196
  function describeApiRoute(apiPath) {
2109
2197
  const endpointPath = apiPath.split("?")[0] ?? "";
2110
2198
  const isChatCompletions = endpointPath === PATH_CHAT_COMPLETIONS || endpointPath === PATH_V1_CHAT_COMPLETIONS;
@@ -2196,10 +2284,13 @@ function buildFileLogEntry(log, upstreamUrl) {
2196
2284
  }
2197
2285
  function parseRequestPath(req, url) {
2198
2286
  const route = describeApiRoute(getProxyApiPath(url));
2199
- const isMessages = req.method === "POST" && (route.endpointPath === PATH_V1_MESSAGES || route.isChatCompletions);
2287
+ const isPost = req.method === "POST";
2288
+ const isChatCompletions = isPost && route.isChatCompletions;
2289
+ const isMessages = isPost && (route.endpointPath === PATH_V1_MESSAGES || route.isChatCompletions);
2200
2290
  return {
2201
2291
  apiPath: route.apiPath,
2202
2292
  isMessages,
2293
+ isChatCompletions,
2203
2294
  normalizedPath: route.normalizedPath
2204
2295
  };
2205
2296
  }
@@ -2299,6 +2390,15 @@ async function handleProxy(req) {
2299
2390
  bodyToForward = stripped.body;
2300
2391
  }
2301
2392
  }
2393
+ if (bodyToForward !== null && parsed.isChatCompletions) {
2394
+ const stripped = stripOpenAIOrphanToolMessages(bodyToForward);
2395
+ if (stripped.removed > 0) {
2396
+ logger.warn(
2397
+ `[handler] Dropped ${stripped.removed} orphan OpenAI tool message(s) with tool_call_id(s) ${JSON.stringify(stripped.orphanIds)} — the client sent a tool result with no matching assistant.tool_calls`
2398
+ );
2399
+ bodyToForward = stripped.body;
2400
+ }
2401
+ }
2302
2402
  const { model, sessionId } = extractRequestMetadata(requestBody, req.headers);
2303
2403
  const matchedProviderConfig = model !== null ? findProviderByModel(model) : null;
2304
2404
  const route = describeApiRoute(parsed.apiPath);
@@ -2372,7 +2472,7 @@ async function handleProxy(req) {
2372
2472
  }
2373
2473
  return handleStreamingResponse(upstreamRes, req, startTime, formatHandler, upstreamUrl, log);
2374
2474
  }
2375
- const Route$f = createFileRoute("/proxy/$")({
2475
+ const Route$g = createFileRoute("/proxy/$")({
2376
2476
  server: {
2377
2477
  handlers: {
2378
2478
  GET: ({ request }) => handleProxy(request),
@@ -2384,7 +2484,7 @@ const Route$f = createFileRoute("/proxy/$")({
2384
2484
  }
2385
2485
  }
2386
2486
  });
2387
- const Route$e = createFileRoute("/api/sessions")({
2487
+ const Route$f = createFileRoute("/api/sessions")({
2388
2488
  server: {
2389
2489
  handlers: {
2390
2490
  GET: () => Response.json(getSessions())
@@ -2401,7 +2501,7 @@ const ProviderInputSchema = object({
2401
2501
  authHeader: _enum(["bearer", "x-api-key"]).optional().default("bearer"),
2402
2502
  apiDocsUrl: string().optional()
2403
2503
  });
2404
- const Route$d = createFileRoute("/api/providers")({
2504
+ const Route$e = createFileRoute("/api/providers")({
2405
2505
  server: {
2406
2506
  handlers: {
2407
2507
  GET: () => {
@@ -2426,13 +2526,528 @@ const Route$d = createFileRoute("/api/providers")({
2426
2526
  }
2427
2527
  }
2428
2528
  });
2429
- const Route$c = createFileRoute("/api/models")({
2529
+ const Route$d = createFileRoute("/api/models")({
2430
2530
  server: {
2431
2531
  handlers: {
2432
2532
  GET: () => Response.json(getModels())
2433
2533
  }
2434
2534
  }
2435
2535
  });
2536
+ let overridePort = null;
2537
+ function setCurrentPort(port) {
2538
+ if (!Number.isInteger(port) || port <= 0 || port > 65535) {
2539
+ throw new Error(`setCurrentPort: invalid port ${port}`);
2540
+ }
2541
+ overridePort = port;
2542
+ }
2543
+ function getCurrentPort() {
2544
+ if (overridePort !== null) return overridePort;
2545
+ const envPort = process.env["PORT"];
2546
+ if (envPort !== void 0 && envPort !== "") {
2547
+ const n = Number(envPort);
2548
+ if (Number.isInteger(n) && n > 0 && n <= 65535) return n;
2549
+ }
2550
+ throw new Error(
2551
+ "Inspector server port not initialized: PORT env var is unset and setCurrentPort() has not been called"
2552
+ );
2553
+ }
2554
+ const DEFAULT_TIMEOUT_MS = 3e4;
2555
+ class LoopbackTimeoutError extends Error {
2556
+ constructor(path2, timeoutMs) {
2557
+ super(`Loopback call to ${path2} timed out after ${timeoutMs}ms`);
2558
+ this.path = path2;
2559
+ this.timeoutMs = timeoutMs;
2560
+ this.name = "LoopbackTimeoutError";
2561
+ }
2562
+ }
2563
+ async function callApi(path2, options = {}) {
2564
+ if (!path2.startsWith("/")) {
2565
+ throw new Error(`callApi: path must start with '/', got: ${path2}`);
2566
+ }
2567
+ const port = getCurrentPort();
2568
+ const url = `http://127.0.0.1:${port}${path2}`;
2569
+ const { timeoutMs = DEFAULT_TIMEOUT_MS, signal: userSignal, ...rest } = options;
2570
+ const controller = new AbortController();
2571
+ const timeoutHandle = setTimeout(() => controller.abort(), timeoutMs);
2572
+ if (userSignal !== void 0 && userSignal !== null) {
2573
+ if (userSignal.aborted) {
2574
+ controller.abort();
2575
+ } else {
2576
+ userSignal.addEventListener("abort", () => controller.abort(), { once: true });
2577
+ }
2578
+ }
2579
+ try {
2580
+ return await fetch(url, { ...rest, signal: controller.signal });
2581
+ } catch (err) {
2582
+ if (err instanceof Error && err.name === "AbortError") {
2583
+ throw new LoopbackTimeoutError(path2, timeoutMs);
2584
+ }
2585
+ throw err;
2586
+ } finally {
2587
+ clearTimeout(timeoutHandle);
2588
+ }
2589
+ }
2590
+ const PREVIEW_MAX_CHARS = 500;
2591
+ function truncate(text) {
2592
+ return text.length <= PREVIEW_MAX_CHARS ? text : text.slice(0, PREVIEW_MAX_CHARS);
2593
+ }
2594
+ function firstAnthropicText(content) {
2595
+ if (typeof content === "string") {
2596
+ return content.length > 0 ? content : null;
2597
+ }
2598
+ for (const block of content) {
2599
+ if (block.type === "text" && typeof block.text === "string" && block.text.length > 0) {
2600
+ return block.text;
2601
+ }
2602
+ }
2603
+ return null;
2604
+ }
2605
+ function firstOpenAIText(content) {
2606
+ if (content === null || content === void 0) return null;
2607
+ if (typeof content === "string") {
2608
+ return content.length > 0 ? content : null;
2609
+ }
2610
+ for (const part of content) {
2611
+ if (part.type === "text" && typeof part.text === "string" && part.text.length > 0) {
2612
+ return part.text;
2613
+ }
2614
+ }
2615
+ return null;
2616
+ }
2617
+ function extractLastUserMessagePreview(log) {
2618
+ if (log.apiFormat === "unknown") return null;
2619
+ if (log.rawRequestBody === null) return null;
2620
+ let json;
2621
+ try {
2622
+ json = JSON.parse(log.rawRequestBody);
2623
+ } catch {
2624
+ return null;
2625
+ }
2626
+ if (log.apiFormat === "anthropic") {
2627
+ const parsed = AnthropicRequestSchema.safeParse(json);
2628
+ if (!parsed.success) return null;
2629
+ for (let i = parsed.data.messages.length - 1; i >= 0; i--) {
2630
+ const msg = parsed.data.messages[i];
2631
+ if (msg && msg.role === "user") {
2632
+ const text = firstAnthropicText(msg.content);
2633
+ return text === null ? null : truncate(text);
2634
+ }
2635
+ }
2636
+ return null;
2637
+ }
2638
+ if (log.apiFormat === "openai") {
2639
+ const parsed = OpenAIRequestSchema.safeParse(json);
2640
+ if (!parsed.success) return null;
2641
+ for (let i = parsed.data.messages.length - 1; i >= 0; i--) {
2642
+ const msg = parsed.data.messages[i];
2643
+ if (msg && msg.role === "user") {
2644
+ const text = firstOpenAIText(msg.content);
2645
+ return text === null ? null : truncate(text);
2646
+ }
2647
+ }
2648
+ return null;
2649
+ }
2650
+ return null;
2651
+ }
2652
+ function extractResponsePreview(log) {
2653
+ if (log.apiFormat === "unknown") return null;
2654
+ if (log.responseText === null) return null;
2655
+ let json;
2656
+ try {
2657
+ json = JSON.parse(log.responseText);
2658
+ } catch {
2659
+ return null;
2660
+ }
2661
+ if (log.apiFormat === "anthropic") {
2662
+ const parsed = AnthropicResponseSchema$1.safeParse(json);
2663
+ if (!parsed.success) return null;
2664
+ for (const block of parsed.data.content) {
2665
+ if (block.type === "text" && typeof block.text === "string" && block.text.length > 0) {
2666
+ return truncate(block.text);
2667
+ }
2668
+ }
2669
+ return null;
2670
+ }
2671
+ if (log.apiFormat === "openai") {
2672
+ const parsed = OpenAIResponseSchema$1.safeParse(json);
2673
+ if (!parsed.success) return null;
2674
+ for (const choice of parsed.data.choices) {
2675
+ const msg = choice.message;
2676
+ if (!msg) continue;
2677
+ const direct = firstOpenAIText(msg.content);
2678
+ if (direct !== null) return truncate(direct);
2679
+ const fallback = msg.reasoning_content ?? msg.thinking ?? msg.think;
2680
+ if (typeof fallback === "string" && fallback.length > 0) return truncate(fallback);
2681
+ }
2682
+ return null;
2683
+ }
2684
+ return null;
2685
+ }
2686
+ const LogsListResponseSchema = object({
2687
+ logs: array(CapturedLogSchema).optional(),
2688
+ total: number().optional(),
2689
+ offset: number().optional(),
2690
+ limit: number().optional()
2691
+ });
2692
+ const PAGINATION_DEFAULTS = { offset: 0, limit: 3 };
2693
+ const LIMIT_HARD_CAP = 5;
2694
+ function clampLimit(input) {
2695
+ if (input === void 0) return PAGINATION_DEFAULTS.limit;
2696
+ if (!Number.isFinite(input) || input < 1) return 1;
2697
+ return Math.min(Math.floor(input), LIMIT_HARD_CAP);
2698
+ }
2699
+ function textJson(data) {
2700
+ return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
2701
+ }
2702
+ function toolError(message) {
2703
+ return { content: [{ type: "text", text: message }], isError: true };
2704
+ }
2705
+ function buildLogSummary(log) {
2706
+ const hasError = log.error !== null && log.error !== void 0 && log.error.length > 0 || log.responseStatus !== null && log.responseStatus !== void 0 && log.responseStatus >= 400;
2707
+ return {
2708
+ id: log.id,
2709
+ timestamp: log.timestamp,
2710
+ provider: log.providerName ?? null,
2711
+ model: log.model,
2712
+ apiFormat: log.apiFormat,
2713
+ method: log.method,
2714
+ path: log.path,
2715
+ status: log.responseStatus,
2716
+ isStreaming: log.streaming,
2717
+ hasError,
2718
+ latencyMs: log.elapsedMs,
2719
+ tokens: {
2720
+ input: log.inputTokens,
2721
+ output: log.outputTokens,
2722
+ cacheCreate: log.cacheCreationInputTokens,
2723
+ cacheRead: log.cacheReadInputTokens
2724
+ },
2725
+ sessionId: log.sessionId,
2726
+ clientPid: log.clientPid ?? null,
2727
+ lastUserMessagePreview: extractLastUserMessagePreview(log),
2728
+ responsePreview: extractResponsePreview(log),
2729
+ hasChunks: log.streamingChunks !== void 0 && log.streamingChunks !== null || log.streamingChunksPath !== null && log.streamingChunksPath !== void 0,
2730
+ hasRawRequestBody: log.rawRequestBody !== null && log.rawRequestBody !== void 0
2731
+ };
2732
+ }
2733
+ async function listLogsImpl(callApi2, args) {
2734
+ const offset = args.offset ?? PAGINATION_DEFAULTS.offset;
2735
+ const limit = clampLimit(args.limit);
2736
+ const params = new URLSearchParams({ offset: String(offset), limit: String(limit) });
2737
+ if (args.sessionId !== void 0 && args.sessionId.length > 0) {
2738
+ params.set("sessionId", args.sessionId);
2739
+ }
2740
+ if (args.model !== void 0 && args.model.length > 0) {
2741
+ params.set("model", args.model);
2742
+ }
2743
+ const res = await callApi2(`/api/logs?${params.toString()}`);
2744
+ if (!res.ok) return toolError(`GET /api/logs returned ${res.status}`);
2745
+ const rawBody = await res.json();
2746
+ const parsed = LogsListResponseSchema.safeParse(rawBody);
2747
+ if (!parsed.success) return toolError("GET /api/logs returned an unparseable shape");
2748
+ const logs = parsed.data.logs ?? [];
2749
+ return textJson(logs.map(buildLogSummary));
2750
+ }
2751
+ async function getLogImpl(callApi2, id) {
2752
+ const res = await callApi2(`/api/logs/${id}`);
2753
+ if (!res.ok) return toolError(`GET /api/logs/${id} returned ${res.status}`);
2754
+ const rawBody = await res.json();
2755
+ const parsed = CapturedLogSchema.safeParse(rawBody);
2756
+ if (!parsed.success) {
2757
+ return toolError(`GET /api/logs/${id} returned an unparseable CapturedLog`);
2758
+ }
2759
+ const { streamingChunks: _chunks, ...rest } = parsed.data;
2760
+ return textJson(rest);
2761
+ }
2762
+ async function getLogChunksImpl(callApi2, id) {
2763
+ const res = await callApi2(`/api/logs/${id}/chunks`);
2764
+ if (!res.ok) return toolError(`GET /api/logs/${id}/chunks returned ${res.status}`);
2765
+ return textJson(await res.json());
2766
+ }
2767
+ async function listSessionsImpl(callApi2) {
2768
+ const res = await callApi2("/api/sessions");
2769
+ if (!res.ok) return toolError(`GET /api/sessions returned ${res.status}`);
2770
+ return textJson(await res.json());
2771
+ }
2772
+ async function listModelsImpl(callApi2) {
2773
+ const res = await callApi2("/api/models");
2774
+ if (!res.ok) return toolError(`GET /api/models returned ${res.status}`);
2775
+ return textJson(await res.json());
2776
+ }
2777
+ async function listProvidersImpl(callApi2) {
2778
+ const res = await callApi2("/api/providers");
2779
+ if (!res.ok) return toolError(`GET /api/providers returned ${res.status}`);
2780
+ return textJson(await res.json());
2781
+ }
2782
+ async function getProviderImpl(callApi2, id) {
2783
+ const res = await callApi2(`/api/providers/${encodeURIComponent(id)}`);
2784
+ if (!res.ok) return toolError(`GET /api/providers/${id} returned ${res.status}`);
2785
+ return textJson(await res.json());
2786
+ }
2787
+ async function replayLogImpl(callApi2, args) {
2788
+ const logRes = await callApi2(`/api/logs/${args.id}`);
2789
+ if (!logRes.ok) return toolError(`GET /api/logs/${args.id} returned ${logRes.status}`);
2790
+ const logRaw = await logRes.json();
2791
+ const logParsed = CapturedLogSchema.safeParse(logRaw);
2792
+ if (!logParsed.success) {
2793
+ return toolError(`GET /api/logs/${args.id} returned an unparseable CapturedLog`);
2794
+ }
2795
+ const log = logParsed.data;
2796
+ if (log.rawRequestBody === null) return toolError("Log has no rawRequestBody to replay");
2797
+ const replayRes = await callApi2(`/api/logs/${args.id}/replay`, {
2798
+ method: "POST",
2799
+ headers: { "content-type": "application/json" },
2800
+ body: JSON.stringify({ modifiedBody: log.rawRequestBody })
2801
+ });
2802
+ if (!replayRes.ok) {
2803
+ return toolError(`POST /api/logs/${args.id}/replay returned ${replayRes.status}`);
2804
+ }
2805
+ return textJson(await replayRes.json());
2806
+ }
2807
+ async function addProviderImpl(callApi2, provider) {
2808
+ const res = await callApi2("/api/providers", {
2809
+ method: "POST",
2810
+ headers: { "content-type": "application/json" },
2811
+ body: JSON.stringify(provider)
2812
+ });
2813
+ if (!res.ok) return toolError(`POST /api/providers returned ${res.status}`);
2814
+ return textJson(await res.json());
2815
+ }
2816
+ async function updateProviderImpl(callApi2, input) {
2817
+ const { id, ...patch } = input;
2818
+ const res = await callApi2(`/api/providers/${encodeURIComponent(id)}`, {
2819
+ method: "PUT",
2820
+ headers: { "content-type": "application/json" },
2821
+ body: JSON.stringify(patch)
2822
+ });
2823
+ if (!res.ok) return toolError(`PUT /api/providers/${id} returned ${res.status}`);
2824
+ return textJson(await res.json());
2825
+ }
2826
+ async function testProviderImpl(callApi2, id) {
2827
+ const res = await callApi2(`/api/providers/${encodeURIComponent(id)}/test`, {
2828
+ method: "POST"
2829
+ });
2830
+ if (!res.ok) return toolError(`POST /api/providers/${id}/test returned ${res.status}`);
2831
+ return textJson(await res.json());
2832
+ }
2833
+ async function safeCall(fn) {
2834
+ try {
2835
+ return await fn();
2836
+ } catch (err) {
2837
+ if (err instanceof LoopbackTimeoutError) {
2838
+ return toolError(`Internal loopback call timed out after ${err.timeoutMs}ms: ${err.path}`);
2839
+ }
2840
+ const message = err instanceof Error ? err.message : String(err);
2841
+ return toolError(`Tool execution failed: ${message}`);
2842
+ }
2843
+ }
2844
+ let initialized = null;
2845
+ let initPromise = null;
2846
+ function buildServer() {
2847
+ const server = new McpServer(
2848
+ { name: "llm-inspector", version: "1.0.0" },
2849
+ { capabilities: { tools: {} } }
2850
+ );
2851
+ registerTools(server);
2852
+ const transport = new WebStandardStreamableHTTPServerTransport({
2853
+ sessionIdGenerator: void 0
2854
+ // stateless — see module docstring
2855
+ });
2856
+ void server.connect(transport);
2857
+ return { server, transport };
2858
+ }
2859
+ async function getServer() {
2860
+ if (initialized !== null) return initialized;
2861
+ if (initPromise === null) {
2862
+ initPromise = Promise.resolve(buildServer()).then((pair) => {
2863
+ initialized = pair;
2864
+ return pair;
2865
+ });
2866
+ }
2867
+ return initPromise;
2868
+ }
2869
+ async function handleMcpRequest(request) {
2870
+ try {
2871
+ const url = new URL(request.url);
2872
+ const port = Number(url.port);
2873
+ if (port > 0) setCurrentPort(port);
2874
+ } catch {
2875
+ }
2876
+ const { transport } = await getServer();
2877
+ return transport.handleRequest(request);
2878
+ }
2879
+ const TOOL_LIST_LOGS_DESC = `List recent captured LLM proxy logs in reverse-chronological order with parsed previews. Useful for "what did I just send?" discovery.
2880
+
2881
+ REFLEXIVE-LOOP WARNING: this list includes the agent's own recent /proxy calls. If results appear to be the agent's own tool calls (e.g., the agent called inspector_list_logs and now sees itself in the results), filter by clientPid (your own PID) or by timestamp on the client side.
2882
+
2883
+ Parameters:
2884
+ - offset (number, default 0): skip this many entries from the start of the filtered list
2885
+ - limit (number, default 3, hard-clamped to 5): how many summaries to return
2886
+ - sessionId (string, optional): filter by session/affinity id
2887
+ - model (string, optional): filter by model name
2888
+
2889
+ Returns: array of "thick summary" objects, each containing:
2890
+ - id, timestamp, provider, model, apiFormat, method, path, status, isStreaming,
2891
+ hasError, latencyMs, tokens { input, output, cacheCreate, cacheRead },
2892
+ sessionId, clientPid,
2893
+ - lastUserMessagePreview (string|null, ≤500 chars) — first text block of the last user message, parsed via the format-specific schema (NOT a raw body slice)
2894
+ - responsePreview (string|null, ≤500 chars) — first text block of the assistant response
2895
+ - hasChunks, hasRawRequestBody (booleans)
2896
+
2897
+ Either preview field is null when the body is unknown format, unparseable, or the relevant content is non-text (e.g., image-only).`;
2898
+ const PROVIDER_WRITE_WARNING = "⚠ This tool mutates provider configuration. Confirm with the user before invoking.";
2899
+ function registerTools(server) {
2900
+ server.registerTool(
2901
+ "inspector_list_logs",
2902
+ {
2903
+ title: "List captured LLM logs",
2904
+ description: TOOL_LIST_LOGS_DESC,
2905
+ inputSchema: object({
2906
+ offset: number().int().nonnegative().optional().describe("Skip this many entries."),
2907
+ limit: number().int().positive().optional().describe("How many summaries to return. Hard-capped at 5."),
2908
+ sessionId: string().optional().describe("Filter by session/affinity id."),
2909
+ model: string().optional().describe("Filter by model name.")
2910
+ })
2911
+ },
2912
+ (args) => safeCall(() => listLogsImpl(callApi, args))
2913
+ );
2914
+ server.registerTool(
2915
+ "inspector_get_log",
2916
+ {
2917
+ title: "Get a single captured log by id",
2918
+ description: "Returns the full CapturedLog for the given id, including rawRequestBody and responseText with no truncation. SSE streaming chunks are NOT included — use inspector_get_log_chunks for those. Returns an error if the id does not exist.",
2919
+ inputSchema: object({
2920
+ id: number().int().positive().describe("The log id.")
2921
+ })
2922
+ },
2923
+ ({ id }) => safeCall(() => getLogImpl(callApi, id))
2924
+ );
2925
+ server.registerTool(
2926
+ "inspector_get_log_chunks",
2927
+ {
2928
+ title: "Get SSE streaming chunks for a captured log",
2929
+ description: "Returns the SSE chunks array for a streaming log, in the order they were received. Returns an error if the log has no chunks (i.e. was non-streaming or the log id is unknown).",
2930
+ inputSchema: object({
2931
+ id: number().int().positive().describe("The log id.")
2932
+ })
2933
+ },
2934
+ ({ id }) => safeCall(() => getLogChunksImpl(callApi, id))
2935
+ );
2936
+ server.registerTool(
2937
+ "inspector_list_sessions",
2938
+ {
2939
+ title: "List known session ids",
2940
+ description: "Returns the array of session ids that have been observed in captured logs. Useful for discovering what sessionId values to pass to inspector_list_logs.",
2941
+ inputSchema: object({})
2942
+ },
2943
+ () => safeCall(() => listSessionsImpl(callApi))
2944
+ );
2945
+ server.registerTool(
2946
+ "inspector_list_models",
2947
+ {
2948
+ title: "List distinct model names seen in captured logs",
2949
+ description: "Returns the array of distinct model names observed across all captured logs.",
2950
+ inputSchema: object({})
2951
+ },
2952
+ () => safeCall(() => listModelsImpl(callApi))
2953
+ );
2954
+ server.registerTool(
2955
+ "inspector_list_providers",
2956
+ {
2957
+ title: "List configured LLM providers",
2958
+ description: "Returns the full ProviderConfig array, including apiKey in PLAINTEXT. MCP is localhost-only — any process that can call /api/mcp can also read <dataDir>/providers.json directly, so the apiKey is not redacted. Do not expose this MCP server to non-trusted local processes.",
2959
+ inputSchema: object({})
2960
+ },
2961
+ () => safeCall(() => listProvidersImpl(callApi))
2962
+ );
2963
+ server.registerTool(
2964
+ "inspector_get_provider",
2965
+ {
2966
+ title: "Get a single provider by id",
2967
+ description: "Returns the full ProviderConfig for the given id, including apiKey in PLAINTEXT (same posture as inspector_list_providers).",
2968
+ inputSchema: object({
2969
+ id: string().describe("The provider id.")
2970
+ })
2971
+ },
2972
+ ({ id }) => safeCall(() => getProviderImpl(callApi, id))
2973
+ );
2974
+ server.registerTool(
2975
+ "inspector_replay_log",
2976
+ {
2977
+ title: "Replay a captured log against its provider",
2978
+ description: "Re-sends the captured request body to the upstream LLM and returns the response summary: success flag, status, responseText, input/output token counts, elapsedMs, and whether the response was streaming. Useful for re-running a request after a fix or a transient failure. Returns an error if the log id is unknown or the upstream call fails.",
2979
+ inputSchema: object({
2980
+ id: number().int().positive().describe("The log id to replay.")
2981
+ })
2982
+ },
2983
+ (args) => safeCall(() => replayLogImpl(callApi, args))
2984
+ );
2985
+ server.registerTool(
2986
+ "inspector_add_provider",
2987
+ {
2988
+ title: "Add a new LLM provider",
2989
+ description: `${PROVIDER_WRITE_WARNING}
2990
+
2991
+ Persists a new provider to <dataDir>/providers.json. Required fields: name, apiKey, format (anthropic|openai), model. Optional: baseUrl, authHeader (default: bearer), apiDocsUrl.`,
2992
+ inputSchema: object({
2993
+ name: string().min(1),
2994
+ apiKey: string().min(1),
2995
+ format: _enum(["anthropic", "openai"]),
2996
+ model: string().min(1),
2997
+ anthropicBaseUrl: string().optional(),
2998
+ openaiBaseUrl: string().optional(),
2999
+ authHeader: _enum(["bearer", "x-api-key"]).optional(),
3000
+ apiDocsUrl: string().optional()
3001
+ })
3002
+ },
3003
+ (provider) => safeCall(() => addProviderImpl(callApi, provider))
3004
+ );
3005
+ server.registerTool(
3006
+ "inspector_update_provider",
3007
+ {
3008
+ title: "Update an existing provider",
3009
+ description: `${PROVIDER_WRITE_WARNING} Updating with nonsense values effectively soft-deletes a provider. Confirm with the user before invoking.
3010
+
3011
+ PATCH-style update: only the fields you supply are changed. Returns the updated ProviderConfig.`,
3012
+ inputSchema: object({
3013
+ id: string().describe("The provider id to update."),
3014
+ name: string().min(1).optional(),
3015
+ apiKey: string().min(1).optional(),
3016
+ format: _enum(["anthropic", "openai"]).optional(),
3017
+ model: string().min(1).optional(),
3018
+ baseUrl: string().min(1).optional(),
3019
+ authHeader: _enum(["bearer", "x-api-key"]).optional(),
3020
+ anthropicBaseUrl: string().optional(),
3021
+ openaiBaseUrl: string().optional(),
3022
+ apiDocsUrl: string().optional()
3023
+ })
3024
+ },
3025
+ (input) => safeCall(() => updateProviderImpl(callApi, input))
3026
+ );
3027
+ server.registerTool(
3028
+ "inspector_test_provider",
3029
+ {
3030
+ title: "Test a provider's connectivity",
3031
+ description: "Runs the same connectivity test the UI's 'Test' button does, against both Anthropic and OpenAI endpoints if configured. Returns the per-endpoint success/failure result. Returns an error if the provider id is unknown.",
3032
+ inputSchema: object({
3033
+ id: string().describe("The provider id to test.")
3034
+ })
3035
+ },
3036
+ ({ id }) => safeCall(() => testProviderImpl(callApi, id))
3037
+ );
3038
+ }
3039
+ const Route$c = createFileRoute("/api/mcp")({
3040
+ server: {
3041
+ handlers: {
3042
+ // The SDK may issue either POST, GET, or DELETE. TanStack Start only
3043
+ // requires us to declare the methods we accept; routing by method is
3044
+ // handled inside the transport.
3045
+ POST: ({ request }) => handleMcpRequest(request),
3046
+ GET: ({ request }) => handleMcpRequest(request),
3047
+ DELETE: ({ request }) => handleMcpRequest(request)
3048
+ }
3049
+ }
3050
+ });
2436
3051
  const Route$b = createFileRoute("/api/logs")({
2437
3052
  server: {
2438
3053
  handlers: {
@@ -3472,45 +4087,50 @@ const Route = createFileRoute("/api/logs/$id/chunks")({
3472
4087
  }
3473
4088
  }
3474
4089
  });
3475
- const IndexRoute = Route$g.update({
4090
+ const IndexRoute = Route$h.update({
3476
4091
  id: "/",
3477
4092
  path: "/",
3478
- getParentRoute: () => Route$h
4093
+ getParentRoute: () => Route$i
3479
4094
  });
3480
- const ProxySplatRoute = Route$f.update({
4095
+ const ProxySplatRoute = Route$g.update({
3481
4096
  id: "/proxy/$",
3482
4097
  path: "/proxy/$",
3483
- getParentRoute: () => Route$h
4098
+ getParentRoute: () => Route$i
3484
4099
  });
3485
- const ApiSessionsRoute = Route$e.update({
4100
+ const ApiSessionsRoute = Route$f.update({
3486
4101
  id: "/api/sessions",
3487
4102
  path: "/api/sessions",
3488
- getParentRoute: () => Route$h
4103
+ getParentRoute: () => Route$i
3489
4104
  });
3490
- const ApiProvidersRoute = Route$d.update({
4105
+ const ApiProvidersRoute = Route$e.update({
3491
4106
  id: "/api/providers",
3492
4107
  path: "/api/providers",
3493
- getParentRoute: () => Route$h
4108
+ getParentRoute: () => Route$i
3494
4109
  });
3495
- const ApiModelsRoute = Route$c.update({
4110
+ const ApiModelsRoute = Route$d.update({
3496
4111
  id: "/api/models",
3497
4112
  path: "/api/models",
3498
- getParentRoute: () => Route$h
4113
+ getParentRoute: () => Route$i
4114
+ });
4115
+ const ApiMcpRoute = Route$c.update({
4116
+ id: "/api/mcp",
4117
+ path: "/api/mcp",
4118
+ getParentRoute: () => Route$i
3499
4119
  });
3500
4120
  const ApiLogsRoute = Route$b.update({
3501
4121
  id: "/api/logs",
3502
4122
  path: "/api/logs",
3503
- getParentRoute: () => Route$h
4123
+ getParentRoute: () => Route$i
3504
4124
  });
3505
4125
  const ApiHealthRoute = Route$a.update({
3506
4126
  id: "/api/health",
3507
4127
  path: "/api/health",
3508
- getParentRoute: () => Route$h
4128
+ getParentRoute: () => Route$i
3509
4129
  });
3510
4130
  const ApiConfigRoute = Route$9.update({
3511
4131
  id: "/api/config",
3512
4132
  path: "/api/config",
3513
- getParentRoute: () => Route$h
4133
+ getParentRoute: () => Route$i
3514
4134
  });
3515
4135
  const ApiProvidersImportRoute = Route$8.update({
3516
4136
  id: "/import",
@@ -3594,12 +4214,13 @@ const rootRouteChildren = {
3594
4214
  ApiConfigRoute: ApiConfigRouteWithChildren,
3595
4215
  ApiHealthRoute,
3596
4216
  ApiLogsRoute: ApiLogsRouteWithChildren,
4217
+ ApiMcpRoute,
3597
4218
  ApiModelsRoute,
3598
4219
  ApiProvidersRoute: ApiProvidersRouteWithChildren,
3599
4220
  ApiSessionsRoute,
3600
4221
  ProxySplatRoute
3601
4222
  };
3602
- const routeTree = Route$h._addFileChildren(rootRouteChildren)._addFileTypes();
4223
+ const routeTree = Route$i._addFileChildren(rootRouteChildren)._addFileTypes();
3603
4224
  function getRouter() {
3604
4225
  const router2 = createRouter({
3605
4226
  routeTree,