Skip to content

Commit e21adfc

Browse files
ref(search-attributes): Yield formatted lines from a generator
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5e9d58d commit e21adfc

2 files changed

Lines changed: 43 additions & 51 deletions

File tree

‎packages/mcp-core/src/tools/support/search-attributes/format.ts‎

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ interface QuerySettings {
3333
period: string;
3434
}
3535

36-
export function formatListResult({
36+
export function* formatListResult({
3737
organizationSlug,
3838
dataset,
3939
period,
@@ -46,41 +46,37 @@ export function formatListResult({
4646
limit: number;
4747
notes?: string[];
4848
importantInfo?: string[];
49-
}): string {
49+
}) {
5050
const labels = DATASET_LABELS[dataset];
51-
const lines: string[] = [];
5251

53-
if (importantInfo) {
54-
importantInfo.map((info) => lines.push(info));
55-
}
52+
yield* importantInfo;
53+
yield `# ${labels.title} in **${organizationSlug}** (last ${period})`;
54+
yield "";
5655

57-
lines.push(
58-
`# ${labels.title} in **${organizationSlug}** (last ${period})`,
59-
"",
60-
);
6156
if (attributes.length === 0) {
62-
return formatNoneSeen(lines, labels.title.toLowerCase(), period);
57+
yield* formatNoneSeen(labels.title.toLowerCase(), period);
58+
return;
6359
}
6460

65-
lines.push(
66-
"**Suggested presentation:** A table works well here, with each attribute's type, description, deprecation status, replacement attributes, and whether it's added by Sentry.",
67-
"",
68-
"Please tell the user each attribute's type, description, examples, whether they were added by sentry, and which attributes are deprecated and what replaces them.",
69-
"",
70-
);
61+
yield "**Suggested presentation:** A table works well here, with each attribute's type, description, deprecation status, replacement attributes, and whether it's added by Sentry.";
62+
yield "";
63+
yield "Please tell the user each attribute's type, description, examples, whether they were added by sentry, and which attributes are deprecated and what replaces them.";
64+
yield "";
7165

7266
const sorted = sortAttributes(attributes, dataset);
7367
const shown = sorted.slice(0, limit);
7468

75-
lines.push(...formatAttributeTable(shown), "", "## Response Notes", "");
69+
yield* formatAttributeTable(shown);
70+
yield "";
71+
yield "## Response Notes";
72+
yield "";
7673
if (shown.length < sorted.length) {
77-
lines.push(`- Showing ${shown.length} of ${sorted.length}.`);
74+
yield `- Showing ${shown.length} of ${sorted.length}.`;
75+
}
76+
yield `- Use a key with \`${labels.associatedTool}\` to query its values.`;
77+
for (const note of notes) {
78+
yield `- ${note}`;
7879
}
79-
lines.push(
80-
`- Use a key with \`${labels.associatedTool}\` to query its values.`,
81-
...notes.map((note) => `- ${note}`),
82-
);
83-
return lines.join("\n");
8480
}
8581

8682
/**
@@ -102,26 +98,20 @@ function sortAttributes(
10298
);
10399
}
104100

105-
function formatNoneSeen(lines: string[], noun: string, period: string): string {
106-
lines.push(
107-
`No ${noun} were seen in the last ${period}.`,
108-
"",
109-
"## Response Notes",
110-
"",
111-
"- Try a longer `period` or a different `dataset`.",
112-
);
113-
return lines.join("\n");
101+
function* formatNoneSeen(noun: string, period: string) {
102+
yield `No ${noun} were seen in the last ${period}.`;
103+
yield "";
104+
yield "## Response Notes";
105+
yield "";
106+
yield "- Try a longer `period` or a different `dataset`.";
114107
}
115108

116-
function formatAttributeTable(attributes: TraceItemAttribute[]): string[] {
117-
return [
118-
"| Attribute | Type | Description | Added by Sentry | Deprecated | Replacement Attribute |",
119-
"| --- | --- | --- | --- | --- | --- |",
120-
...attributes.map(
121-
(attribute) =>
122-
`| \`${escapeCell(attribute.key)}\` | ${attribute.type} | ${escapeCell(describeCell(attribute))} | ${attribute.attributeSource.source_type === "sentry" ? "yes" : ""} | ${attribute.context?.isDeprecated ? "yes" : ""} | ${escapeCell(attribute.context?.replacementAttribute ?? "")} |`,
123-
),
124-
];
109+
function* formatAttributeTable(attributes: TraceItemAttribute[]) {
110+
yield "| Attribute | Type | Description | Added by Sentry | Deprecated | Replacement Attribute |";
111+
yield "| --- | --- | --- | --- | --- | --- |";
112+
for (const attribute of attributes) {
113+
yield `| \`${escapeCell(attribute.key)}\` | ${attribute.type} | ${escapeCell(describeCell(attribute))} | ${attribute.attributeSource.source_type === "sentry" ? "yes" : ""} | ${attribute.context?.isDeprecated ? "yes" : ""} | ${escapeCell(attribute.context?.replacementAttribute ?? "")} |`;
114+
}
125115
}
126116

127117
/** Blank when there's no description */

‎packages/mcp-core/src/tools/support/search-attributes/search.ts‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,17 @@ export async function runSearchAttributes(
7171

7272
const dataset = params.dataset;
7373
const listAttributes = (importantInfo?: string[], notes?: string[]) =>
74-
formatListResult({
75-
organizationSlug,
76-
dataset,
77-
period,
78-
attributes: rows,
79-
limit: params.limit ?? LIST_LIMIT,
80-
notes,
81-
importantInfo,
82-
});
74+
[
75+
...formatListResult({
76+
organizationSlug,
77+
dataset,
78+
period,
79+
attributes: rows,
80+
limit: params.limit ?? LIST_LIMIT,
81+
notes,
82+
importantInfo,
83+
}),
84+
].join("\n");
8385

8486
if (query) {
8587
if (!hasAgentProvider()) {

0 commit comments

Comments
 (0)