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 
71a1d23
 
 
 
 
 
 
 
c6a01e9
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import "dotenv/config";
import {
  loadPersonas,
  getPersona,
  formatPersonaSummary,
  searchPersonas,
  type Persona,
} from "./utils/persona_loader.js";
import { generateUserStory, type UserStoryInput } from "./tools/user_story.js";
import {
  generateCustomerExperienceTale,
  type CustomerExperienceTaleInput,
} from "./tools/customer_experience_tale.js";
import {
  generateFeatureImpactStory,
  type FeatureImpactStoryInput,
} from "./tools/feature_impact_story.js";
import {
  generateJourneyMapStory,
  type JourneyMapStoryInput,
} from "./tools/journey_map_story.js";
import { generatePersonas } from "./tools/generate_personas.js";

interface ToolSchema {
  name: string;
  description: string;
  input_schema: {
    type: string;
    properties: Record<string, any>;
    required: string[];
  };
}

interface ResourceSchema {
  uri: string;
  name: string;
  description: string;
  mimeType: string;
}

class HealixPathServer {
  private personas: Map<string, Persona>;

  constructor() {
    this.personas = loadPersonas();
  }

  getTools(): ToolSchema[] {
    return [
      {
        name: "list_personas",
        description:
          "List all available personas with their key attributes (id, name, age, location, job title, industry, tech literacy, goals, pain points)",
        input_schema: {
          type: "object",
          properties: {
            limit: {
              type: "number",
              description: "Optional: Maximum number of personas to return (default: all)",
            },
          },
          required: [],
        },
      },
      {
        name: "search_personas",
        description:
          "Search and filter personas by role/job title, industry, age range, tech literacy, or location",
        input_schema: {
          type: "object",
          properties: {
            role: {
              type: "string",
              description: "Filter by job title or role (partial match, case-insensitive)",
            },
            industry: {
              type: "string",
              description: "Filter by industry (partial match, case-insensitive)",
            },
            age_range: {
              type: "string",
              description: "Filter by age range (format: 'min-max', e.g., '25-35')",
            },
            tech_literacy: {
              type: "string",
              description: "Filter by tech literacy level (e.g., 'high', 'moderate', 'low')",
            },
            location: {
              type: "string",
              description: "Filter by location - city, state, or country (partial match)",
            },
          },
          required: [],
        },
      },
      {
        name: "user_story",
        description:
          "Generate a structured narrative user story with acceptance criteria and risks",
        input_schema: {
          type: "object",
          properties: {
            product: {
              type: "string",
              description: "Description of the product or feature",
            },
            persona_id: {
              type: "string",
              description:
                "ID of the persona to use (use list_personas to see available IDs)",
            },
            persona_override: {
              type: "string",
              description:
                "Custom persona description if persona_id is not provided",
            },
          },
          required: ["product"],
        },
      },
      {
        name: "customer_experience_tale",
        description:
          "Create a narrative describing a persona experiencing a specific problem",
        input_schema: {
          type: "object",
          properties: {
            problem: {
              type: "string",
              description: "The problem the persona is experiencing",
            },
            persona_id: {
              type: "string",
              description: "ID of the persona to use",
            },
            persona_override: {
              type: "string",
              description:
                "Custom persona description if persona_id is not provided",
            },
          },
          required: ["problem"],
        },
      },
      {
        name: "feature_impact_story",
        description:
          "Show the before and after of a persona interacting with a feature",
        input_schema: {
          type: "object",
          properties: {
            feature_description: {
              type: "string",
              description: "Description of the feature",
            },
            persona_id: {
              type: "string",
              description: "ID of the persona to use",
            },
            persona_override: {
              type: "string",
              description:
                "Custom persona description if persona_id is not provided",
            },
          },
          required: ["feature_description"],
        },
      },
      {
        name: "journey_map_story",
        description:
          "Generate a narrative for a specific stage of the user journey",
        input_schema: {
          type: "object",
          properties: {
            stage: {
              type: "string",
              description:
                "Journey stage (e.g., awareness, consideration, adoption, retention)",
            },
            product: {
              type: "string",
              description: "Description of the product",
            },
            persona_id: {
              type: "string",
              description: "ID of the persona to use",
            },
            persona_override: {
              type: "string",
              description:
                "Custom persona description if persona_id is not provided",
            },
          },
          required: ["stage", "product"],
        },
      },
      {
        name: "generate_personas",
        description:
          "Generate random user personas based on a natural language description. Creates realistic, diverse personas matching the specified criteria with randomized attributes for variety.",
        input_schema: {
          type: "object",
          properties: {
            description: {
              type: "string",
              description:
                "Natural language description of the personas to generate (e.g., 'people who love to cook but never have time to in their 20-30s', 'sandwich enthusiasts who are 50+ years old')",
            },
            count: {
              type: "number",
              description:
                "Number of personas to generate (minimum: 1, maximum: 5, default: 1)",
            },
          },
          required: ["description", "count"],
        },
      },
    ];
  }

