MCPcopy Create free account
hub / github.com/facebook/memlab / registerStringPatterns

Function registerStringPatterns

packages/mcp-server/src/tools/string-patterns.ts:28–155  ·  view source on GitHub ↗
(server: McpServer)

Source from the content-addressed store, hash-verified

26}
27
28export function registerStringPatterns(server: McpServer): void {
29 server.tool(
30 'memlab_string_patterns',
31 'Group strings by common prefix and show aggregate counts and sizes. Instead of seeing 20 individual histogram entries for CSV strings that all start with the same header, see them grouped: "Strings matching prefix: 20 instances, 250 MB total." Useful for identifying families of related strings like API responses, repeated log messages, or duplicated data.',
32 {
33 prefix_length: z
34 .number()
35 .optional()
36 .default(50)
37 .describe(
38 'Number of characters to use for prefix grouping (default 50). Shorter values create broader groups.',
39 ),
40 min_count: z
41 .number()
42 .optional()
43 .default(5)
44 .describe('Minimum number of strings in a group to show (default 5)'),
45 min_total_size: z
46 .number()
47 .optional()
48 .describe(
49 'Minimum total self size in bytes for a group to be shown. Use to filter to significant groups.',
50 ),
51 limit: z
52 .number()
53 .optional()
54 .default(20)
55 .describe('Maximum number of groups to return (default 20)'),
56 },
57 async ({prefix_length, min_count, min_total_size, limit}) => {
58 try {
59 const snapshot = getSnapshot();
60
61 const groups = new Map<
62 string,
63 {
64 count: number;
65 total_self_size: number;
66 total_retained_size: number;
67 min_len: number;
68 max_len: number;
69 example_ids: number[];
70 }
71 >();
72
73 snapshot.nodes.forEach(node => {
74 if (node.type !== 'string') return;
75 if (node.name === 'system / SlicedString') return;
76 if (node.name === 'system / ConsString') return;
77 if (node.id <= 3) return;
78
79 const prefix = extractPrefix(node.name, prefix_length);
80 const existing = groups.get(prefix);
81 if (existing) {
82 existing.count++;
83 existing.total_self_size += node.self_size;
84 existing.total_retained_size += node.retainedSize;
85 existing.min_len = Math.min(existing.min_len, node.name.length);

Callers 1

index.tsFile · 0.85

Calls 12

getSnapshotFunction · 0.85
extractPrefixFunction · 0.85
toolResultFunction · 0.85
formatNumberFunction · 0.85
markdownTableFunction · 0.85
errorResultFunction · 0.85
entriesMethod · 0.80
forEachMethod · 0.65
getMethod · 0.65
filterMethod · 0.65
formatBytesFunction · 0.50
setMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…