@m4trix/trace-viewer 0.18.0 → 0.19.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.
- package/README.md +19 -6
- package/dist/cli.js +91 -8
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +35 -7
- package/dist/index.js +92 -7
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import * as _m4trix_tracing from '@m4trix/tracing';
|
|
2
2
|
import { TraceViewerApi, Trace, TraceRun, TraceRecord } from '@m4trix/tracing';
|
|
3
|
+
import http, { IncomingMessage, ServerResponse } from 'node:http';
|
|
3
4
|
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
4
5
|
import * as _trpc_server from '@trpc/server';
|
|
5
6
|
import { AnyRouter } from '@trpc/server';
|
|
6
|
-
import http from 'node:http';
|
|
7
7
|
|
|
8
8
|
type AwsStackTraceViewerOptions = {
|
|
9
9
|
dynamoTable?: string;
|
|
@@ -15,7 +15,7 @@ type AwsStackTraceViewerOptions = {
|
|
|
15
15
|
declare function createAwsStackTraceViewerApi(options?: AwsStackTraceViewerOptions): TraceViewerApi;
|
|
16
16
|
|
|
17
17
|
type AdapterKind = 'fs' | 'aws-stack';
|
|
18
|
-
/** `serve` starts the HTTP viewer; `mcp` serves the trace MCP over stdio. */
|
|
18
|
+
/** `serve` starts the HTTP viewer (with the MCP at `/mcp`); `mcp` serves the trace MCP over stdio. */
|
|
19
19
|
type CliCommand = 'serve' | 'mcp';
|
|
20
20
|
type ParsedCli = {
|
|
21
21
|
command: CliCommand;
|
|
@@ -23,6 +23,8 @@ type ParsedCli = {
|
|
|
23
23
|
/** Resolved absolute or cwd-relative path for fs adapter */
|
|
24
24
|
path: string | undefined;
|
|
25
25
|
port: number;
|
|
26
|
+
/** Serve the MCP endpoint at `/mcp` alongside the viewer. */
|
|
27
|
+
mcp: boolean;
|
|
26
28
|
};
|
|
27
29
|
declare const DEFAULT_PORT = 4319;
|
|
28
30
|
declare const DEFAULT_FS_RELATIVE_PATH = "tmp/tracing-example";
|
|
@@ -102,14 +104,25 @@ declare class PayloadIndex {
|
|
|
102
104
|
private evict;
|
|
103
105
|
}
|
|
104
106
|
|
|
105
|
-
|
|
107
|
+
declare const MCP_HTTP_PATH = "/mcp";
|
|
108
|
+
type TraceMcpHttpHandlerOptions = {
|
|
106
109
|
traceViewerApi: TraceViewerApi;
|
|
107
110
|
index?: PayloadIndexOptions;
|
|
108
111
|
version?: string;
|
|
112
|
+
/** Extra hostnames accepted in the Host header, e.g. when bound to a non-loopback interface. */
|
|
113
|
+
allowedHostnames?: string[];
|
|
109
114
|
};
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
|
|
115
|
+
type TraceMcpHttpHandler = (req: IncomingMessage, res: ServerResponse) => Promise<void>;
|
|
116
|
+
/**
|
|
117
|
+
* Guards against DNS rebinding and cross-site requests from browser pages: the Host must be
|
|
118
|
+
* loopback (or explicitly allowed) and an Origin, when sent, must be loopback too.
|
|
119
|
+
*/
|
|
120
|
+
declare function isAllowedMcpRequest(req: IncomingMessage, allowedHostnames?: string[]): boolean;
|
|
121
|
+
/**
|
|
122
|
+
* Streamable HTTP MCP endpoint in stateless mode: each POST gets a fresh protocol server, while
|
|
123
|
+
* the tool state (payload index) is shared, so payloads loaded by one call stay searchable.
|
|
124
|
+
*/
|
|
125
|
+
declare function createTraceMcpHttpHandler(options: TraceMcpHttpHandlerOptions): TraceMcpHttpHandler;
|
|
113
126
|
|
|
114
127
|
type RunFilter = {
|
|
115
128
|
type?: string[];
|
|
@@ -248,6 +261,17 @@ declare class TraceTools {
|
|
|
248
261
|
private loadPayloadValue;
|
|
249
262
|
}
|
|
250
263
|
|
|
264
|
+
type TraceMcpServerOptions = {
|
|
265
|
+
traceViewerApi: TraceViewerApi;
|
|
266
|
+
index?: PayloadIndexOptions;
|
|
267
|
+
version?: string;
|
|
268
|
+
/** Shared tool state (payload index). Pass one instance to keep loaded payloads across servers. */
|
|
269
|
+
tools?: TraceTools;
|
|
270
|
+
};
|
|
271
|
+
declare function createTraceMcpServer(options: TraceMcpServerOptions): McpServer;
|
|
272
|
+
/** Serves the trace MCP over stdio. Logs go to stderr; stdout carries the protocol. */
|
|
273
|
+
declare function startTraceMcpStdioServer(options: TraceMcpServerOptions): Promise<McpServer>;
|
|
274
|
+
|
|
251
275
|
type TraceViewerContext = {
|
|
252
276
|
traceViewerApi: TraceViewerApi;
|
|
253
277
|
};
|
|
@@ -319,7 +343,11 @@ type StartServerOptions = {
|
|
|
319
343
|
traceViewerApi: TraceViewerApi;
|
|
320
344
|
port: number;
|
|
321
345
|
host?: string;
|
|
346
|
+
/** Serve the trace MCP (Streamable HTTP) at `/mcp`. Default true. */
|
|
347
|
+
mcp?: boolean;
|
|
348
|
+
/** Version reported by the MCP server. */
|
|
349
|
+
version?: string;
|
|
322
350
|
};
|
|
323
351
|
declare function startTraceViewerServer(options: StartServerOptions): http.Server;
|
|
324
352
|
|
|
325
|
-
export { type AdapterKind, type AppRouter, type AwsStackTraceViewerOptions, type CliCommand, CliParseError, DEFAULT_FS_RELATIVE_PATH, DEFAULT_PORT, type ParsedCli, type TraceMcpServerOptions, TraceTools, type TraceViewerContext, appRouter, cliHelpText, createAwsStackTraceViewerApi, createFsTraceViewerApi, createTraceMcpServer, parseCliArgs, startTraceMcpStdioServer, startTraceViewerServer };
|
|
353
|
+
export { type AdapterKind, type AppRouter, type AwsStackTraceViewerOptions, type CliCommand, CliParseError, DEFAULT_FS_RELATIVE_PATH, DEFAULT_PORT, MCP_HTTP_PATH, type ParsedCli, type TraceMcpHttpHandlerOptions, type TraceMcpServerOptions, TraceTools, type TraceViewerContext, appRouter, cliHelpText, createAwsStackTraceViewerApi, createFsTraceViewerApi, createTraceMcpHttpHandler, createTraceMcpServer, isAllowedMcpRequest, parseCliArgs, startTraceMcpStdioServer, startTraceViewerServer };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { TraceStore, TraceViewerApi, FsPayloadStoreAdapter, FsStructureStoreAdapter } from '@m4trix/tracing';
|
|
2
2
|
import { S3PayloadStoreAdapter, DynamoStructureStoreAdapter, resolveS3PayloadStoreOptionsFromEnv, resolveDynamoStructureStoreOptionsFromEnv } from '@m4trix/tracing/aws';
|
|
3
3
|
import { resolve, relative, sep, extname, join, dirname } from 'path';
|
|
4
|
+
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
|
4
5
|
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
5
6
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
6
7
|
import { z } from 'zod';
|
|
@@ -47,6 +48,7 @@ function parseCliArgs(argv) {
|
|
|
47
48
|
let adapter = "fs";
|
|
48
49
|
let path = DEFAULT_FS_RELATIVE_PATH;
|
|
49
50
|
let port = DEFAULT_PORT;
|
|
51
|
+
let mcp = true;
|
|
50
52
|
for (let i = 0; i < args.length; i++) {
|
|
51
53
|
const a = args[i];
|
|
52
54
|
if (a === "--adapter") {
|
|
@@ -74,6 +76,10 @@ function parseCliArgs(argv) {
|
|
|
74
76
|
port = n;
|
|
75
77
|
continue;
|
|
76
78
|
}
|
|
79
|
+
if (a === "--no-mcp") {
|
|
80
|
+
mcp = false;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
77
83
|
if (a === "--help" || a === "-h") {
|
|
78
84
|
throw new CliParseError("HELP");
|
|
79
85
|
}
|
|
@@ -91,7 +97,7 @@ function parseCliArgs(argv) {
|
|
|
91
97
|
} else if (!path) {
|
|
92
98
|
path = DEFAULT_FS_RELATIVE_PATH;
|
|
93
99
|
}
|
|
94
|
-
return { command, adapter, path, port };
|
|
100
|
+
return { command, adapter, path, port, mcp };
|
|
95
101
|
}
|
|
96
102
|
function cliHelpText(program) {
|
|
97
103
|
return `
|
|
@@ -102,18 +108,19 @@ Usage:
|
|
|
102
108
|
${program} mcp [--adapter fs|aws-stack] [--path <dir>]
|
|
103
109
|
|
|
104
110
|
Commands:
|
|
105
|
-
serve Start the web viewer (default)
|
|
106
|
-
mcp Serve
|
|
111
|
+
serve Start the web viewer and the MCP endpoint at /mcp (default)
|
|
112
|
+
mcp Serve the MCP over stdio instead (for clients without HTTP support)
|
|
107
113
|
|
|
108
114
|
Options:
|
|
109
115
|
--adapter fs|aws-stack Storage backend (default: fs)
|
|
110
116
|
--path <dir> Trace root for fs adapter (default: ${DEFAULT_FS_RELATIVE_PATH})
|
|
111
117
|
--port <n> HTTP port for serve (default: ${DEFAULT_PORT})
|
|
118
|
+
--no-mcp Do not serve the MCP endpoint at /mcp
|
|
112
119
|
-h, --help Show this help
|
|
113
120
|
|
|
114
121
|
Examples:
|
|
115
122
|
${program} --adapter fs --path ./tmp/tracing-example --port ${DEFAULT_PORT}
|
|
116
|
-
claude mcp add m4trix-traces
|
|
123
|
+
claude mcp add --transport http m4trix-traces http://127.0.0.1:${DEFAULT_PORT}/mcp
|
|
117
124
|
`.trim();
|
|
118
125
|
}
|
|
119
126
|
function createFsTraceViewerApi(traceRootPath) {
|
|
@@ -1729,7 +1736,7 @@ async function respond(task) {
|
|
|
1729
1736
|
}
|
|
1730
1737
|
}
|
|
1731
1738
|
function createTraceMcpServer(options) {
|
|
1732
|
-
const tools = new TraceTools(options.traceViewerApi, { index: options.index });
|
|
1739
|
+
const tools = options.tools ?? new TraceTools(options.traceViewerApi, { index: options.index });
|
|
1733
1740
|
const server = new McpServer(
|
|
1734
1741
|
{ name: "m4trix-traces", version: options.version ?? "0.0.0" },
|
|
1735
1742
|
{ instructions: SERVER_INSTRUCTIONS }
|
|
@@ -1938,6 +1945,69 @@ async function startTraceMcpStdioServer(options) {
|
|
|
1938
1945
|
await server.connect(new StdioServerTransport());
|
|
1939
1946
|
return server;
|
|
1940
1947
|
}
|
|
1948
|
+
|
|
1949
|
+
// src/mcp/http.ts
|
|
1950
|
+
var MCP_HTTP_PATH = "/mcp";
|
|
1951
|
+
var LOOPBACK_HOSTNAMES = /* @__PURE__ */ new Set(["localhost", "127.0.0.1", "[::1]", "::1"]);
|
|
1952
|
+
function hostnameOf(value) {
|
|
1953
|
+
try {
|
|
1954
|
+
return new URL(value.includes("://") ? value : `http://${value}`).hostname;
|
|
1955
|
+
} catch {
|
|
1956
|
+
return null;
|
|
1957
|
+
}
|
|
1958
|
+
}
|
|
1959
|
+
function isAllowedMcpRequest(req, allowedHostnames = []) {
|
|
1960
|
+
const allowed = /* @__PURE__ */ new Set([...LOOPBACK_HOSTNAMES, ...allowedHostnames]);
|
|
1961
|
+
const host = req.headers.host ? hostnameOf(req.headers.host) : null;
|
|
1962
|
+
if (!host || !allowed.has(host)) return false;
|
|
1963
|
+
const origin = req.headers.origin;
|
|
1964
|
+
if (origin === void 0) return true;
|
|
1965
|
+
const originHost = hostnameOf(origin);
|
|
1966
|
+
return originHost !== null && LOOPBACK_HOSTNAMES.has(originHost);
|
|
1967
|
+
}
|
|
1968
|
+
function sendJsonRpcError(res, status, message) {
|
|
1969
|
+
res.writeHead(status, { "Content-Type": "application/json" });
|
|
1970
|
+
res.end(JSON.stringify({ jsonrpc: "2.0", error: { code: -32e3, message }, id: null }));
|
|
1971
|
+
}
|
|
1972
|
+
function createTraceMcpHttpHandler(options) {
|
|
1973
|
+
const tools = new TraceTools(options.traceViewerApi, { index: options.index });
|
|
1974
|
+
return async (req, res) => {
|
|
1975
|
+
if (!isAllowedMcpRequest(req, options.allowedHostnames)) {
|
|
1976
|
+
sendJsonRpcError(res, 403, "Forbidden: MCP endpoint only accepts local requests.");
|
|
1977
|
+
return;
|
|
1978
|
+
}
|
|
1979
|
+
if (req.method !== "POST") {
|
|
1980
|
+
res.setHeader("Allow", "POST");
|
|
1981
|
+
sendJsonRpcError(
|
|
1982
|
+
res,
|
|
1983
|
+
405,
|
|
1984
|
+
"Method not allowed: this stateless MCP endpoint only accepts POST."
|
|
1985
|
+
);
|
|
1986
|
+
return;
|
|
1987
|
+
}
|
|
1988
|
+
const server = createTraceMcpServer({
|
|
1989
|
+
traceViewerApi: options.traceViewerApi,
|
|
1990
|
+
version: options.version,
|
|
1991
|
+
tools
|
|
1992
|
+
});
|
|
1993
|
+
const transport = new StreamableHTTPServerTransport({
|
|
1994
|
+
sessionIdGenerator: void 0,
|
|
1995
|
+
enableJsonResponse: true
|
|
1996
|
+
});
|
|
1997
|
+
res.on("close", () => {
|
|
1998
|
+
void transport.close();
|
|
1999
|
+
void server.close();
|
|
2000
|
+
});
|
|
2001
|
+
try {
|
|
2002
|
+
await server.connect(transport);
|
|
2003
|
+
await transport.handleRequest(req, res);
|
|
2004
|
+
} catch (error) {
|
|
2005
|
+
if (!res.headersSent) {
|
|
2006
|
+
sendJsonRpcError(res, 500, error instanceof Error ? error.message : String(error));
|
|
2007
|
+
}
|
|
2008
|
+
}
|
|
2009
|
+
};
|
|
2010
|
+
}
|
|
1941
2011
|
var t = initTRPC.context().create();
|
|
1942
2012
|
var listInput = z.object({
|
|
1943
2013
|
limit: z.number().int().positive().max(500).optional(),
|
|
@@ -2025,27 +2095,42 @@ function serveClientAsset(req, res) {
|
|
|
2025
2095
|
res.end("client bundle missing; run pnpm --filter @m4trix/trace-viewer build");
|
|
2026
2096
|
}
|
|
2027
2097
|
}
|
|
2098
|
+
function isMcpPath(url) {
|
|
2099
|
+
return url === MCP_HTTP_PATH || url.startsWith(`${MCP_HTTP_PATH}?`) || url.startsWith(`${MCP_HTTP_PATH}/`);
|
|
2100
|
+
}
|
|
2028
2101
|
function startTraceViewerServer(options) {
|
|
2029
|
-
const { traceViewerApi, port, host = "127.0.0.1" } = options;
|
|
2102
|
+
const { traceViewerApi, port, host = "127.0.0.1", mcp = true } = options;
|
|
2030
2103
|
const trpcHandler = createHTTPHandler({
|
|
2031
2104
|
router: appRouter,
|
|
2032
2105
|
createContext: () => ({ traceViewerApi }),
|
|
2033
2106
|
basePath: "/trpc/"
|
|
2034
2107
|
});
|
|
2108
|
+
const mcpHandler = mcp ? createTraceMcpHttpHandler({
|
|
2109
|
+
traceViewerApi,
|
|
2110
|
+
version: options.version,
|
|
2111
|
+
allowedHostnames: [host]
|
|
2112
|
+
}) : null;
|
|
2035
2113
|
const server = http.createServer((req, res) => {
|
|
2036
2114
|
const url = req.url ?? "/";
|
|
2037
2115
|
if (url.startsWith("/trpc")) {
|
|
2038
2116
|
void trpcHandler(req, res);
|
|
2039
2117
|
return;
|
|
2040
2118
|
}
|
|
2119
|
+
if (mcpHandler && isMcpPath(url)) {
|
|
2120
|
+
void mcpHandler(req, res);
|
|
2121
|
+
return;
|
|
2122
|
+
}
|
|
2041
2123
|
serveClientAsset(req, res);
|
|
2042
2124
|
});
|
|
2043
2125
|
server.listen(port, host, () => {
|
|
2044
2126
|
console.log(`Trace viewer listening on http://${host}:${port}`);
|
|
2127
|
+
if (mcpHandler) {
|
|
2128
|
+
console.log(`Trace MCP endpoint: http://${host}:${port}${MCP_HTTP_PATH}`);
|
|
2129
|
+
}
|
|
2045
2130
|
});
|
|
2046
2131
|
return server;
|
|
2047
2132
|
}
|
|
2048
2133
|
|
|
2049
|
-
export { CliParseError, DEFAULT_FS_RELATIVE_PATH, DEFAULT_PORT, TraceTools, appRouter, cliHelpText, createAwsStackTraceViewerApi, createFsTraceViewerApi, createTraceMcpServer, parseCliArgs, startTraceMcpStdioServer, startTraceViewerServer };
|
|
2134
|
+
export { CliParseError, DEFAULT_FS_RELATIVE_PATH, DEFAULT_PORT, MCP_HTTP_PATH, TraceTools, appRouter, cliHelpText, createAwsStackTraceViewerApi, createFsTraceViewerApi, createTraceMcpHttpHandler, createTraceMcpServer, isAllowedMcpRequest, parseCliArgs, startTraceMcpStdioServer, startTraceViewerServer };
|
|
2050
2135
|
//# sourceMappingURL=index.js.map
|
|
2051
2136
|
//# sourceMappingURL=index.js.map
|