@0x1f320.sh/why-did-you-render-mcp 1.0.0-dev.1
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/LICENSE +21 -0
- package/README.md +129 -0
- package/dist/client/index.cjs +73 -0
- package/dist/client/index.d.cts +12 -0
- package/dist/client/index.d.ts +12 -0
- package/dist/client/index.js +72 -0
- package/dist/server/index.d.ts +2 -0
- package/dist/server/index.js +340 -0
- package/package.json +73 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 0x1F320 <me@0x1f320.sh>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# why-did-you-render-mcp
|
|
2
|
+
|
|
3
|
+
An [MCP](https://modelcontextprotocol.io/) server that bridges [why-did-you-render](https://github.com/welldone-software/why-did-you-render) data from the browser to coding agents. It captures unnecessary React re-render reports in real time and exposes them as MCP tools, so agents can diagnose and fix performance issues without manual browser inspection.
|
|
4
|
+
|
|
5
|
+
## How It Works
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
Browser (React app)
|
|
9
|
+
│
|
|
10
|
+
│ why-did-you-render detects unnecessary re-render
|
|
11
|
+
│
|
|
12
|
+
▼
|
|
13
|
+
Client (runs in browser) ── WebSocket ──▶ MCP Server (Node.js)
|
|
14
|
+
│
|
|
15
|
+
├─ Persists to ~/.wdyr-mcp/renders/
|
|
16
|
+
│
|
|
17
|
+
▼
|
|
18
|
+
Coding Agent (Claude, etc.)
|
|
19
|
+
queries via MCP tools
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
The **client** runs inside your React app alongside `why-did-you-render`. Whenever an unnecessary re-render is detected, it sanitizes the render data and sends it over WebSocket to the **MCP server**. The server stores reports as JSONL files and exposes them through MCP tools that coding agents can query.
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
npm install @0x1f320.sh/why-did-you-render-mcp @welldone-software/why-did-you-render
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Setup
|
|
31
|
+
|
|
32
|
+
### 1. Configure why-did-you-render with the client
|
|
33
|
+
|
|
34
|
+
In your app's entry point (e.g. `src/main.tsx` or `src/index.tsx`), set up `why-did-you-render` with the MCP client as its notifier:
|
|
35
|
+
|
|
36
|
+
```tsx
|
|
37
|
+
import React from "react";
|
|
38
|
+
import whyDidYouRender from "@welldone-software/why-did-you-render";
|
|
39
|
+
import { buildOptions } from "@0x1f320.sh/why-did-you-render-mcp/client";
|
|
40
|
+
|
|
41
|
+
if (process.env.NODE_ENV === "development") {
|
|
42
|
+
const { notifier } = buildOptions();
|
|
43
|
+
|
|
44
|
+
whyDidYouRender(React, {
|
|
45
|
+
notifier,
|
|
46
|
+
trackAllPureComponents: true,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
The client automatically uses `location.origin` as the project identifier and connects to `ws://localhost:4649` by default. You can customize both:
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
const { notifier } = buildOptions({
|
|
55
|
+
wsUrl: "ws://localhost:5555",
|
|
56
|
+
projectId: "my-app",
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### 2. Add the MCP server to your agent
|
|
61
|
+
|
|
62
|
+
Add the server to your MCP client configuration. For example, in Claude Desktop's `claude_desktop_config.json`:
|
|
63
|
+
|
|
64
|
+
```json
|
|
65
|
+
{
|
|
66
|
+
"mcpServers": {
|
|
67
|
+
"why-did-you-render": {
|
|
68
|
+
"command": "npx",
|
|
69
|
+
"args": ["@0x1f320.sh/why-did-you-render-mcp"]
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Or if you installed it globally:
|
|
76
|
+
|
|
77
|
+
```json
|
|
78
|
+
{
|
|
79
|
+
"mcpServers": {
|
|
80
|
+
"why-did-you-render": {
|
|
81
|
+
"command": "why-did-you-render-mcp"
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### 3. Start your dev server and interact with the app
|
|
88
|
+
|
|
89
|
+
Once both the MCP server and your React dev server are running, interact with your app in the browser. The agent can now query re-render data using the MCP tools below.
|
|
90
|
+
|
|
91
|
+
## MCP Tools
|
|
92
|
+
|
|
93
|
+
| Tool | Description |
|
|
94
|
+
| --- | --- |
|
|
95
|
+
| `get_unnecessary_renders` | Returns all captured unnecessary re-renders. Optionally filter by `component` name. |
|
|
96
|
+
| `get_render_summary` | Returns a summary of re-renders grouped by component with counts. |
|
|
97
|
+
| `get_projects` | Lists all active projects (identified by their origin URL). |
|
|
98
|
+
| `clear_renders` | Clears all stored render data. Optionally scope to a specific project. |
|
|
99
|
+
|
|
100
|
+
When multiple projects are active, tools accept an optional `project` parameter (the browser's origin URL, e.g. `http://localhost:3000`). If omitted and only one project exists, it is auto-selected.
|
|
101
|
+
|
|
102
|
+
## Architecture
|
|
103
|
+
|
|
104
|
+
```
|
|
105
|
+
Browser (project-a) ──┐
|
|
106
|
+
Browser (project-b) ──┤
|
|
107
|
+
▼
|
|
108
|
+
MCP #1 → WS(:4649) (first instance binds)
|
|
109
|
+
MCP #2 → WS(:4649) → skip (EADDRINUSE)
|
|
110
|
+
│
|
|
111
|
+
▼
|
|
112
|
+
~/.wdyr-mcp/renders/ (JSONL files, shared across instances)
|
|
113
|
+
├─ http___localhost_3000.jsonl
|
|
114
|
+
└─ http___localhost_5173.jsonl
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
- **Multiple MCP instances** can run simultaneously. Only the first instance starts the WebSocket server; others gracefully skip. All instances share the same JSONL data directory.
|
|
118
|
+
- **Multi-project support** — Each project is identified by `location.origin`. Render data is stored in per-project JSONL files.
|
|
119
|
+
- **No daemon required** — Each MCP instance is independent. The WebSocket server is opportunistically claimed by whichever instance starts first.
|
|
120
|
+
|
|
121
|
+
## Configuration
|
|
122
|
+
|
|
123
|
+
| Environment Variable | Default | Description |
|
|
124
|
+
| --- | --- | --- |
|
|
125
|
+
| `WDYR_WS_PORT` | `4649` | WebSocket server port |
|
|
126
|
+
|
|
127
|
+
## License
|
|
128
|
+
|
|
129
|
+
MIT
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/client/utils/describe-value.ts
|
|
3
|
+
function describeValue(value) {
|
|
4
|
+
if (value === null) return "null";
|
|
5
|
+
if (value === void 0) return "undefined";
|
|
6
|
+
if (typeof value === "function") return `function ${value.name || "anonymous"}`;
|
|
7
|
+
if (typeof value !== "object") return String(value);
|
|
8
|
+
if (Array.isArray(value)) return `Array(${value.length})`;
|
|
9
|
+
const name = Object.getPrototypeOf(value)?.constructor?.name;
|
|
10
|
+
if (name && name !== "Object") return name;
|
|
11
|
+
return "Object";
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/client/utils/sanitize-differences.ts
|
|
15
|
+
function sanitizeDifferences(diffs) {
|
|
16
|
+
if (!Array.isArray(diffs)) return false;
|
|
17
|
+
return diffs.map((diff) => ({
|
|
18
|
+
pathString: diff.pathString,
|
|
19
|
+
diffType: diff.diffType,
|
|
20
|
+
prevValue: describeValue(diff.prevValue),
|
|
21
|
+
nextValue: describeValue(diff.nextValue)
|
|
22
|
+
}));
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/client/utils/sanitize-reason.ts
|
|
26
|
+
function sanitizeReason(reason) {
|
|
27
|
+
return {
|
|
28
|
+
propsDifferences: sanitizeDifferences(reason.propsDifferences),
|
|
29
|
+
stateDifferences: sanitizeDifferences(reason.stateDifferences),
|
|
30
|
+
hookDifferences: sanitizeDifferences(reason.hookDifferences)
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region src/client/index.ts
|
|
35
|
+
const DEFAULT_WS_URL = "ws://localhost:4649";
|
|
36
|
+
function buildOptions(opts) {
|
|
37
|
+
const wsUrl = opts?.wsUrl ?? DEFAULT_WS_URL;
|
|
38
|
+
const projectId = opts?.projectId ?? globalThis.location?.origin ?? "default";
|
|
39
|
+
let ws = null;
|
|
40
|
+
let queue = [];
|
|
41
|
+
function connect() {
|
|
42
|
+
ws = new WebSocket(wsUrl);
|
|
43
|
+
ws.addEventListener("open", () => {
|
|
44
|
+
for (const msg of queue) ws?.send(JSON.stringify(msg));
|
|
45
|
+
queue = [];
|
|
46
|
+
});
|
|
47
|
+
ws.addEventListener("close", () => {
|
|
48
|
+
ws = null;
|
|
49
|
+
setTimeout(connect, 1e3);
|
|
50
|
+
});
|
|
51
|
+
ws.addEventListener("error", () => {
|
|
52
|
+
ws?.close();
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
connect();
|
|
56
|
+
function send(msg) {
|
|
57
|
+
if (ws?.readyState === WebSocket.OPEN) ws.send(JSON.stringify(msg));
|
|
58
|
+
else queue.push(msg);
|
|
59
|
+
}
|
|
60
|
+
return { notifier(info) {
|
|
61
|
+
send({
|
|
62
|
+
type: "render",
|
|
63
|
+
projectId,
|
|
64
|
+
payload: {
|
|
65
|
+
displayName: info.displayName,
|
|
66
|
+
reason: sanitizeReason(info.reason),
|
|
67
|
+
hookName: info.hookName
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
} };
|
|
71
|
+
}
|
|
72
|
+
//#endregion
|
|
73
|
+
exports.buildOptions = buildOptions;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { UpdateInfo } from "@welldone-software/why-did-you-render";
|
|
2
|
+
|
|
3
|
+
//#region src/client/index.d.ts
|
|
4
|
+
interface ClientOptions {
|
|
5
|
+
wsUrl?: string;
|
|
6
|
+
projectId?: string;
|
|
7
|
+
}
|
|
8
|
+
declare function buildOptions(opts?: ClientOptions): {
|
|
9
|
+
notifier(info: UpdateInfo): void;
|
|
10
|
+
};
|
|
11
|
+
//#endregion
|
|
12
|
+
export { ClientOptions, buildOptions };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { UpdateInfo } from "@welldone-software/why-did-you-render";
|
|
2
|
+
|
|
3
|
+
//#region src/client/index.d.ts
|
|
4
|
+
interface ClientOptions {
|
|
5
|
+
wsUrl?: string;
|
|
6
|
+
projectId?: string;
|
|
7
|
+
}
|
|
8
|
+
declare function buildOptions(opts?: ClientOptions): {
|
|
9
|
+
notifier(info: UpdateInfo): void;
|
|
10
|
+
};
|
|
11
|
+
//#endregion
|
|
12
|
+
export { ClientOptions, buildOptions };
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
//#region src/client/utils/describe-value.ts
|
|
2
|
+
function describeValue(value) {
|
|
3
|
+
if (value === null) return "null";
|
|
4
|
+
if (value === void 0) return "undefined";
|
|
5
|
+
if (typeof value === "function") return `function ${value.name || "anonymous"}`;
|
|
6
|
+
if (typeof value !== "object") return String(value);
|
|
7
|
+
if (Array.isArray(value)) return `Array(${value.length})`;
|
|
8
|
+
const name = Object.getPrototypeOf(value)?.constructor?.name;
|
|
9
|
+
if (name && name !== "Object") return name;
|
|
10
|
+
return "Object";
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/client/utils/sanitize-differences.ts
|
|
14
|
+
function sanitizeDifferences(diffs) {
|
|
15
|
+
if (!Array.isArray(diffs)) return false;
|
|
16
|
+
return diffs.map((diff) => ({
|
|
17
|
+
pathString: diff.pathString,
|
|
18
|
+
diffType: diff.diffType,
|
|
19
|
+
prevValue: describeValue(diff.prevValue),
|
|
20
|
+
nextValue: describeValue(diff.nextValue)
|
|
21
|
+
}));
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region src/client/utils/sanitize-reason.ts
|
|
25
|
+
function sanitizeReason(reason) {
|
|
26
|
+
return {
|
|
27
|
+
propsDifferences: sanitizeDifferences(reason.propsDifferences),
|
|
28
|
+
stateDifferences: sanitizeDifferences(reason.stateDifferences),
|
|
29
|
+
hookDifferences: sanitizeDifferences(reason.hookDifferences)
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/client/index.ts
|
|
34
|
+
const DEFAULT_WS_URL = "ws://localhost:4649";
|
|
35
|
+
function buildOptions(opts) {
|
|
36
|
+
const wsUrl = opts?.wsUrl ?? DEFAULT_WS_URL;
|
|
37
|
+
const projectId = opts?.projectId ?? globalThis.location?.origin ?? "default";
|
|
38
|
+
let ws = null;
|
|
39
|
+
let queue = [];
|
|
40
|
+
function connect() {
|
|
41
|
+
ws = new WebSocket(wsUrl);
|
|
42
|
+
ws.addEventListener("open", () => {
|
|
43
|
+
for (const msg of queue) ws?.send(JSON.stringify(msg));
|
|
44
|
+
queue = [];
|
|
45
|
+
});
|
|
46
|
+
ws.addEventListener("close", () => {
|
|
47
|
+
ws = null;
|
|
48
|
+
setTimeout(connect, 1e3);
|
|
49
|
+
});
|
|
50
|
+
ws.addEventListener("error", () => {
|
|
51
|
+
ws?.close();
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
connect();
|
|
55
|
+
function send(msg) {
|
|
56
|
+
if (ws?.readyState === WebSocket.OPEN) ws.send(JSON.stringify(msg));
|
|
57
|
+
else queue.push(msg);
|
|
58
|
+
}
|
|
59
|
+
return { notifier(info) {
|
|
60
|
+
send({
|
|
61
|
+
type: "render",
|
|
62
|
+
projectId,
|
|
63
|
+
payload: {
|
|
64
|
+
displayName: info.displayName,
|
|
65
|
+
reason: sanitizeReason(info.reason),
|
|
66
|
+
hookName: info.hookName
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
} };
|
|
70
|
+
}
|
|
71
|
+
//#endregion
|
|
72
|
+
export { buildOptions };
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync, readdirSync, unlinkSync } from "node:fs";
|
|
6
|
+
import { homedir } from "node:os";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
import { WebSocketServer } from "ws";
|
|
9
|
+
//#region src/server/store/utils/read-jsonl.ts
|
|
10
|
+
function readJsonl(file) {
|
|
11
|
+
if (!existsSync(file)) return [];
|
|
12
|
+
return readFileSync(file, "utf-8").split("\n").filter(Boolean).map((line) => JSON.parse(line));
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/server/store/utils/sanitize-project-id.ts
|
|
16
|
+
function sanitizeProjectId(projectId) {
|
|
17
|
+
return projectId.replaceAll(/[^a-zA-Z0-9_.-]/g, "_");
|
|
18
|
+
}
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/server/store/utils/to-result.ts
|
|
21
|
+
function toResult(stored) {
|
|
22
|
+
return {
|
|
23
|
+
project: stored.projectId,
|
|
24
|
+
displayName: stored.displayName,
|
|
25
|
+
reason: stored.reason,
|
|
26
|
+
...stored.hookName != null && { hookName: stored.hookName }
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/server/store/render-store.ts
|
|
31
|
+
const FLUSH_DELAY_MS = 200;
|
|
32
|
+
var RenderStore = class {
|
|
33
|
+
dir;
|
|
34
|
+
buffers = /* @__PURE__ */ new Map();
|
|
35
|
+
timers = /* @__PURE__ */ new Map();
|
|
36
|
+
constructor(dir) {
|
|
37
|
+
this.dir = dir ?? join(homedir(), ".wdyr-mcp", "renders");
|
|
38
|
+
mkdirSync(this.dir, { recursive: true });
|
|
39
|
+
}
|
|
40
|
+
addRender(report, projectId) {
|
|
41
|
+
const stored = {
|
|
42
|
+
...report,
|
|
43
|
+
projectId
|
|
44
|
+
};
|
|
45
|
+
let buf = this.buffers.get(projectId);
|
|
46
|
+
if (!buf) {
|
|
47
|
+
buf = [];
|
|
48
|
+
this.buffers.set(projectId, buf);
|
|
49
|
+
}
|
|
50
|
+
buf.push(stored);
|
|
51
|
+
const existing = this.timers.get(projectId);
|
|
52
|
+
if (existing) clearTimeout(existing);
|
|
53
|
+
this.timers.set(projectId, setTimeout(() => this.flush(projectId), FLUSH_DELAY_MS));
|
|
54
|
+
}
|
|
55
|
+
flush(projectId) {
|
|
56
|
+
if (projectId) this.flushProject(projectId);
|
|
57
|
+
else for (const id of this.buffers.keys()) this.flushProject(id);
|
|
58
|
+
}
|
|
59
|
+
flushProject(projectId) {
|
|
60
|
+
const buf = this.buffers.get(projectId);
|
|
61
|
+
if (!buf || buf.length === 0) return;
|
|
62
|
+
const lines = buf.map((s) => JSON.stringify(s)).join("\n");
|
|
63
|
+
appendFileSync(this.projectFile(projectId), `${lines}\n`);
|
|
64
|
+
buf.length = 0;
|
|
65
|
+
const timer = this.timers.get(projectId);
|
|
66
|
+
if (timer) {
|
|
67
|
+
clearTimeout(timer);
|
|
68
|
+
this.timers.delete(projectId);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
getAllRenders(projectId) {
|
|
72
|
+
this.flush(projectId);
|
|
73
|
+
if (projectId) return readJsonl(this.projectFile(projectId)).map(toResult);
|
|
74
|
+
return this.jsonlFiles().flatMap((f) => readJsonl(join(this.dir, f)).map(toResult));
|
|
75
|
+
}
|
|
76
|
+
getRendersByComponent(componentName, projectId) {
|
|
77
|
+
return this.getAllRenders(projectId).filter((r) => r.displayName === componentName);
|
|
78
|
+
}
|
|
79
|
+
clearRenders(projectId) {
|
|
80
|
+
if (projectId) {
|
|
81
|
+
this.buffers.delete(projectId);
|
|
82
|
+
const timer = this.timers.get(projectId);
|
|
83
|
+
if (timer) {
|
|
84
|
+
clearTimeout(timer);
|
|
85
|
+
this.timers.delete(projectId);
|
|
86
|
+
}
|
|
87
|
+
const file = this.projectFile(projectId);
|
|
88
|
+
if (existsSync(file)) unlinkSync(file);
|
|
89
|
+
} else {
|
|
90
|
+
for (const [id, timer] of this.timers) clearTimeout(timer);
|
|
91
|
+
this.buffers.clear();
|
|
92
|
+
this.timers.clear();
|
|
93
|
+
for (const f of this.jsonlFiles()) unlinkSync(join(this.dir, f));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
getProjects() {
|
|
97
|
+
this.flush();
|
|
98
|
+
const projects = /* @__PURE__ */ new Set();
|
|
99
|
+
for (const f of this.jsonlFiles()) {
|
|
100
|
+
const firstLine = readFileSync(join(this.dir, f), "utf-8").split("\n")[0];
|
|
101
|
+
if (!firstLine) continue;
|
|
102
|
+
const stored = JSON.parse(firstLine);
|
|
103
|
+
projects.add(stored.projectId);
|
|
104
|
+
}
|
|
105
|
+
return [...projects];
|
|
106
|
+
}
|
|
107
|
+
getSummary(projectId) {
|
|
108
|
+
const renders = this.getAllRenders(projectId);
|
|
109
|
+
const summary = {};
|
|
110
|
+
for (const r of renders) {
|
|
111
|
+
summary[r.project] ??= {};
|
|
112
|
+
const project = summary[r.project];
|
|
113
|
+
project[r.displayName] = (project[r.displayName] ?? 0) + 1;
|
|
114
|
+
}
|
|
115
|
+
return summary;
|
|
116
|
+
}
|
|
117
|
+
projectFile(projectId) {
|
|
118
|
+
return join(this.dir, `${sanitizeProjectId(projectId)}.jsonl`);
|
|
119
|
+
}
|
|
120
|
+
jsonlFiles() {
|
|
121
|
+
return readdirSync(this.dir).filter((f) => f.endsWith(".jsonl"));
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
//#endregion
|
|
125
|
+
//#region src/server/store/index.ts
|
|
126
|
+
const store = new RenderStore();
|
|
127
|
+
//#endregion
|
|
128
|
+
//#region src/server/tools/utils/resolve-project.ts
|
|
129
|
+
/**
|
|
130
|
+
* Resolves the project to use. If a project is explicitly given, use it.
|
|
131
|
+
* If only one project exists, auto-select it. If multiple exist, return
|
|
132
|
+
* a message asking the agent to disambiguate.
|
|
133
|
+
*/
|
|
134
|
+
function resolveProject(project) {
|
|
135
|
+
if (project) return { projectId: project };
|
|
136
|
+
const projects = store.getProjects();
|
|
137
|
+
if (projects.length === 0) return { projectId: void 0 };
|
|
138
|
+
if (projects.length === 1) return { projectId: projects[0] };
|
|
139
|
+
return {
|
|
140
|
+
projectId: void 0,
|
|
141
|
+
error: [
|
|
142
|
+
"Multiple projects are recording render data. Ask the user which project they are working on (e.g. their dev server URL like http://localhost:3000).",
|
|
143
|
+
"",
|
|
144
|
+
"Active projects:",
|
|
145
|
+
...projects.map((p) => `- ${p}`)
|
|
146
|
+
].join("\n")
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
//#endregion
|
|
150
|
+
//#region src/server/tools/utils/text-result.ts
|
|
151
|
+
function textResult(text) {
|
|
152
|
+
return { content: [{
|
|
153
|
+
type: "text",
|
|
154
|
+
text
|
|
155
|
+
}] };
|
|
156
|
+
}
|
|
157
|
+
//#endregion
|
|
158
|
+
//#region src/server/tools/clear-renders.ts
|
|
159
|
+
function register$3(server) {
|
|
160
|
+
server.registerTool("clear_renders", {
|
|
161
|
+
title: "Clear Renders",
|
|
162
|
+
description: "Clears collected render data. If multiple projects are active and no project is specified, the tool will ask you to disambiguate.",
|
|
163
|
+
inputSchema: { project: z.string().optional().describe("Project identifier (the browser's origin URL, e.g. http://localhost:3000). Omit to auto-detect.") }
|
|
164
|
+
}, async ({ project }) => {
|
|
165
|
+
const resolved = resolveProject(project);
|
|
166
|
+
if (resolved.error) return textResult(resolved.error);
|
|
167
|
+
store.clearRenders(resolved.projectId);
|
|
168
|
+
return textResult(resolved.projectId ? `Render data cleared for ${resolved.projectId}.` : "All render data cleared.");
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
//#endregion
|
|
172
|
+
//#region src/server/tools/get-projects.ts
|
|
173
|
+
function register$2(server) {
|
|
174
|
+
server.registerTool("get_projects", {
|
|
175
|
+
title: "Get Projects",
|
|
176
|
+
description: "Returns a list of project identifiers (browser origin URLs) that have recorded render data.",
|
|
177
|
+
inputSchema: {}
|
|
178
|
+
}, async () => {
|
|
179
|
+
const projects = store.getProjects();
|
|
180
|
+
if (projects.length === 0) return textResult("No projects have recorded render data yet.");
|
|
181
|
+
return textResult(`Active projects:\n${projects.map((p) => `- ${p}`).join("\n")}`);
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
//#endregion
|
|
185
|
+
//#region src/server/tools/get-render-summary.ts
|
|
186
|
+
function register$1(server) {
|
|
187
|
+
server.registerTool("get_render_summary", {
|
|
188
|
+
title: "Get Render Summary",
|
|
189
|
+
description: "Returns a summary of unnecessary re-renders grouped by component name with counts. If multiple projects are active and no project is specified, the tool will ask you to disambiguate.",
|
|
190
|
+
inputSchema: { project: z.string().optional().describe("Project identifier (the browser's origin URL, e.g. http://localhost:3000). Omit to auto-detect.") }
|
|
191
|
+
}, async ({ project }) => {
|
|
192
|
+
const resolved = resolveProject(project);
|
|
193
|
+
if (resolved.error) return textResult(resolved.error);
|
|
194
|
+
const summary = store.getSummary(resolved.projectId);
|
|
195
|
+
if (Object.keys(summary).length === 0) return textResult("No unnecessary renders recorded yet.");
|
|
196
|
+
const lines = [];
|
|
197
|
+
for (const [projectId, components] of Object.entries(summary)) {
|
|
198
|
+
lines.push(`[${projectId}]`);
|
|
199
|
+
for (const [name, count] of Object.entries(components)) lines.push(` ${name}: ${count} re-render(s)`);
|
|
200
|
+
}
|
|
201
|
+
return textResult(`Unnecessary re-render summary:\n\n${lines.join("\n")}`);
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
//#endregion
|
|
205
|
+
//#region src/server/tools/get-unnecessary-renders.ts
|
|
206
|
+
function register(server) {
|
|
207
|
+
server.registerTool("get_unnecessary_renders", {
|
|
208
|
+
title: "Get Unnecessary Renders",
|
|
209
|
+
description: "Returns all unnecessary re-renders collected from the browser. If multiple projects are active and no project is specified, the tool will ask you to disambiguate by asking the user for their dev server URL.",
|
|
210
|
+
inputSchema: {
|
|
211
|
+
component: z.string().optional().describe("Filter by component name. Omit to get all renders."),
|
|
212
|
+
project: z.string().optional().describe("Project identifier (the browser's origin URL, e.g. http://localhost:3000). Omit to auto-detect.")
|
|
213
|
+
}
|
|
214
|
+
}, async ({ component, project }) => {
|
|
215
|
+
const resolved = resolveProject(project);
|
|
216
|
+
if (resolved.error) return textResult(resolved.error);
|
|
217
|
+
const renders = component ? store.getRendersByComponent(component, resolved.projectId) : store.getAllRenders(resolved.projectId);
|
|
218
|
+
if (renders.length === 0) return textResult(component ? `No unnecessary renders recorded for "${component}".` : "No unnecessary renders recorded yet. Make sure the browser is connected and triggering re-renders.");
|
|
219
|
+
return textResult(JSON.stringify(renders, null, 2));
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
//#endregion
|
|
223
|
+
//#region src/server/tools/index.ts
|
|
224
|
+
function registerTools(server) {
|
|
225
|
+
register(server);
|
|
226
|
+
register$1(server);
|
|
227
|
+
register$2(server);
|
|
228
|
+
register$3(server);
|
|
229
|
+
}
|
|
230
|
+
//#endregion
|
|
231
|
+
//#region src/server/liveness.ts
|
|
232
|
+
const DEFAULT_INTERVAL_MS = 3e4;
|
|
233
|
+
var HeartbeatManager = class {
|
|
234
|
+
connections = /* @__PURE__ */ new Map();
|
|
235
|
+
timer;
|
|
236
|
+
constructor(wss, store, intervalMs = DEFAULT_INTERVAL_MS) {
|
|
237
|
+
this.wss = wss;
|
|
238
|
+
this.store = store;
|
|
239
|
+
this.timer = setInterval(() => this.check(), intervalMs);
|
|
240
|
+
}
|
|
241
|
+
trackConnection(ws) {
|
|
242
|
+
this.connections.set(ws, {
|
|
243
|
+
isAlive: true,
|
|
244
|
+
projectId: null
|
|
245
|
+
});
|
|
246
|
+
ws.on("pong", () => {
|
|
247
|
+
const meta = this.connections.get(ws);
|
|
248
|
+
if (meta) meta.isAlive = true;
|
|
249
|
+
});
|
|
250
|
+
ws.on("close", () => {
|
|
251
|
+
this.connections.delete(ws);
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
setProjectId(ws, projectId) {
|
|
255
|
+
const meta = this.connections.get(ws);
|
|
256
|
+
if (meta) meta.projectId = projectId;
|
|
257
|
+
}
|
|
258
|
+
stop() {
|
|
259
|
+
clearInterval(this.timer);
|
|
260
|
+
this.connections.clear();
|
|
261
|
+
}
|
|
262
|
+
check() {
|
|
263
|
+
for (const [ws, meta] of this.connections) {
|
|
264
|
+
if (!meta.isAlive) {
|
|
265
|
+
this.connections.delete(ws);
|
|
266
|
+
ws.terminate();
|
|
267
|
+
if (meta.projectId && !this.hasOtherConnection(ws, meta.projectId)) {
|
|
268
|
+
console.error(`[wdyr-mcp] client for ${meta.projectId} is dead, clearing render data`);
|
|
269
|
+
this.store.clearRenders(meta.projectId);
|
|
270
|
+
}
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
meta.isAlive = false;
|
|
274
|
+
ws.ping();
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
hasOtherConnection(deadWs, projectId) {
|
|
278
|
+
for (const [ws, meta] of this.connections) if (ws !== deadWs && meta.projectId === projectId) return true;
|
|
279
|
+
return false;
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
//#endregion
|
|
283
|
+
//#region src/server/ws.ts
|
|
284
|
+
function createWsServer(port) {
|
|
285
|
+
const wss = new WebSocketServer({
|
|
286
|
+
port,
|
|
287
|
+
host: "127.0.0.1"
|
|
288
|
+
});
|
|
289
|
+
const heartbeat = new HeartbeatManager(wss, store);
|
|
290
|
+
wss.on("error", (err) => {
|
|
291
|
+
if (err.code === "EADDRINUSE") console.error(`[wdyr-mcp] Port ${port} already in use, another instance owns the WS server. Skipping.`);
|
|
292
|
+
else console.error("[wdyr-mcp] WS server error:", err);
|
|
293
|
+
});
|
|
294
|
+
wss.on("listening", () => {
|
|
295
|
+
console.error(`[wdyr-mcp] WebSocket server listening on ws://localhost:${port}`);
|
|
296
|
+
});
|
|
297
|
+
wss.on("connection", (ws) => {
|
|
298
|
+
console.error(`[wdyr-mcp] browser connected (ws://localhost:${port})`);
|
|
299
|
+
heartbeat.trackConnection(ws);
|
|
300
|
+
ws.on("message", (raw) => {
|
|
301
|
+
try {
|
|
302
|
+
const msg = JSON.parse(String(raw));
|
|
303
|
+
if (msg.type === "render") {
|
|
304
|
+
const projectId = msg.projectId ?? "default";
|
|
305
|
+
heartbeat.setProjectId(ws, projectId);
|
|
306
|
+
store.addRender(msg.payload, projectId);
|
|
307
|
+
}
|
|
308
|
+
} catch {
|
|
309
|
+
console.error("[wdyr-mcp] invalid message received");
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
ws.on("close", () => {
|
|
313
|
+
console.error("[wdyr-mcp] browser disconnected");
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
wss.on("close", () => {
|
|
317
|
+
heartbeat.stop();
|
|
318
|
+
});
|
|
319
|
+
return wss;
|
|
320
|
+
}
|
|
321
|
+
//#endregion
|
|
322
|
+
//#region src/server/index.ts
|
|
323
|
+
const DEFAULT_WS_PORT = 4649;
|
|
324
|
+
const server = new McpServer({
|
|
325
|
+
name: "why-did-you-render",
|
|
326
|
+
version: "0.0.0"
|
|
327
|
+
});
|
|
328
|
+
registerTools(server);
|
|
329
|
+
async function main() {
|
|
330
|
+
createWsServer(Number(process.env.WDYR_WS_PORT) || DEFAULT_WS_PORT);
|
|
331
|
+
const transport = new StdioServerTransport();
|
|
332
|
+
await server.connect(transport);
|
|
333
|
+
console.error("[wdyr-mcp] MCP server running on stdio");
|
|
334
|
+
}
|
|
335
|
+
main().catch((error) => {
|
|
336
|
+
console.error("[wdyr-mcp] Fatal error:", error);
|
|
337
|
+
process.exit(1);
|
|
338
|
+
});
|
|
339
|
+
//#endregion
|
|
340
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@0x1f320.sh/why-did-you-render-mcp",
|
|
3
|
+
"version": "1.0.0-dev.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "MCP server that collects why-did-you-render data from browser and exposes it to coding agents",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": {
|
|
8
|
+
"name": "0x1F320",
|
|
9
|
+
"email": "me@0x1f320.sh",
|
|
10
|
+
"url": "https://0x1f320.sh"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/0x1f320/why-did-you-render-mcp.git"
|
|
15
|
+
},
|
|
16
|
+
"homepage": "https://github.com/0x1f320/why-did-you-render-mcp#readme",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/0x1f320/why-did-you-render-mcp/issues"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"mcp",
|
|
22
|
+
"react",
|
|
23
|
+
"why-did-you-render",
|
|
24
|
+
"performance",
|
|
25
|
+
"re-render"
|
|
26
|
+
],
|
|
27
|
+
"bin": {
|
|
28
|
+
"why-did-you-render-mcp": "./dist/server/index.js"
|
|
29
|
+
},
|
|
30
|
+
"exports": {
|
|
31
|
+
".": "./dist/server/index.js",
|
|
32
|
+
"./client": {
|
|
33
|
+
"import": "./dist/client/index.js",
|
|
34
|
+
"require": "./dist/client/index.cjs"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"dist"
|
|
39
|
+
],
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsdown",
|
|
42
|
+
"dev": "tsdown --watch",
|
|
43
|
+
"check": "biome check .",
|
|
44
|
+
"check:fix": "biome check --fix .",
|
|
45
|
+
"typecheck": "tsc --noEmit",
|
|
46
|
+
"test": "vitest run",
|
|
47
|
+
"test:watch": "vitest",
|
|
48
|
+
"test:coverage": "vitest run --coverage"
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"@modelcontextprotocol/sdk": "^1.12.1",
|
|
52
|
+
"ws": "^8.18.0",
|
|
53
|
+
"zod": "^3.24.4"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@biomejs/biome": "^1.9.4",
|
|
57
|
+
"@types/node": "^22.14.1",
|
|
58
|
+
"@types/ws": "^8.18.0",
|
|
59
|
+
"@vitest/coverage-v8": "^4.1.2",
|
|
60
|
+
"semantic-release": "^25.0.3",
|
|
61
|
+
"tsdown": "^0.12.4",
|
|
62
|
+
"typescript": "^5.8.3",
|
|
63
|
+
"vitest": "^4.1.2"
|
|
64
|
+
},
|
|
65
|
+
"peerDependencies": {
|
|
66
|
+
"@welldone-software/why-did-you-render": "^10.0.1"
|
|
67
|
+
},
|
|
68
|
+
"packageManager": "pnpm@10.33.0+sha512.10568bb4a6afb58c9eb3630da90cc9516417abebd3fabbe6739f0ae795728da1491e9db5a544c76ad8eb7570f5c4bb3d6c637b2cb41bfdcdb47fa823c8649319",
|
|
69
|
+
"publishConfig": {
|
|
70
|
+
"access": "public"
|
|
71
|
+
},
|
|
72
|
+
"pnpm": {}
|
|
73
|
+
}
|