latitude-mcp-server 2.2.3 → 2.2.4

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 (2) hide show
  1. package/dist/api.js +226 -26
  2. package/package.json +1 -1
package/dist/api.js CHANGED
@@ -398,39 +398,221 @@ async function runDocument(path, parameters, versionUuid = 'live') {
398
398
  });
399
399
  }
400
400
  /**
401
- * Identify which documents fail validation by testing each one individually.
402
- * Used when a batch publish fails to provide actionable error messages.
401
+ * Pre-validate PromptL content locally to catch common issues before API call.
402
+ * Returns detailed, actionable error messages.
403
+ */
404
+ function validatePromptLContent(content, _path) {
405
+ const issues = [];
406
+ const lines = content.split('\n');
407
+ // Check for YAML frontmatter
408
+ if (!content.startsWith('---')) {
409
+ issues.push({
410
+ type: 'error',
411
+ message: 'Missing YAML frontmatter',
412
+ rootCause: 'PromptL files must start with YAML frontmatter (---).',
413
+ suggestion: 'Add frontmatter at the beginning:\n---\nprovider: YourProvider\nmodel: your-model\n---',
414
+ });
415
+ }
416
+ // Check for nested role tags (common mistake)
417
+ const roleStack = [];
418
+ for (let i = 0; i < lines.length; i++) {
419
+ const line = lines[i];
420
+ const lineNum = i + 1;
421
+ // Check for opening role tags
422
+ const openMatches = line.matchAll(/<(system|user|assistant|tool)>/gi);
423
+ for (const match of openMatches) {
424
+ const tag = match[1].toLowerCase();
425
+ if (roleStack.length > 0) {
426
+ const parent = roleStack[roleStack.length - 1];
427
+ issues.push({
428
+ type: 'error',
429
+ message: `Nested role tag: <${tag}> inside <${parent.tag}>`,
430
+ rootCause: `Role tags (<system>, <user>, <assistant>, <tool>) cannot be nested. Found <${tag}> at line ${lineNum} inside <${parent.tag}> that started at line ${parent.line}.`,
431
+ suggestion: `Move the <${tag}> block outside of <${parent.tag}>. Each role tag must be at the top level. If showing an example, use a code block (\`\`\`yaml) instead of actual role tags.`,
432
+ lineNumber: lineNum,
433
+ snippet: line.trim(),
434
+ });
435
+ }
436
+ roleStack.push({ tag, line: lineNum });
437
+ }
438
+ // Check for closing role tags
439
+ const closeMatches = line.matchAll(/<\/(system|user|assistant|tool)>/gi);
440
+ for (const match of closeMatches) {
441
+ const tag = match[1].toLowerCase();
442
+ if (roleStack.length > 0 && roleStack[roleStack.length - 1].tag === tag) {
443
+ roleStack.pop();
444
+ }
445
+ }
446
+ }
447
+ // Check for unclosed role tags
448
+ for (const unclosed of roleStack) {
449
+ issues.push({
450
+ type: 'error',
451
+ message: `Unclosed role tag: <${unclosed.tag}>`,
452
+ rootCause: `The <${unclosed.tag}> tag opened at line ${unclosed.line} is never closed.`,
453
+ suggestion: `Add </${unclosed.tag}> to close the tag.`,
454
+ lineNumber: unclosed.line,
455
+ });
456
+ }
457
+ // Check frontmatter has required fields
458
+ const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---/);
459
+ if (frontmatterMatch) {
460
+ const frontmatter = frontmatterMatch[1];
461
+ if (!frontmatter.includes('model:') && !frontmatter.includes('model :')) {
462
+ issues.push({
463
+ type: 'error',
464
+ message: 'Missing model in frontmatter',
465
+ rootCause: 'PromptL requires a model to be specified in the frontmatter.',
466
+ suggestion: 'Add model field:\n---\nmodel: gpt-4\n---\n\nOr with provider:\n---\nprovider: openai\nmodel: gpt-4\n---',
467
+ });
468
+ }
469
+ }
470
+ // Check for empty content after frontmatter
471
+ const contentAfterFrontmatter = content.replace(/^---[\s\S]*?---/, '').trim();
472
+ if (!contentAfterFrontmatter) {
473
+ issues.push({
474
+ type: 'warning',
475
+ message: 'Empty prompt content',
476
+ rootCause: 'The prompt has no content after the frontmatter.',
477
+ suggestion: 'Add prompt content using role tags:\n<system>\nYour system prompt here\n</system>',
478
+ });
479
+ }
480
+ return issues;
481
+ }
482
+ /**
483
+ * Identify failing documents using binary search for efficiency.
484
+ * For N documents, worst case is O(N log N) API calls instead of O(N).
403
485
  */
