@sanctuary-framework/mcp-server 0.4.1 → 0.5.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/dist/index.d.cts CHANGED
@@ -1363,48 +1363,116 @@ declare function classifyField(fieldName: string): FieldClassification;
1363
1363
  declare function recommendPolicy(context: Record<string, unknown>, provider?: string): PolicyRecommendation;
1364
1364
 
1365
1365
  /**
1366
- * Sanctuary MCP Server — In-Memory Storage Backend
1367
- *
1368
- * Used for testing. Implements the same interface as filesystem storage
1369
- * but stores everything in memory. Data does not persist across restarts.
1370
- */
1371
-
1372
- declare class MemoryStorage implements StorageBackend {
1373
- private store;
1374
- private storageKey;
1375
- write(namespace: string, key: string, data: Uint8Array): Promise<void>;
1376
- read(namespace: string, key: string): Promise<Uint8Array | null>;
1377
- delete(namespace: string, key: string, _secureOverwrite?: boolean): Promise<boolean>;
1378
- list(namespace: string, prefix?: string): Promise<StorageEntryMeta[]>;
1379
- exists(namespace: string, key: string): Promise<boolean>;
1380
- totalSize(): Promise<number>;
1381
- /** Clear all stored data (useful in tests) */
1382
- clear(): void;
1383
- }
1384
-
1385
- /**
1386
- * Sanctuary MCP Server — Filesystem Storage Backend
1366
+ * Sanctuary MCP Server — Prompt Injection Detection Layer
1387
1367
  *
1388
- * Default storage backend using the local filesystem.
1389
- * Files are stored as: {basePath}/{namespace}/{key}.enc
1368
+ * Fast, zero-dependency detection of common prompt injection patterns.
1369
+ * Scans tool arguments for role override, security bypass, encoding evasion,
1370
+ * data exfiltration, and prompt stuffing signals.
1390
1371
  *
1391
1372
  * Security invariants:
1392
- * - Secure deletion overwrites file content with random bytes before unlinking
1393
- * - Directory creation uses restrictive permissions (0o700)
1394
- * - File creation uses restrictive permissions (0o600)
1395
- */
1396
-
1397
- declare class FilesystemStorage implements StorageBackend {
1398
- private basePath;
1399
- constructor(basePath: string);
1400
- private entryPath;
1401
- private namespacePath;
1402
- write(namespace: string, key: string, data: Uint8Array): Promise<void>;
1403
- read(namespace: string, key: string): Promise<Uint8Array | null>;
1404
- delete(namespace: string, key: string, secureOverwrite?: boolean): Promise<boolean>;
1405
- list(namespace: string, prefix?: string): Promise<StorageEntryMeta[]>;
1406
- exists(namespace: string, key: string): Promise<boolean>;
1407
- totalSize(): Promise<number>;
1373
+ * - Always returns a result, never throws
1374
+ * - Typical scan completes in < 5ms
1375
+ * - False positives minimized via field-aware scanning
1376
+ * - Recursive scanning of nested objects/arrays
1377
+ */
1378
+ interface InjectionDetectorConfig {
1379
+ enabled: boolean;
1380
+ sensitivity: "low" | "medium" | "high";
1381
+ on_detection: "escalate" | "block" | "log";
1382
+ custom_patterns?: string[];
1383
+ }
1384
+ interface InjectionSignal {
1385
+ type: string;
1386
+ pattern: string;
1387
+ location: string;
1388
+ severity: "low" | "medium" | "high";
1389
+ }
1390
+ interface DetectionResult {
1391
+ flagged: boolean;
1392
+ confidence: number;
1393
+ signals: InjectionSignal[];
1394
+ recommendation: "allow" | "escalate" | "block";
1395
+ }
1396
+ declare class InjectionDetector {
1397
+ private config;
1398
+ private stats;
1399
+ constructor(config?: Partial<InjectionDetectorConfig>);
1400
+ /**
1401
+ * Scan tool arguments for injection signals.
1402
+ * @param toolName Full tool name (e.g., "sanctuary/state_read")
1403
+ * @param args Tool arguments
1404
+ * @returns DetectionResult with all detected signals
1405
+ */
1406
+ scan(toolName: string, args: Record<string, unknown>): DetectionResult;
1407
+ /**
1408
+ * Recursively scan a value and all nested values.
1409
+ */
1410
+ private scanValue;
1411
+ /**
1412
+ * Scan a single string for injection signals.
1413
+ */
1414
+ private scanString;
1415
+ /**
1416
+ * Detect base64 strings and zero-width character evasion.
1417
+ */
1418
+ private detectEncodingEvasion;
1419
+ /**
1420
+ * Detect URLs and emails in fields that shouldn't have them.
1421
+ */
1422
+ private detectDataExfiltration;
1423
+ /**
1424
+ * Detect prompt stuffing: very large strings or high repetition.
1425
+ */
1426
+ private detectPromptStuffing;
1427
+ /**
1428
+ * Determine if this field is inherently safe from role override.
1429
+ */
1430
+ private isSafeField;
1431
+ /**
1432
+ * Determine if this is a tool name field (where tool refs are expected).
1433
+ */
1434
+ private isToolNameField;
1435
+ /**
1436
+ * Determine if this field is safe for URLs.
1437
+ */
1438
+ private isUrlSafeField;
1439
+ /**
1440
+ * Determine if this field is safe for emails.
1441
+ */
1442
+ private isEmailSafeField;
1443
+ /**
1444
+ * Determine if this field is safe for structured data (JSON/XML).
1445
+ */
1446
+ private isStructuredField;
1447
+ /**
1448
+ * SEC-032: Map common cross-script confusable characters to their Latin equivalents.
1449
+ * NFKC normalization handles fullwidth and compatibility forms, but does NOT map
1450
+ * Cyrillic/Greek lookalikes to Latin (they're distinct codepoints by design).
1451
+ * This covers the most common confusables used in injection evasion.
1452
+ */
1453
+ private normalizeConfusables;
1454
+ /**
1455
+ * Compute confidence score based on signals.
1456
+ * More high-severity signals = higher confidence.
1457
+ */
1458
+ private computeConfidence;
1459
+ /**
1460
+ * Compute recommendation based on signals and sensitivity.
1461
+ */
1462
+ private computeRecommendation;
1463
+ /**
1464
+ * Get statistics about scans performed.
1465
+ */
1466
+ getStats(): {
1467
+ total_scans: number;
1468
+ total_flags: number;
1469
+ total_blocks: number;
1470
+ signals_by_type: Record<string, number>;
1471
+ };
1472
+ /**
1473
+ * Reset statistics.
1474
+ */
1475
+ resetStats(): void;
1408
1476
  }
1409
1477
 
1410
1478
  /**
@@ -1640,12 +1708,20 @@ declare class BaselineTracker {
1640
1708
  * - All gate decisions (approve, deny, allow) are audit-logged.
1641
1709
  */
1642
1710
 
1711
+ /** Callback invoked when an injection is detected, for dashboard broadcasting */
1712
+ type InjectionAlertCallback = (alert: {
1713
+ toolName: string;
1714
+ result: DetectionResult;
1715
+ timestamp: string;
1716
+ }) => void;
1643
1717
  declare class ApprovalGate {
1644
1718
  private policy;
1645
1719
  private baseline;
1646
1720
  private channel;
1647
1721
  private auditLog;
1648
- constructor(policy: PrincipalPolicy, baseline: BaselineTracker, channel: ApprovalChannel, auditLog: AuditLog);
1722
+ private injectionDetector;
1723
+ private onInjectionAlert?;
1724
+ constructor(policy: PrincipalPolicy, baseline: BaselineTracker, channel: ApprovalChannel, auditLog: AuditLog, injectionDetector?: InjectionDetector, onInjectionAlert?: InjectionAlertCallback);
1649
1725
  /**
1650
1726
  * Evaluate a tool call against the Principal Policy.
1651
1727
  *
@@ -1669,6 +1745,189 @@ declare class ApprovalGate {
1669
1745
  private summarizeArgs;
1670
1746
  /** Get the baseline tracker for saving at session end */
1671
1747
  getBaseline(): BaselineTracker;
1748
+ /** Get the injection detector for stats/configuration access */
1749
+ getInjectionDetector(): InjectionDetector;
1750
+ }
1751
+
1752
+ /**
1753
+ * Sanctuary MCP Server — Tool Router
1754
+ *
1755
+ * Routes sanctuary/* tool calls to their layer-specific handlers.
1756
+ * Every tool call passes through schema validation and the ApprovalGate
1757
+ * (if configured) before execution. Neither can be bypassed.
1758
+ *
1759
+ * This module is the abstraction boundary for MCP SDK version migration —
1760
+ * if the SDK API changes, only this module needs updating.
1761
+ */
1762
+
1763
+ /** Tool handler function signature */
1764
+ type ToolHandler = (args: Record<string, unknown>) => Promise<{
1765
+ content: Array<{
1766
+ type: "text";
1767
+ text: string;
1768
+ }>;
1769
+ }>;
1770
+
1771
+ /**
1772
+ * Sanctuary MCP Server — L2 Context Gating: Automatic Enforcer
1773
+ *
1774
+ * The context gate enforcer wraps tool handlers to automatically filter
1775
+ * their arguments before execution. Unlike context_gate_filter (which agents
1776
+ * call voluntarily), the enforcer runs automatically on every tool call
1777
+ * when enabled.
1778
+ *
1779
+ * This enforces minimum-necessary-context by default and makes bypassing
1780
+ * context protection explicit (requires reconfiguration).
1781
+ *
1782
+ * Security invariants:
1783
+ * - The enforcer wraps every tool handler when enabled
1784
+ * - Filtering decisions are audit-logged
1785
+ * - Default action on missing policy: fallback to built-in sensitive patterns
1786
+ * - Denied fields block the entire request (with logged reason)
1787
+ * - Redacted fields are stripped from tool arguments
1788
+ * - log_only mode logs what would be filtered but passes original args
1789
+ */
1790
+
1791
+ interface EnforcerConfig {
1792
+ /** Enable/disable automatic filtering (default: true) */
1793
+ enabled: boolean;
1794
+ /** Policy ID to use when no specific one is set */
1795
+ default_policy_id?: string;
1796
+ /** Tool name prefixes to skip filtering (e.g., ["sanctuary/"] to skip system tools) */
1797
+ bypass_prefixes: string[];
1798
+ /** Log but don't filter — for gradual rollout (default: false) */
1799
+ log_only: boolean;
1800
+ /** What to do when a field triggers deny action: "block" or "redact" */
1801
+ on_deny: "block" | "redact";
1802
+ }
1803
+ interface EnforcerStatus {
1804
+ enabled: boolean;
1805
+ log_only: boolean;
1806
+ default_policy_id: string | null;
1807
+ stats: {
1808
+ calls_inspected: number;
1809
+ calls_bypassed: number;
1810
+ fields_redacted: number;
1811
+ fields_hashed: number;
1812
+ fields_blocked: number;
1813
+ calls_blocked: number;
1814
+ };
1815
+ }
1816
+ declare class ContextGateEnforcer {
1817
+ private policyStore;
1818
+ private auditLog;
1819
+ private config;
1820
+ private stats;
1821
+ constructor(policyStore: ContextGatePolicyStore, auditLog: AuditLog, config: EnforcerConfig);
1822
+ /**
1823
+ * Wrap a tool handler to apply automatic context gating.
1824
+ *
1825
+ * The wrapped handler:
1826
+ * 1. Checks if tool should be filtered (based on bypass_prefixes)
1827
+ * 2. If not filtering, calls original handler directly
1828
+ * 3. If filtering:
1829
+ * a. Gets the active policy or falls back to built-in patterns
1830
+ * b. Calls filterContext() with tool arguments
1831
+ * c. If any field triggered "deny" and on_deny is "block", returns error
1832
+ * d. If on_deny is "redact", replaces denied fields with "[REDACTED]"
1833
+ * e. Calls original handler with filtered arguments
1834
+ * f. Logs the filtering decision
1835
+ * 4. In log_only mode: runs filter, logs what would happen, passes original args
1836
+ */
1837
+ wrapHandler(toolName: string, originalHandler: ToolHandler): ToolHandler;
1838
+ /**
1839
+ * Filter tool arguments using an explicit policy.
1840
+ */
1841
+ private filterWithPolicy;
1842
+ /**
1843
+ * Filter tool arguments using built-in sensitive patterns.
1844
+ * This provides baseline protection when no explicit policy is configured.
1845
+ */
1846
+ private filterWithBuiltinPatterns;
1847
+ /**
1848
+ * Check if a tool should be filtered based on bypass prefixes.
1849
+ *
1850
+ * SEC-033: Uses exact namespace component matching, not bare startsWith().
1851
+ * A prefix of "sanctuary/" matches "sanctuary/state_read" but NOT
1852
+ * "sanctuary_evil/steal_data" (no slash boundary confusion). The prefix
1853
+ * must match exactly up to its length, and the prefix must end with "/"
1854
+ * to enforce namespace boundaries (if it doesn't, we add one for safety).
1855
+ */
1856
+ shouldFilter(toolName: string): boolean;
1857
+ /**
1858
+ * Extract provider category from tool name.
1859
+ * Default: "tool-api". Override for specific patterns.
1860
+ */
1861
+ private extractProviderCategory;
1862
+ /**
1863
+ * Build filtered arguments from filter decisions.
1864
+ */
1865
+ private buildFilteredArgs;
1866
+ /**
1867
+ * Set the active policy ID.
1868
+ */
1869
+ setDefaultPolicy(policyId: string): void;
1870
+ /**
1871
+ * Get current enforcer status and stats.
1872
+ */
1873
+ getStatus(): EnforcerStatus;
1874
+ /**
1875
+ * Toggle enforcer enabled state.
1876
+ */
1877
+ setEnabled(enabled: boolean): void;
1878
+ /**
1879
+ * Toggle log_only mode.
1880
+ */
1881
+ setLogOnly(logOnly: boolean): void;
1882
+ /**
1883
+ * Reset stats counters.
1884
+ */
1885
+ resetStats(): void;
1886
+ }
1887
+
1888
+ /**
1889
+ * Sanctuary MCP Server — In-Memory Storage Backend
1890
+ *
1891
+ * Used for testing. Implements the same interface as filesystem storage
1892
+ * but stores everything in memory. Data does not persist across restarts.
1893
+ */
1894
+
1895
+ declare class MemoryStorage implements StorageBackend {
1896
+ private store;
1897
+ private storageKey;
1898
+ write(namespace: string, key: string, data: Uint8Array): Promise<void>;
1899
+ read(namespace: string, key: string): Promise<Uint8Array | null>;
1900
+ delete(namespace: string, key: string, _secureOverwrite?: boolean): Promise<boolean>;
1901
+ list(namespace: string, prefix?: string): Promise<StorageEntryMeta[]>;
1902
+ exists(namespace: string, key: string): Promise<boolean>;
1903
+ totalSize(): Promise<number>;
1904
+ /** Clear all stored data (useful in tests) */
1905
+ clear(): void;
1906
+ }
1907
+
1908
+ /**
1909
+ * Sanctuary MCP Server — Filesystem Storage Backend
1910
+ *
1911
+ * Default storage backend using the local filesystem.
1912
+ * Files are stored as: {basePath}/{namespace}/{key}.enc
1913
+ *
1914
+ * Security invariants:
1915
+ * - Secure deletion overwrites file content with random bytes before unlinking
1916
+ * - Directory creation uses restrictive permissions (0o700)
1917
+ * - File creation uses restrictive permissions (0o600)
1918
+ */
1919
+
1920
+ declare class FilesystemStorage implements StorageBackend {
1921
+ private basePath;
1922
+ constructor(basePath: string);
1923
+ private entryPath;
1924
+ private namespacePath;
1925
+ write(namespace: string, key: string, data: Uint8Array): Promise<void>;
1926
+ read(namespace: string, key: string): Promise<Uint8Array | null>;
1927
+ delete(namespace: string, key: string, secureOverwrite?: boolean): Promise<boolean>;
1928
+ list(namespace: string, prefix?: string): Promise<StorageEntryMeta[]>;
1929
+ exists(namespace: string, key: string): Promise<boolean>;
1930
+ totalSize(): Promise<number>;
1672
1931
  }
1673
1932
 
1674
1933
  /**
@@ -1818,7 +2077,7 @@ declare class DashboardApprovalChannel implements ApprovalChannel {
1818
2077
  private handlePendingList;
1819
2078
  private handleAuditLog;
1820
2079
  private handleDecision;
1821
- private broadcastSSE;
2080
+ broadcastSSE(event: string, data: unknown): void;
1822
2081
  /**
1823
2082
  * Broadcast an audit entry to connected dashboards.
1824
2083
  * Called externally when audit events happen.
@@ -1834,6 +2093,30 @@ declare class DashboardApprovalChannel implements ApprovalChannel {
1834
2093
  * Called externally after baseline changes.
1835
2094
  */
1836
2095
  broadcastBaselineUpdate(): void;
2096
+ /**
2097
+ * Broadcast a tool call event to connected dashboards.
2098
+ * Called from the gate or router when a tool is invoked.
2099
+ */
2100
+ broadcastToolCall(data: {
2101
+ tool: string;
2102
+ tier: number;
2103
+ allowed: boolean;
2104
+ timestamp: string;
2105
+ }): void;
2106
+ /**
2107
+ * Broadcast a context gate decision to connected dashboards.
2108
+ */
2109
+ broadcastContextGateDecision(data: {
2110
+ tool: string;
2111
+ fields_filtered: number;
2112
+ fields_total: number;
2113
+ action: string;
2114
+ timestamp: string;
2115
+ }): void;
2116
+ /**
2117
+ * Broadcast current protection status to connected dashboards.
2118
+ */
2119
+ broadcastProtectionStatus(data: Record<string, unknown>): void;
1837
2120
  /** Get the number of pending requests */
1838
2121
  get pendingCount(): number;
1839
2122
  /** Get the number of connected SSE clients */
@@ -2241,4 +2524,4 @@ declare function createSanctuaryServer(options?: {
2241
2524
  storage?: StorageBackend;
2242
2525
  }): Promise<SanctuaryServer>;
2243
2526
 
2244
- export { ApprovalGate, AuditLog, AutoApproveChannel, BaselineTracker, type BridgeAttestationRequest, type BridgeAttestationResult, type BridgeCommitment, type BridgeVerificationResult, TEMPLATES as CONTEXT_GATE_TEMPLATES, CallbackApprovalChannel, CommitmentStore, type ConcordiaOutcome, type ContextAction, type ContextFilterResult, type ContextGatePolicy, ContextGatePolicyStore, type ContextGateRule, type ContextGateTemplate, DashboardApprovalChannel, type DashboardConfig, type FederationCapabilities, type FederationPeer, FederationRegistry, type FieldClassification, type FieldFilterResult, FilesystemStorage, type GateResult, type HandshakeChallenge, type HandshakeCompletion, type HandshakeResponse, type HandshakeResult, MemoryStorage, type PedersenCommitment, type PeerTrustEvaluation, type PolicyRecommendation, PolicyStore, type PrincipalPolicy, type ProviderCategory, ReputationStore, type SHRBody, type SHRVerificationResult, type SanctuaryConfig, type SanctuaryServer, type SignedSHR, type SovereigntyTier, StateStore, StderrApprovalChannel, TIER_WEIGHTS, type TierMetadata, type TieredAttestation, WebhookApprovalChannel, type WebhookCallbackPayload, type WebhookConfig, type WebhookPayload, type ZKProofOfKnowledge, type ZKRangeProof, canonicalize, classifyField, completeHandshake, computeWeightedScore, createBridgeCommitment, createPedersenCommitment, createProofOfKnowledge, createRangeProof, createSanctuaryServer, evaluateField, filterContext, generateSHR, getTemplate, initiateHandshake, listTemplateIds, loadConfig, loadPrincipalPolicy, recommendPolicy, resolveTier, respondToHandshake, signPayload, tierDistribution, verifyBridgeCommitment, verifyCompletion, verifyPedersenCommitment, verifyProofOfKnowledge, verifyRangeProof, verifySHR, verifySignature };
2527
+ export { ApprovalGate, AuditLog, AutoApproveChannel, BaselineTracker, type BridgeAttestationRequest, type BridgeAttestationResult, type BridgeCommitment, type BridgeVerificationResult, TEMPLATES as CONTEXT_GATE_TEMPLATES, CallbackApprovalChannel, CommitmentStore, type ConcordiaOutcome, type ContextAction, type ContextFilterResult, ContextGateEnforcer, type ContextGatePolicy, ContextGatePolicyStore, type ContextGateRule, type ContextGateTemplate, DashboardApprovalChannel, type DashboardConfig, type DetectionResult, type EnforcerConfig, type FederationCapabilities, type FederationPeer, FederationRegistry, type FieldClassification, type FieldFilterResult, FilesystemStorage, type GateResult, type HandshakeChallenge, type HandshakeCompletion, type HandshakeResponse, type HandshakeResult, InjectionDetector, type InjectionDetectorConfig, type InjectionSignal, MemoryStorage, type PedersenCommitment, type PeerTrustEvaluation, type PolicyRecommendation, PolicyStore, type PrincipalPolicy, type ProviderCategory, ReputationStore, type SHRBody, type SHRVerificationResult, type SanctuaryConfig, type SanctuaryServer, type SignedSHR, type SovereigntyTier, StateStore, StderrApprovalChannel, TIER_WEIGHTS, type TierMetadata, type TieredAttestation, WebhookApprovalChannel, type WebhookCallbackPayload, type WebhookConfig, type WebhookPayload, type ZKProofOfKnowledge, type ZKRangeProof, canonicalize, classifyField, completeHandshake, computeWeightedScore, createBridgeCommitment, createPedersenCommitment, createProofOfKnowledge, createRangeProof, createSanctuaryServer, evaluateField, filterContext, generateSHR, getTemplate, initiateHandshake, listTemplateIds, loadConfig, loadPrincipalPolicy, recommendPolicy, resolveTier, respondToHandshake, signPayload, tierDistribution, verifyBridgeCommitment, verifyCompletion, verifyPedersenCommitment, verifyProofOfKnowledge, verifyRangeProof, verifySHR, verifySignature };