-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-mcp-protocol.mjs
More file actions
197 lines (174 loc) · 7.41 KB
/
Copy pathtest-mcp-protocol.mjs
File metadata and controls
197 lines (174 loc) · 7.41 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
/**
* Tests the MCP server via actual JSON-RPC protocol over stdio —
* exactly how Claude Code communicates with it.
* Uses newline-delimited JSON (the SDK's default stdio transport).
*/
import { spawn } from "child_process";
import { createInterface } from "readline";
const SERVER = spawn("node", ["dist/index.js"], {
cwd: "/Users/prem/projects/userflow-mcp",
stdio: ["pipe", "pipe", "pipe"],
});
let messageId = 0;
const pending = new Map();
const rl = createInterface({ input: SERVER.stdout });
rl.on("line", (line) => {
if (!line.trim()) return;
try {
const msg = JSON.parse(line);
const resolver = pending.get(msg.id);
if (resolver) {
resolver(msg);
pending.delete(msg.id);
}
} catch {}
});
SERVER.stderr.on("data", () => {}); // suppress stderr
function send(method, params = {}) {
const id = ++messageId;
const msg = JSON.stringify({ jsonrpc: "2.0", id, method, params });
SERVER.stdin.write(msg + "\n");
return new Promise((resolve) => {
pending.set(id, resolve);
setTimeout(() => {
if (pending.has(id)) {
pending.delete(id);
resolve({ error: "timeout" });
}
}, 60000);
});
}
function notify(method, params = {}) {
const msg = JSON.stringify({ jsonrpc: "2.0", method, params });
SERVER.stdin.write(msg + "\n");
}
async function main() {
console.log("━━━ MCP Protocol Test (Real JSON-RPC) ━━━\n");
// 1. Initialize
console.log("[1] initialize...");
const init = await send("initialize", {
protocolVersion: "2024-11-05",
capabilities: {},
clientInfo: { name: "claude-code-test", version: "1.0.0" },
});
console.log(` ✅ Server: ${init.result.serverInfo.name} v${init.result.serverInfo.version}`);
console.log(` Capabilities: tools=${!!init.result.capabilities.tools}`);
notify("notifications/initialized");
// 2. List tools
console.log("\n[2] tools/list...");
const tools = await send("tools/list");
const toolNames = tools.result.tools.map((t) => t.name);
console.log(` ✅ ${toolNames.length} tools: ${toolNames.join(", ")}`);
// 3. list_personas
console.log("\n[3] list_personas...");
const personas = await send("tools/call", { name: "list_personas", arguments: {} });
const personaText = personas.result.content[0].text;
const personaCount = (personaText.match(/### /g) || []).length;
console.log(` ✅ ${personaCount} personas loaded`);
// 4. start_session
console.log("\n[4] start_session (gitaguide.vercel.app, persona: Alex)...");
const session = await send("tools/call", {
name: "start_session",
arguments: { url: "https://gitaguide.vercel.app", persona: "Alex" },
});
if (session.error) {
console.log(` ❌ Error: ${JSON.stringify(session.error)}`);
SERVER.kill();
process.exit(1);
}
const textContent = session.result.content.find((c) => c.type === "text");
const imageContent = session.result.content.find((c) => c.type === "image");
const sessionIdMatch = textContent.text.match(/Session ID:\*\*\s*(\S+)/);
const sessionId = sessionIdMatch?.[1];
console.log(` ✅ Session: ${sessionId?.slice(0, 8)}...`);
console.log(` 📸 Screenshot: ${imageContent ? `${(imageContent.data.length / 1024).toFixed(0)}KB` : "missing"}`);
// Show page state excerpt
const urlMatch = textContent.text.match(/\*\*URL:\*\*\s*(\S+)/);
const titleMatch = textContent.text.match(/\*\*Title:\*\*\s*(.+)/);
console.log(` 📄 ${titleMatch?.[1] || "?"} — ${urlMatch?.[1] || "?"}`);
if (!sessionId) {
console.log(" ❌ No session ID, aborting");
SERVER.kill();
process.exit(1);
}
// 5. step: scroll
console.log("\n[5] step: scroll (Claude reasoning as Alex)...");
const step1 = await send("tools/call", {
name: "step",
arguments: {
session_id: sessionId,
action: "scroll",
thought: "The hero section says 'You are Arjuna' — interesting spiritual concept. Let me scroll to see what this app actually does and if there's a clear CTA.",
emotional_state: "curious",
},
});
const s1Text = step1.result.content.find((c) => c.type === "text");
const s1Img = step1.result.content.find((c) => c.type === "image");
console.log(` ✅ ${s1Text.text.split("\n")[0]}`);
console.log(` 📸 Screenshot: ${s1Img ? `${(s1Img.data.length / 1024).toFixed(0)}KB` : "missing"}`);
// 6. step: click to navigate
console.log("\n[6] step: click sign-in link...");
const step2 = await send("tools/call", {
name: "step",
arguments: {
session_id: sessionId,
action: "click",
target: "a[href*='login']",
thought: "I want to try this app but there's no 'Get Started' button — only 'Sign in'. As a first-timer, this is a bit confusing. Do I need an account already?",
emotional_state: "confused",
friction: [{
severity: "medium",
description: "No distinction between sign-up and sign-in for new users",
suggestion: "Add a clear 'Get Started Free' or 'Create Account' CTA separate from sign-in",
}],
},
});
const s2Text = step2.result.content.find((c) => c.type === "text");
const s2Img = step2.result.content.find((c) => c.type === "image");
console.log(` ✅ ${s2Text.text.split("\n")[0]}`);
console.log(` 📸 Screenshot: ${s2Img ? `${(s2Img.data.length / 1024).toFixed(0)}KB` : "missing"}`);
// 7. get_page_state
console.log("\n[7] get_page_state (re-examine login page)...");
const ps = await send("tools/call", {
name: "get_page_state",
arguments: { session_id: sessionId },
});
const psData = JSON.parse(ps.result.content.find((c) => c.type === "text").text);
const psImg = ps.result.content.find((c) => c.type === "image");
console.log(` ✅ URL: ${psData.url}`);
console.log(` 📋 Buttons: ${psData.buttons?.length}, Forms: ${psData.formFields?.length}, Links: ${psData.links?.length}`);
console.log(` 📸 Screenshot: ${psImg ? `${(psImg.data.length / 1024).toFixed(0)}KB` : "missing"}`);
// 8. end_session
console.log("\n[8] end_session...");
const end = await send("tools/call", {
name: "end_session",
arguments: {
session_id: sessionId,
goal_achieved: false,
summary: "Alex explored the landing page and navigated to login. The spiritual theme is beautiful but the first-time user path needs work — no clear 'Get Started' CTA, and sign-in vs sign-up is ambiguous.",
},
});
const report = end.result.content[0].text;
console.log(` ✅ Report generated (${report.length} chars)`);
// Print report preview
console.log(`\n━━━ Session Report (preview) ━━━\n`);
console.log(report.split("\n").slice(0, 30).join("\n"));
console.log(`\n... (${report.split("\n").length - 30} more lines)`);
console.log(`\n━━━ All MCP Tools Verified ━━━`);
console.log(`✅ initialize — server handshake`);
console.log(`✅ tools/list — ${toolNames.length} tools registered`);
console.log(`✅ list_personas — ${personaCount} personas`);
console.log(`✅ start_session — browser + screenshot + persona`);
console.log(`✅ step (scroll) — action + new screenshot`);
console.log(`✅ step (click) — navigation + friction recording`);
console.log(`✅ get_page_state — passive observation`);
console.log(`✅ end_session — report generation`);
console.log(`\n🎉 UserFlow MCP v0.2 is production-ready.\n`);
SERVER.kill();
process.exit(0);
}
main().catch((err) => {
console.error("Test failed:", err);
SERVER.kill();
process.exit(1);
});