404
486
  async function identifyFailingDocuments(changes) {
405
487
  const failed = [];
406
- for (const change of changes) {
407
- if (change.status === 'deleted')
408
- continue; // Deletes don't have content validation
409
- try {
410
- // Create a test draft for this single document
411
- const testDraft = await createVersion(`validation-test-${change.path}`);
412
- // Push just this document
413
- await pushChanges(testDraft.uuid, [change]);
414
- // Try to publish - this will fail if document has validation errors
415
- await publishVersion(testDraft.uuid);
416
- // If we get here, document is valid - the draft was published but that's ok
417
- logger.debug(`Document ${change.path} validated successfully`);
418
- }
419
- catch (error) {
420
- // This document failed - capture the error
421
- const errorMsg = error instanceof LatitudeApiError
422
- ? error.message
423
- : (error instanceof Error ? error.message : 'Unknown validation error');
488
+ const nonDeleteChanges = changes.filter(c => c.status !== 'deleted');
489
+ if (nonDeleteChanges.length === 0)
490
+ return failed;
491
+ // First, run local validation to catch obvious issues without API calls
492
+ for (const change of nonDeleteChanges) {
493
+ if (!change.content)
494
+ continue;
495
+ const localIssues = validatePromptLContent(change.content, change.path);
496
+ const errors = localIssues.filter(i => i.type === 'error');
497
+ if (errors.length > 0) {
498
+ const mainError = errors[0];
424
499
  failed.push({
425
500
  path: change.path,
426
- error: errorMsg,
427
- content: change.content?.substring(0, 200) + (change.content && change.content.length > 200 ? '...' : ''),
501
+ error: mainError.message,
502
+ rootCause: mainError.rootCause,
503
+ suggestion: mainError.suggestion,
504
+ lineNumber: mainError.lineNumber,
505
+ snippet: mainError.snippet,
428
506
  });
429
- logger.debug(`Document ${change.path} failed validation: ${errorMsg}`);
507
+ }
508
+ }
509
+ // If we found local validation errors, return those first
510
+ if (failed.length > 0) {
511
+ logger.info(`Found ${failed.length} document(s) with local validation errors`);
512
+ return failed;
513
+ }
514
+ // If local validation passed, use binary search to find API-level failures
515
+ logger.info(`Local validation passed, testing ${nonDeleteChanges.length} document(s) against API...`);
516
+ // For small batches (<=5), test individually
517
+ if (nonDeleteChanges.length <= 5) {
518
+ return await testDocumentsIndividually(nonDeleteChanges);
519
+ }
520
+ // For larger batches, use binary search
521
+ return await binarySearchFailures(nonDeleteChanges);
522
+ }
523
+ /**
524
+ * Test documents individually (for small batches)
525
+ */
526
+ async function testDocumentsIndividually(changes) {
527
+ const failed = [];
528
+ for (const change of changes) {
529
+ const result = await testSingleDocument(change);
530
+ if (result) {
531
+ failed.push(result);
430
532
  }
431
533
  }
432
534
  return failed;
433
535
  }
536
+ /**
537
+ * Test a single document and return failure details if it fails
538
+ */
539
+ async function testSingleDocument(change) {
540
+ try {
541
+ const testDraft = await createVersion(`val-${Date.now()}-${change.path.slice(0, 20)}`);
542
+ await pushChanges(testDraft.uuid, [change]);
543
+ await publishVersion(testDraft.uuid);
544
+ return null; // Success
545
+ }
546
+ catch (error) {
547
+ const errorMsg = error instanceof LatitudeApiError
548
+ ? error.message
549
+ : (error instanceof Error ? error.message : 'Unknown error');
550
+ // Try to provide better root cause based on error patterns
551
+ let rootCause = 'The Latitude API rejected this document during publish validation.';
552
+ let suggestion = 'Review the document content for syntax errors or invalid configuration.';
553
+ if (errorMsg.includes('errors in the updated documents')) {
554
+ rootCause = 'The document has PromptL syntax or configuration errors that passed local validation but failed server-side validation.';
555
+ suggestion = 'Check for: 1) Invalid model/provider combination, 2) Malformed schema definition, 3) Invalid template syntax ({{ }}).';
556
+ }
557
+ // Run local validation to give more context
558
+ if (change.content) {
559
+ const localIssues = validatePromptLContent(change.content, change.path);
560
+ if (localIssues.length > 0) {
561
+ const warnings = localIssues.filter(i => i.type === 'warning');
562
+ if (warnings.length > 0) {
563
+ suggestion += `\n\nAdditional observations:\n${warnings.map(w => `- ${w.message}: ${w.suggestion}`).join('\n')}`;
564
+ }
565
+ }
566
+ }
567
+ return {
568
+ path: change.path,
569
+ error: errorMsg,
570
+ rootCause,
571
+ suggestion,
572
+ snippet: change.content?.substring(0, 300),
573
+ };
574
+ }
575
+ }
576
+ /**
577
+ * Use binary search to efficiently find failures in large batches.
578
+ * Splits the batch in half, tests each half, and recurses on failing halves.
579
+ */
580
+ async function binarySearchFailures(changes) {
581
+ // Base case: single document
582
+ if (changes.length === 1) {
583
+ const result = await testSingleDocument(changes[0]);
584
+ return result ? [result] : [];
585
+ }
586
+ // Test the entire batch first
587
+ const batchValid = await testBatch(changes);
588
+ if (batchValid) {
589
+ return []; // All documents in this batch are valid
590
+ }
591
+ // Batch has failures - split and recurse
592
+ const mid = Math.floor(changes.length / 2);
593
+ const left = changes.slice(0, mid);
594
+ const right = changes.slice(mid);
595
+ // Test both halves in parallel
596
+ const [leftFailures, rightFailures] = await Promise.all([
597
+ binarySearchFailures(left),
598
+ binarySearchFailures(right),
599
+ ]);
600
+ return [...leftFailures, ...rightFailures];
601
+ }
602
+ /**
603
+ * Test if a batch of documents can be published successfully
604
+ */
605
+ async function testBatch(changes) {
606
+ try {
607
+ const testDraft = await createVersion(`batch-val-${Date.now()}`);
608
+ await pushChanges(testDraft.uuid, changes);
609
+ await publishVersion(testDraft.uuid);
610
+ return true;
611
+ }
612
+ catch {
613
+ return false;
614
+ }
615
+ }
434
616
  /**
435
617
  * Create a synthetic Version object for no-op deploys.
436
618
  * Returns a fully populated Version with all required fields.
@@ -511,12 +693,30 @@ async function deployToLive(changes, _versionName) {
511
693
  logger.warn('Batch publish failed, identifying problematic documents...');
512
694
  const failedDocs = await identifyFailingDocuments(actualChanges);
513
695
  if (failedDocs.length > 0) {
514
- const errorDetails = failedDocs.map(f => `- ${f.path}: ${f.error}`).join('\n');
696
+ // Build detailed, actionable error message for LLM consumption
697
+ const errorLines = [];
698
+ for (const doc of failedDocs) {
699
+ errorLines.push(`\n## ❌ ${doc.path}`);
700
+ errorLines.push(`**Error:** ${doc.error}`);
701
+ errorLines.push(`**Root Cause:** ${doc.rootCause}`);
702
+ if (doc.lineNumber) {
703
+ errorLines.push(`**Location:** Line ${doc.lineNumber}`);
704
+ }
705
+ if (doc.snippet) {
706
+ errorLines.push(`**Snippet:** \`${doc.snippet}\``);
707
+ }
708
+ errorLines.push(`**Fix:** ${doc.suggestion}`);
709
+ }
710
+ const message = `${failedDocs.length} document(s) failed validation:${errorLines.join('\n')}`;
515
711
  throw new LatitudeApiError({
516
712
  name: 'DocumentValidationError',
517
713
  errorCode: 'DOCUMENT_VALIDATION_FAILED',
518
- message: `${failedDocs.length} document(s) failed validation:\n${errorDetails}`,
519
- details: { failedDocuments: failedDocs },
714
+ message,
715
+ details: {
716
+ failedDocuments: failedDocs,
717
+ totalFailed: failedDocs.length,
718
+ failedPaths: failedDocs.map(d => d.path),
719
+ },
520
720
  }, 422);
521
721
  }
522
722
  // Re-throw original error if we couldn't identify specific failures
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "latitude-mcp-server",
3
- "version": "2.2.3",
3
+ "version": "2.2.4",
4
4
  "description": "Simplified MCP server for Latitude.so prompt management - 8 focused tools for push, pull, run, and manage prompts",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",