  getResources(): ResourceSchema[] {
    const resources: ResourceSchema[] = [
      {
        uri: "personas://all",
        name: "All Personas",
        description: "Complete list of all available personas with their full details",
        mimeType: "application/json",
      },
    ];

    // Add individual persona resources
    for (const [id, persona] of this.personas.entries()) {
      resources.push({
        uri: `personas://${id}`,
        name: `${persona.name} - ${persona.job?.title || persona.role || "User"}`,
        description: `Detailed information about ${persona.name}`,
        mimeType: "application/json",
      });
    }

    return resources;
  }

  async readResource(uri: string): Promise<string> {
    if (uri === "personas://all") {
      const allPersonas = Array.from(this.personas.values()).map(formatPersonaSummary);
      return JSON.stringify(allPersonas, null, 2);
    }

    // Handle individual persona URIs
    if (uri.startsWith("personas://")) {
      const personaId = uri.replace("personas://", "");
      const persona = this.personas.get(personaId);
      
      if (!persona) {
        return JSON.stringify({ error: `Persona not found: ${personaId}` });
      }

      return JSON.stringify(persona, null, 2);
    }

    return JSON.stringify({ error: `Unknown resource URI: ${uri}` });
  }

  async processTool(
    toolName: string,
    toolInput: Record<string, unknown>
  ): Promise<string> {
    // Handle list_personas tool
    if (toolName === "list_personas") {
      const limit = typeof toolInput.limit === "number" ? toolInput.limit : undefined;
      const allPersonas = Array.from(this.personas.values()).map(formatPersonaSummary);
      const personas = limit ? allPersonas.slice(0, limit) : allPersonas;
      return JSON.stringify({ personas, total: this.personas.size }, null, 2);
    }

    // Handle search_personas tool
    if (toolName === "search_personas") {
      const filters = {
        role: typeof toolInput.role === "string" ? toolInput.role : undefined,
        industry: typeof toolInput.industry === "string" ? toolInput.industry : undefined,
        age_range: typeof toolInput.age_range === "string" ? toolInput.age_range : undefined,
        tech_literacy: typeof toolInput.tech_literacy === "string" ? toolInput.tech_literacy : undefined,
        location: typeof toolInput.location === "string" ? toolInput.location : undefined,
      };

      const results = searchPersonas(this.personas, filters);
      const summaries = results.map(formatPersonaSummary);
      
      return JSON.stringify({ 
        personas: summaries, 
        count: summaries.length,
        filters: Object.fromEntries(Object.entries(filters).filter(([_, v]) => v !== undefined))
      }, null, 2);
    }

    let persona: Persona | undefined;

    // Load persona if specified
    if (typeof toolInput.persona_id === "string") {
      const loadedPersona = getPersona(toolInput.persona_id, this.personas);
      if (!loadedPersona) {
        return JSON.stringify({
          error: `Persona not found: ${toolInput.persona_id}`,
        });
      }
      persona = loadedPersona;
    }

    try {
      switch (toolName) {
        case "user_story": {
          const input = toolInput as unknown as UserStoryInput;
          const userStory = await generateUserStory(input, persona);
          return JSON.stringify(userStory);
        }

        case "customer_experience_tale": {
          const input = toolInput as unknown as CustomerExperienceTaleInput;
          const tale = await generateCustomerExperienceTale(input, persona);
          return JSON.stringify(tale);
        }

        case "feature_impact_story": {
          const input = toolInput as unknown as FeatureImpactStoryInput;
          const impactStory = await generateFeatureImpactStory(input, persona);
          return JSON.stringify(impactStory);
        }

        case "journey_map_story": {
          const input = toolInput as unknown as JourneyMapStoryInput;
          const journeyStory = await generateJourneyMapStory(input, persona);
          return JSON.stringify(journeyStory);
        }

        case "generate_personas": {
          const description = typeof toolInput.description === "string" ? toolInput.description : "";
          const count = typeof toolInput.count === "number" ? toolInput.count : 1;
          
          if (!description) {
            return JSON.stringify({ error: "Description is required" });
          }
          
          const personas = await generatePersonas({ description, count });
          return personas; // Already JSON stringified
        }

        default:
          return JSON.stringify({ error: `Unknown tool: ${toolName}` });
      }
    } catch (error) {
      return JSON.stringify({
        error: `Failed to process tool: ${error instanceof Error ? error.message : String(error)}`,
      });
    }
  }
}

// Example usage and server setup
async function main(): Promise<void> {
  const server = new HealixPathServer();

  console.log("🚀 HealixPath MCP Server");
  console.log("=======================");
  console.log("\nAvailable Tools:");
  server.getTools().forEach((tool) => {
    console.log(`\n- ${tool.name}`);
    console.log(`  ${tool.description}`);
  });

  console.log("\n\nAvailable Resources:");
  server.getResources().slice(0, 5).forEach((resource) => {
    console.log(`\n- ${resource.uri}`);
    console.log(`  ${resource.description}`);
  });
  console.log(`\n... and ${server.getResources().length - 5} more persona resources`);

  console.log("\n\n📝 Example Tool Calls:");
  console.log("---");

  // Example 0: List Personas
  const listResult = await server.processTool("list_personas", { limit: 3 });
  console.log("\n0. List Personas (first 3):");
  console.log(listResult);

  // Example 0b: Search Personas
  const searchResult = await server.processTool("search_personas", {
    industry: "healthcare",
    tech_literacy: "moderate"
  });
  console.log("\n0b. Search Personas (Healthcare + Moderate Tech):");
  console.log(searchResult);

  // Example 0c: Read Resource
  const resourceResult = await server.readResource("personas://all");
  console.log("\n0c. Read Resource (all personas summary - truncated):");
  console.log(resourceResult.substring(0, 500) + "...");

  // Example 1: User Story
  const userStoryResult = await server.processTool("user_story", {
    product: "Healthcare appointment scheduling app",
    persona_id: "persona-001",
  });
  console.log("\n1. User Story:");
  console.log(userStoryResult);

  // Example 2: Customer Experience Tale
  const taleResult = await server.processTool("customer_experience_tale", {
    problem: "Mobile app crashes during checkout",
    persona_id: "persona-001",
  });
  console.log("\n2. Customer Experience Tale:");
  console.log(taleResult);

  // Example 3: Feature Impact Story
  const impactResult = await server.processTool("feature_impact_story", {
    feature_description: "One-click scheduling with automatic reminders",
    persona_id: "persona-001",
  });
  console.log("\n3. Feature Impact Story:");
  console.log(impactResult);

  // Example 4: Journey Map Story
  const journeyResult = await server.processTool("journey_map_story", {
    stage: "adoption",
    product: "Healthcare platform",
    persona_id: "persona-003",
  });
  console.log("\n4. Journey Map Story:");
  console.log(journeyResult);

  // Example 5: Generate Personas
  const generateResult = await server.processTool("generate_personas", {
    description: "people who love to cook but never have time to in their 20-30s",
    count: 2,
  });
  console.log("\n5. Generate Personas (cooking enthusiasts, 20-30s, time-constrained):");
  console.log(generateResult);

  console.log("\n\n✅ HealixPath Server is ready to serve MCP clients!");
}

main().catch(console.error);